尝试大改,可读性有点差的版本,基本做到能实时处理弹幕

This commit is contained in:
2022-06-03 23:08:43 +08:00
parent 3f95b07009
commit 74a15849ab
16 changed files with 181 additions and 204 deletions

@ -1,16 +1,23 @@
# ! IMPORT ! make sure you ran mitmproxy with this script,
# eg: `/path/to/mitmproxy -s mitmproxy.py`
import uuid
import time
from mitmproxy import http
import re
import requests
import base64
session = requests.session()
class Writer:
def websocket_message(self, flow: http.HTTPFlow) :
re_c = re.search('webcast3-ws-web-.*\.douyin\.com', flow.request.host)
if re_c :
with open('/Users/geng/douyin_live/' + uuid.uuid4().hex, 'wb') as f:
mess = flow.websocket.messages[-1].content
f.write(bytes(mess))
def websocket_message(self, flow: http.HTTPFlow):
re_c = re.search('webcast\d-ws-web-.*\.douyin\.com', flow.request.host)
if re_c:
message = flow.websocket.messages[-1].content
session.post("http://127.0.0.1:5000/message", data=message, headers={
"X-MITM_TS": str(time.time()),
"X_REFERER": flow.request.host
}, timeout=(1, 1))
addons = [Writer()]

@ -1,43 +0,0 @@
import concurrent.futures
import queue
import time
from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
from messages.utils import unpackMsgBin
q = queue.Queue()
class Watcher:
DIRECTORY_TO_WATCH = ""
def __init__(self, directory):
self.observer = Observer()
self.DIRECTORY_TO_WATCH = directory
def run(self):
event_handler = Handler()
self.observer.schedule(event_handler, self.DIRECTORY_TO_WATCH, recursive=True)
self.observer.start()
try:
while True:
with concurrent.futures.ThreadPoolExecutor() as executor:
time.sleep(0.2)
executor.submit(unpackMsgBin, q.get())
except:
self.observer.stop()
self.observer.join()
class Handler(FileSystemEventHandler):
@staticmethod
def on_any_event(event):
if event.is_directory:
return None
elif event.event_type == 'created':
q.put(event.src_path)

@ -2,7 +2,7 @@ import requests
import json
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.edge.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import Proxy, ProxyType
from selenium.webdriver.common.by import By
@ -10,12 +10,10 @@ from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support.expected_conditions import presence_of_element_located
from config.helper import config
from store.mongo import MongoStore
def go(url):
chrome_options = Options()
chrome_options.add_argument('--proxy-server=%s' % config()['webdriver']['proxy'])
chrome_options.add_argument('--headless')
chrome_options.add_argument('--proxy-server=%s:%s' % (config()['mitm']['host'], config()['mitm']['port']))
# 2022-04-09 添加一个忽略证书
chrome_options.add_argument('-ignore-certificate-errors')
@ -24,10 +22,10 @@ def go(url):
proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = config()['webdriver']['proxy']
proxy.ssl_proxy = config()['webdriver']['proxy']
proxy.http_proxy = "%s:%s" % (config()['mitm']['host'], config()['mitm']['port'])
proxy.ssl_proxy = "%s:%s" % (config()['mitm']['host'], config()['mitm']['port'])
capabilities = DesiredCapabilities.CHROME
capabilities = DesiredCapabilities.EDGE
proxy.add_to_capabilities(capabilities)
with webdriver.Chrome(options=chrome_options,
@ -45,19 +43,7 @@ def go(url):
json_obj = json.loads(json_str)
roomInfo = json_obj['initialState']['roomStore']['roomInfo']
store = MongoStore()
store.set_collection('room')
store.insert_one({
'roomId': roomInfo['roomId'],
'web_rid': roomInfo['web_rid'],
'title': roomInfo['room']['title'],
'user_count_str': roomInfo['room']['user_count_str'],
'cover': roomInfo['room']['cover']['url_list'][0],
'admin_user_ids': roomInfo['room']['admin_user_ids'],
'owner': roomInfo['room']['owner']
})
store.close()
print(roomInfo)
wait.until(presence_of_element_located((By.CLASS_NAME, "oSu9Aw19")))