Add command line options.
Optionally specify a save directory. Optionally specify a retention period (in days).
This commit is contained in:
parent
bf047176d3
commit
95aeb812a2
30
dl.py
30
dl.py
@ -1,21 +1,42 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
import os
|
||||
import opml
|
||||
import feedparser
|
||||
import youtube_dl
|
||||
import sys
|
||||
from pathlib import Path
|
||||
import argparse
|
||||
|
||||
if sys.version_info[0] < 3:
|
||||
raise Exception('Must be using Python 3')
|
||||
|
||||
from time import time, mktime, strptime
|
||||
from datetime import datetime
|
||||
from dateutil.relativedelta import relativedelta
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
||||
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()))
|
||||
@ -27,6 +48,15 @@ else:
|
||||
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)}.')
|
||||
# video.unlink()
|
||||
|
||||
urls = []
|
||||
|
||||
|
Reference in New Issue
Block a user