Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/wling-art/U1bot
Browse files Browse the repository at this point in the history
  • Loading branch information
wling-art committed Jan 10, 2024
2 parents 328658e + 635e548 commit 048d2d5
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 48 deletions.
2 changes: 1 addition & 1 deletion .env
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ ncm_admin_level=2
wordcloud_colormap=viridis
wordcloud_background_color=black
wordcloud_stopwords_path="./停用词.txt"
wordcloud_exclude_user_ids=["2854196310"] #45
wordcloud_userdict_path
wordcloud_timezone

# chatrecorder 迁移所需的配置(QQ号)
# https://github.com/noneplugin/nonebot-plugin-chatrecorder#chatrecorder_record_migration_bot_id
CHATRECORDER_RECORD_MIGRATION_BOT_ID
Expand Down
18 changes: 0 additions & 18 deletions .github/workflows/main.yml

This file was deleted.

28 changes: 18 additions & 10 deletions src/plugins/nonebot_plugin_githubcard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
from nonebot.rule import T_State
from nonebot import get_driver
from nonebot.log import logger
from nonebot.adapters.onebot.v11 import Bot, GroupMessageEvent, MessageSegment
from .config import Config
from .data_source import get_github_reposity_information
from nonebot.plugin import on_regex
from nonebot.plugin import PluginMetadata
from nonebot.plugin import on_regex, PluginMetadata

import re

__plugin_meta__ = PluginMetadata(
name="github card",
description="展示github仓库卡片",
usage="github仓库卡片\n发出 github.com/用户名/仓库名 的格式即可自动检测")
name="githubcard",
description="检测GitHub仓库链接并自动发送卡片信息(适用于Onebot V11)",
usage='通过正则表达式检测Github链接',
type='application',
homepage='https://github.com/ElainaFanBoy/nonebot_plugin_githubcard',
supported_adapters={"~onebot.v11"},
extra={
"unique_name": "githubcard",
"author": "Nanako <[email protected]>",
"version": "0.2.1",
},
)

global_config = get_driver().config
config = Config(**global_config.dict())
github = on_regex(r"https?://github\.com/([^/]+/[^/]+)",
priority=10,
block=False)
github = on_regex(
r"https?://github\.com/([^/]+/[^/]+)", priority=10, block=False)


def match_link_parts(link):
Expand All @@ -28,6 +37,5 @@ def match_link_parts(link):
async def github_handle(bot: Bot, event: GroupMessageEvent, state: T_State):
url = match_link_parts(event.get_plaintext())
imageUrl = await get_github_reposity_information(url)
if imageUrl == "获取信息失败":
raise AssertionError
assert (imageUrl != "获取信息失败")
await github.send(MessageSegment.image(imageUrl))
1 change: 1 addition & 0 deletions src/plugins/nonebot_plugin_githubcard/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

class Config(BaseModel, extra=Extra.ignore):
github_token: Optional[str] = None
github_type: Optional[int] = 0


global_config = nonebot.get_driver().config
Expand Down
48 changes: 29 additions & 19 deletions src/plugins/nonebot_plugin_githubcard/data_source.py
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
'''
Author: Night-stars-1 [email protected]
Date: 2023-10-01 21:25:18
LastEditors: Night-stars-1 [email protected]
LastEditTime: 2023-10-01 21:33:20
Description:
Copyright (c) 2023 by Night-stars-1, All Rights Reserved.
'''

import aiohttp
from .config import githubcard_config


token = githubcard_config.github_token
github_type = githubcard_config.github_type

Headers1 = {
"user-agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
}
Headers2 = {
"Authorization":
f"Bearer {token}",
"Accept":
"application/vnd.github+json",
"user-agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"
}
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"}
Headers2 = {"Authorization": f"Bearer {token}", "Accept": "application/vnd.github+json",
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 Safari/537.36"}

headers = Headers1 if token is None else Headers2
if token is None:
headers = Headers1
else:
headers = Headers2


async def get_github_reposity_information(url: str) -> str:
try:
UserName, RepoName = url.replace("https://github.com/", "").split("/")
except Exception:
except:
UserName, RepoName = url.replace("github.com/", "").split("/")
async with aiohttp.ClientSession() as session, session.get(f"https://api.github.com/users/{UserName}",
headers=headers,
timeout=5) as response:
RawData = await response.json()
AvatarUrl = RawData["avatar_url"]
return f"https://image.thum.io/get/width/1280/crop/640/viewportWidth/1280/png/noanimate/https://socialify.git.ci/{UserName}/{RepoName}/image?description=1&font=Source%20Code%20Pro&forks=1&issues=1&language=1&name=1&owner=1&pattern=Charlie%20Brown&pulls=1&stargazers=1&theme=Dark&logo={AvatarUrl}"
async with aiohttp.ClientSession() as session:
async with session.get(f"https://api.github.com/users/{UserName}", headers=headers, timeout=5) as response:
RawData = await response.json()
AvatarUrl = RawData["avatar_url"]
if github_type == 0:
ImageUrl = f"https://image.thum.io/get/width/1280/crop/640/viewportWidth/1280/png/noanimate/https://socialify.git.ci/{UserName}/{RepoName}/image?description=1&font=Rokkitt&forks=1&issues=1&language=1&name=1&owner=1&pattern=Circuit%20Board&pulls=1&stargazers=1&theme=Light&logo={AvatarUrl}"
else:
ImageUrl = f"https://opengraph.githubassets.com/githubcard/{UserName}/{RepoName}"
return ImageUrl

0 comments on commit 048d2d5

Please sign in to comment.