reduce
This commit is contained in:
1 parent
eee737a81d
commit
dbdfa7e66e
5 files changed
+40
-104
No files matched your search
+4
-6
@@ -23,7 +23,7 @@ links_id = {
|
||||
'星际2PartinG跳跳胖丁神族天梯第一视角': {'Twitch': 'partingthebigboy'},
|
||||
'星际2Rogue脑虫虫族天梯第一视角': {'Twitch': 'roguejinair'}
|
||||
# 'test':{'Twitch':'roguejinair', 'Panda':'439695'},
|
||||
# 'test1':{'Twitch':'rotterdam08', 'Panda':'1160340'}
|
||||
# 'test1':{'Twitch':'alivekr1', 'Panda':'1160340'}
|
||||
}
|
||||
|
||||
root_url = {
|
||||
@@ -33,13 +33,11 @@ root_url = {
|
||||
|
||||
|
||||
class Enginebase(object):
|
||||
def __init__(self, dictionary, key, suffix):
|
||||
self.dic = dictionary
|
||||
self.key = key
|
||||
self.urlpath = self.dic[key]
|
||||
def __init__(self, iterms, suffix):
|
||||
|
||||
self.key, self.urlpath = iterms
|
||||
self.url = self.join_url()
|
||||
self.suffix = suffix
|
||||
# self.file_name = self.join_filename(suffix)
|
||||
|
||||
def join_url(self):
|
||||
url = {}
|
||||
|
||||
+16
-41
@@ -1,6 +1,5 @@
|
||||
import os
|
||||
import signal
|
||||
|
||||
import requests
|
||||
import json
|
||||
import youtube_dl
|
||||
@@ -15,8 +14,8 @@ headers = {
|
||||
|
||||
class Downloadbase(Enginebase):
|
||||
|
||||
def __init__(self, dictionary, key, suffix, queue):
|
||||
Enginebase.__init__(self, dictionary, key, suffix)
|
||||
def __init__(self, items, suffix, queue):
|
||||
Enginebase.__init__(self, items, suffix)
|
||||
self.queue = queue
|
||||
|
||||
def check_stream(self):
|
||||
@@ -26,8 +25,6 @@ class Downloadbase(Enginebase):
|
||||
except youtube_dl.utils.DownloadError:
|
||||
# logger.debug('%s未开播或读取下载信息失败' % self.key)
|
||||
print('%s未开播或读取下载信息失败' % self.key)
|
||||
# logger.debug('准备补充上传'+key_)
|
||||
# supplemental_upload(self.dic, pfile_name_, key_, url_, value_)
|
||||
return False
|
||||
|
||||
def get_sinfo(self):
|
||||
@@ -44,19 +41,7 @@ class Downloadbase(Enginebase):
|
||||
print(info_list)
|
||||
return info_list
|
||||
|
||||
def is_recursion(self, value_):
|
||||
if value_ is None:
|
||||
|
||||
value_ = self.dic[self.key]
|
||||
self.dic.pop(self.key)
|
||||
|
||||
value_ = value_
|
||||
else:
|
||||
logger.info('准备递归下载' + self.key)
|
||||
|
||||
return value_
|
||||
|
||||
def download(self, ydl_opts, event, value):
|
||||
def download(self, ydl_opts, event):
|
||||
self.dl(ydl_opts)
|
||||
|
||||
def dl(self, ydl_opts):
|
||||
@@ -71,17 +56,14 @@ class Downloadbase(Enginebase):
|
||||
|
||||
@staticmethod
|
||||
def rename(file_name):
|
||||
# fname = os.path.splitext(file_name)[0]
|
||||
# suffix = os.path.splitext(file_name)[1]
|
||||
try:
|
||||
# logger.info('更名{0}'.format(pfile_name_+ '.part'))
|
||||
os.rename(file_name + '.part', file_name)
|
||||
logger.info('更名{0}为{1}'.format(file_name + '.part', file_name))
|
||||
except FileExistsError:
|
||||
os.rename(file_name + '.part', file_name)
|
||||
logger.info('FileExistsError:更名{0}为{1}'.format(file_name + '.part', file_name))
|
||||
|
||||
def run(self, event, value=None):
|
||||
def run(self, event):
|
||||
file_name = self.file_name
|
||||
event.dict_['url'] = self.url[self.__class__.__name__]
|
||||
# if event.dict_.get('file_name'):
|
||||
@@ -89,7 +71,6 @@ class Downloadbase(Enginebase):
|
||||
# else:
|
||||
# event.dict_['file_name'] = [file_name]
|
||||
if self.check_stream():
|
||||
value = self.is_recursion(value)
|
||||
ydl_opts = {
|
||||
'outtmpl': file_name,
|
||||
# 'format': '720p'
|
||||
@@ -98,18 +79,18 @@ class Downloadbase(Enginebase):
|
||||
}
|
||||
try:
|
||||
logger.info('开始下载%s:%s' % (self.__class__.__name__, self.key))
|
||||
self.download(ydl_opts, event, value)
|
||||
self.download(ydl_opts, event)
|
||||
except youtube_dl.utils.DownloadError:
|
||||
self.rename(file_name)
|
||||
self.run(event, value)
|
||||
logger.info('准备递归下载')
|
||||
self.run(event)
|
||||
finally:
|
||||
self.dic[self.key] = value
|
||||
logger.info('退出下载')
|
||||
|
||||
|
||||
class Twitch(Downloadbase):
|
||||
def __init__(self, dictionary, key, queue, suffix='mp4'):
|
||||
Downloadbase.__init__(self, dictionary=dictionary, key=key, suffix=suffix, queue=queue)
|
||||
def __init__(self, items, queue, suffix='mp4'):
|
||||
Downloadbase.__init__(self, items, suffix=suffix, queue=queue)
|
||||
|
||||
def check_stream(self):
|
||||
try:
|
||||
@@ -137,7 +118,7 @@ class Twitch(Downloadbase):
|
||||
return None
|
||||
return stream
|
||||
|
||||
def download(self, ydl_opts, event, value):
|
||||
def download(self, ydl_opts, event):
|
||||
print('开始下载twitch', self.key)
|
||||
info_list = self.get_sinfo()
|
||||
|
||||
@@ -152,13 +133,10 @@ class Twitch(Downloadbase):
|
||||
|
||||
|
||||
class Panda(Downloadbase):
|
||||
def __init__(self, dictionary, key, queue, suffix='flv'):
|
||||
Downloadbase.__init__(self, dictionary=dictionary, key=key, suffix=suffix, queue=queue)
|
||||
def __init__(self, items, queue, suffix='flv'):
|
||||
Downloadbase.__init__(self, items, suffix=suffix, queue=queue)
|
||||
|
||||
def download(self, ydl_opts, event, value):
|
||||
# file_name = ydl_opts['outtmpl']
|
||||
# fname = os.path.splitext(file_name)[0]
|
||||
# suffix = os.path.splitext(file_name)[1]
|
||||
def download(self, ydl_opts, event):
|
||||
|
||||
print('开始下载panda', self.key)
|
||||
signal.signal(signal.SIGTERM, work.signal_handler)
|
||||
@@ -173,15 +151,12 @@ class Panda(Downloadbase):
|
||||
|
||||
if self.check_stream():
|
||||
logger.info('实际未下载完成' + self.key)
|
||||
# if os.path.isfile(file_name):
|
||||
# os.rename(file_name, fname + str(time.time())[:10] + suffix)
|
||||
# logger.info(
|
||||
# '存在{0}更名为{1}'.format(file_name, fname + '时间' + suffix))
|
||||
self.run(event, value=value)
|
||||
logger.info('准备递归下载')
|
||||
self.run(event)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
# get_twitch_stream('https://api.twitch.tv/kraken/streams/1160340','233')
|
||||
for k in links_id:
|
||||
pd = Panda(dictionary=links_id, key=k, queue=1)
|
||||
pd = Panda(k, queue=1)
|
||||
pd.check_stream()
|
||||
+2
-31
@@ -1,5 +1,5 @@
|
||||
import time
|
||||
from eventdriven import *
|
||||
from eventdriven import eventType, event
|
||||
__all__ = ['event', 'eventType', 'Putevent']
|
||||
|
||||
|
||||
@@ -13,37 +13,8 @@ class Putevent(object):
|
||||
while True:
|
||||
for type_ in self.dict.copy():
|
||||
_event = eventType.Event(type_)
|
||||
_event.dict_['dict'] = self.dict
|
||||
self.eventManager.put(_event)
|
||||
time.sleep(interval)
|
||||
|
||||
|
||||
def func(event):
|
||||
print('running'+event.type_)
|
||||
time.sleep(10)
|
||||
print('done'+event.type_)
|
||||
def fun(event):
|
||||
print('running'+event.type_)
|
||||
time.sleep(10)
|
||||
print('done'+event.type_)
|
||||
if __name__ == '__main__':
|
||||
manager = event.EventEngine()
|
||||
|
||||
e = eventType.Event(type_='1')
|
||||
e1 = eventType.Event(type_='2')
|
||||
e2 = eventType.Event(type_='3')
|
||||
e3 = eventType.Event(type_='4')
|
||||
|
||||
manager.register(e.type_, func)
|
||||
manager.register(e.type_, fun)
|
||||
manager.register(e1.type_, func)
|
||||
manager.register(e2.type_, func)
|
||||
manager.register(e3.type_, func)
|
||||
manager.start()
|
||||
print('go')
|
||||
while True:
|
||||
for i in event.d:
|
||||
_event = eventType.Event(i)
|
||||
manager.put(_event)
|
||||
# print('put'+i)
|
||||
print(event.d)
|
||||
time.sleep(1)
|
||||
+12
-21
@@ -2,31 +2,22 @@
|
||||
from queue import Queue, Empty
|
||||
from threading import Thread
|
||||
from multiprocessing import Pool
|
||||
|
||||
d = {
|
||||
'1':'',
|
||||
'2':'',
|
||||
'3':'',
|
||||
'4':'',
|
||||
}
|
||||
from functools import wraps
|
||||
|
||||
|
||||
def signevent(dic):
|
||||
def decorator(func):
|
||||
@wraps(func)
|
||||
def wrapper(self,*args, **kw):
|
||||
value_ = dic[event.type_]
|
||||
dic.pop(event.type_)
|
||||
print(dic)
|
||||
try:
|
||||
func(self, *args, **kw)
|
||||
finally:
|
||||
dic[event.type_] = value_
|
||||
# print('add',event.type_)
|
||||
return wrapper
|
||||
return decorator
|
||||
def signevent(func):
|
||||
@wraps(func)
|
||||
def wrapper(handlers, event):
|
||||
value_ = event.dict_['dict'].pop(event.type_)
|
||||
try:
|
||||
func(handlers, event)
|
||||
finally:
|
||||
event.dict_['dict'][event.type_] = value_
|
||||
# print('add',event.type_)
|
||||
return wrapper
|
||||
|
||||
|
||||
@signevent
|
||||
def process(handlers, event):
|
||||
"""处理事件"""
|
||||
# 检查是否存在对该事件进行监听的处理函数
|
||||
|
||||
@@ -16,10 +16,10 @@ class Batch(object):
|
||||
self.dict = dic
|
||||
self.queue = Queue
|
||||
|
||||
def get_downloader(self, dl_dict, key, queue):
|
||||
def get_downloader(self, dl_dict, items, queue):
|
||||
obj = []
|
||||
for dl in dl_dict:
|
||||
downloader = getattr(download, dl)(self.dict, key, queue)
|
||||
downloader = getattr(download, dl)(items, queue)
|
||||
obj.append(downloader)
|
||||
return obj
|
||||
|
||||
@@ -37,14 +37,15 @@ class Batch(object):
|
||||
self.__eventManager.register(event_.type_, dl)
|
||||
|
||||
def register(self):
|
||||
for key in self.dict.copy():
|
||||
for d in self.dict.copy().items():
|
||||
key, value = d
|
||||
event_ = Event(type_=key)
|
||||
|
||||
dl = self.get_downloader(self.dict[key], key, self.queue)
|
||||
dl = self.get_downloader(value, d, self.queue)
|
||||
handler = self.get_handler(dl)
|
||||
self.addhandler(event_, handler)
|
||||
|
||||
uploader = upload.Upload(self.dict, key)
|
||||
uploader = upload.Upload(d)
|
||||
self.__eventManager.register(event_.type_, uploader.start)
|
||||
|
||||
|
||||
|
||||
Reference in new issue
Block a user