Download:
child 17:facad31ec0dc
parent 15:0a64b0583576
16:2a4c890a667a
Anton Shestakov <engored@ya.ru>, Fri, 26 Sep 2014 14:35:43 +0900
youtube-watch: download option, don't rely on publication time for new_entries.

1 файлов изменено, 26 вставок(+), 9 удалений(-) [+]
youtube-watch/watch.py file | annotate | diff | comparison | revisions
--- a/youtube-watch/watch.py Thu Sep 25 14:35:55 2014 +0900
+++ b/youtube-watch/watch.py Fri Sep 26 14:35:43 2014 +0900
@@ -1,3 +1,4 @@
+import subprocess
import sys
from argparse import ArgumentParser, FileType
from calendar import timegm
@@ -14,12 +15,14 @@
return feed['entries']
-def formatter(template, limit, entries):
+def prepare_entries(entries, limit=None):
entries = sorted(entries, key=itemgetter('published_parsed'))
-
if limit is not None:
entries = entries[-limit:]
+ return entries
+
+def formatter(template, entries):
for entry in entries:
entry['link'] = entry['link'].replace('&feature=youtube_gdata', '')
entry['published_nice'] = strftime('%Y-%m-%d %H:%M:%S', localtime(timegm(entry['published_parsed'])))
@@ -63,6 +66,15 @@
return entries
+def download(entries):
+ if entries:
+ subprocess.call(['cclive', '--continue', '--timestamp', '--stream', 'best'] + [entry['link'] for entry in entries])
+
+
+def identify(entry):
+ return entry['id'].rpartition(':')[2]
+
+
def main():
parser = ArgumentParser()
parser.add_argument('-U', '--user', action='append', dest='users', help='author of uploads', metavar='USER [+]')
@@ -71,6 +83,7 @@
parser.add_argument('-l', '--limit', type=int, help='limit number of initial videos')
parser.add_argument('-f', '--follow', action='store_true', help='continuous mode')
parser.add_argument('-d', '--delay', type=int, help='delay for continuous mode', default=60)
+ parser.add_argument('-D', '--download', action='store_true', help='download each video')
args = parser.parse_args()
template = get_template(args.template)
@@ -80,21 +93,25 @@
users += args.input_file.read().splitlines()
entries = fetch_entries(users)
+ earlier = {identify(entry) for entry in entries}
+ entries = prepare_entries(entries, args.limit)
- formatter(template, args.limit, entries)
+ formatter(template, entries)
+ if args.download:
+ download(entries)
if not args.follow:
return
- earlier = None
-
try:
while True:
- if earlier is not None:
- new_entries = [entry for entry in entries if entry['published_parsed'] > earlier]
- formatter(template, None, new_entries)
- earlier = max([entry['published_parsed'] for entry in entries])
entries = fetch_entries(users, args.delay)
+ new_entries = [entry for entry in entries if identify(entry) not in earlier]
+ earlier |= {identify(entry) for entry in new_entries}
+ new_entries = prepare_entries(new_entries)
+ formatter(template, new_entries)
+ if args.download:
+ download(new_entries)
except KeyboardInterrupt:
print