Пример #1
0
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)
Пример #2
0
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)