Archived
1
0
Fork 0
This repository has been archived on 2024-04-26. You can view files and clone it, but cannot push or open issues or pull requests.
akari-bot/modules/user/userlib.py

210 lines
10 KiB
Python
Raw Normal View History

2020-06-13 12:43:43 +00:00
import re
2021-02-09 15:10:51 +00:00
import traceback
2020-06-13 12:43:43 +00:00
import urllib
2020-08-12 08:01:00 +00:00
2020-09-19 10:35:13 +00:00
import aiohttp
2021-02-01 15:13:11 +00:00
from modules.utils.UTC8 import UTC8
from modules.wiki.helper import check_wiki_available
2021-02-19 12:20:00 +00:00
from modules.wiki.wikilib import wikilib
from .tool import gender
2020-08-12 16:01:34 +00:00
2020-08-12 08:01:00 +00:00
async def get_data(url: str, fmt: str):
async with aiohttp.ClientSession() as session:
2020-08-12 16:01:34 +00:00
async with session.get(url, timeout=aiohttp.ClientTimeout(total=20)) as req:
2020-08-12 08:01:00 +00:00
if hasattr(req, fmt):
return await getattr(req, fmt)()
else:
raise ValueError(f"NoSuchMethod: {fmt}")
2020-08-12 16:01:34 +00:00
2020-09-25 15:19:54 +00:00
async def getwikiname(wikiurl):
2020-06-13 12:43:43 +00:00
try:
2021-02-09 15:57:18 +00:00
wikinameurl = wikiurl + '?action=query&meta=siteinfo&siprop=general&format=json'
2020-09-25 15:19:54 +00:00
wikiname = await get_data(wikinameurl, 'json')
2021-02-09 15:57:18 +00:00
Wikiname = wikiname['query']['general']['sitename']
2020-06-13 12:43:43 +00:00
except Exception:
Wikiname = 'Unknown'
2020-09-25 15:19:54 +00:00
return Wikiname
async def get_user_group(wikiurl):
groups = {}
user_group_link = wikiurl + '?action=query&meta=allmessages&amprefix=group-&format=json'
get_json = await get_data(user_group_link, 'json')
j = get_json['query']['allmessages']
for x in j:
2021-02-09 13:19:15 +00:00
name = re.sub('^group-', '', x['name'])
groups[name] = x['*']
return groups
2021-02-09 13:10:47 +00:00
def trans_user_group(user_group: list, group_dict: dict):
trans = []
for x in user_group:
2021-02-09 13:20:27 +00:00
if x != '*':
if x in group_dict:
trans.append(group_dict[x])
else:
trans.append(x)
return ''.join(trans)
2020-09-25 15:19:54 +00:00
def d(str1):
a = re.sub(r'<dd>|</dd>', '', str1)
return a
async def GetUser(wikiurl, username, argv=None):
2021-02-09 15:57:18 +00:00
print(wikiurl)
GetInterwiki = await wikilib().get_interwiki(wikiurl)
match_interwiki = re.match(r'(.*?):(.*)', username)
if match_interwiki:
if match_interwiki.group(1) in GetInterwiki:
2021-02-09 13:10:47 +00:00
wikiurl = await check_wiki_available(GetInterwiki[match_interwiki.group(1)])
if wikiurl:
2021-02-09 15:57:18 +00:00
return await GetUser(wikiurl[0], match_interwiki.group(2), argv)
2021-02-01 15:13:11 +00:00
UserJsonURL = wikiurl + '?action=query&list=users&ususers=' + username + '&usprop=groups%7Cblockinfo%7Cregistration%7Ceditcount%7Cgender&format=json'
2021-02-09 15:57:18 +00:00
try:
GetUserJson = await get_data(UserJsonURL, 'json')
except:
return '发生错误无法获取到页面信息请检查是否设置了对应Interwiki。'
2020-09-25 15:19:54 +00:00
Wikiname = await getwikiname(wikiurl)
GetUserGroupsList = await get_user_group(wikiurl)
2021-02-09 15:10:51 +00:00
GetArticleUrl = await wikilib().get_article_path(wikiurl)
2020-06-13 12:43:43 +00:00
try:
2020-09-25 15:19:54 +00:00
User = GetUserJson['query']['users'][0]['name']
Editcount = str(GetUserJson['query']['users'][0]['editcount'])
Group = trans_user_group(GetUserJson['query']['users'][0]['groups'], GetUserGroupsList)
2020-09-25 15:19:54 +00:00
Gender = gender(GetUserJson['query']['users'][0]['gender'])
Registration = UTC8(GetUserJson['query']['users'][0]['registration'], 'full')
2020-08-13 10:31:52 +00:00
rmuser = re.sub('User:', '', username)
2020-09-25 15:19:54 +00:00
Blockmessage = ''
if 'blockedby' in GetUserJson['query']['users'][0]:
2020-09-25 15:19:54 +00:00
BlockedBy = GetUserJson['query']['users'][0]['blockedby']
if BlockedBy:
Blockedtimestamp = UTC8(GetUserJson['query']['users'][0]['blockedtimestamp'], 'full')
Blockexpiry = UTC8(str(GetUserJson['query']['users'][0]['blockexpiry']), 'full')
2021-02-01 15:13:11 +00:00
Blockmessage = f'\n{User}正在被封禁!' + \
f'\n{BlockedBy}封禁,时间从{Blockedtimestamp}{Blockexpiry}'
if 'blockreason' in GetUserJson['query']['users'][0]:
2020-09-25 15:19:54 +00:00
Blockreason = GetUserJson['query']['users'][0]['blockreason']
if Blockreason:
Blockmessage += f',理由:“{Blockreason}'
if argv == '-r' or argv == '-p':
2021-02-09 16:02:00 +00:00
Editcount_Api = Editcount
2020-09-25 15:19:54 +00:00
from bs4 import BeautifulSoup as bs
2021-02-09 15:10:51 +00:00
try:
2021-02-09 15:57:18 +00:00
clawerurl = GetArticleUrl + 'UserProfile:' + username
clawer = await get_data(clawerurl, 'text')
soup = bs(clawer, 'html.parser')
2021-02-09 15:10:51 +00:00
stats = soup.find('div', class_='section stats')
point = soup.find('div', class_='score').text
dd = stats.find_all('dd')
2021-02-19 12:20:00 +00:00
Editcount = ('\n编辑过的Wiki' + str(dd[0]) + '\n创建数:' + str(dd[1]) + ' | 编辑数:' + str(
dd[2]) + '\n删除数:' + str(
2021-02-09 15:10:51 +00:00
dd[3]) + ' | 巡查数:' + str(dd[4]) + '\n本站排名:' + str(dd[5]) + ' | 全域排名:' + str(dd[6]) + '\n好友:' + str(
dd[7]))
Editcount = re.sub(r'<dd>|</dd>', '', Editcount)
Editcount += f' | Wikipoints{point}'
except:
Editcount = '无法获取到增强型用户页中的编辑信息。'
2021-02-09 16:02:00 +00:00
dd = ['?', '?', Editcount_Api, '?', '?', '?', '?']
2021-02-09 15:57:18 +00:00
point = '?'
2020-09-25 15:19:54 +00:00
if argv == '-p':
import uuid
import os
Registration = UTC8(GetUserJson['query']['users'][0]['registration'], 'notimezone')
matchlink = re.match(r'https?://(.*)/', wikiurl)
filepath = os.path.abspath('./assets/Favicon/' + matchlink.group(1) + '/')
if not os.path.exists(filepath):
2021-02-01 15:13:11 +00:00
favicon_path = os.path.abspath('./assets/Favicon/')
if not os.path.exists(favicon_path):
os.mkdir(favicon_path)
2020-09-25 15:19:54 +00:00
os.mkdir(filepath)
wikipng = os.path.abspath('./assets/Favicon/' + matchlink.group(1) + '/Wiki.png')
if not os.path.exists(wikipng):
from .dpng import dpng
2021-02-09 15:33:37 +00:00
if not await dpng(wikiurl, matchlink.group(1)):
2020-09-25 15:19:54 +00:00
raise
from .tpg import tpg
2021-02-09 15:33:37 +00:00
if 'blockedby' in GetUserJson['query']['users'][0]:
2020-09-25 15:19:54 +00:00
BlockedBy = GetUserJson['query']['users'][0]['blockedby']
if BlockedBy:
Blockedtimestamp = UTC8(GetUserJson['query']['users'][0]['blockedtimestamp'], 'notimezone')
Blockexpiry = UTC8(str(GetUserJson['query']['users'][0]['blockexpiry']), 'notimezone')
Brs = 1
try:
from .hh import hh1
Blockreason = hh1(GetUserJson['query']['users'][0]['blockreason'])
Brs = 2
except KeyError:
pass
if Brs == 1:
imagepath = tpg(favicon=wikipng,
2021-02-01 15:13:11 +00:00
wikiname=Wikiname,
username=User,
gender=Gender,
registertime=Registration,
contributionwikis=d(str(dd[0])),
createcount=d(str(dd[1])),
editcount=d(str(dd[2])),
deletecount=d(str(dd[3])),
patrolcount=d(str(dd[4])),
sitetop=d(str(dd[5])),
globaltop=d(str(dd[6])),
wikipoint=point,
blockbyuser=BlockedBy,
blocktimestamp1=Blockedtimestamp,
blocktimestamp2=Blockexpiry,
bantype='YN')
2020-09-25 15:19:54 +00:00
elif Brs == 2:
imagepath = tpg(favicon=wikipng,
2021-02-01 15:13:11 +00:00
wikiname=Wikiname,
username=User,
gender=Gender,
registertime=Registration,
contributionwikis=d(str(dd[0])),
createcount=d(str(dd[1])),
editcount=d(str(dd[2])),
deletecount=d(str(dd[3])),
patrolcount=d(str(dd[4])),
sitetop=d(str(dd[5])),
globaltop=d(str(dd[6])),
wikipoint=point,
blockbyuser=BlockedBy,
blocktimestamp1=Blockedtimestamp,
blocktimestamp2=Blockexpiry,
blockreason=Blockreason,
bantype='Y')
2021-02-09 15:33:37 +00:00
else:
2020-09-25 15:19:54 +00:00
imagepath = tpg(favicon=wikipng,
2021-02-01 15:13:11 +00:00
wikiname=Wikiname,
username=User,
gender=Gender,
registertime=Registration,
contributionwikis=d(str(dd[0])),
createcount=d(str(dd[1])),
editcount=d(str(dd[2])),
deletecount=d(str(dd[3])),
patrolcount=d(str(dd[4])),
sitetop=d(str(dd[5])),
globaltop=d(str(dd[6])),
wikipoint=point)
2020-09-25 15:19:54 +00:00
if argv == '-p':
2021-02-09 15:10:51 +00:00
return f'{GetArticleUrl}User:{urllib.parse.quote(rmuser.encode("UTF-8"))}[[uimgc:{imagepath}]]'
2021-02-19 12:20:00 +00:00
return (GetArticleUrl + 'User:' + urllib.parse.quote(rmuser.encode('UTF-8')) + '\n' +
2020-09-25 15:19:54 +00:00
Wikiname + '\n' +
f'用户:{User} | 编辑数:{Editcount}\n' +
f'用户组:{Group}\n' +
f'性别:{Gender}\n' +
f'注册时间:{Registration}' + Blockmessage)
except Exception as e:
2020-09-25 15:19:54 +00:00
if 'missing' in GetUserJson['query']['users'][0]:
return '没有找到此用户。'
else:
2021-02-01 15:13:11 +00:00
traceback.print_exc()
2021-02-09 15:33:37 +00:00
return '发生错误:' + str(e)