Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules.youtube: add mode --all to dump all videos + minor cleanup #49

Merged
merged 1 commit into from
Sep 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 31 additions & 17 deletions src/orger/modules/youtube.py
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)
Loading