diff --git a/core/utils.py b/core/utils.py index 5c74cf05..9c3c56c9 100644 --- a/core/utils.py +++ b/core/utils.py @@ -1,8 +1,8 @@ import aiohttp -async def get_url(url: str): +async def get_url(url: str, headers=None): async with aiohttp.ClientSession() as session: - async with session.get(url, timeout=aiohttp.ClientTimeout(total=20)) as req: + async with session.get(url, timeout=aiohttp.ClientTimeout(total=20), headers=headers) as req: text = await req.text() return text diff --git a/modules/cytoid/__init__.py b/modules/cytoid/__init__.py new file mode 100644 index 00000000..26fda9d2 --- /dev/null +++ b/modules/cytoid/__init__.py @@ -0,0 +1,13 @@ +import re +from .profile import cytoid_profile + +async def cytoid(kwargs: dict): + command = kwargs['trigger_msg'] + command = re.sub('cytoid ', '', command) + command_split = command.split(' ') + if command_split[0] == 'profile': + kwargs['trigger_msg'] = re.sub('profile ', '', command) + await cytoid_profile(kwargs) + + +command = {'cytoid': cytoid} \ No newline at end of file diff --git a/modules/cytoid/profile.py b/modules/cytoid/profile.py new file mode 100644 index 00000000..043fbbc9 --- /dev/null +++ b/modules/cytoid/profile.py @@ -0,0 +1,45 @@ +import json + +import re + +from core.template import sendMessage +from core.utils import get_url +from graia.application.message.elements.internal import Plain, Image, UploadMethods +from graia.application import MessageChain, Group, Friend + + +async def cytoid_profile(kwargs: dict): + if Group in kwargs: + mth = UploadMethods.Group + if Friend in kwargs: + mth = UploadMethods.Friend + name = kwargs['trigger_msg'] + profile_url = 'http://services.cytoid.io/profile/' + name + profile = json.loads(await get_url(profile_url)) + uid = profile['user']['uid'] + nick = profile['user']['name'] + if nick is None: + nick = False + avatar = profile['user']['avatar']['large'] + basicExp = profile['exp']['basicExp'] + levelExp = profile['exp']['levelExp'] + totalExp = profile['exp']['totalExp'] + currentLevel = profile['exp']['currentLevel'] + nextLevelExp = profile['exp']['nextLevelExp'] + rating = profile['exp']['rating'] + grade = profile['grade'] + grade = f'A: {grade["A"]}, B: {grade["B"]}, C: {grade["C"]}, D: {grade["D"]}, F: {grade["F"]}, S: {grade["S"]}, SS: {grade["SS"]}' + text = f'UID: {uid}\n' +\ + (f'Nickname: {nick}\n' if nick else '') +\ + f'BasicExp: {basicExp}\n' +\ + f'LevelExp: {levelExp}\n' +\ + f'TotalExp: {totalExp}\n' +\ + f'CurrentLevel: {currentLevel}\n' +\ + f'NextLevelExp: {nextLevelExp}\n' +\ + f'Rating: {rating}\n' +\ + f'Grade: {grade}' + msg = MessageChain.create([Image.fromNetworkAddress(avatar, method=mth), Plain(text)]) + await sendMessage(kwargs, msg) + + +