1
0
mirror of https://github.com/vimagick/dockerfiles.git synced 2024-12-16 11:37:32 +02:00
dockerfiles/youtube/youtube-worker/worker.py

59 lines
1.3 KiB
Python
Raw Normal View History

2015-07-12 18:43:50 +02:00
#!/usr/bin/env python
#
# youtube_dl worker
#
import logging
import os
import redis
2015-07-12 19:34:17 +02:00
import time
2015-07-12 18:43:50 +02:00
import youtube_dl
def download(url):
2015-07-12 19:34:17 +02:00
def hook(info):
2015-07-12 19:46:37 +02:00
now = int(time.time())
2015-07-12 19:34:17 +02:00
status = info['status']
if status == 'downloading':
2015-07-12 19:46:37 +02:00
rdb.zadd('running', now, url)
2015-07-12 19:34:17 +02:00
elif info['status'] == 'error':
rdb.zrem('running', url)
2015-07-12 19:46:37 +02:00
rdb.zadd('error', now, url)
2015-07-12 19:34:17 +02:00
elif status == 'finished':
rdb.zrem('running', url)
2015-07-12 19:46:37 +02:00
rdb.zadd('finished', now, url)
2015-07-12 19:34:17 +02:00
if rdb.zrank('finished', url) != None:
return False
opts = {
'format': os.getenv('FORMAT', 'best'),
'progress_hooks': [hook],
}
with youtube_dl.YoutubeDL(opts) as ydl:
2015-07-12 18:43:50 +02:00
ydl.download([url])
2015-07-12 19:34:17 +02:00
return True
2015-07-12 18:43:50 +02:00
if __name__ == '__main__':
2015-07-12 19:01:12 +02:00
logging.basicConfig(format='%(asctime)s [%(levelname)s] %(message)s', datefmt='%FT%T', level='INFO')
2015-07-12 18:43:50 +02:00
logging.info('connect redis')
2015-07-12 19:34:17 +02:00
rdb = redis.StrictRedis(host='redis', db=os.getenv('DATABASE', 0), password=os.getenv('PASSWORD'))
2015-07-12 18:43:50 +02:00
rdb.ping()
while True:
try:
2015-07-12 19:34:17 +02:00
_, url = rdb.brpop('pending')
2015-07-12 18:43:50 +02:00
logging.info('process: %s', url)
2015-07-12 19:34:17 +02:00
ok = download(url)
logging.info('success: %s', ok)
2015-07-12 18:43:50 +02:00
except Exception as ex:
logging.error('error: %s', ex)