From 65fa432290d3a3a15db9f2e4b5c1acf36e7bd809 Mon Sep 17 00:00:00 2001 From: qcminecraft Date: Wed, 3 Feb 2021 11:56:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=91=E5=B8=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 1 + x61bot.py | 40 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..61de0ed --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +测试 \ No newline at end of file diff --git a/x61bot.py b/x61bot.py index 21f5a56..fe491d0 100644 --- a/x61bot.py +++ b/x61bot.py @@ -7,6 +7,9 @@ import requests import xmltodict import time +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() @@ -19,18 +22,49 @@ def xml_to_json(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: ", rss_url) + print("Fetch: [" + rule_name + "] ", rss_url) + time_start = time.time() c = conn.cursor() - result = xmltodict.parse(requests.get(rss_url).content) + fetch = requests.get(rss_url) + if fetch.status_code != 200: + print("Failed to fetch") + return False + result = xmltodict.parse(fetch.content) 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") return result if __name__ == '__main__': - print("Misskey X61 RSS Bot") + print("Misskey X61 RSS Bot initialized") spider("duowei", "https://rsshub.app/dwnews/yaowen/global") + + req = misskey_post("https://misskey.io", "Beep.. Beep Beep! Beep:"+str(time.time())) conn.commit() conn.close() + + time_end = time.time() + print("X61 bot: done in", time_end - time_start, "s")