From 76bc38277716348f4ecc5d8adbc199d40d9c1064 Mon Sep 17 00:00:00 2001 From: Kyomotoi <1172294279@qq.com> Date: Tue, 5 May 2020 15:12:35 +0800 Subject: =?UTF-8?q?=E5=81=9A=E4=BA=86=E4=BA=9B=E5=B0=8F=E6=94=B9=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 以话慢慢补全功能 目前能用的 只有other --- AyaBot/plugins/__pycache__/other.cpython-37.pyc | Bin 0 -> 2479 bytes AyaBot/plugins/aio/__init__.py | 8 ++ .../aio/__pycache__/__init__.cpython-37.pyc | Bin 0 -> 432 bytes AyaBot/plugins/aio/requests.py | 71 ++++++++++++++++ AyaBot/plugins/awsl.py | 63 -------------- AyaBot/plugins/bilibili.py | 33 -------- AyaBot/plugins/bilibili.pytest | 33 ++++++++ AyaBot/plugins/bilibilitest/__init__.py | 5 ++ .../__pycache__/__init__.cpython-37.pyc | Bin 0 -> 259 bytes .../bilibilitest/__pycache__/index.cpython-37.pyc | Bin 0 -> 3333 bytes AyaBot/plugins/bilibilitest/aio/__init__.py | 8 ++ .../aio/__pycache__/__init__.cpython-37.pyc | Bin 0 -> 445 bytes AyaBot/plugins/bilibilitest/aio/requests.py | 71 ++++++++++++++++ AyaBot/plugins/bilibilitest/index.py | 91 +++++++++++++++++++++ AyaBot/plugins/other.py | 57 +++++++++++++ 15 files changed, 344 insertions(+), 96 deletions(-) create mode 100644 AyaBot/plugins/__pycache__/other.cpython-37.pyc create mode 100644 AyaBot/plugins/aio/__init__.py create mode 100644 AyaBot/plugins/aio/__pycache__/__init__.cpython-37.pyc create mode 100644 AyaBot/plugins/aio/requests.py delete mode 100644 AyaBot/plugins/awsl.py delete mode 100644 AyaBot/plugins/bilibili.py create mode 100644 AyaBot/plugins/bilibili.pytest create mode 100644 AyaBot/plugins/bilibilitest/__init__.py create mode 100644 AyaBot/plugins/bilibilitest/__pycache__/__init__.cpython-37.pyc create mode 100644 AyaBot/plugins/bilibilitest/__pycache__/index.cpython-37.pyc create mode 100644 AyaBot/plugins/bilibilitest/aio/__init__.py create mode 100644 AyaBot/plugins/bilibilitest/aio/__pycache__/__init__.cpython-37.pyc create mode 100644 AyaBot/plugins/bilibilitest/aio/requests.py create mode 100644 AyaBot/plugins/bilibilitest/index.py create mode 100644 AyaBot/plugins/other.py (limited to 'AyaBot/plugins') diff --git a/AyaBot/plugins/__pycache__/other.cpython-37.pyc b/AyaBot/plugins/__pycache__/other.cpython-37.pyc new file mode 100644 index 0000000..5962b9f Binary files /dev/null and b/AyaBot/plugins/__pycache__/other.cpython-37.pyc differ diff --git a/AyaBot/plugins/aio/__init__.py b/AyaBot/plugins/aio/__init__.py new file mode 100644 index 0000000..3cc81f0 --- /dev/null +++ b/AyaBot/plugins/aio/__init__.py @@ -0,0 +1,8 @@ +import asyncio +from functools import partial +from typing import Any + + +async def run_sync_func(func, *args, **kwargs) -> Any: + return await asyncio.get_event_loop().run_in_executor( + None, partial(func, *args, **kwargs)) diff --git a/AyaBot/plugins/aio/__pycache__/__init__.cpython-37.pyc b/AyaBot/plugins/aio/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..416e999 Binary files /dev/null and b/AyaBot/plugins/aio/__pycache__/__init__.cpython-37.pyc differ diff --git a/AyaBot/plugins/aio/requests.py b/AyaBot/plugins/aio/requests.py new file mode 100644 index 0000000..4f7b9d3 --- /dev/null +++ b/AyaBot/plugins/aio/requests.py @@ -0,0 +1,71 @@ +from typing import Optional, Any + +import requests +from requests import * + +from . import run_sync_func + + +class AsyncResponse: + def __init__(self, response: requests.Response): + self.raw_response = response + + @property + def ok(self) -> bool: + return self.raw_response.ok + + def __repr__(self): + return '' % self.raw_response.status_code + + def __bool__(self): + return self.ok + + @property + async def content(self) -> Optional[bytes]: + return await run_sync_func(lambda: self.raw_response.content) + + @property + async def text(self) -> str: + return await run_sync_func(lambda: self.raw_response.text) + + async def json(self, **kwargs) -> Any: + return await run_sync_func(self.raw_response.json, **kwargs) + + +async def request(method, url, **kwargs) -> AsyncResponse: + return AsyncResponse(await run_sync_func(requests.request, + method=method, url=url, **kwargs)) + + +async def get(url, params=None, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.get, url=url, params=params, **kwargs)) + + +async def options(url, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.options, url=url, **kwargs)) + + +async def head(url, **kwargs) -> AsyncResponse: + return AsyncResponse(await run_sync_func(requests.head, url=url, **kwargs)) + + +async def post(url, data=None, json=None, **kwargs) -> AsyncResponse: + return AsyncResponse(await run_sync_func(requests.post, url=url, + data=data, json=json, **kwargs)) + + +async def put(url, data=None, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.put, url=url, data=data, **kwargs)) + + +async def patch(url, data=None, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.patch, url=url, data=data, **kwargs)) + + +async def delete(url, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.delete, url=url, **kwargs)) diff --git a/AyaBot/plugins/awsl.py b/AyaBot/plugins/awsl.py deleted file mode 100644 index d2f976a..0000000 --- a/AyaBot/plugins/awsl.py +++ /dev/null @@ -1,63 +0,0 @@ -import os -import random -from nonebot import on_command, CommandSession, permission as perm, on_request -from datetime import datetime -from typing import Optional - -import pytz -from pandas import Timestamp - - -CST_TIMEZONE = 'Asia/Shanghai' - - -def beijing_now(freq: Optional[str] = None) -> datetime: - now = datetime.now(pytz.timezone(CST_TIMEZONE)) - if freq is not None: - now = Timestamp(now).round(freq) - return now - - -def beijing_from_timestamp(timestamp: int) -> datetime: - return datetime.fromtimestamp(timestamp, pytz.timezone(CST_TIMEZONE)) - - -@on_command('阿这', only_to_me=False) -async def _(session: CommandSession): - await session.send('阿这') - -@on_command('喵', aliases=['喵喵', '喵喵喵'], only_to_me=False) -async def _(session: CommandSession): - await session.send('喵~') - -@on_command('奶宝', aliases=['@๑ ^ ₃•๑', '奶够翘'], only_to_me=False) -async def _(session: CommandSession): - await session.send('别叫了别叫了,8在') - -@on_command('抽签', only_to_me=False) -async def _(session: CommandSession): - await session.send(str(random.choice(['大凶', '小凶', '凶', '吉', '小吉', '中吉', '大吉']))) - -@on_command('掷骰子', aliases=['投骰子'], only_to_me=False) -async def _(session: CommandSession): - await session.send(str(random.randint(1,6))) - -@on_command('?', aliases=['?', '❓'], only_to_me=False) -async def _(session: CommandSession): - await session.send('?') - -@on_command('seach_this_group_p', aliases=['本群总人数', '总人数', '群人数'], only_to_me=False, permission=perm.GROUP) -async def _(session: CommandSession): - try: - seach_group_member = await session.bot.get_group_member_list( - group_id=session.ctx['group_id'] - ) - except: - await session.send('获取数据时出问题,请重试') - return - - await session.send(f'本群目前共有{len(seach_group_member)}人') - - - - \ No newline at end of file diff --git a/AyaBot/plugins/bilibili.py b/AyaBot/plugins/bilibili.py deleted file mode 100644 index fa4554b..0000000 --- a/AyaBot/plugins/bilibili.py +++ /dev/null @@ -1,33 +0,0 @@ -import requests -import nonebot -import re -from nonebot import on_command, CommandSession, CQHttpError - - -@on_command('fan', aliases=['搜番', '查番', '番剧搜索', '搜索番剧'], only_to_me=False) -async def seach_fan(session: CommandSession): - year = session.get('year', prompt='你想查找哪一年的番呢?(示范:2020)') - month = session.get('month', prompt='你想查找哪个月的番呢(示范:4)?') - await session.send('查询的情况如下:') - await session.send('日期:' + year + month) - # await session.send('标题:' + title) - # await session.send('链接:' + link) - - -@seach_fan.args_parser -async def _(session: CommandSession): - # if session.is_first_run: - # return - if session.current_key == 'year': - if not re.fullmatch(r'\d{4}', session.current_arg_text): - session.pause('日期格式有误,请重新输入(示范:2020)') - - if session.current_key == 'month': - if not re.fullmatch(r'\d{2}', session.current_arg_text): - session.pause('日期格式有误,请重新输入(示范:4)') - - - # try: - # except CQHttpError: - # await session.send('请求未响应或出错,请重试') - diff --git a/AyaBot/plugins/bilibili.pytest b/AyaBot/plugins/bilibili.pytest new file mode 100644 index 0000000..fa4554b --- /dev/null +++ b/AyaBot/plugins/bilibili.pytest @@ -0,0 +1,33 @@ +import requests +import nonebot +import re +from nonebot import on_command, CommandSession, CQHttpError + + +@on_command('fan', aliases=['搜番', '查番', '番剧搜索', '搜索番剧'], only_to_me=False) +async def seach_fan(session: CommandSession): + year = session.get('year', prompt='你想查找哪一年的番呢?(示范:2020)') + month = session.get('month', prompt='你想查找哪个月的番呢(示范:4)?') + await session.send('查询的情况如下:') + await session.send('日期:' + year + month) + # await session.send('标题:' + title) + # await session.send('链接:' + link) + + +@seach_fan.args_parser +async def _(session: CommandSession): + # if session.is_first_run: + # return + if session.current_key == 'year': + if not re.fullmatch(r'\d{4}', session.current_arg_text): + session.pause('日期格式有误,请重新输入(示范:2020)') + + if session.current_key == 'month': + if not re.fullmatch(r'\d{2}', session.current_arg_text): + session.pause('日期格式有误,请重新输入(示范:4)') + + + # try: + # except CQHttpError: + # await session.send('请求未响应或出错,请重试') + diff --git a/AyaBot/plugins/bilibilitest/__init__.py b/AyaBot/plugins/bilibilitest/__init__.py new file mode 100644 index 0000000..05f2855 --- /dev/null +++ b/AyaBot/plugins/bilibilitest/__init__.py @@ -0,0 +1,5 @@ +from nonebot import CommandGroup + +__plugin_name__ = 'BiliBili搜番' + +from . import index \ No newline at end of file diff --git a/AyaBot/plugins/bilibilitest/__pycache__/__init__.cpython-37.pyc b/AyaBot/plugins/bilibilitest/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..d409765 Binary files /dev/null and b/AyaBot/plugins/bilibilitest/__pycache__/__init__.cpython-37.pyc differ diff --git a/AyaBot/plugins/bilibilitest/__pycache__/index.cpython-37.pyc b/AyaBot/plugins/bilibilitest/__pycache__/index.cpython-37.pyc new file mode 100644 index 0000000..6ba7b16 Binary files /dev/null and b/AyaBot/plugins/bilibilitest/__pycache__/index.cpython-37.pyc differ diff --git a/AyaBot/plugins/bilibilitest/aio/__init__.py b/AyaBot/plugins/bilibilitest/aio/__init__.py new file mode 100644 index 0000000..3cc81f0 --- /dev/null +++ b/AyaBot/plugins/bilibilitest/aio/__init__.py @@ -0,0 +1,8 @@ +import asyncio +from functools import partial +from typing import Any + + +async def run_sync_func(func, *args, **kwargs) -> Any: + return await asyncio.get_event_loop().run_in_executor( + None, partial(func, *args, **kwargs)) diff --git a/AyaBot/plugins/bilibilitest/aio/__pycache__/__init__.cpython-37.pyc b/AyaBot/plugins/bilibilitest/aio/__pycache__/__init__.cpython-37.pyc new file mode 100644 index 0000000..b379548 Binary files /dev/null and b/AyaBot/plugins/bilibilitest/aio/__pycache__/__init__.cpython-37.pyc differ diff --git a/AyaBot/plugins/bilibilitest/aio/requests.py b/AyaBot/plugins/bilibilitest/aio/requests.py new file mode 100644 index 0000000..4f7b9d3 --- /dev/null +++ b/AyaBot/plugins/bilibilitest/aio/requests.py @@ -0,0 +1,71 @@ +from typing import Optional, Any + +import requests +from requests import * + +from . import run_sync_func + + +class AsyncResponse: + def __init__(self, response: requests.Response): + self.raw_response = response + + @property + def ok(self) -> bool: + return self.raw_response.ok + + def __repr__(self): + return '' % self.raw_response.status_code + + def __bool__(self): + return self.ok + + @property + async def content(self) -> Optional[bytes]: + return await run_sync_func(lambda: self.raw_response.content) + + @property + async def text(self) -> str: + return await run_sync_func(lambda: self.raw_response.text) + + async def json(self, **kwargs) -> Any: + return await run_sync_func(self.raw_response.json, **kwargs) + + +async def request(method, url, **kwargs) -> AsyncResponse: + return AsyncResponse(await run_sync_func(requests.request, + method=method, url=url, **kwargs)) + + +async def get(url, params=None, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.get, url=url, params=params, **kwargs)) + + +async def options(url, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.options, url=url, **kwargs)) + + +async def head(url, **kwargs) -> AsyncResponse: + return AsyncResponse(await run_sync_func(requests.head, url=url, **kwargs)) + + +async def post(url, data=None, json=None, **kwargs) -> AsyncResponse: + return AsyncResponse(await run_sync_func(requests.post, url=url, + data=data, json=json, **kwargs)) + + +async def put(url, data=None, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.put, url=url, data=data, **kwargs)) + + +async def patch(url, data=None, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.patch, url=url, data=data, **kwargs)) + + +async def delete(url, **kwargs) -> AsyncResponse: + return AsyncResponse( + await run_sync_func(requests.delete, url=url, **kwargs)) diff --git a/AyaBot/plugins/bilibilitest/index.py b/AyaBot/plugins/bilibilitest/index.py new file mode 100644 index 0000000..144ff7e --- /dev/null +++ b/AyaBot/plugins/bilibilitest/index.py @@ -0,0 +1,91 @@ +import math +import re +import requests +from typing import Optional, List, Any, Dict +from nonebot import CommandSession, CommandGroup +from aiocache import cached + +from datetime import datetime +import pytz +from pandas import Timestamp + +# from .aio import * + +#get TIME +CST = 'Asia/Shanghai' + +def beijing_time_now(freq: Optional[str] = None) -> datetime: + now = datetime.now(pytz.timezone(CST)) + if freq is not None: + now = Timestamp(now).round(freq) + return now + + +def beijing_from_timestamp(timestamp: int) -> datetime: + return datetime.fromtimestamp(timestamp, pytz.timezone(CST)) + + + +cg = CommandGroup('bilibili_anime') + +API_URL = 'https://bangumi.bilibili.com/media/web_api/search/result?season_version=-1&area=-1&is_finish=-1©right=-1&season_status=-1&season_month={month}&pub_date={year}&style_id=-1&order=3&st=1&sort=0&page=1&season_type=1&pagesize=20' +WEB_URL = 'https://www.bilibili.com/anime/index/#season_version=-1&area=-1&is_finish=-1©right=-1&season_status=-1&season_month={month}&pub_date={year}&style_id=-1&order=3&st=1&sort=0&page=1' + + +@cached(ttl= 5 * 60) +async def get_anime_list(year: int, month: int) -> Optional[List[Dict[str, Any]]]: + api_url = API_URL.format(year=year, month=month) + res = await requests.get(api_url) + payload = await res.json() + + if not payload or payload.get('code') != 0: + return None + + return payload['result']['data'] + + +@cg.command('fan', aliases=['新番', '番剧索引', '番剧'], only_to_me=False) +async def fan(session: CommandSession): + now = beijing_time_now() + year = session.state.get('year', now.year) + month = session.state.get('month', now.month) + month = math.ceil(month / 3) * 3 - 3 + 1 + + anime_list = await get_anime_list(year, month) + if not anime_list: + session.finish('并没有找到相关的番剧...再试一次..?') + + reply = f'{year}/{month}番剧\n按照热度进行排序,前20部如下: \n' + for anime in anime_list: + title = anime.get('title') + index_show = anime.get('index_show', 'Error') + if not title: + continue + reply += f'{title} {index_show}\n' + + web_url = WEB_URL.format(year=year, month=month) + reply += f'\n详细请见官网 {web_url}' + session.finish(reply) + + +@fan.args_parser +async def _(session: CommandSession): + arg = session.current_arg_text.split() + + year = None + month = None + if len(arg) == 2 and re.fullmatch(r'(?:20)?\d{2}', arg[0]) and re.fullmatch(r'\d{1,2}', arg[1]): + year = int(arg[0]) if len(arg[0]) > 2 else 2000 + int (arg[0]) + month = int(arg[1]) + elif len(arg) == 1 and re.fullmatch(r'\d{1,2}', arg[0]): + month = int(arg[0]) + elif len(arg) == 1 and re.fullmatch(r'(?:20)?\d{2}-\d{1,2}', arg[0]): + year, month = [int(x) for x in arg[0].split('-')] + year = 2000 + year if year < 1000 else year + elif len(arg): + await session.send('脑子变奇怪了...无法识别master的信息,先给份本季的番8...') + + if year is not None: + session.state['year'] = year + if month is not None: + session.state['month'] = month \ No newline at end of file diff --git a/AyaBot/plugins/other.py b/AyaBot/plugins/other.py new file mode 100644 index 0000000..2b2cac1 --- /dev/null +++ b/AyaBot/plugins/other.py @@ -0,0 +1,57 @@ +import os +import random +from nonebot import on_command, CommandSession, permission as perm, on_request +from datetime import datetime +from typing import Optional + +import pytz +from pandas import Timestamp + + +CST = 'Asia/Shanghai' + +def get_beijing_time(freq: Optional[str] = None) -> datetime: + now = datetime.now(pytz.timezone(CST)) + if freq is not None: + now = Timestamp(now).round(freq) + return now + +def beijing_from_timestamp(timestamp: int) -> datetime: + return datetime.fromtimestamp(timestamp, pytz.timezone(CST)) + + +@on_command('阿这', only_to_me=False) +async def _(session: CommandSession): + await session.send('阿这') + +@on_command('喵', aliases=['喵喵', '喵喵喵'], only_to_me=False) +async def _(session: CommandSession): + await session.send('喵~') + +@on_command('奶宝', aliases=['@๑ ^ ₃•๑', '奶够翘'], only_to_me=False) +async def _(session: CommandSession): + await session.send('别叫了别叫了,8在') + +@on_command('抽签', only_to_me=False) +async def _(session: CommandSession): + await session.send(str(random.choice(['大凶', '小凶', '凶', '吉', '小吉', '中吉', '大吉']))) + +@on_command('掷骰子', aliases=['投骰子'], only_to_me=False) +async def _(session: CommandSession): + await session.send(str(random.randint(1,6))) + +@on_command('?', aliases=['?', '❓'], only_to_me=False) +async def _(session: CommandSession): + await session.send('?') + +@on_command('seach_this_group_p', aliases=['本群总人数', '总人数', '群人数'], only_to_me=False, permission=perm.GROUP) +async def _(session: CommandSession): + try: + seach_group_member = await session.bot.get_group_member_list( + group_id=session.ctx['group_id'] + ) + except: + await session.send('获取数据时出问题,请重试') + return + + await session.send(f'本群目前共有{len(seach_group_member)}人') \ No newline at end of file -- cgit v1.2.3