Skip to content

Commit

Permalink
setup
Browse files Browse the repository at this point in the history
  • Loading branch information
ForgQi committed Jan 19, 2021
1 parent 84de72a commit 7114083
Show file tree
Hide file tree
Showing 30 changed files with 120 additions and 76 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ test.py
/upload.py
/work.py
*.zip
*.egg-info
11 changes: 3 additions & 8 deletions Bilibili.py → biliup/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
#!/usr/bin/python3
# coding:utf8
import asyncio
import sys
import common
from engine import main
from common.Daemon import Daemon
from .engine import main
from .common.Daemon import Daemon

if __name__ == '__main__':

sys.excepthook = common.new_hook

def _main():
daemon = Daemon('watch_process.pid')
if len(sys.argv) == 2:
if 'start' == sys.argv[1]:
Expand Down
7 changes: 7 additions & 0 deletions biliup/__main__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/usr/bin/python3
# coding:utf8
from . import _main


if __name__ == '__main__':
_main()
2 changes: 1 addition & 1 deletion common/Daemon.py → biliup/common/Daemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import os
import time
import atexit
from engine import main
from ..engine import main

logger = logging.getLogger('log01')

Expand Down
4 changes: 4 additions & 0 deletions common/__init__.py → biliup/common/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import os
from datetime import datetime, timezone, timedelta
from .log import SafeRotatingFileHandler
import logging.config
import sys


def time_now():
Expand All @@ -15,6 +17,8 @@ def new_hook(t, v, tb):
logger.error("Uncaught exception:", exc_info=(t, v, tb))


logging.SafeRotatingFileHandler = SafeRotatingFileHandler
sys.excepthook = new_hook
log_file_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'configlog.ini')
logging.config.fileConfig(log_file_path)
logger = logging.getLogger('log01')
2 changes: 1 addition & 1 deletion common/configlog.ini → biliup/common/configlog.ini
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ formatter=form02
args=(sys.stdout,)

[handler_handler02]
class=common.log.SafeRotatingFileHandler
class=SafeRotatingFileHandler
level=INFO
formatter=form01
args=("ds_update.log", "W0", 1, 1)
Expand Down
2 changes: 1 addition & 1 deletion common/decorators.py → biliup/common/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def decorator(cls):

@classmethod
def sorted_checker(cls, urls):
from engine.plugins import general
from ..engine.plugins import general
curls = urls.copy()
checker_plugins = {}
for plugin in cls.download_plugins:
Expand Down
File renamed without changes.
File renamed without changes.
16 changes: 8 additions & 8 deletions common/reload.py → biliup/common/reload.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import asyncio
import sys
import os
import time
import subprocess
import logging

from common.timer import Timer
from .timer import Timer

logger = logging.getLogger('log01')

Expand Down Expand Up @@ -73,11 +72,12 @@ async def atimer(self):
for watched in self.watched:
watched.stop()
self.stop()
parent_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) # 获得所在的目录的父级目
path = os.path.join(parent_path, 'Bilibili.py')
if sys.platform == 'win32':
args = ["python", path]
else:
args = [path, 'start']
# parent_path = os.path.abspath(os.path.dirname(os.path.dirname(__file__))) # 获得所在的目录的父级目
# path = os.path.join(parent_path, '__main__.py')
# if sys.platform == 'win32':
# args = ["python", path]
# else:
# args = [path, 'start']
args = ['biliup', 'start']
subprocess.Popen(args)
return logger.info('重启')
File renamed without changes.
14 changes: 7 additions & 7 deletions engine/__init__.py → biliup/engine/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

import yaml

from common.decorators import Plugin
from common.event import Event, event_manager
from common.reload import AutoReload
from common.timer import Timer
from ..common.decorators import Plugin
from ..common.event import Event, event_manager
from ..common.reload import AutoReload
from ..common.timer import Timer

with open(r'config.yaml', encoding='utf-8') as stream:
config = yaml.load(stream, Loader=yaml.FullLoader)
Expand All @@ -20,17 +20,17 @@ def invert_dict(d: dict):


async def main():
import engine.plugins
from . import plugins
streamers = config['streamers']
streamer_url = {k: v['url'] for k, v in streamers.items()}
inverted_index = invert_dict(streamer_url)
urls = list(inverted_index.keys())
url_status = dict.fromkeys(inverted_index, 0)
checker = Plugin(engine.plugins).sorted_checker(urls)
checker = Plugin(plugins).sorted_checker(urls)
# 初始化事件管理器
event_manager.context = {**config, 'urls': urls, 'url_status': url_status,
'checker': checker, 'inverted_index': inverted_index, 'streamer_url': streamer_url}
from engine.handler import CHECK_UPLOAD, CHECK
from ..engine.handler import CHECK_UPLOAD, CHECK
event_manager.start()

