add url and title
This commit is contained in:
parent
46e1eb10b5
commit
12b7aa919e
1 changed files with 37 additions and 4 deletions
41
my/vk.py
41
my/vk.py
|
@ -1,33 +1,66 @@
|
||||||
from datetime import datetime
|
from datetime import datetime
|
||||||
import json
|
import json
|
||||||
from typing import NamedTuple, Iterator, Dict, Union, Sequence
|
from typing import NamedTuple, Iterator, Dict, Union, Sequence, Optional
|
||||||
|
|
||||||
from my_configuration import paths
|
from my_configuration import paths
|
||||||
|
|
||||||
|
|
||||||
class Favorite(NamedTuple):
|
class Favorite(NamedTuple):
|
||||||
dt: datetime
|
dt: datetime
|
||||||
|
title: str
|
||||||
|
url: Optional[str]
|
||||||
text: str
|
text: str
|
||||||
|
|
||||||
|
|
||||||
Res = Union[Favorite, Exception]
|
Res = Union[Favorite, Exception]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
skip = (
|
||||||
|
'graffiti',
|
||||||
|
'poll',
|
||||||
|
|
||||||
|
# TODO could be useful..
|
||||||
|
'note',
|
||||||
|
'doc',
|
||||||
|
'audio',
|
||||||
|
'photo',
|
||||||
|
'album',
|
||||||
|
'video',
|
||||||
|
'page',
|
||||||
|
)
|
||||||
|
|
||||||
def parse_fav(j: Dict) -> Favorite:
|
def parse_fav(j: Dict) -> Favorite:
|
||||||
a = j['attachments']
|
# TODO copy_history??
|
||||||
# TODO unpack?
|
url = None
|
||||||
|
title = '' # TODO ???
|
||||||
|
atts = j.get('attachments', [])
|
||||||
|
for a in atts:
|
||||||
|
if any(k in a for k in skip):
|
||||||
|
continue
|
||||||
|
link = a['link']
|
||||||
|
title = link['title']
|
||||||
|
url = link['url']
|
||||||
|
# TODOlink['description'] ?
|
||||||
|
|
||||||
|
# TODO would be nice to include user
|
||||||
return Favorite(
|
return Favorite(
|
||||||
dt=datetime.utcfromtimestamp(j['date']),
|
dt=datetime.utcfromtimestamp(j['date']),
|
||||||
|
title=title,
|
||||||
|
url=url,
|
||||||
text=j['text'],
|
text=j['text'],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def _iter_favs() -> Iterator[Res]:
|
def _iter_favs() -> Iterator[Res]:
|
||||||
jj = json.loads(paths.vk.favs_file.read_text())
|
jj = json.loads(paths.vk.favs_file.read_text())
|
||||||
for j in jj:
|
for j in jj:
|
||||||
try:
|
try:
|
||||||
yield parse_fav(j)
|
yield parse_fav(j)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
yield e
|
ex = RuntimeError(f"Error while processing\n{j}")
|
||||||
|
ex.__cause__ = e
|
||||||
|
yield ex
|
||||||
|
|
||||||
|
|
||||||
def favorites() -> Sequence[Res]:
|
def favorites() -> Sequence[Res]:
|
||||||
|
|
Loading…
Add table
Reference in a new issue