1
0
mirror of https://github.com/vimagick/dockerfiles.git synced 2024-12-25 02:04:06 +02:00
This commit is contained in:
kev 2015-07-13 01:34:17 +08:00
parent 6419e55b82
commit 7c73585c86

View File

@ -6,27 +6,53 @@
import logging import logging
import os import os
import redis import redis
import time
import youtube_dl import youtube_dl
def download(url): def download(url):
with youtube_dl.YoutubeDL() as ydl: def hook(info):
time = int(time.time())
status = info['status']
if status == 'downloading':
rdb.zadd('running', time, url)
elif info['status'] == 'error':
rdb.zrem('running', url)
rdb.zadd('error', time, url)
elif status == 'finished':
rdb.zrem('running', url)
rdb.zadd('finished', time, url)
if rdb.zrank('finished', url) != None:
return False
opts = {
'format': os.getenv('FORMAT', 'best'),
'progress_hooks': [hook],
}
with youtube_dl.YoutubeDL(opts) as ydl:
ydl.download([url]) ydl.download([url])
return True
if __name__ == '__main__': if __name__ == '__main__':
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', datefmt='%FT%T', level='INFO') logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', datefmt='%FT%T', level='INFO')
logging.info('connect redis') logging.info('connect redis')
rdb = redis.StrictRedis(host='redis', password=os.getenv('PASSWORD')) rdb = redis.StrictRedis(host='redis', db=os.getenv('DATABASE', 0), password=os.getenv('PASSWORD'))
rdb.ping() rdb.ping()
while True: while True:
try: try:
_, url = rdb.brpop('urls') _, url = rdb.brpop('pending')
logging.info('process: %s', url) logging.info('process: %s', url)
download(url) ok = download(url)
logging.info('success: %s', ok)
except Exception as ex: except Exception as ex:
logging.error('error: %s', ex) logging.error('error: %s', ex)