bots now work properly with pleroma!

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

89
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,53 +164,56 @@ 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): for oi in j['orderedItems']:
try: # if (not pleroma and oi['type'] == "Create") or (pleroma and oi['to']['type'] == "Create"):
c.execute("REPLACE INTO toots (id, userid, uri, content) VALUES (?, ?, ?, ?)", if oi['type'] == "Create":
(t['id'], # its a toost baby
f.id, content = oi['object']['content']
t['uri'], if oi['object']['summary'] != None:
t['toot'] #don't download CW'd toots
) continue
) toot = extract_toot(content)
except: # print(toot)
pass try:
if pleroma:
else: if c.execute("SELECT COUNT(*) FROM toots WHERE id LIKE ?", (oi['object']['id'],)).fetchone()[0] > 0:
while len(j['orderedItems']) > 0: #we've caught up to the notices we've already downloaded, so we can stop now
for oi in j['orderedItems']: done = True
if (not pleroma and oi['type'] == "Create") or (pleroma and oi['to']['type'] == "Create"): break
# its a toost baby pid = re.search(r"[^\/]+$", oi['object']['id']).group(0)
content = oi['object']['content'] c.execute("REPLACE INTO toots (id, userid, uri, content) VALUES (?, ?, ?, ?)",
if oi['object']['summary'] != None: (pid,
#don't download CW'd toots f.id,
continue oi['object']['id'],
toot = extract_toot(content) toot
# print(toot)
try:
pid = re.search(r"[^\/]+$", oi['object']['id']).group(0)
c.execute("REPLACE INTO toots (id, userid, uri, content) VALUES (?, ?, ?, ?)",
(pid,
f.id,
oi['object']['id'],
toot
)
) )
pass )
except: pass
pass #ignore any toots that don't go into the DB except:
# sys.exit(0) pass #ignore any toots that don't go into the DB
r = requests.get(j['prev'], timeout=10) # sys.exit(0)
j = r.json() if not pleroma:
print('.', end='', flush=True) r = requests.get(j['prev'], timeout=15)
else:
r = requests.get(j['next'], timeout=15)
j = r.json()
print('.', end='', flush=True)
print(" Done!") print(" Done!")
db.commit() db.commit()
except: except: