diff --git a/misskey.py b/misskey.py new file mode 100644 index 0000000..34cdc0b --- /dev/null +++ b/misskey.py @@ -0,0 +1,32 @@ +#!/usr/bin/python3 +# -*- coding: UTF-8 -*- +# Misskey API Docs: https://misskey.io/api-doc +import requests +import json + + +class Misskey: + config = {} + debug = True + + def post(self, baseurl, content, channel=""): + print("Creating new post to", baseurl, ":", content) + req_url = baseurl + "/api/notes/create" + if self.debug: + visb = "specified" + else: + visb = "public" + body = { + "visibility": visb, + "text": content, + "localOnly": channel != "", + "i": self.config['token'] + } + if channel != "": + body["channelId"] = channel + result = requests.post(req_url, json=body) + if result.status_code == 200: + return result + else: + print("Failed to post:", result.json()['error']['message']) + return False diff --git a/rules.json b/rules.json new file mode 100644 index 0000000..de577a6 --- /dev/null +++ b/rules.json @@ -0,0 +1,6 @@ +{ + "DWNews":{ + "rss_source": "https://rsshub.app/dwnews/yaowen/global", + "identify": "misskey.io" + } +} \ No newline at end of file diff --git a/x61bot.py b/x61bot.py index fe491d0..933e85e 100644 --- a/x61bot.py +++ b/x61bot.py @@ -1,49 +1,34 @@ #!/usr/bin/python3 # -*- coding: UTF-8 -*- -# Misskey API Docs: https://misskey.io/api-doc + import json import sqlite3 import requests import xmltodict import time +from misskey import Misskey time_start = time.time() print( ''' __ __ _ _ __ __ _ _ \n | \/ (_) | | / //_ | | | | \n | \ / |_ ___ ___| | _____ _ _ __ __/ /_ | | |__ ___ | |_ \n | |\/| | / __/ __| |/ / _ \ | | | \ \/ / '_ \| | '_ \ / _ \| __|\n | | | | \__ \__ \ < __/ |_| | > <| (_) | | |_) | (_) | |_ \n |_| |_|_|___/___/_|\_\___|\__, | /_/\_\\___/|_|_.__/ \___/ \__|\n __/ | \n |___/ ''') -config_file = open('config.json', 'r') -config = json.loads(config_file.read()) -config_file.close() conn = sqlite3.connect('DB.sqlite3') print("Opened database successfully") +def json_read(file): + config_file = open(file, 'r') + config = json.loads(config_file.read()) + config_file.close() + return config + def xml_to_json(xml): pars = xmltodict.parse(xml) return json.dumps(pars) -def misskey_post(baseurl, content, channel=""): - print("Creating new post to", baseurl, ":", content) - req_url = baseurl + "/api/notes/create" - body = { - "visibility": "specified", - "text": content, - "localOnly": channel != "", - "i": "0v1t2poJeEh9RI74VqCCzJLHk9N6Wyds" - } - if channel != "": - body["channelId"] = channel - result = requests.post(req_url, json=body) - if result.status_code == 200: - return result - else: - print("Failed to post:", result.json()['error']['message']) - return False - - def spider(rule_name, rss_url): - print("Fetch: [" + rule_name + "] ", rss_url) - time_start = time.time() + print("Fetch RSS: [" + rule_name + "] ", rss_url) + start = time.time() c = conn.cursor() fetch = requests.get(rss_url) if fetch.status_code != 200: @@ -53,16 +38,25 @@ def spider(rule_name, rss_url): c.execute('INSERT INTO "main"."spider_log" ("rule_name", "rss_url", "result_json", "timestamp") ' 'VALUES (?, ?, ?, ?)', (rule_name, rss_url, json.dumps(result), time.time())) c.close() - time_end = time.time() - print("Fetch done in", time_end - time_start, "s") + end = time.time() + print("Fetch done in", end - start, "s") return result +def fetch_detail(url): + print() + + if __name__ == '__main__': print("Misskey X61 RSS Bot initialized") - spider("duowei", "https://rsshub.app/dwnews/yaowen/global") + spider("DuoWei", "https://rsshub.app/dwnews/yaowen/global") - req = misskey_post("https://misskey.io", "Beep.. Beep Beep! Beep:"+str(time.time())) + config = json_read("config.json") + rules = json_read("rules.json") + + Misskey.config = config['misskey.io'] + + req = Misskey.post(baseurl="https://misskey.io", content="Beep.. Beep Beep! Beep:" + str(time.time()), self=Misskey) conn.commit() conn.close()