RSSToMisskey/x61bot.py

71 lines
2.5 KiB
Python
Raw Normal View History

2021-02-03 01:06:44 +01:00
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
# Misskey API Docs: https://misskey.io/api-doc
import json
import sqlite3
import requests
import xmltodict
import time
2021-02-03 04:56:54 +01:00
time_start = time.time()
print(
''' __ __ _ _ __ __ _ _ \n | \/ (_) | | / //_ | | | | \n | \ / |_ ___ ___| | _____ _ _ __ __/ /_ | | |__ ___ | |_ \n | |\/| | / __/ __| |/ / _ \ | | | \ \/ / '_ \| | '_ \ / _ \| __|\n | | | | \__ \__ \ < __/ |_| | > <| (_) | | |_) | (_) | |_ \n |_| |_|_|___/___/_|\_\___|\__, | /_/\_\\___/|_|_.__/ \___/ \__|\n __/ | \n |___/ ''')
2021-02-03 01:06:44 +01:00
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 xml_to_json(xml):
pars = xmltodict.parse(xml)
return json.dumps(pars)
2021-02-03 04:56:54 +01:00
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
2021-02-03 01:06:44 +01:00
def spider(rule_name, rss_url):
2021-02-03 04:56:54 +01:00
print("Fetch: [" + rule_name + "] ", rss_url)
time_start = time.time()
2021-02-03 01:06:44 +01:00
c = conn.cursor()
2021-02-03 04:56:54 +01:00
fetch = requests.get(rss_url)
if fetch.status_code != 200:
print("Failed to fetch")
return False
result = xmltodict.parse(fetch.content)
2021-02-03 01:06:44 +01:00
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()
2021-02-03 04:56:54 +01:00
time_end = time.time()
print("Fetch done in", time_end - time_start, "s")
2021-02-03 01:06:44 +01:00
return result
if __name__ == '__main__':
2021-02-03 04:56:54 +01:00
print("Misskey X61 RSS Bot initialized")
2021-02-03 01:06:44 +01:00
spider("duowei", "https://rsshub.app/dwnews/yaowen/global")
2021-02-03 04:56:54 +01:00
req = misskey_post("https://misskey.io", "Beep.. Beep Beep! Beep:"+str(time.time()))
2021-02-03 01:06:44 +01:00
conn.commit()
conn.close()
2021-02-03 04:56:54 +01:00
time_end = time.time()
print("X61 bot: done in", time_end - time_start, "s")