From c8d863d86f5fa230e88ec9983ee402ecde7c54fa Mon Sep 17 00:00:00 2001 From: SouthFox Date: Tue, 8 Nov 2022 17:42:42 +0800 Subject: [PATCH] feat/add grab all toots context command --- BDSM/commands.py | 44 +++++++++++++++++++++++++++++++++++++++++++- BDSM/toot.py | 3 ++- 2 files changed, 45 insertions(+), 2 deletions(-) diff --git a/BDSM/commands.py b/BDSM/commands.py index 520f0b3..447bee1 100644 --- a/BDSM/commands.py +++ b/BDSM/commands.py @@ -2,7 +2,9 @@ import click from BDSM import app, db -from BDSM.models import Settings +from BDSM.models import Settings, Toot, Other +from BDSM.toot import app_login, toot_process +from mastodon import MastodonNotFoundError @app.cli.command() @@ -13,3 +15,43 @@ def initdb(drop): db.drop_all() db.create_all() click.echo('Initialized database.') + +@app.cli.command() +def graball(): + """Grab all toots context""" + settings = Settings.query.first() + account = settings.account[1:] + username, domain = account.split("@") + url = "https://" + domain + mastodon, user = app_login(url) + acct = mastodon.me().acct + + toots = Toot.query.filter(Toot.in_reply_to_id.isnot(None)).all() + toots_id = [] + for i in toots: + if (Toot.query.get(i.in_reply_to_id) != None + or Other.query.get(i.in_reply_to_id) != None): + continue + #context api excluding itself + toots_id.append(i.id) + + while(toots_id != []): + toot_id = toots_id.pop(0) + + try: + context = mastodon.status_context(toot_id) + except MastodonNotFoundError: + print('NotFound!') + continue + + statuses = [] + statuses= context['ancestors'] + context['descendants'] + + for i in statuses: + if i['id'] in toots_id: + toots_id.remove(i['id']) + + toot_process(statuses, acct) + db.session.commit() + + print(len(toots_id)) diff --git a/BDSM/toot.py b/BDSM/toot.py index ed24dd5..3a6dab9 100644 --- a/BDSM/toot.py +++ b/BDSM/toot.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +from types import NoneType from mastodon import Mastodon from BDSM import db from BDSM.models import Other, Toot, Tag, Media, Emoji, Poll @@ -140,7 +141,7 @@ def toot_process(statuses, my_acct, duplicates_counter=0): if not is_reblog: data=Emoji.query.filter_by(shortcode=shortcode, acct=acct).first() - if data is None: + if data is None or NoneType: emoji_data = Emoji(shortcode=shortcode, acct=acct, url=emoji['url'],