summaryrefslogtreecommitdiff
path: root/ATRI/plugins/plugin_sqlite/__init__.py
blob: 16630264f578177c80809893d51672d96be6bfcc (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
# -*- encoding: utf-8 -*-
'''
@File    :   __init__.py
@Time    :   2020/10/25 15:01:29
@Author  :   Kyomotoi
@Contact :   kyomotoiowo@gmail.com
@Github  :   https://github.com/Kyomotoi
@License :   Copyright © 2018-2020 Kyomotoi, All Rights Reserved.
'''
__author__ = 'kyomotoi'

import os
import json
import sqlite3
from pathlib import Path
from nonebot.plugin import on_command
from nonebot.permission import SUPERUSER
from nonebot.adapters.cqhttp import Bot, Event

from utils.utils_error import errorRepo
from utils.utils_request import aio_get_bytes


SetuData = on_command('setu', permission=SUPERUSER)

@SetuData.handle() # type: ignore
async def _(bot: Bot, event: Event, state: dict) -> None:
    msg0 = "-==ATRI Setu Data System==-\n"
    msg0 += "Upload:\n"
    msg0 += " - setu [type] [pid]\n"
    msg0 += " * type: normal, nearR18 r18\n"
    msg0 += "Delete:\n"
    msg0 += " - setu-delete [pid]"
    await SetuData.finish(msg0)


UploadSetu = on_command('setu-upload', permission=SUPERUSER)

@UploadSetu.handle() # type: ignore
async def _(bot: Bot, event: Event, state: dict) -> None:
    msg = str(event.message).strip().split(' ')

    if msg[0] and msg[1]:
        pass
    else:
        msg0 = "请检查格式奥~!\n"
        msg0 += "setu-upload [type] [pid]\n"
        msg0 += "type: normal, nearR18, r18"
        await UploadSetu.finish(msg0)

    if msg[0] not in ["noraml", "nearR18", "nearr18", "r18", "R18"]:
        msg0 = "请检查类型~!\n"
        msg0 += "type: normal, nearR18, r18"
        await UploadSetu.finish(msg0)

    s_type = msg[0]
    pid = msg[1]

    URL = f'https://api.imjad.cn/pixiv/v1/?type=illust&id={pid}'
    info = {}

    try:
        info = json.loads(await aio_get_bytes(URL))
    except:
        await UploadSetu.finish(errorRepo("网络请求出错"))
    
    info = info["response"][0]
    title = info["title"]
    tags = info["tags"]
    account = info["user"]["account"]
    name = info["user"]["name"]
    u_id = info["user"]["id"]
    user_link = f'https://www.pixiv.net/users/' + f'{u_id}'
    img = f'https://pixiv.cat/{pid}.jpg'

    data_setu = (f'{pid}', f'{title}', f'{tags}', f'{account}', f'{name}', f'{u_id}', f'{user_link}', f'{img}')

    if s_type == "nearr18":
        s_type = "nearR18"
    elif s_type == "R18":
        s_type = "r18"
    else:
        pass

    if os.path.exists(f'ATRI/data/data_Sqlite/setu/{s_type}.db'):
        print('数据文件存在!')
    else:
        await DeleteSetu.finish("数据库都不在添加🔨!?罢了我现创一个")
        con = sqlite3.connect(Path('.') / 'ATRI' / 'data' / 'data_Sqlite' / 'setu' / f'{s_type}.db')
        cur = con.cursor()
        cur.execute(f'CREATE TABLE {s_type}(pid PID, title TITLE, tags TAGS, account ACCOUNT, name NAME, u_id UID, user_link USERLINK, img IMG, UNIQUE(pid, title, tags, account, name, u_id, user_link, img))')
        con.commit()
        cur.close()
        await bot.send(event, '完成')

    con = sqlite3.connect(Path('.') / 'ATRI' / 'data' / 'data_Sqlite' / 'setu' / f'{s_type}.db')
    cur = con.cursor()
    cur.execute(f'INSERT INTO {s_type}(pid, title, tags, account, name, u_id, user_link, img) VALUES(?, ?, ?, ?, ?, ?, ?, ?)', data_setu)
    con.commit()
    cur.close()

    await UploadSetu.finish(f"数据上传完成~!\n涩图库[{s_type}]涩图 +1")


DeleteSetu = on_command('setu-delete', permission=SUPERUSER)

@DeleteSetu.handle() # type: ignore
async def _(bot: Bot, event: Event, state: dict) -> None:
    msg = str(event.message).strip().split(' ')
    
    if msg[0] and msg[1]:
        pass
    else:
        msg0 = "请检查格式奥~!\n"
        msg0 += "setu-delete [type] [pid]\n"
        msg0 += "type: normal, nearR18, r18"
        await DeleteSetu.finish(msg0)

    if msg[0] not in ["noraml", "nearR18", "nearr18", "r18", "R18"]:
        msg0 = "请检查类型~!\n"
        msg0 += "type: normal, nearR18, r18"
        await UploadSetu.finish(msg0)

    s_type = msg[0]
    pid = msg[1]

    if s_type == "nearr18":
        s_type = "nearR18"
    elif s_type == "R18":
        s_type = "r18"
    else:
        pass

    if os.path.exists(f'ATRI/data/data_Sqlite/setu/{s_type}.db'):
        print('数据文件存在!')
    else:
        await DeleteSetu.finish("数据库都不在删🔨!?")
    
    con = sqlite3.connect(Path('.') / 'ATRI' / 'data' / 'data_Sqlite' / 'setu' / f'{s_type}.db')
    cur = con.cursor()
    cur.execute(f'DELETE FROM {s_type} WHERE pid = {pid}')
    con.commit()
    con.close()

    await UploadSetu.finish(f"数据删除完成~!\n涩图库[{s_type}]涩图 -1")