2018-08-14 07:45:17 -07:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
2018-09-28 14:44:35 -07:00
|
|
|
import os
|
2015-05-11 21:22:27 -07:00
|
|
|
import opml
|
|
|
|
import feedparser
|
|
|
|
import youtube_dl
|
2018-02-07 08:43:37 -08:00
|
|
|
import sys
|
2018-09-28 14:41:33 -07:00
|
|
|
from pathlib import Path
|
2018-09-28 14:44:35 -07:00
|
|
|
import argparse
|
2015-05-11 21:22:27 -07:00
|
|
|
|
2018-02-07 08:43:37 -08:00
|
|
|
if sys.version_info[0] < 3:
|
2018-02-07 08:49:23 -08:00
|
|
|
raise Exception('Must be using Python 3')
|
2018-02-07 08:43:37 -08:00
|
|
|
|
2015-05-11 21:22:27 -07:00
|
|
|
from time import time, mktime, strptime
|
|
|
|
from datetime import datetime
|
2018-09-28 14:44:35 -07:00
|
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
|
2018-09-28 14:44:56 -07:00
|
|
|
parser = argparse.ArgumentParser('Download YouTube subscriptions.')
|
|
|
|
parser.add_argument('--save-directory', '-s',
|
|
|
|
dest='output',
|
|
|
|
default=None,
|
|
|
|
help='The directory to which to save the videos.')
|
|
|
|
parser.add_argument('--retain', '-c',
|
|
|
|
dest='retain',
|
|
|
|
default=None,
|
|
|
|
help='Retain videos up to the given number of days since today.')
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
|
|
|
|
|
|
|
# The current run time.
|
|
|
|
ftime = time()
|
|
|
|
|
|
|
|
outlines = opml.parse('subs.xml')
|
|
|
|
|
|
|
|
if args.output is not None:
|
|
|
|
os.chdir(Path(args.output).absolute())
|
|
|
|
|
|
|
|
if not Path('last.txt').exists():
|
|
|
|
with open('last.txt', 'w') as f:
|
|
|
|
f.write(str(time()))
|
|
|
|
print('Initialized a last.txt file with current timestamp.')
|
2015-05-11 21:22:27 -07:00
|
|
|
else:
|
2018-09-28 14:44:56 -07:00
|
|
|
with open('last.txt', 'r') as f:
|
|
|
|
content = f.read()
|
|
|
|
# The last run time.
|
|
|
|
ptime = datetime.utcfromtimestamp(float(content))
|
|
|
|
|
|
|
|
if args.retain is not None:
|
|
|
|
# Find the videos in this directory which are older than the time
|
|
|
|
# stamp since the last run and remove them.
|
|
|
|
keeptime = datetime.fromtimestamp(ftime) - relativedelta(days=float(args.retain))
|
|
|
|
for video in Path('.').glob('*.mp4'):
|
|
|
|
mtime = datetime.utcfromtimestamp(os.path.getmtime(video))
|
|
|
|
if mtime < keeptime:
|
|
|
|
print(f'Removing {str(video)}.')
|
2018-09-28 14:55:22 -07:00
|
|
|
video.unlink()
|
2018-09-28 14:44:56 -07:00
|
|
|
|
|
|
|
urls = []
|
|
|
|
|
|
|
|
for outline in outlines[0]:
|
|
|
|
urls.append(outline.xmlUrl)
|
|
|
|
|
|
|
|
videos = []
|
|
|
|
for i, url in enumerate(urls):
|
|
|
|
print(f'Parsing through channel {i + 1} of {len(urls)}', end='\r')
|
|
|
|
feed = feedparser.parse(url)
|
|
|
|
for item in feed['items']:
|
|
|
|
timef = item['published_parsed']
|
|
|
|
dt = datetime.fromtimestamp(mktime(timef))
|
|
|
|
if dt > ptime:
|
|
|
|
videos.append(item['link'])
|
|
|
|
|
|
|
|
if len(videos) == 0:
|
|
|
|
print('Sorry, no new video found')
|
|
|
|
else:
|
|
|
|
print(f'{len(videos)} new videos found')
|
|
|
|
|
2018-09-28 14:50:50 -07:00
|
|
|
ydl_opts = {'ignoreerrors': True, 'quiet': True}
|
2018-09-28 14:44:56 -07:00
|
|
|
|
|
|
|
with youtube_dl.YoutubeDL(ydl_opts) as ydl:
|
|
|
|
ydl.download(videos)
|
|
|
|
|
|
|
|
with open('last.txt', 'w') as f:
|
|
|
|
f.write(str(ftime))
|