feat/add grab all toots context command

This commit is contained in:
SouthFox 2022-11-08 17:42:42 +08:00
parent 51dcceed35
commit c8d863d86f
2 changed files with 45 additions and 2 deletions

View file

@ -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))

View file

@ -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'],