41 lines
1.2 KiB
Python
41 lines
1.2 KiB
Python
import datetime
|
|
import httpx
|
|
import json
|
|
import os
|
|
import re
|
|
from .. import config
|
|
|
|
PATTERN = re.compile(r"(.*)\(([0-9]+)\)$")
|
|
|
|
|
|
def extract(url):
|
|
_session = httpx.Client(http2=True)
|
|
html = _session.get(url, headers=config.headers)
|
|
with open(str(datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
|
|
) + 'test.json', mode='w', encoding='utf-8') as f:
|
|
json.dump(html.json(), f, ensure_ascii=False)
|
|
|
|
|
|
def save(data, filename, extention):
|
|
with open(filename + "_" + (datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')) + extention,
|
|
mode='w', encoding='utf-8') as f:
|
|
f.writelines(data)
|
|
|
|
|
|
def checkpath(filepath):
|
|
splitter = os.path.splitext(os.path.basename(filepath))
|
|
body = splitter[0]
|
|
extention = splitter[1]
|
|
newpath = filepath
|
|
counter = 1
|
|
while os.path.exists(newpath):
|
|
match = re.search(PATTERN, body)
|
|
if match:
|
|
counter = int(match[2]) + 1
|
|
num_with_bracket = f'({str(counter)})'
|
|
body = f'{match[1]}{num_with_bracket}'
|
|
else:
|
|
body = f'{body}({str(counter)})'
|
|
newpath = os.path.join(os.path.dirname(filepath), body + extention)
|
|
return newpath
|