summaryrefslogtreecommitdiff
path: root/ATRI/plugins/github.py
blob: 118d6f50db18bf90930e396143573f23650d7da0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
'''
File: github.py
Created Date: 2021-02-26 23:22:34
Author: Kyomotoi
Email: [email protected]
License: GPLv3
Project: https://github.com/Kyomotoi/ATRI
--------
Last Modified: Sunday, 7th March 2021 3:11:56 pm
Modified By: Kyomotoi ([email protected])
--------
Copyright (c) 2021 Kyomotoi
'''

import re
import json

from nonebot.plugin import on_message
from nonebot.adapters.cqhttp import Bot, MessageEvent

from ATRI.rule import is_in_banlist, is_in_dormant
from ATRI.utils.request import get_bytes
from ATRI.exceptions import RequestTimeOut


URL = "https://api.github.com/repos/{owner}/{repo}/issues/{issue_number}"


github_issues = on_message(rule=is_in_banlist() & is_in_dormant())

@github_issues.handle()
async def _github_issues(bot: Bot, event: MessageEvent) -> None:
    msg = str(event.message)
    patt = r"https://github.com/(.*)/(.*)/issues/(.*)"
    need_info = re.findall(patt, msg)
    if not need_info:
        return
    
    for i in need_info:
        need_info = list(i)
        owner = need_info[0]
        repo = need_info[1]
        issue_number = need_info[2]
        url = URL.format(owner=owner,
                         repo=repo,
                         issue_number=issue_number)
        
        try:
            data = await get_bytes(url)
        except RequestTimeOut:
            return
        
        data = json.loads(data)
        msg0 = (
            f"{repo}: #{issue_number} {data['state']}\n"
            f"comments: {data['comments']}\n"
            f"update: {data['updated_at']}\n"
            f"{data['body']}"
        )
        await github_issues.finish(msg0)