intro pyyml

This commit is contained in:
耿伦伦 2021-11-30 20:33:32 +08:00
parent 909f862626
commit 93ff79125f
15 changed files with 57 additions and 10 deletions

0
config/__init__.py Normal file
View File

Binary file not shown.

Binary file not shown.

9
config/helper.py Normal file
View File

@ -0,0 +1,9 @@
from pathlib import Path
import yaml
def config():
settings_file = str(Path(__file__).parent.absolute()) + '/settings.yml'
with open(settings_file, 'r') as f:
return yaml.load(f, Loader=yaml.FullLoader)

11
config/settings.yml Normal file
View File

@ -0,0 +1,11 @@
watchdog:
dir: '/Users/geng/douyin_live/'
webdriver:
bin: '/usr/local/bin/chromedriver'
proxy: '127.0.0.1:8080'
mongo:
uri : 'mongodb://localhost:27017/'
dbname: 'tiktok'
enabled: 0

10
main.py
View File

@ -1,9 +1,15 @@
import sys import sys
from urllib.parse import urlparse
from scripts import watcher, webdriver from scripts import watcher, webdriver
from config.helper import config
if __name__ == '__main__': if __name__ == '__main__':
webdriver.lunch(sys.argv[1]) if len(sys.argv) == 1 or not urlparse(sys.argv[1]).scheme:
print('Invalid url provided, please check...')
sys.exit(1)
w = watcher.Watcher(directory='/Users/geng/douyin_live') webdriver.go(sys.argv[1])
w = watcher.Watcher(directory=config()['watchdog']['dir'])
w.run() w.run()

View File

@ -1,5 +1,7 @@
from store.mongo import MongoStore from store.mongo import MongoStore
from config.helper import config
class Base: class Base:
instance = None instance = None
@ -11,6 +13,9 @@ class Base:
return self.instance.user return self.instance.user
def persists(self): def persists(self):
if config()['mongo']['enabled'] != 'on':
return
try: try:
store = MongoStore() store = MongoStore()
store.set_collection('user') store.set_collection('user')

View File

@ -11,6 +11,7 @@ protobuf==3.19.1
pycparser==2.21 pycparser==2.21
pymongo==3.12.1 pymongo==3.12.1
pyOpenSSL==21.0.0 pyOpenSSL==21.0.0
PyYAML==6.0
selenium==4.1.0 selenium==4.1.0
six==1.16.0 six==1.16.0
sniffio==1.2.0 sniffio==1.2.0

Binary file not shown.

View File

@ -4,10 +4,12 @@
import uuid import uuid
from mitmproxy import http from mitmproxy import http
from config.helper import config
class Writer: class Writer:
def response(self, flow: http.HTTPFlow) -> None: def response(self, flow: http.HTTPFlow) -> None:
if flow.request.host == 'live.douyin.com': if flow.request.host == 'live.douyin.com':
with open('/Users/geng/douyin_live/' + uuid.uuid4().hex, 'wb') as f: with open(config().mitmproxy.log_dir + uuid.uuid4().hex, 'wb') as f:
f.write(bytes(flow.response.content)) f.write(bytes(flow.response.content))
addons = [Writer()] addons = [Writer()]

View File

@ -1,13 +1,24 @@
from selenium import webdriver from selenium import webdriver
from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.common.proxy import Proxy, ProxyType
def lunch(url): from config.helper import config
def go(url):
chrome_options = Options() chrome_options = Options()
chrome_options.add_argument('--proxy-server=127.0.0.1:8080') chrome_options.add_argument('--proxy-server=%s' % config()['webdriver']['proxy'])
chrome_options.add_argument('--headless') # chrome_options.add_argument('--headless')
chrome_options.add_experimental_option('detach', True) chrome_options.add_experimental_option('detach', True)
driver = webdriver.Chrome(options=chrome_options) proxy = Proxy()
proxy.proxy_type = ProxyType.MANUAL
proxy.http_proxy = config()['webdriver']['proxy']
proxy.ssl_proxy = config()['webdriver']['proxy']
capabilities = DesiredCapabilities.CHROME
proxy.add_to_capabilities(capabilities)
driver = webdriver.Chrome(options=chrome_options, desired_capabilities=capabilities, executable_path=config()['webdriver']['bin'])
driver.get(url) driver.get(url)

View File

@ -1,9 +1,11 @@
import pymongo import pymongo
from config.helper import config
class MongoStore: class MongoStore:
def __init__(self): def __init__(self):
self.client = pymongo.MongoClient("mongodb://localhost:27017/") self.client = pymongo.MongoClient(config()['mongo']['uri'])
self.db = self.client['tiktok'] self.db = self.client[config()['mongo']['dbname']]
def set_collection(self, collection): def set_collection(self, collection):
self.collection = self.db[collection] self.collection = self.db[collection]