bots now work properly with pleroma!

This commit is contained in:
Lynne 2018-11-07 15:39:12 +10:00
parent 1ec058027c
commit bae9c69d17

45
main.py
View file

@ -156,7 +156,7 @@ for f in following:
uri = j['aliases'][0] uri = j['aliases'][0]
else: else:
uri = j['aliases'][1] uri = j['aliases'][1]
uri = "{}/outbox?page=true&min_id={}".format(uri, last_toot) uri = "{}/outbox?page=true".format(uri)
r = requests.get(uri, timeout=10) r = requests.get(uri, timeout=10)
j = r.json() j = r.json()
except Exception: except Exception:
@ -164,30 +164,25 @@ for f in following:
sys.exit(1) sys.exit(1)
pleroma = False pleroma = False
if 'first' in j: if type(j['first']) != str:
print("{} is a pleroma instance -- falling back to legacy toot collection method".format(instance)) print("Pleroma instance detected")
pleroma = True pleroma = True
j = j['first']
if not pleroma:
print("Mastodon instance detected")
uri = "{}/outbox?page=true&min_id={}".format(uri, last_toot)
r = requests.get(uri)
j = r.json()
j = j['first']
print("Downloading and parsing toots", end='', flush=True) print("Downloading and parsing toots", end='', flush=True)
current = None done = False
try: try:
if pleroma: while not done and len(j['orderedItems']) > 0:
for t in get_toots_legacy(client, f.id):
try:
c.execute("REPLACE INTO toots (id, userid, uri, content) VALUES (?, ?, ?, ?)",
(t['id'],
f.id,
t['uri'],
t['toot']
)
)
except:
pass
else:
while len(j['orderedItems']) > 0:
for oi in j['orderedItems']: for oi in j['orderedItems']:
if (not pleroma and oi['type'] == "Create") or (pleroma and oi['to']['type'] == "Create"): # if (not pleroma and oi['type'] == "Create") or (pleroma and oi['to']['type'] == "Create"):
if oi['type'] == "Create":
# its a toost baby # its a toost baby
content = oi['object']['content'] content = oi['object']['content']
if oi['object']['summary'] != None: if oi['object']['summary'] != None:
@ -196,6 +191,11 @@ for f in following:
toot = extract_toot(content) toot = extract_toot(content)
# print(toot) # print(toot)
try: try:
if pleroma:
if c.execute("SELECT COUNT(*) FROM toots WHERE id LIKE ?", (oi['object']['id'],)).fetchone()[0] > 0:
#we've caught up to the notices we've already downloaded, so we can stop now
done = True
break
pid = re.search(r"[^\/]+$", oi['object']['id']).group(0) pid = re.search(r"[^\/]+$", oi['object']['id']).group(0)
c.execute("REPLACE INTO toots (id, userid, uri, content) VALUES (?, ?, ?, ?)", c.execute("REPLACE INTO toots (id, userid, uri, content) VALUES (?, ?, ?, ?)",
(pid, (pid,
@ -208,7 +208,10 @@ for f in following:
except: except:
pass #ignore any toots that don't go into the DB pass #ignore any toots that don't go into the DB
# sys.exit(0) # sys.exit(0)
r = requests.get(j['prev'], timeout=10) if not pleroma:
r = requests.get(j['prev'], timeout=15)
else:
r = requests.get(j['next'], timeout=15)
j = r.json() j = r.json()
print('.', end='', flush=True) print('.', end='', flush=True)
print(" Done!") print(" Done!")