async def check_timer():
Expand Down
4 changes: 2 additions & 2 deletions engine/downloader.py → biliup/engine/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import time
from urllib.error import HTTPError

from common.decorators import Plugin
from engine.plugins import general, BatchCheckBase
from ..common.decorators import Plugin
from .plugins import general, BatchCheckBase

logger = logging.getLogger('log01')

Expand Down
13 changes: 7 additions & 6 deletions engine/handler.py → biliup/engine/handler.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import common
from common import logger
from common.event import Event, event_manager
from engine.downloader import download, check_url
from engine.plugins.upload import UploadBase
from engine.uploader import upload
# import common
from .. import common
from ..common import logger
from ..common.event import Event, event_manager
from .downloader import download, check_url
from .plugins.upload import UploadBase
from .uploader import upload

CHECK = 'check'
CHECK_UPLOAD = 'check_upload'
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import requests

from common.decorators import Plugin
from engine.plugins import match1, logger
from engine.plugins.base_adapter import FFmpegdl
from ...common.decorators import Plugin
from ..plugins import match1, logger
from .base_adapter import FFmpegdl

VALID_URL_BASE = r"https?://play\.afreecatv\.com/(?P<username>\w+)(?:/\d+)?"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
import streamlink
import youtube_dl

import engine
from engine.plugins import logger
from .. import config
from ..plugins import logger

fake_headers = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
Expand Down Expand Up @@ -157,7 +157,7 @@ def __init__(self, fname, url, suffix=None):
self.opt_args = []
self.default_output_args = [
'-bsf:a', 'aac_adtstoasc',
'-fs', f"{engine.config.get('file_size') if engine.config.get('file_size') else '2621440000'}"
'-fs', f"{config.get('file_size') if config.get('file_size') else '2621440000'}"
]
self.default_input_args = ['-headers', ''.join('%s: %s\r\n' % x for x in fake_headers.items()),
'-reconnect_streamed', '1', '-reconnect_delay_max', '20', '-rw_timeout', '20000000']
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ykdl.common import url_to_module

from common.decorators import Plugin
from engine.plugins.base_adapter import FFmpegdl
from ...common.decorators import Plugin
from .base_adapter import FFmpegdl


@Plugin.download(regexp=r'(?:https?://)?(?:(?:www|m|live)\.)?bilibili\.com')
Expand Down
6 changes: 3 additions & 3 deletions engine/plugins/douyu.py → biliup/engine/plugins/douyu.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from ykdl.common import url_to_module
from ykdl.util.jsengine import chakra_available, quickjs_available, external_interpreter

from common.decorators import Plugin
from engine.plugins import logger
from engine.plugins.base_adapter import FFmpegdl
from ...common.decorators import Plugin
from ..plugins import logger
from .base_adapter import FFmpegdl


@Plugin.download(regexp=r'(?:https?://)?(?:(?:www|m)\.)?douyu\.com')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ykdl.common import url_to_module

from engine.plugins import logger
from engine.plugins.base_adapter import YDownload, SDownload, FFmpegdl
from ..plugins import logger
from .base_adapter import YDownload, SDownload, FFmpegdl


class Generic(FFmpegdl):
Expand Down
6 changes: 3 additions & 3 deletions engine/plugins/huya.py → biliup/engine/plugins/huya.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

import requests

from common.decorators import Plugin
from engine.plugins import match1, logger
from engine.plugins.base_adapter import fake_headers, FFmpegdl
from ...common.decorators import Plugin
from ..plugins import match1, logger
from .base_adapter import fake_headers, FFmpegdl


@Plugin.download(regexp=r'(?:https?://)?(?:(?:www|m)\.)?huya\.com')
Expand Down
6 changes: 3 additions & 3 deletions engine/plugins/twitch.py → biliup/engine/plugins/twitch.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

import requests

from common.decorators import Plugin
from engine.plugins import BatchCheckBase, match1
from engine.plugins.base_adapter import FFmpegdl
from ...common.decorators import Plugin
from ..plugins import BatchCheckBase, match1
from .base_adapter import FFmpegdl

VALID_URL_BASE = r'(?:https?://)?(?:(?:www|go|m)\.)?twitch\.tv/(?P<id>[0-9_a-zA-Z]+)'
_OPERATION_HASHES = {
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.ui import WebDriverWait

import engine
from common.decorators import Plugin
from engine.plugins.upload import UploadBase, logger
from engine.plugins.upload.slider import slider_cracker
from ... import config
from ....common.decorators import Plugin
from ..upload import UploadBase, logger
from .slider import slider_cracker


@Plugin.upload("bilibili")
Expand Down Expand Up @@ -54,7 +54,7 @@ def upload(self, file_list):
options = webdriver.ChromeOptions()

options.add_argument('headless')
self.driver = webdriver.Chrome(executable_path=engine.config.get('chromedriver_path'), chrome_options=options)
self.driver = webdriver.Chrome(executable_path=config.get('chromedriver_path'), chrome_options=options)
# service_log_path=service_log_path)
try:
self.driver.get("https://www.bilibili.com")
Expand Down Expand Up @@ -112,9 +112,9 @@ def login(self, filename):
WebDriverWait(self.driver, 10).until(
ec.presence_of_element_located((By.XPATH, r'//*[@id="login-username"]')))
username = self.driver.find_element_by_xpath(r'//*[@id="login-username"]')
username.send_keys(engine.config['user']['account']['username'])
username.send_keys(config['user']['account']['username'])
password = self.driver.find_element_by_xpath('//*[@id="login-passwd"]')
password.send_keys(engine.config['user']['account']['password'])
password.send_keys(config['user']['account']['password'])
self.driver.find_element_by_class_name("btn-login").click()
# logger.info('第四步')
# try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@
from requests import utils
from requests.adapters import HTTPAdapter, Retry

import engine
from common.decorators import Plugin
from engine.plugins.upload import UploadBase, logger
from ... import config
from ....common.decorators import Plugin
from ..upload import UploadBase, logger


@Plugin.upload(platform="bili_web")
class BiliWeb(UploadBase):
def __init__(self, principal, data, user, lines='AUTO', threads=3, tid=174, tags=None, cover_path=None):
super().__init__(principal, data, persistence_path='engine/bili.cookie')
super().__init__(principal, data, persistence_path='bili.cookie')
if tags is None:
tags = ['星际争霸2', '电子竞技']
self.user = user
Expand Down Expand Up @@ -134,13 +134,16 @@ def login_by_password(self, username, password):
r = response.json()
if r['code'] != 0 and r.get('data') is None:
raise RuntimeError(r)
for cookie in r['data']['cookie_info']['cookies']:
self.__session.cookies.set(cookie['name'], cookie['value'])
if 'bili_jct' == cookie['name']:
self.__bili_jct = cookie['value']
self.cookies = self.__session.cookies.get_dict()
self.access_token = r['data']['token_info']['access_token']
self.refresh_token = r['data']['token_info']['refresh_token']
try:
for cookie in r['data']['cookie_info']['cookies']:
self.__session.cookies.set(cookie['name'], cookie['value'])
if 'bili_jct' == cookie['name']:
self.__bili_jct = cookie['value']
self.cookies = self.__session.cookies.get_dict()
self.access_token = r['data']['token_info']['access_token']
self.refresh_token = r['data']['token_info']['refresh_token']
except:
raise RuntimeError(r)
return r

def login_by_cookies(self, cookie):
Expand Down Expand Up @@ -374,14 +377,14 @@ def submit(self):

logger.info(f'用户权重: {user_weight} => 网页端分p数量受到限制使用客户端api端提交')
if not self.access_token:
self.login_by_password(**engine.config['user']['account'])
self.login_by_password(**config['user']['account'])
self.store()
while True:
ret = self.__session.post(f'http://member.bilibili.com/x/vu/client/add?access_key={self.access_token}',
timeout=5, json=asdict(self.video)).json()
if ret['code'] == -101:
logger.info(f'刷新token{ret}')
self.login_by_password(**engine.config['user']['account'])
self.login_by_password(**config['user']['account'])
self.store()
continue
break
Expand Down
File renamed without changes.
6 changes: 3 additions & 3 deletions engine/uploader.py → biliup/engine/uploader.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import inspect

from common import logger
from common.decorators import Plugin
import engine
from .. import engine
from ..common import logger
from ..common.decorators import Plugin


def upload(platform, index, data):
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[build-system]
requires = ["setuptools", "wheel"]
build-backend = "setuptools.build_meta"
Loading

0 comments on commit 7114083

Please sign in to comment.