def main(): taskDispatcher = TaskDispatcher() while True: try: url = input('输入暴力猴链接或本地m3u8路径: ').strip() fileName = input('输入文件名: ').strip() isMultiPart = url.find('www.bilibili.com') != -1 pRange = input('输入首、尾P(空格分隔)或单P: ').strip() if isMultiPart else None except KeyboardInterrupt: break taskDispatcher.dispatch(url=url, fileName=fileName, pRange=pRange)
class Runner: def __init__(self): self.taskDispatcher = TaskDispatcher() def start(self): if config.interactive: self.startInteractive() else: self.startDaemon(config.port) # 交互模式,接受用户输入 def startInteractive(self): while True: try: url = input('输入暴力猴链接或本地m3u8路径: ').strip() fileName = input('输入文件名: ').strip() isMultiPart = url.find('www.bilibili.com') != -1 pRange = input('输入首、尾P(空格分隔)或单P: ').strip() if isMultiPart else None except KeyboardInterrupt: break self.taskDispatcher.dispatch(url=url, fileName=fileName, pRange=pRange) # 守护模式,监听web请求 def startDaemon(self, port): # 消费者 t = threading.Thread(target=self._downloadThread, daemon=True) t.start() # 生产者 server = DownloadServer(port) while True: try: print("Listening on port %d for clients..." % port) server.serve_forever() except KeyboardInterrupt: if self.taskDispatcher.task: self.taskDispatcher.shutdown() else: break def _downloadThread(self): while True: task = DownloadServer.taskQueue.get() print('Handle: "%s"' % task['fileName']) self.taskDispatcher.dispatch(**task)
# -*- coding:utf-8 -*- import json import threading import queue from dispatcher import TaskDispatcher import tools from tools import WebServer, CLIENT_CLOSE_EXCEPTION import os if(os.name=="nt"): os.environ['PATH']=os.environ['PATH']+";"+os.path.abspath(os.path.dirname(__file__)) taskDispatcher = TaskDispatcher() taskQueue = queue.Queue() dataCacheSize = 10 # 下载线程 def downloadLoop(): while True: task = taskQueue.get() print('Handle: "%s"' % task['fileName']) taskDispatcher.dispatch(**task) class DownloadServer(WebServer): ESTABLISHED = 0 IN_TRANSIT = 1 def do_POST(self, client):
def __init__(self): self.taskDispatcher = TaskDispatcher()