twitter: add permalink to Talon objects; extract shared method

This commit is contained in:
Dima Gerasimov 2022-05-30 16:57:08 +01:00 committed by karlicoss
parent 19da373a0a
commit de7972be05
4 changed files with 30 additions and 14 deletions

View file

@ -49,7 +49,7 @@ def inputs() -> Sequence[Path]:
return get_files(config.export_path)[-1:] return get_files(config.export_path)[-1:]
Tid = str from .common import TweetId, permalink
# TODO make sure it's not used anywhere else and simplify interface # TODO make sure it's not used anywhere else and simplify interface
@ -58,7 +58,7 @@ class Tweet(NamedTuple):
screen_name: str screen_name: str
@property @property
def id_str(self) -> str: def id_str(self) -> TweetId:
return self.raw['id_str'] return self.raw['id_str']
@property @property
@ -68,7 +68,7 @@ class Tweet(NamedTuple):
@property @property
def permalink(self) -> str: def permalink(self) -> str:
return f'https://twitter.com/{self.screen_name}/status/{self.tid}' return permalink(screen_name=self.screen_name, id=self.id_str)
@property @property
def text(self) -> str: def text(self) -> str:
@ -92,11 +92,11 @@ class Tweet(NamedTuple):
# TODO deprecate tid? # TODO deprecate tid?
@property @property
def tid(self) -> Tid: def tid(self) -> TweetId:
return self.id_str return self.id_str
@property @property
def dt(self) -> datetime: def dt(self) -> datetime_aware:
return self.created_at return self.created_at
@ -104,14 +104,13 @@ class Like(NamedTuple):
raw: Json raw: Json
screen_name: str screen_name: str
# TODO need to make permalink/link/url consistent across my stuff..
@property @property
def permalink(self) -> str: def permalink(self) -> str:
# doesn'tseem like link it export is more specific... # doesn'tseem like link it export is more specific...
return f'https://twitter.com/{self.screen_name}/status/{self.tid}' return permalink(screen_name=self.screen_name, id=self.id_str)
@property @property
def id_str(self) -> Tid: def id_str(self) -> TweetId:
return self.raw['tweetId'] return self.raw['tweetId']
@property @property
@ -121,13 +120,14 @@ class Like(NamedTuple):
# TODO deprecate? # TODO deprecate?
@property @property
def tid(self) -> Tid: def tid(self) -> TweetId:
return self.id_str return self.id_str
from functools import lru_cache from functools import lru_cache
class ZipExport: class ZipExport:
def __init__(self, archive_path: Path) -> None: def __init__(self, archive_path: Path) -> None:
# TODO use ZipPath
self.epath = archive_path self.epath = archive_path
self.old_format = False # changed somewhere around 2020.03 self.old_format = False # changed somewhere around 2020.03
@ -189,3 +189,7 @@ def stats() -> Stats:
**stat(tweets), **stat(tweets),
**stat(likes), **stat(likes),
} }
## Deprecated stuff
Tid = TweetId

View file

@ -8,6 +8,7 @@ from more_itertools import unique_everseen
# TODO add proper Protocol for Tweet # TODO add proper Protocol for Tweet
Tweet = Any Tweet = Any
TweetId = str
from my.core import warn_if_empty, Res from my.core import warn_if_empty, Res
@ -19,3 +20,7 @@ def merge_tweets(*sources: Iterator[Res[Tweet]]) -> Iterator[Res[Tweet]]:
else: else:
return r.id_str return r.id_str
yield from unique_everseen(chain(*sources), key=key) yield from unique_everseen(chain(*sources), key=key)
def permalink(*, screen_name: str, id: str) -> str:
return f'https://twitter.com/{screen_name}/status/{id}'

View file

@ -25,15 +25,21 @@ def inputs() -> Sequence[Path]:
return get_files(config.export_path) return get_files(config.export_path)
from .common import TweetId, permalink
@dataclass(unsafe_hash=True) @dataclass(unsafe_hash=True)
class Tweet: class Tweet:
id_str: str id_str: TweetId
created_at: datetime_aware created_at: datetime_aware
screen_name: str screen_name: str
text: str text: str
urls: Sequence[str] urls: Sequence[str]
@property
def permalink(self) -> str:
return permalink(screen_name=self.screen_name, id=self.id_str)
# meh... just wrappers to tell apart tweets from favorites... # meh... just wrappers to tell apart tweets from favorites...
@dataclass(unsafe_hash=True) @dataclass(unsafe_hash=True)

View file

@ -35,11 +35,14 @@ def get_db_path() -> Path:
return max(get_files(config.export_path)) return max(get_files(config.export_path))
from .common import TweetId, permalink
class Tweet(NamedTuple): class Tweet(NamedTuple):
row: Json row: Json
@property @property
def id_str(self) -> str: def id_str(self) -> TweetId:
return self.row['id_str'] return self.row['id_str']
@property @property
@ -50,7 +53,6 @@ class Tweet(NamedTuple):
dt = datetime.fromtimestamp(seconds, tz=tz) dt = datetime.fromtimestamp(seconds, tz=tz)
return dt return dt
# TODO permalink -- take user into account?
@property @property
def screen_name(self) -> str: def screen_name(self) -> str:
return self.row['screen_name'] return self.row['screen_name']
@ -66,10 +68,9 @@ class Tweet(NamedTuple):
return [] return []
return ustr.split(',') return ustr.split(',')
# TODO move to common
@property @property
def permalink(self) -> str: def permalink(self) -> str:
return f'https://twitter.com/{self.screen_name}/status/{self.id_str}' return permalink(screen_name=self.screen_name, id=self.id_str)
# TODO urls # TODO urls