-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modules.youtube: add mode --all to dump all videos + minor cleanup
- Loading branch information
Showing
1 changed file
with
31 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,51 @@ | ||
#!/usr/bin/env python3 | ||
from orger import Mirror | ||
from orger.inorganic import node, link | ||
from orger.common import dt_heading, error | ||
|
||
from my.media.youtube import watched | ||
from __future__ import annotations | ||
|
||
from itertools import groupby | ||
|
||
from orger import Mirror | ||
from orger.common import dt_heading, error | ||
from orger.inorganic import link, node | ||
|
||
|
||
class YoutubeView(Mirror): | ||
@property | ||
def mode(self) -> str: | ||
assert self.cmdline_args is not None | ||
return self.cmdline_args.mode | ||
|
||
def get_items(self) -> Mirror.Results: | ||
from my.media.youtube import watched | ||
|
||
good = [] | ||
for i in watched(): | ||
if isinstance(i, Exception): | ||
yield error(i) | ||
else: | ||
good.append(i) | ||
|
||
by_url = lambda w: w.url | ||
by_when = lambda w: w.when | ||
items = [ | ||
max(group, key=by_when) | ||
for _, group in groupby(sorted(good, key=by_url), key=by_url) | ||
] | ||
items = sorted(items, key=by_when) | ||
# TODO for each url only take latest? | ||
if self.mode == 'last': | ||
by_url = lambda w: w.url | ||
items = [ | ||
max(group, key=by_when) | ||
for _, group in groupby(sorted(good, key=by_url), key=by_url) | ||
] | ||
items = sorted(good, key=by_when) | ||
for item in items: | ||
deleted = item.url == item.title # todo move to HPI? | ||
deleted = item.url == item.title # todo move to HPI? | ||
l = link(title=item.title + (' (DELETED)' if deleted else ''), url=item.url) | ||
yield (item.url, node( | ||
heading=dt_heading(item.when, l), | ||
)) | ||
yield ( | ||
item.url, | ||
node(heading=dt_heading(item.when, l)) | ||
) | ||
|
||
|
||
def setup_parser(p) -> None: | ||
p.add_argument( | ||
'--mode', choices=['all', 'last'], default='last', help="'all' would dump all watch history, 'last' only last watch for each video" | ||
) | ||
|
||
|
||
if __name__ == '__main__': | ||
YoutubeView.main() | ||
YoutubeView.main(setup_parser=setup_parser) |