-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlistens
executable file
·75 lines (66 loc) · 2.07 KB
/
listens
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
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
"""
Print merged listens with a local datetime, using https://github.com/purarue/my_feed
"""
import sys
import contextlib
from datetime import datetime
from typing import Iterator, cast
import click
from pura.jsonfast import dumps
from my.core.query_range import select_range, RangeTuple
from my_feed.sources.model import FeedItem
from my_feed.transform import transform
from my_feed.sources import listens, mpv, facebook_spotify_listens, offline_listens
def sources() -> Iterator[FeedItem]:
yield from transform(listens.history)()
yield from transform(mpv.history)()
yield from transform(facebook_spotify_listens.history)()
yield from offline_listens.history()
@click.command(help=__doc__)
@click.option(
"-r",
"--recent",
default=None,
help="only list items within this range",
)
@click.option(
"-d/-j",
"--desc/--json",
is_flag=True,
default=True,
help="Print single line description instead of JSON",
)
def main(recent: str | None, desc: bool) -> None:
feeditems: Iterator[FeedItem] = sources()
# only sort if we're filtering to recent items
if recent:
feeditems = cast(
Iterator[FeedItem],
select_range(
feeditems,
order_by_value_type=datetime,
unparsed_range=RangeTuple(before="now", within=recent, after=None),
drop_exceptions=True,
),
)
for ll in feeditems:
when = str(datetime.fromtimestamp(int(ll.when.timestamp())))
if desc:
sys.stdout.write(f"{when} | {ll.title} | {ll.creator} | {ll.subtitle}\n")
else:
sys.stdout.write(
dumps(
{
"when": when,
"title": ll.title,
"artist": ll.creator,
"album": ll.subtitle,
}
)
)
sys.stdout.write("\n")
if __name__ == "__main__":
with contextlib.suppress(KeyboardInterrupt):
main()
sys.stdout.flush()