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/github/repo.py

77 lines
2.8 KiB
Python
Raw Normal View History

2022-08-31 04:06:01 +00:00
import asyncio
2021-07-30 08:06:10 +00:00
import traceback
2022-06-28 06:11:03 +00:00
from core.builtins.message import MessageSession
from core.elements import Image, Plain, Url, ErrorMessage
2022-08-18 18:22:00 +00:00
from core.utils import get_url, download_to_cache
2021-11-20 01:35:38 +00:00
from modules.github.utils import time_diff, dirty_check, darkCheck
2021-07-30 08:06:10 +00:00
2021-08-07 07:56:48 +00:00
2021-07-30 08:06:10 +00:00
async def repo(msg: MessageSession):
try:
2022-08-10 13:22:44 +00:00
result = await get_url('https://api.github.com/repos/' + msg.parsed_msg['<name>'], 200, fmt='json')
2021-07-30 08:06:10 +00:00
if 'message' in result and result['message'] == 'Not Found':
2022-08-10 13:22:44 +00:00
await msg.finish('此仓库不存在,请检查输入。')
2021-07-30 08:06:10 +00:00
elif 'message' in result and result['message']:
2022-08-10 13:22:44 +00:00
await msg.finish(result['message'])
2021-07-30 08:06:10 +00:00
rlicense = 'Unknown'
if 'license' in result and result['license'] is not None:
if 'spdx_id' in result['license']:
rlicense = result['license']['spdx_id']
is_fork = result['fork']
parent = False
if result['homepage'] is not None:
2022-01-17 13:28:49 +00:00
website = 'Website: ' + str(Url(result['homepage'])) + '\n'
2021-07-30 08:06:10 +00:00
else:
website = ''
if result['mirror_url'] is not None:
2022-01-17 13:28:49 +00:00
mirror = f' (This is a mirror of {str(Url(result["mirror_url"]))} )'
2021-07-30 08:06:10 +00:00
else:
mirror = ''
if is_fork:
parent_name = result['parent']['name']
parent = f' (This is a fork of {parent_name} )'
desc = result['description']
if desc is None:
desc = ''
else:
desc = '\n' + result['description']
2021-08-01 14:26:55 +00:00
message = f'''{result['full_name']} ({result['id']}){desc}
2021-07-30 08:06:10 +00:00
Language · {result['language']} | Fork · {result['forks_count']} | Star · {result['stargazers_count']} | Watch · {result['watchers_count']}
License: {rlicense}
Created {time_diff(result['created_at'])} ago | Updated {time_diff(result['updated_at'])} ago
2022-01-17 13:28:49 +00:00
{website}{str(Url(result['html_url']))}'''
2021-07-30 08:06:10 +00:00
if mirror:
2021-08-01 14:26:55 +00:00
message += '\n' + mirror
2021-07-30 08:06:10 +00:00
if parent:
2021-08-01 14:26:55 +00:00
message += '\n' + parent
2021-07-30 08:06:10 +00:00
2021-08-01 14:26:55 +00:00
is_dirty = await dirty_check(message, result['owner']['login']) or darkCheck(message)
2021-07-30 08:06:10 +00:00
if is_dirty:
2021-08-01 14:26:55 +00:00
message = 'https://wdf.ink/6OUp'
2022-05-21 16:04:29 +00:00
await msg.finish([Plain(message)])
2021-07-30 08:06:10 +00:00
else:
2022-08-18 18:22:00 +00:00
await msg.sendMessage([Plain(message)])
2022-08-31 04:06:01 +00:00
async def download():
download_pic = await download_to_cache(f'https://opengraph.githubassets.com/c9f4179f4d560950b2355c82aa2b7750bffd945744f9b8ea3f93cc24779745a0/{result["full_name"]}')
if download_pic:
await msg.finish([Image(download_pic)])
asyncio.create_task(download())
2022-08-10 13:22:44 +00:00
except ValueError:
await msg.finish('发生错误:找不到仓库,请检查拼写是否正确。')
2021-07-30 08:06:10 +00:00
except Exception as e:
2022-08-10 13:22:44 +00:00
await msg.sendMessage(ErrorMessage(str(e)))
traceback.print_exc()