bots now work properly with pleroma!

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

93
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']
print("Downloading and parsing toots", end='', flush=True)
current = None
try:
if pleroma:
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: if not pleroma:
while len(j['orderedItems']) > 0: print("Mastodon instance detected")
for oi in j['orderedItems']: uri = "{}/outbox?page=true&min_id={}".format(uri, last_toot)
if (not pleroma and oi['type'] == "Create") or (pleroma and oi['to']['type'] == "Create"): r = requests.get(uri)
# its a toost baby j = r.json()
content = oi['object']['content'] j = j['first']
if oi['object']['summary'] != None:
#don't download CW'd toots print("Downloading and parsing toots", end='', flush=True)
continue done = False
toot = extract_toot(content) try:
# print(toot) while not done and len(j['orderedItems']) > 0:
try: for oi in j['orderedItems']:
pid = re.search(r"[^\/]+$", oi['object']['id']).group(0) # 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":
(pid, # its a toost baby
f.id, content = oi['object']['content']
oi['object']['id'], if oi['object']['summary'] != None:
toot #don't download CW'd toots
) continue
toot = extract_toot(content)
# print(toot)
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)
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: