-
Notifications
You must be signed in to change notification settings - Fork 70
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
79 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,46 +1,26 @@ | ||
import sys | ||
import json | ||
from pathlib import Path | ||
from logging import Logger | ||
from XstreamDL_CLI.cmdargs import CmdArgs | ||
|
||
|
||
class Headers: | ||
def __init__(self, logger: Logger): | ||
self.logger = logger | ||
self.referer = '' | ||
self.user_agent = ( | ||
'Mozilla/5.0 (Windows NT 10.0; Win64; x64) ' | ||
'AppleWebKit/537.36 (KHTML, like Gecko) ' | ||
'Chrome/88.0.4324.190 Safari/537.36' | ||
) | ||
self.headers = {} | ||
|
||
def get(self, args: CmdArgs) -> dict: | ||
self.__generate(args) | ||
return self.headers | ||
|
||
def __generate(self, args: CmdArgs): | ||
''' | ||
- 不指定user-agent 使用默认值 | ||
- 指定user-agent 使用指定值 如果为"" 那么user-agent就是"" | ||
- 不指定referer 就不设定referer 否则设定 | ||
''' | ||
if args.user_agent != '': | ||
self.headers['user-agent'] = args.user_agent | ||
if getattr(sys, 'frozen', False): | ||
config_path = Path(sys.executable).parent / args.headers | ||
else: | ||
self.headers['user-agent'] = self.user_agent | ||
if args.referer != '': | ||
self.headers['referer'] = args.referer | ||
if args.headers != '': | ||
self.__add_headers(args.headers) | ||
config_path = Path(__file__).parent.parent / args.headers | ||
if config_path.exists() is False: | ||
self.logger.warning(f'{config_path.stem} is not exists, put your config file to {config_path.parent.resolve().as_posix()}') | ||
return | ||
try: | ||
self.headers = json.loads(config_path.read_text(encoding='utf-8')) | ||
except Exception as e: | ||
self.logger.error(f'try to load {config_path.resolve().as_posix()} failed', exc_info=e) | ||
self.logger.debug(f'use headers:\n{json.dumps(self.headers, ensure_ascii=False, indent=4)}') | ||
|
||
def __add_headers(self, text: str): | ||
text = text.strip() | ||
for one_header in text.split('|'): | ||
data = one_header.strip().split(':', maxsplit=1) | ||
if len(data) == '': | ||
continue | ||
if len(data) == 1: | ||
self.headers[data[0]] = '' | ||
else: | ||
self.headers[data[0]] = data[1] | ||
return self.headers |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
aiohttp | ||
aiohttp_socks | ||
argparse | ||
pycryptodome |