mirror of
https://github.com/fotoente/MIsskey-ebooks-bot
synced 2025-02-16 20:38:28 +00:00
Add support for multiple refernce users
This commit is contained in:
parent
09a7efbcbf
commit
11d23e07e9
3 changed files with 39 additions and 29 deletions
|
@ -1,6 +1,5 @@
|
||||||
[misskey]
|
[misskey]
|
||||||
instance_read=[domain to read from; domain.tld]
|
users=[semicolon of users @user@domain.tld]
|
||||||
user_read=[user to read from]
|
|
||||||
instance_write=[domain where the bot is; domain.tld]
|
instance_write=[domain where the bot is; domain.tld]
|
||||||
token=[token here]
|
token=[token here]
|
||||||
|
|
||||||
|
|
2
rdbot.py
2
rdbot.py
|
@ -23,7 +23,7 @@ class MyBot(commands.Bot):
|
||||||
@tasks.loop(3600)
|
@tasks.loop(3600)
|
||||||
async def loop_1h(self):
|
async def loop_1h(self):
|
||||||
text = create_sentence()
|
text = create_sentence()
|
||||||
await bot.client.note.send(content=text)
|
await bot.client.note.send(content=text, visibility="home")
|
||||||
|
|
||||||
@tasks.loop(43200)
|
@tasks.loop(43200)
|
||||||
async def loop_12h(self):
|
async def loop_12h(self):
|
||||||
|
|
63
roboduck.py
63
roboduck.py
|
@ -19,6 +19,15 @@ def check_str_to_bool(text) -> bool:
|
||||||
else:
|
else:
|
||||||
return True
|
return True
|
||||||
|
|
||||||
|
def get_user_id(username, instance):
|
||||||
|
url = "https://" + instance + "/api/users/show"
|
||||||
|
try:
|
||||||
|
req = requests.post(url, json={"username": username, "host": instance})
|
||||||
|
req.raise_for_status()
|
||||||
|
except requests.exceptions.HTTPError as err:
|
||||||
|
print("Couldn't get Username! " + str(err))
|
||||||
|
sys.exit(1)
|
||||||
|
return req.json()["id"]
|
||||||
|
|
||||||
def get_notes(**kwargs):
|
def get_notes(**kwargs):
|
||||||
noteid = "k"
|
noteid = "k"
|
||||||
|
@ -26,7 +35,10 @@ def get_notes(**kwargs):
|
||||||
min_notes = 0
|
min_notes = 0
|
||||||
notesList = []
|
notesList = []
|
||||||
returnList = []
|
returnList = []
|
||||||
|
username = kwargs["username"]
|
||||||
|
instance = kwargs["instance"]
|
||||||
|
|
||||||
|
print("Reading notes for @" + username + "@" + instance + ".")
|
||||||
if (kwargs):
|
if (kwargs):
|
||||||
if ("min_notes" in kwargs):
|
if ("min_notes" in kwargs):
|
||||||
# print("min_notes found!")
|
# print("min_notes found!")
|
||||||
|
@ -52,16 +64,7 @@ def get_notes(**kwargs):
|
||||||
config.read(os.path.join(os.path.dirname(__file__), 'bot.cfg'))
|
config.read(os.path.join(os.path.dirname(__file__), 'bot.cfg'))
|
||||||
# print(os.path.join(os.path.dirname(__file__), 'bot.cfg'))
|
# print(os.path.join(os.path.dirname(__file__), 'bot.cfg'))
|
||||||
|
|
||||||
url = "https://" + config.get("misskey", "instance_read") + "/api/users/show"
|
userid = get_user_id(username, instance)
|
||||||
host = config.get("misskey", "instance_read")
|
|
||||||
try:
|
|
||||||
req = requests.post(url, json={"username": config.get("misskey", "user_read"), "host": host})
|
|
||||||
req.raise_for_status()
|
|
||||||
except requests.exceptions.HTTPError as err:
|
|
||||||
print("Couldn't get Username! " + str(err))
|
|
||||||
sys.exit(1)
|
|
||||||
|
|
||||||
userid = req.json()["id"]
|
|
||||||
|
|
||||||
# Read & Sanitize Inputs from Config File
|
# Read & Sanitize Inputs from Config File
|
||||||
try:
|
try:
|
||||||
|
@ -88,7 +91,7 @@ def get_notes(**kwargs):
|
||||||
break
|
break
|
||||||
|
|
||||||
try:
|
try:
|
||||||
req = requests.post("https://" + config.get("misskey", "instance_read") + "/api/users/notes", json={
|
req = requests.post("https://" + instance + "/api/users/notes", json={
|
||||||
"userId": userid,
|
"userId": userid,
|
||||||
"includeReplies": includeReplies,
|
"includeReplies": includeReplies,
|
||||||
"limit": 100,
|
"limit": 100,
|
||||||
|
@ -107,7 +110,7 @@ def get_notes(**kwargs):
|
||||||
notesList.append(jsonObj)
|
notesList.append(jsonObj)
|
||||||
if (len(notesList) == 0):
|
if (len(notesList) == 0):
|
||||||
print("No new notes to load!")
|
print("No new notes to load!")
|
||||||
return 0
|
return []
|
||||||
|
|
||||||
oldnote = noteid
|
oldnote = noteid
|
||||||
|
|
||||||
|
@ -130,7 +133,7 @@ def get_notes(**kwargs):
|
||||||
content = content.replace("::", ": :") # Break long emoji chains
|
content = content.replace("::", ": :") # Break long emoji chains
|
||||||
content = content.replace("@", "@" + chr(8203))
|
content = content.replace("@", "@" + chr(8203))
|
||||||
|
|
||||||
dict = {"id": element["id"], "text": content, "timestamp": lastTimestamp}
|
dict = {"id": element["id"], "text": content, "timestamp": lastTimestamp, "user_id": userid }
|
||||||
returnList.append(dict)
|
returnList.append(dict)
|
||||||
|
|
||||||
return returnList
|
return returnList
|
||||||
|
@ -300,12 +303,18 @@ def update():
|
||||||
database = sqlite3.connect(databasepath)
|
database = sqlite3.connect(databasepath)
|
||||||
print("Connected to roboduck.db succesfull...")
|
print("Connected to roboduck.db succesfull...")
|
||||||
|
|
||||||
data = database.cursor()
|
config = configparser.ConfigParser()
|
||||||
data.execute("SELECT id FROM notes WHERE timestamp = (SELECT MAX(timestamp) FROM notes);")
|
config.read((Path(__file__).parent).joinpath('bot.cfg'))
|
||||||
|
for user in config.get("misskey","users").split(";"):
|
||||||
|
username = user.split("@")[1]
|
||||||
|
instance = user.split("@")[2]
|
||||||
|
userid = get_user_id(username, instance)
|
||||||
|
data = database.cursor()
|
||||||
|
data.execute("SELECT id FROM notes WHERE timestamp = (SELECT MAX(timestamp) FROM notes WHERE user_id=:user_id) AND user_id=:user_id;", {"user_id": userid})
|
||||||
|
|
||||||
sinceNote = data.fetchone()[0]
|
sinceNote = data.fetchone()[0]
|
||||||
|
|
||||||
notesList = get_notes(lastnote=sinceNote)
|
notesList.extend(get_notes(lastnote=sinceNote, username=username, instance=instance))
|
||||||
|
|
||||||
if (notesList == 0):
|
if (notesList == 0):
|
||||||
database.close()
|
database.close()
|
||||||
|
@ -313,8 +322,8 @@ def update():
|
||||||
|
|
||||||
print("Insert new notes to database...")
|
print("Insert new notes to database...")
|
||||||
for note in notesList:
|
for note in notesList:
|
||||||
database.execute("INSERT OR IGNORE INTO notes (id, text, timestamp) VALUES(?, ?, ?)",
|
database.execute("INSERT OR IGNORE INTO notes (id, text, timestamp, user_id) VALUES(?, ?, ?, ?)",
|
||||||
[note["id"], note["text"], note["timestamp"]])
|
[note["id"], note["text"], note["timestamp"], note["user_id"]])
|
||||||
|
|
||||||
database.commit()
|
database.commit()
|
||||||
print("Notes updated!")
|
print("Notes updated!")
|
||||||
|
@ -349,7 +358,7 @@ def init_bot():
|
||||||
print("Connected to roboduck.db succesfull...")
|
print("Connected to roboduck.db succesfull...")
|
||||||
|
|
||||||
print("Creating Table...")
|
print("Creating Table...")
|
||||||
database.execute("CREATE TABLE notes (id CHAR(10) PRIMARY KEY, text CHAR(5000), timestamp INT);")
|
database.execute("CREATE TABLE notes (id CHAR(10) PRIMARY KEY, text CHAR(5000), timestamp INT, user_id CHAR(10));")
|
||||||
|
|
||||||
print("Table NOTES created...")
|
print("Table NOTES created...")
|
||||||
|
|
||||||
|
@ -362,15 +371,17 @@ def init_bot():
|
||||||
# print(err)
|
# print(err)
|
||||||
initnotes = 1000
|
initnotes = 1000
|
||||||
|
|
||||||
print("Try reading first " + str(initnotes) + " notes.")
|
for user in config.get("misskey","users").split(";"):
|
||||||
|
|
||||||
notesList = get_notes(min_notes=initnotes)
|
print("Try reading first " + str(initnotes) + " notes for " + user + ".")
|
||||||
|
|
||||||
print("Writing notes into database...")
|
notesList = get_notes(min_notes=initnotes, username=user.split("@")[1], instance=user.split("@")[2])
|
||||||
|
|
||||||
for note in notesList:
|
print("Writing notes into database...")
|
||||||
database.execute("INSERT INTO notes (id, text, timestamp) VALUES(?, ?, ?)",
|
|
||||||
[note["id"], note["text"], note["timestamp"]])
|
for note in notesList:
|
||||||
|
database.execute("INSERT INTO notes (id, text, timestamp, user_id) VALUES(?, ?, ?, ?)",
|
||||||
|
[note["id"], note["text"], note["timestamp"], note["user_id"]])
|
||||||
|
|
||||||
database.commit()
|
database.commit()
|
||||||
database.close()
|
database.close()
|
||||||
|
|
Loading…
Add table
Reference in a new issue