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/user/userlib.py

77 lines
4.2 KiB
Python
Raw Normal View History

2020-08-12 16:01:34 +00:00
import aiohttp
2020-06-13 12:43:43 +00:00
import json
import re
import re
import urllib
2020-08-12 08:01:00 +00:00
2020-08-12 16:01:34 +00:00
from UTC8 import UTC8
from .gender import gender
from .yhz import yhz
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-08-12 08:01:00 +00:00
async def User1(url, str3):
2020-06-13 12:43:43 +00:00
q = str3
2020-08-12 16:01:34 +00:00
url1 = url + 'api.php?action=query&list=users&ususers=' + q + '&usprop=groups%7Cblockinfo%7Cregistration%7Ceditcount%7Cgender&format=json'
url2 = url + 'api.php?action=query&meta=allmessages&ammessages=mainpage&format=json'
file = await get_data(url1, 'json')
file2 = await get_data(url2, 'json')
2020-06-13 12:43:43 +00:00
try:
Wikiname = file2['query']['allmessages'][0]['*']
except Exception:
Wikiname = 'Unknown'
try:
User = '用户:' + file['query']['users'][0]['name']
Editcount = ' | 编辑数:' + str(file['query']['users'][0]['editcount'])
Group = '用户组:' + yhz(str(file['query']['users'][0]['groups']))
Gender = '性别:' + gender(file['query']['users'][0]['gender'])
2020-08-12 16:01:34 +00:00
Registration = '注册时间:' + UTC8(file['query']['users'][0]['registration'], 'full')
2020-06-13 12:43:43 +00:00
Blockedby = str(file['query']['users'][0]['blockedby'])
2020-08-12 16:01:34 +00:00
Blockedtimestamp = UTC8(file['query']['users'][0]['blockedtimestamp'], 'full')
Blockexpiry = UTC8(str(file['query']['users'][0]['blockexpiry']), 'full')
2020-06-13 12:43:43 +00:00
Blockreason = str(file['query']['users'][0]['blockreason'])
try:
g = re.sub('User:', '', str3)
if not Blockreason:
2020-08-12 16:01:34 +00:00
return (url + 'UserProfile:' + urllib.parse.quote(g.encode(
'UTF-8')) + '\n' + Wikiname + '\n' + User + Editcount + '\n' + Group + '\n' + Gender + '\n' + Registration + '\n' +
file['query']['users'][0][
'name'] + '正在被封禁!\n' + Blockedby + '封禁,时间从' + Blockedtimestamp + '' + Blockexpiry)
2020-06-13 12:43:43 +00:00
else:
2020-08-12 16:01:34 +00:00
return (url + 'UserProfile:' + urllib.parse.quote(g.encode(
'UTF-8')) + '\n' + Wikiname + '\n' + User + '\n' + Editcount + '\n' + Group + '\n' + Gender + '\n' + Registration + '\n' +
file['query']['users'][0][
'name'] + '正在被封禁!\n' + Blockedby + '封禁,时间从' + Blockedtimestamp + '' + Blockexpiry + ',理由:“' + Blockreason + '')
2020-06-13 12:43:43 +00:00
except Exception:
g = re.sub('User:', '', str3)
if not Blockreason:
2020-08-12 16:01:34 +00:00
return (url + 'UserProfile:' + urllib.parse.quote(g.encode(
'UTF-8')) + '\n' + Wikiname + '\n' + User + Editcount + '\n' + Group + '\n' + Gender + '\n' + Registration + '\n' +
file['query']['users'][0][
'name'] + '正在被封禁!\n' + Blockedby + '封禁,时间从' + Blockedtimestamp + '' + Blockexpiry)
2020-06-13 12:43:43 +00:00
else:
2020-08-12 16:01:34 +00:00
return (url + 'UserProfile:' + urllib.parse.quote(str3.encode(
'UTF-8')) + '\n' + Wikiname + '\n' + User + Editcount + '\n' + Group + '\n' + Gender + '\n' + Registration + '\n' +
file['query']['users'][0][
'name'] + '正在被封禁!\n' + Blockedby + '封禁,时间从' + Blockedtimestamp + '' + Blockexpiry + ',理由:“' + Blockreason + '')
2020-06-13 12:43:43 +00:00
except Exception:
try:
User = '用户:' + file['query']['users'][0]['name']
Editcount = ' | 编辑数:' + str(file['query']['users'][0]['editcount'])
Group = '用户组:' + yhz(str(file['query']['users'][0]['groups']))
Gender = '性别:' + gender(file['query']['users'][0]['gender'])
2020-08-12 16:01:34 +00:00
Registration = '注册时间:' + UTC8(file['query']['users'][0]['registration'], 'full')
2020-06-13 12:43:43 +00:00
g = re.sub('User:', '', str3)
2020-08-12 16:01:34 +00:00
return (url + 'UserProfile:' + urllib.parse.quote(g.encode(
'UTF-8')) + '\n' + Wikiname + '\n' + User + Editcount + '\n' + Group + '\n' + Gender + '\n' + Registration)
2020-06-13 12:43:43 +00:00
except Exception:
2020-08-12 16:01:34 +00:00
return ('没有找到此用户。')