Compare commits

...

3 commits

4 changed files with 80 additions and 25 deletions

View file

@ -2,7 +2,9 @@
import click import click
from BDSM import app, db 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() @app.cli.command()
@ -13,3 +15,43 @@ def initdb(drop):
db.drop_all() db.drop_all()
db.create_all() db.create_all()
click.echo('Initialized database.') 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

@ -80,9 +80,9 @@
{% if toot.is_myself %} {% if toot.is_myself %}
<span><i class="fa-solid fa-satellite-dish"></i><a href="{{ url_for('grab', toot_id=toot.id) }}" <span><i class="fa-solid fa-satellite-dish"></i><a href="{{ url_for('grab', toot_id=toot.id) }}"
target=" _blank">抓取回复</a></span> target=" _blank">抓取回复</a></span>
{% endif %}
<span><i class="fa-solid fa-up-right-and-down-left-from-center"></i><a <span><i class="fa-solid fa-up-right-and-down-left-from-center"></i><a
href="{{ url_for('context', toot_id=toot.id) }}" target=" _blank">上下文</a></span> href="{{ url_for('context', toot_id=toot.id) }}" target=" _blank">上下文</a></span>
{% endif %}
</div> </div>
</div> </div>
</div> </div>

View file

@ -1,5 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from types import NoneType
from mastodon import Mastodon from mastodon import Mastodon
from BDSM import db from BDSM import db
from BDSM.models import Other, Toot, Tag, Media, Emoji, Poll 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: if not is_reblog:
data=Emoji.query.filter_by(shortcode=shortcode, acct=acct).first() 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, emoji_data = Emoji(shortcode=shortcode,
acct=acct, acct=acct,
url=emoji['url'], url=emoji['url'],
@ -221,27 +222,32 @@ def toot_process(statuses, my_acct, duplicates_counter=0):
def archive_toot(url): def archive_toot(url):
mastodon, user = app_login(url) mastodon, user = app_login(url)
acct = mastodon.me().acct acct = mastodon.me().acct
def archive(statuses, skip_duplicates=True):
happy_counter = 20
duplicates_counter = 0
while(True):
duplicates_counter = toot_process(statuses, acct)
db.session.commit()
print(str(happy_counter) + ' / ' + statuses_count)
happy_counter += 20
if duplicates_counter >= 10 and skip_duplicates:
print("检测到重复嘟文达到十次,取消存档……")
break
statuses = mastodon.fetch_next(statuses)
if statuses == None:
break
statuses_count = str(mastodon.me().statuses_count) statuses_count = str(mastodon.me().statuses_count)
statuses = mastodon.account_statuses(user["id"], limit=20) statuses = mastodon.account_statuses(user["id"], limit=20)
# xx = statuses['created_at'].astimezone(tz_cn) archive(statuses)
# pprint(xx.strftime("%m/%d/%Y, %H:%M:%S"))
# pprint(statuses)
happy_counter = 20 statuses_count = '???'
duplicates_counter = 0 statuses = mastodon.favourites()
archive(statuses)
while(True): statuses = mastodon.bookmarks()
duplicates_counter = toot_process(statuses, acct) archive(statuses, skip_duplicates=False)
db.session.commit()
print(str(happy_counter) + ' / ' + statuses_count)
happy_counter += 20
if duplicates_counter >= 10:
print("检测到重复嘟文达到十次,取消存档……")
break
statuses = mastodon.fetch_next(statuses)
# statuses = None
if statuses == None:
break

View file

@ -2,7 +2,7 @@
import os import os
import pytz import pytz
from flask import render_template, request, url_for, redirect, flash from flask import render_template, request, url_for, redirect, flash, abort
from flask_sqlalchemy import Pagination from flask_sqlalchemy import Pagination
from BDSM import app, db from BDSM import app, db
from BDSM.models import Media, Settings, Toot, Emoji, Other from BDSM.models import Media, Settings, Toot, Emoji, Other
@ -63,7 +63,14 @@ def context(toot_id):
return toots return toots
toots = [] toots = []
toots.append(Toot.query.get_or_404(toot_id))
toot_ = Toot.query.get(toot_id)
if toot_ == None:
toot_ = Other.query.get(toot_id)
if toot_ == None:
abort(404)
toots.append(toot_)
toots = process_toot(toots) toots = process_toot(toots)
toots[0].reply = get_reply(toot_id) toots[0].reply = get_reply(toot_id)