基本完成
This commit is contained in:
parent
eac23a7510
commit
03bd04c4f9
29
misskey.py
29
misskey.py
|
@ -2,27 +2,42 @@
|
|||
# -*- coding: UTF-8 -*-
|
||||
# Misskey API Docs: https://misskey.io/api-doc
|
||||
import requests
|
||||
import json
|
||||
import re
|
||||
|
||||
|
||||
class Misskey:
|
||||
config = {}
|
||||
debug = True
|
||||
baseurl = ""
|
||||
|
||||
def post(self, baseurl, content, channel="", visibility="public"):
|
||||
print("Creating new post to", baseurl, ":", content)
|
||||
req_url = baseurl + "/api/notes/create"
|
||||
def post(self, content, i, channel="", visibility="public",):
|
||||
print("Creating new post to", self.baseurl, ":", content)
|
||||
req_url = self.baseurl + "/api/notes/create"
|
||||
body = {
|
||||
"visibility": visibility,
|
||||
"text": content,
|
||||
"localOnly": channel != "",
|
||||
"i": self.config['token']
|
||||
"i": i
|
||||
}
|
||||
if channel != "":
|
||||
body["channelId"] = channel
|
||||
result = requests.post(req_url, json=body)
|
||||
if result.status_code == 200:
|
||||
return result
|
||||
return True
|
||||
else:
|
||||
print("Failed to post:", result.json()['error']['message'])
|
||||
return False
|
||||
|
||||
def upload_from_url(self, i, url):
|
||||
print("Uploading img to misskey")
|
||||
req_url = self.baseurl + "/api/drive/files/upload-from-url"
|
||||
body = {
|
||||
"url": url,
|
||||
"i": i
|
||||
}
|
||||
r = requests.post(req_url, json=body)
|
||||
if r.status_code == 204:
|
||||
print("Done!")
|
||||
return True
|
||||
else:
|
||||
print("Failed to upload:", r.json()['error']['message'])
|
||||
return False
|
||||
|
|
11
rules.json
11
rules.json
|
@ -1,10 +1,17 @@
|
|||
{
|
||||
"DWNews":{
|
||||
"rss_source": "https://rsshub.app/dwnews/yaowen/global",
|
||||
"identity": "misskey.io"
|
||||
"identity": "WDNews@x61.uk",
|
||||
"extra_content": "#News"
|
||||
},
|
||||
"NHK": {
|
||||
"rss_source": "https://rsshub.app/nhk/news_web_easy",
|
||||
"identity": "misskey.io"
|
||||
"identity": "misskey.io",
|
||||
"extra_content": ""
|
||||
},
|
||||
"SoliDot": {
|
||||
"rss_source": "https://rsshub.app/solidot/www",
|
||||
"identity": "misskey.dev",
|
||||
"extra_content": ""
|
||||
}
|
||||
}
|
26
x61bot.py
26
x61bot.py
|
@ -7,6 +7,7 @@ import requests
|
|||
import xmltodict
|
||||
import time
|
||||
from misskey import Misskey
|
||||
from bs4 import BeautifulSoup
|
||||
|
||||
time_start = time.time()
|
||||
print(
|
||||
|
@ -21,6 +22,7 @@ def json_read(file):
|
|||
config_file.close()
|
||||
return config
|
||||
|
||||
|
||||
def xml_to_json(xml):
|
||||
pars = xmltodict.parse(xml)
|
||||
return json.dumps(pars)
|
||||
|
@ -39,8 +41,12 @@ def spider(rule_name, rss_url):
|
|||
'VALUES (?, ?, ?, ?)', (rule_name, rss_url, json.dumps(result), time.time()))
|
||||
item_list = result['rss']['channel']['item']
|
||||
for i in item_list:
|
||||
unique = c.execute('SELECT * FROM "main"."result" WHERE "title" = ? LIMIT 0,1', (i['title'],)).fetchone()
|
||||
if not (unique is None):
|
||||
print("Skip: ", i['title'])
|
||||
continue
|
||||
print("Got: ", i['title'])
|
||||
desc = i['description'].replace("<blockquote>", "“").replace("</blockquote>", "”")
|
||||
desc = i['description'].replace("<blockquote>", "“").replace("</blockquote>", "”")
|
||||
c.execute('INSERT INTO "main"."result" ("rule_name", "url", "title", "description", "timestamp")'
|
||||
' VALUES (?, ?, ?, ?, ?)', (rule_name, i['link'], i['title'], desc, time.time()))
|
||||
|
||||
|
@ -50,7 +56,7 @@ def spider(rule_name, rss_url):
|
|||
return result
|
||||
|
||||
|
||||
def fetch_detail(url):
|
||||
def fetch_img(url):
|
||||
print()
|
||||
|
||||
|
||||
|
@ -62,10 +68,20 @@ if __name__ == '__main__':
|
|||
|
||||
for key in rules:
|
||||
spider(key, rules[key]['rss_source'])
|
||||
name = rules[key]['identity']
|
||||
Misskey.baseurl = config[name]['url']
|
||||
|
||||
Misskey.config = config['misskey.io']
|
||||
|
||||
# req = Misskey.post(baseurl="https://misskey.io", content="Beep.. Beep Beep! Beep:" + str(time.time()), self=Misskey)
|
||||
c = conn.cursor()
|
||||
r = c.execute('''SELECT * FROM "main"."result"
|
||||
WHERE "rule_name" = ? AND
|
||||
"post_time" = '0' ORDER BY "rid" DESC''', (key,)).fetchone()
|
||||
if not(r is None):
|
||||
res = c.execute('UPDATE "main"."result" SET "post_time" = ? WHERE rowid = ?', (time.time(), r[0]))
|
||||
if not (res is None):
|
||||
content = r[3]+"\n<"+r[2]+">\n\n"+rules[key]['extra_content']
|
||||
Misskey.post(self=Misskey,
|
||||
content=content,
|
||||
i=config[name]['token'], visibility=config[name]['visibility'])
|
||||
conn.commit()
|
||||
conn.close()
|
||||
|
||||
|
|
Loading…
Reference in New Issue