-
Notifications
You must be signed in to change notification settings - Fork 11
/
utils.py
92 lines (74 loc) · 2.37 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import hashlib
import io
import re
from urllib import parse
import qrcode
import requests
from bs4 import BeautifulSoup
from PIL import Image
def get_qrcode_img(content):
img = qrcode.make(content)
output = io.BytesIO()
img.save(output, 'JPEG', quality=70)
output.seek(0, 0)
return output
def normalize_whitespace(text):
text = re.sub(r'[\n\r\t]', ' ', text)
text = re.sub(r' +', ' ', text)
return text.strip()
def extract_feed(html_content, url=''):
if not html_content:
return html_content
soup = BeautifulSoup(html_content, 'lxml')
for i in soup.find_all('a'): # 删除超链接
if i.get('href'):
del i['href']
if i.get('target'):
del i['target']
for i in soup.find_all('img'): # 处理图片,防盗链
if i.get('srcset'):
del i['srcset']
if i.get('height'):
del i['height']
if i.get('width'):
del i['width']
src = i['src']
if src.startswith('/'):
t = parse.urlparse(url)
src = t.scheme + '://' + t.netloc + src
i['src'] = '/api/get-img?src=' + \
parse.quote(src) + '&url=' + parse.quote(url)
res = [str(i) for i in soup.body.contents]
return ''.join(res).replace('\xa0', '').strip()
def parse_url_path(s):
return parse.quote(s, safe=";/?:@&=+$,%")
def get_client(endpoint, username, password) -> list:
hash = hashlib.md5('{}:{}'.format(username, password).encode('utf8'))
hash = hash.hexdigest()
data = {'api_key': hash}
try:
r = requests.post(endpoint, data=data)
if r.status_code == requests.codes.OK:
if r.json()['auth'] == 1:
return [endpoint, data]
raise Exception("status_code:{}".format(r.status_code))
except Exception as e:
raise e
def resize_img(raw):
im = Image.open(raw)
width = im.size[0]
if width <= 1080:
height = im.size[1]
else:
bili = 1080 / width
width = int(width * bili)
height = int(im.size[1] * bili)
im = im.resize((width, height), Image.ANTIALIAS)
im = im.convert('RGB')
output = io.BytesIO()
im.save(output, 'JPEG', quality=90)
output.seek(0, 0)
return output
def get_json_from_fever(endpoint: str, data: dict, api: str):
r = requests.post(endpoint + '?api' + api, data=data)
return r.json()