my.vk.favorites: some minor cleanup

This commit is contained in:
Dima Gerasimov 2024-08-10 18:35:30 +03:00 committed by karlicoss
parent 34593c032d
commit c69a0b43ba

View file

@ -1,29 +1,27 @@
# todo: uses my private export script? # todo: uses my private export script?, timezone
from datetime import datetime from dataclasses import dataclass
from datetime import datetime, timezone
import json import json
from typing import NamedTuple, Iterable, Sequence, Optional from typing import Iterator, Iterable, Optional
from my.core import Json, datetime_aware, stat, Stats
from my.core.error import Res
from my.config import vk as config # type: ignore[attr-defined] from my.config import vk as config # type: ignore[attr-defined]
class Favorite(NamedTuple): @dataclass
dt: datetime class Favorite:
dt: datetime_aware
title: str title: str
url: Optional[str] url: Optional[str]
text: str text: str
from ..core import Json
from ..core.error import Res
skip = ( skip = (
'graffiti', 'graffiti',
'poll', 'poll',
'note', # TODO could be useful..
# TODO could be useful..
'note',
'doc', 'doc',
'audio', 'audio',
'photo', 'photo',
@ -32,6 +30,7 @@ skip = (
'page', 'page',
) )
def parse_fav(j: Json) -> Favorite: def parse_fav(j: Json) -> Favorite:
# TODO copy_history?? # TODO copy_history??
url = None url = None
@ -47,14 +46,14 @@ def parse_fav(j: Json) -> Favorite:
# TODO would be nice to include user # TODO would be nice to include user
return Favorite( return Favorite(
dt=datetime.utcfromtimestamp(j['date']), dt=datetime.fromtimestamp(j['date'], tz=timezone.utc),
title=title, title=title,
url=url, url=url,
text=j['text'], text=j['text'],
) )
def _iter_favs() -> Iterable[Res]: def _iter_favs() -> Iterator[Res]:
jj = json.loads(config.favs_file.read_text()) jj = json.loads(config.favs_file.read_text())
for j in jj: for j in jj:
try: try:
@ -65,7 +64,7 @@ def _iter_favs() -> Iterable[Res]:
yield ex yield ex
def favorites() -> Sequence[Res]: def favorites() -> Iterable[Res]:
it = _iter_favs() it = _iter_favs()
# trick to sort errors along with the actual objects # trick to sort errors along with the actual objects
# TODO wonder if there is a shorter way? # TODO wonder if there is a shorter way?
@ -82,6 +81,5 @@ def favorites() -> Sequence[Res]:
return sorted_items return sorted_items
def stats(): def stats() -> Stats:
from ..core import stat
return stat(favorites) return stat(favorites)