From 25e855e0c309eaf8ce6eedb2c21e893d1f485d81 Mon Sep 17 00:00:00 2001 From: yzhh Date: Mon, 15 Jun 2020 19:27:34 +0800 Subject: [PATCH] Special namespace supported --- bot.py | 23 +++++++++++++++-------- userp/__init__.py | 2 +- userp/checkuser.py | 11 +++++++++++ wikim/m.py | 35 ++++++++++++++++++++--------------- wikim/wikilib.py | 22 +++++++++++++--------- 5 files changed, 60 insertions(+), 33 deletions(-) create mode 100644 userp/checkuser.py diff --git a/bot.py b/bot.py index 90bb27fa..8287e62b 100644 --- a/bot.py +++ b/bot.py @@ -17,6 +17,7 @@ from newbie import new from newbee import newnew from mcbv import mcbv from mcdv import mcdv +from userp import checkuser qq = 2052142661 # 字段 qq 的值 authKey = '1145141919810' # 字段 authKey 的值 mirai_api_http_locate = 'localhost:11919/ws' # httpapi所在主机的地址端口,如果 setting.yml 文件里字段 "enableWebsocket" 的值为 "true" 则需要将 "/" 换成 "/ws", 否则将接收不到消息. @@ -86,11 +87,14 @@ async def msg(app: Mirai, group: Group,member: Member, message: MessageChain): h2 = re.sub('_', ' ', h2) except Exception as e: print(str(e)) - if pathexist(h): - await app.sendGroupMessage(group, [Plain(Userp(h,h2)),Image.fromFileSystem("/home/oasisakari/botassests/usercard/"+h2+".png")]) + if checkuser(h,h2) == True: + if pathexist(h): + await app.sendGroupMessage(group, [Plain(Userp(h,h2)),Image.fromFileSystem("/home/oasisakari/botassests/usercard/"+h2+".png")]) + else: + await app.sendGroupMessage(group, [Plain('检测到此次为第一次访问该Wiki,下载资源可能会耗费一定的时间,请耐心等待。')]) + await app.sendGroupMessage(group, [Plain(Userp(h,h2)),Image.fromFileSystem("/home/oasisakari/botassests/usercard/"+h2+".png")]) else: - await app.sendGroupMessage(group, [Plain('检测到此次为第一次访问该Wiki,下载资源可能会耗费一定的时间,请耐心等待。')]) - await app.sendGroupMessage(group, [Plain(Userp(h,h2)),Image.fromFileSystem("/home/oasisakari/botassests/usercard/"+h2+".png")]) + await app.sendGroupMessage(group, [Plain('没有找到此用户。')]) except Exception as e: print(str(e)) @@ -162,11 +166,14 @@ async def msg(app: Mirai, friend: Friend, message: MessageChain): h2 = re.sub('_', ' ', h2) except Exception as e: print(str(e)) - if pathexist(h): - await app.sendFriendMessage(friend, [Plain(Userp(h,h2)),Image.fromFileSystem("/home/oasisakari/botassests/usercard/"+h2+".png")]) + if checkuser(h,h2) == True: + if pathexist(h): + await app.sendFriendMessage(friend, [Plain(Userp(h,h2)),Image.fromFileSystem("/home/oasisakari/botassests/usercard/"+h2+".png")]) + else: + await app.sendFriendMessage(friend, [Plain('检测到此次为第一次访问该Wiki,下载资源可能会耗费一定的时间,请耐心等待。')]) + await app.sendFriendMessage(friend, [Plain(Userp(h,h2)),Image.fromFileSystem("/home/oasisakari/botassests/usercard/"+h2+".png")]) else: - await app.sendFriendMessage(friend, [Plain('检测到此次为第一次访问该Wiki,下载资源可能会耗费一定的时间,请耐心等待。')]) - await app.sendFriendMessage(friend, [Plain(Userp(h,h2)),Image.fromFileSystem("/home/oasisakari/botassests/usercard/"+h2+".png")]) + await app.sendFriendMessage(group, [Plain('没有找到此用户。')]) except Exception as e: print(str(e)) diff --git a/userp/__init__.py b/userp/__init__.py index bf1cb719..a585545d 100644 --- a/userp/__init__.py +++ b/userp/__init__.py @@ -39,6 +39,6 @@ def Userp(path,Username): h = '/Userprofile:' +User return(metaurl+urllib.parse.quote(h.encode('UTF-8'))) except Exception: - return ('没有找到此用户。') + return ('N') except Exception as e: return ('发生错误:'+str(e)) \ No newline at end of file diff --git a/userp/checkuser.py b/userp/checkuser.py new file mode 100644 index 00000000..6a23e914 --- /dev/null +++ b/userp/checkuser.py @@ -0,0 +1,11 @@ +import requests +import json +async def checkuser(path,username): + url = 'https://'+path+'.gamepedia.com/api.php?action=query&list=users&ususers='+username+'&usprop=groups%7Cblockinfo%7Cregistration%7Ceditcount%7Cgender&format=json' + q = requests.get(url) + w = json.loads(q.text) + miss = file['query']['users'][0]['missing'] + if not miss: + return True + else: + return False \ No newline at end of file diff --git a/wikim/m.py b/wikim/m.py index 65b0e8d3..8ccea963 100644 --- a/wikim/m.py +++ b/wikim/m.py @@ -3,6 +3,7 @@ import requests import json import re import asyncio +import urllib async def m(lang,str1): if lang =='en': metaurl = 'https://minecraft.gamepedia.com/api.php?action=query&format=json&prop=info&inprop=url&redirects&titles=' @@ -18,21 +19,25 @@ async def m(lang,str1): y = sorted(x.keys())[0] z = x[y]['fullurl'] if int(y) == -1: - try: - if lang =='en': - h = re.match(r'https://minecraft.gamepedia.com/(.*)', z, re.M | re.I) - searchurl = 'https://minecraft.gamepedia.com/api.php?action=query&generator=search&gsrsearch='+h.group(1)+'&gsrsort=just_match&gsrenablerewrites&prop=info&gsrlimit=1&format=json' - else: - h = re.match(r'https://minecraft-(.*).gamepedia.com/(.*)', z, re.M | re.I) - searchurl = 'https://minecraft-'+h.group(1)+'.gamepedia.com/api.php?action=query&generator=search&gsrsearch='+h.group(2)+'&gsrsort=just_match&gsrenablerewrites&prop=info&gsrlimit=1&format=json' - f = requests.get(searchurl) - g = json.loads(f.text) - j=g['query']['pages'] - b = sorted(j.keys())[0] - m = j[b]['title'] - return ('找不到条目,您是否要找的是:'+m+'?') - except Exception: - return('找不到条目。') + miss = x[y]['missing'] + if not miss: + return ('您要的'+pagename+':'+urllib.parse.quote(pagename.encode('UTF-8'))) + else: + try: + if lang =='en': + h = re.match(r'https://minecraft.gamepedia.com/(.*)', z, re.M | re.I) + searchurl = 'https://minecraft.gamepedia.com/api.php?action=query&generator=search&gsrsearch='+h.group(1)+'&gsrsort=just_match&gsrenablerewrites&prop=info&gsrlimit=1&format=json' + else: + h = re.match(r'https://minecraft-(.*).gamepedia.com/(.*)', z, re.M | re.I) + searchurl = 'https://minecraft-'+h.group(1)+'.gamepedia.com/api.php?action=query&generator=search&gsrsearch='+h.group(2)+'&gsrsort=just_match&gsrenablerewrites&prop=info&gsrlimit=1&format=json' + f = requests.get(searchurl) + g = json.loads(f.text) + j=g['query']['pages'] + b = sorted(j.keys())[0] + m = j[b]['title'] + return ('找不到条目,您是否要找的是:'+m+'?') + except Exception: + return('找不到条目。') else: try: if lang =='en': diff --git a/wikim/wikilib.py b/wikim/wikilib.py index b63f6a19..b3fb5a9f 100644 --- a/wikim/wikilib.py +++ b/wikim/wikilib.py @@ -1,7 +1,7 @@ import json import re import requests - +import urllib def Wiki(path1,pagename): metaurl = path1 +'/api.php?action=query&format=json&prop=info&inprop=url&redirects&titles=' + pagename metatext = requests.get(metaurl, timeout=10) @@ -12,14 +12,18 @@ def Wiki(path1,pagename): z = x[y]['fullurl'] if int(y) == -1: try: - h = re.match(path1+r'/(.*)', z, re.M | re.I) - searchurl = path1+'/api.php?action=query&generator=search&gsrsearch=' + h.group(1) + '&gsrsort=just_match&gsrenablerewrites&prop=info&gsrlimit=1&format=json' - f = requests.get(searchurl) - g = json.loads(f.text) - j = g['query']['pages'] - b = sorted(j.keys())[0] - m = j[b]['title'] - return ('找不到条目,您是否要找的是:' + m +'?') + miss = x[y]['missing'] + if not miss: + return ('您要的'+pagename+':'+urllib.parse.quote(pagename.encode('UTF-8'))) + else: + h = re.match(path1+r'/(.*)', z, re.M | re.I) + searchurl = path1+'/api.php?action=query&generator=search&gsrsearch=' + h.group(1) + '&gsrsort=just_match&gsrenablerewrites&prop=info&gsrlimit=1&format=json' + f = requests.get(searchurl) + g = json.loads(f.text) + j = g['query']['pages'] + b = sorted(j.keys())[0] + m = j[b]['title'] + return ('找不到条目,您是否要找的是:' + m +'?') except Exception: return ('找不到条目。') else: