-
Notifications
You must be signed in to change notification settings - Fork 61
/
Copy pathcommon.py
64 lines (49 loc) · 1.59 KB
/
common.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
"""
This defines Protocol classes, which make sure that each different
type of shared models have a standardized interface
"""
from my.core import __NOT_HPI_MODULE__ # isort: skip
from collections.abc import Iterator
from itertools import chain
from typing import Protocol
from my.core import Json, datetime_aware
# common fields across all the Protocol classes, so generic code can be written
class RedditBase(Protocol):
@property
def raw(self) -> Json: ...
@property
def created(self) -> datetime_aware: ...
@property
def id(self) -> str: ...
@property
def url(self) -> str: ...
@property
def text(self) -> str: ...
# Note: doesn't include GDPR Save's since they don't have the same metadata
class Save(RedditBase, Protocol):
@property
def subreddit(self) -> str: ...
# Note: doesn't include GDPR Upvote's since they don't have the same metadata
class Upvote(RedditBase, Protocol):
@property
def title(self) -> str: ...
# From rexport, pushshift and the reddit GDPR export
class Comment(RedditBase, Protocol):
pass
# From rexport and the GDPR export
class Submission(RedditBase, Protocol):
@property
def title(self) -> str: ...
def _merge_comments(*sources: Iterator[Comment]) -> Iterator[Comment]:
#from .rexport import logger
#ignored = 0
emitted: set[str] = set()
for e in chain(*sources):
uid = e.id
if uid in emitted:
#ignored += 1
#logger.info('ignoring %s: %s', uid, e)
continue
yield e
emitted.add(uid)
#logger.info(f"Ignored {ignored} comments...")