基本完成
This commit is contained in:
parent
eac23a7510
commit
03bd04c4f9
29
misskey.py
29
misskey.py
|
@ -2,27 +2,42 @@
|
||||||
# -*- coding: UTF-8 -*-
|
# -*- coding: UTF-8 -*-
|
||||||
# Misskey API Docs: https://misskey.io/api-doc
|
# Misskey API Docs: https://misskey.io/api-doc
|
||||||
import requests
|
import requests
|
||||||
import json
|
import re
|
||||||
|
|
||||||
|
|
||||||
class Misskey:
|
class Misskey:
|
||||||
config = {}
|
|
||||||
debug = True
|
debug = True
|
||||||
|
baseurl = ""
|
||||||
|
|
||||||
def post(self, baseurl, content, channel="", visibility="public"):
|
def post(self, content, i, channel="", visibility="public",):
|
||||||
print("Creating new post to", baseurl, ":", content)
|
print("Creating new post to", self.baseurl, ":", content)
|
||||||
req_url = baseurl + "/api/notes/create"
|
req_url = self.baseurl + "/api/notes/create"
|
||||||
body = {
|
body = {
|
||||||
"visibility": visibility,
|
"visibility": visibility,
|
||||||
"text": content,
|
"text": content,
|
||||||
"localOnly": channel != "",
|
"localOnly": channel != "",
|
||||||
"i": self.config['token']
|
"i": i
|
||||||
}
|
}
|
||||||
if channel != "":
|
if channel != "":
|
||||||
body["channelId"] = channel
|
body["channelId"] = channel
|
||||||
result = requests.post(req_url, json=body)
|
result = requests.post(req_url, json=body)
|
||||||
if result.status_code == 200:
|
if result.status_code == 200:
|
||||||
return result
|
return True
|
||||||
else:
|
else:
|
||||||
print("Failed to post:", result.json()['error']['message'])
|
print("Failed to post:", result.json()['error']['message'])
|
||||||
return False
|
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":{
|
"DWNews":{
|
||||||
"rss_source": "https://rsshub.app/dwnews/yaowen/global",
|
"rss_source": "https://rsshub.app/dwnews/yaowen/global",
|
||||||
"identity": "misskey.io"
|
"identity": "WDNews@x61.uk",
|
||||||
|
"extra_content": "#News"
|
||||||
},
|
},
|
||||||
"NHK": {
|
"NHK": {
|
||||||
"rss_source": "https://rsshub.app/nhk/news_web_easy",
|
"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 xmltodict
|
||||||
import time
|
import time
|
||||||
from misskey import Misskey
|
from misskey import Misskey
|
||||||
|
from bs4 import BeautifulSoup
|
||||||
|
|
||||||
time_start = time.time()
|
time_start = time.time()
|
||||||
print(
|
print(
|
||||||
|
@ -21,6 +22,7 @@ def json_read(file):
|
||||||
config_file.close()
|
config_file.close()
|
||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
def xml_to_json(xml):
|
def xml_to_json(xml):
|
||||||
pars = xmltodict.parse(xml)
|
pars = xmltodict.parse(xml)
|
||||||
return json.dumps(pars)
|
return json.dumps(pars)
|
||||||
|
@ -39,8 +41,12 @@ def spider(rule_name, rss_url):
|
||||||
'VALUES (?, ?, ?, ?)', (rule_name, rss_url, json.dumps(result), time.time()))
|
'VALUES (?, ?, ?, ?)', (rule_name, rss_url, json.dumps(result), time.time()))
|
||||||
item_list = result['rss']['channel']['item']
|
item_list = result['rss']['channel']['item']
|
||||||
for i in item_list:
|
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'])
|
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")'
|
c.execute('INSERT INTO "main"."result" ("rule_name", "url", "title", "description", "timestamp")'
|
||||||
' VALUES (?, ?, ?, ?, ?)', (rule_name, i['link'], i['title'], desc, time.time()))
|
' VALUES (?, ?, ?, ?, ?)', (rule_name, i['link'], i['title'], desc, time.time()))
|
||||||
|
|
||||||
|
@ -50,7 +56,7 @@ def spider(rule_name, rss_url):
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def fetch_detail(url):
|
def fetch_img(url):
|
||||||
print()
|
print()
|
||||||
|
|
||||||
|
|
||||||
|
@ -62,10 +68,20 @@ if __name__ == '__main__':
|
||||||
|
|
||||||
for key in rules:
|
for key in rules:
|
||||||
spider(key, rules[key]['rss_source'])
|
spider(key, rules[key]['rss_source'])
|
||||||
|
name = rules[key]['identity']
|
||||||
|
Misskey.baseurl = config[name]['url']
|
||||||
|
|
||||||
Misskey.config = config['misskey.io']
|
c = conn.cursor()
|
||||||
|
r = c.execute('''SELECT * FROM "main"."result"
|
||||||
# req = Misskey.post(baseurl="https://misskey.io", content="Beep.. Beep Beep! Beep:" + str(time.time()), self=Misskey)
|
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.commit()
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue