mirror of
https://github.com/fotoente/MIsskey-ebooks-bot
synced 2024-11-25 21:00:23 +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]
|
||||
instance_read=[domain to read from; domain.tld]
|
||||
user_read=[user to read from]
|
||||
users=[semicolon of users @user@domain.tld]
|
||||
instance_write=[domain where the bot is; domain.tld]
|
||||
token=[token here]
|
||||
|
||||
|
|
2
rdbot.py
2
rdbot.py
|
@ -23,7 +23,7 @@ class MyBot(commands.Bot):
|
|||
@tasks.loop(3600)
|
||||
async def loop_1h(self):
|
||||
text = create_sentence()
|
||||
await bot.client.note.send(content=text)
|
||||
await bot.client.note.send(content=text, visibility="home")
|
||||
|
||||
@tasks.loop(43200)
|
||||
async def loop_12h(self):
|
||||
|
|
63
roboduck.py
63
roboduck.py
|
@ -19,6 +19,15 @@ def check_str_to_bool(text) -> bool:
|
|||
else:
|
||||
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):
|
||||
noteid = "k"
|
||||
|
@ -26,7 +35,10 @@ def get_notes(**kwargs):
|
|||
min_notes = 0
|
||||
notesList = []
|
||||
returnList = []
|
||||
username = kwargs["username"]
|
||||
instance = kwargs["instance"]
|
||||
|
||||
print("Reading notes for @" + username + "@" + instance + ".")
|
||||
if (kwargs):
|
||||
if ("min_notes" in kwargs):
|
||||
# print("min_notes found!")
|
||||
|
@ -52,16 +64,7 @@ def get_notes(**kwargs):
|
|||
config.read(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"
|
||||
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"]
|
||||
userid = get_user_id(username, instance)
|
||||
|
||||
# Read & Sanitize Inputs from Config File
|
||||
try:
|
||||
|
@ -88,7 +91,7 @@ def get_notes(**kwargs):
|
|||
break
|
||||
|
||||
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,
|
||||
"includeReplies": includeReplies,
|
||||
"limit": 100,
|
||||
|
@ -107,7 +110,7 @@ def get_notes(**kwargs):
|
|||
notesList.append(jsonObj)
|
||||
if (len(notesList) == 0):
|
||||
print("No new notes to load!")
|
||||
return 0
|
||||
return []
|
||||
|
||||
oldnote = noteid
|
||||
|
||||
|
@ -130,7 +133,7 @@ def get_notes(**kwargs):
|
|||
content = content.replace("::", ": :") # Break long emoji chains
|
||||
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)
|
||||
|
||||
return returnList
|
||||
|
@ -300,12 +303,18 @@ def update():
|
|||
database = sqlite3.connect(databasepath)
|
||||
print("Connected to roboduck.db succesfull...")
|
||||
|
||||
data = database.cursor()
|
||||
data.execute("SELECT id FROM notes WHERE timestamp = (SELECT MAX(timestamp) FROM notes);")
|
||||
config = configparser.ConfigParser()
|
||||
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):
|
||||
database.close()
|
||||
|
@ -313,8 +322,8 @@ def update():
|
|||
|
||||
print("Insert new notes to database...")
|
||||
for note in notesList:
|
||||
database.execute("INSERT OR IGNORE INTO notes (id, text, timestamp) VALUES(?, ?, ?)",
|
||||
[note["id"], note["text"], note["timestamp"]])
|
||||
database.execute("INSERT OR IGNORE INTO notes (id, text, timestamp, user_id) VALUES(?, ?, ?, ?)",
|
||||
[note["id"], note["text"], note["timestamp"], note["user_id"]])
|
||||
|
||||
database.commit()
|
||||
print("Notes updated!")
|
||||
|
@ -349,7 +358,7 @@ def init_bot():
|
|||
print("Connected to roboduck.db succesfull...")
|
||||
|
||||
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...")
|
||||
|
||||
|
@ -362,15 +371,17 @@ def init_bot():
|
|||
# print(err)
|
||||
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:
|
||||
database.execute("INSERT INTO notes (id, text, timestamp) VALUES(?, ?, ?)",
|
||||
[note["id"], note["text"], note["timestamp"]])
|
||||
print("Writing notes into database...")
|
||||
|
||||
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.close()
|
||||
|
|
Loading…
Reference in a new issue