2015-07-12 18:43:50 +02:00
|
|
|
#!/usr/bin/env python
|
2015-07-13 06:45:26 +02:00
|
|
|
#-*- coding: utf-8 -*-
|
2015-07-12 18:43:50 +02:00
|
|
|
#
|
|
|
|
# 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:58:37 +02:00
|
|
|
elif status == 'error':
|
2015-07-12 19:34:17 +02:00
|
|
|
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:
|
2015-07-12 20:05:59 +02:00
|
|
|
logging.warn('This video already exists.')
|
2015-07-12 19:34:17 +02:00
|
|
|
return False
|
|
|
|
|
2015-07-12 19:58:37 +02:00
|
|
|
try:
|
|
|
|
opts = {
|
|
|
|
'format': os.getenv('FORMAT', 'best'),
|
2015-07-13 06:45:26 +02:00
|
|
|
'outtmpl': unicode(os.getenv('OUTTMPL', '%(title)s-%(id)s.%(ext)s'), 'utf-8'),
|
2015-07-12 19:58:37 +02:00
|
|
|
'progress_hooks': [hook],
|
2015-07-13 04:31:43 +02:00
|
|
|
'writeinfojson': True,
|
2015-07-12 19:58:37 +02:00
|
|
|
}
|
|
|
|
with youtube_dl.YoutubeDL(opts) as ydl:
|
|
|
|
ydl.download([url])
|
|
|
|
except Exception as ex:
|
|
|
|
logging.error('error: %s', ex)
|
|
|
|
rdb.zrem('running', url)
|
|
|
|
rdb.zadd('error', int(time.time()), url)
|
|
|
|
return False
|
2015-07-12 18:43:50 +02:00
|
|
|
|
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)
|
|
|
|
|