rat/extra.py

50 lines
1.3 KiB
Python
Raw Normal View History

2023-07-11 11:32:13 +00:00
'''Extra APIs'''
import requests
import bs4
import re
2023-07-11 14:23:48 +00:00
from shared import *
2023-07-12 14:52:47 +00:00
# TODO: known bug, can't extract from super old editor images.
2023-07-11 14:23:48 +00:00
def extract_image_name(url):
match = re.search(r'/(\w+)\.jpg', url)
2023-07-12 14:52:47 +00:00
try:
return match.group(1) + '.jpg'
except:
return '404.jpg'
2023-07-11 14:23:48 +00:00
2023-07-13 10:48:49 +00:00
@cache.memoize(timeout=60)
2023-07-11 14:23:48 +00:00
def find_tieba_info(tname):
2023-07-11 11:32:13 +00:00
"""Get the tiebat avatar for the forum name.
:param tname: the name of the target forum.
:returns: the internal ID of the corresponding avatar.
"""
info = { 'name': tname }
2023-07-13 10:48:49 +00:00
res = requests.get('https://tieba.baidu.com/f',
params={'kw': tname},
allow_redirects=False)
# Baidu will bring us to the search page, so we ignore it.
if res.status_code == 302:
raise ValueError('您搜索的贴吧不存在')
2023-07-11 11:32:13 +00:00
soup = bs4.BeautifulSoup(res.text, 'html.parser')
elems = soup.select('#forum-card-head')
2023-07-11 14:23:48 +00:00
info['avatar'] = extract_image_name(elems[0]['src'])
2023-07-11 11:32:13 +00:00
footer = soup.select('.th_footer_l')[0]
stat_elems = footer.findAll('span', {'class': 'red_text'}, recursive=False)
stats = list(map(lambda x: int(x.text), stat_elems))
info |= { 'topic': stats[0], 'thread': stats[1], 'member': stats[2] }
slogan = soup.select('.card_slogan')[0]
info['desc'] = slogan.text
return info