feat/show other account replys

This commit is contained in:
SouthFox 2022-11-08 02:25:06 +08:00
parent 6793fda0c8
commit 670e7d42ba
3 changed files with 216 additions and 166 deletions

View file

@ -77,7 +77,9 @@
{% endif %} {% endif %}
</div> </div>
<div class="action-bar"> <div class="action-bar">
{% if not toot.is_reblog %} {% if toot.is_myself %}
<span><i class="fa-solid fa-satellite-dish"></i><a href="{{ url_for('grab', toot_id=toot.id) }}"
target=" _blank">抓取回复</a></span>
<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 %} {% endif %}

View file

@ -15,7 +15,7 @@ def app_register(url):
scopes=["read"] scopes=["read"]
) )
def archive_toot(url): def app_login(url):
mastodon = Mastodon( mastodon = Mastodon(
client_id='pyBDSM_clientcred.secret', client_id='pyBDSM_clientcred.secret',
access_token='user.secret', access_token='user.secret',
@ -36,22 +36,26 @@ def archive_toot(url):
# exit in either case # exit in either case
sys.exit(1) sys.exit(1)
return mastodon, user
def get_context(url, toot_id):
mastodon, user = app_login(url)
acct = mastodon.me().acct acct = mastodon.me().acct
statuses_count = str(mastodon.me().statuses_count) context = mastodon.status_context(toot_id)
statuses = []
statuses= context['ancestors'] + context['descendants']
toot_process(statuses, acct)
statuses = mastodon.account_statuses(user["id"], limit=20) db.session.commit()
# xx = statuses['created_at'].astimezone(tz_cn)
# pprint(xx.strftime("%m/%d/%Y, %H:%M:%S"))
# pprint(statuses)
happy_counter = 20
duplicates_counter = 0
while(True):
def toot_process(statuses, my_acct, duplicates_counter=0):
for status in statuses: for status in statuses:
is_reblog = False is_reblog = False
is_myself = False
if status['reblog'] != None: if status['reblog'] != None:
if acct == status['reblog']['account']['acct']: if my_acct == status['reblog']['account']['acct']:
reblog_myself = True reblog_myself = True
else: else:
reblog_myself = False reblog_myself = False
@ -73,7 +77,13 @@ def archive_toot(url):
status = status['reblog'] status = status['reblog']
id = status['id'] id = status['id']
acct = status['account']['acct'] acct = status['account']['acct']
if my_acct == acct:
is_myself = True
else:
is_myself = False
url = status['url'] url = status['url']
created_at = status['created_at'] created_at = status['created_at']
@ -166,7 +176,11 @@ def archive_toot(url):
favourites_count = status['favourites_count'] favourites_count = status['favourites_count']
language = status['language'] language = status['language']
table = Other() if is_reblog else Toot() if is_reblog or not is_myself:
table = Other()
else:
table = Toot()
table.id=id table.id=id
table.acct = acct table.acct = acct
table.url=url table.url=url
@ -199,7 +213,23 @@ def archive_toot(url):
# VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)''' # VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'''
# cur.execute(sql,(id,url,created_at,edited_at,in_reply_to_id,in_reply_to_account_id,content,media_list,spoiler_text,\ # cur.execute(sql,(id,url,created_at,edited_at,in_reply_to_id,in_reply_to_account_id,content,media_list,spoiler_text,\
# poll_id,emoji_list,visibility,reblogged,favourited,bookmarked,sensitive,reblogs_count,favourites_count,language)) # poll_id,emoji_list,visibility,reblogged,favourited,bookmarked,sensitive,reblogs_count,favourites_count,language))
return duplicates_counter
def archive_toot(url):
mastodon, user = app_login(url)
acct = mastodon.me().acct
statuses_count = str(mastodon.me().statuses_count)
statuses = mastodon.account_statuses(user["id"], limit=20)
# xx = statuses['created_at'].astimezone(tz_cn)
# pprint(xx.strftime("%m/%d/%Y, %H:%M:%S"))
# pprint(statuses)
happy_counter = 20
duplicates_counter = 0
while(True):
duplicates_counter = toot_process(statuses, acct)
db.session.commit() db.session.commit()
print(str(happy_counter) + ' / ' + statuses_count) print(str(happy_counter) + ' / ' + statuses_count)
happy_counter += 20 happy_counter += 20

View file

@ -6,7 +6,7 @@ from flask import render_template, request, url_for, redirect, flash
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
from BDSM.toot import app_register, archive_toot from BDSM.toot import app_register, archive_toot, get_context
from mastodon import Mastodon from mastodon import Mastodon
from types import SimpleNamespace from types import SimpleNamespace
from datetime import timezone from datetime import timezone
@ -53,7 +53,8 @@ def search():
def context(toot_id): def context(toot_id):
def get_reply(reply_id): def get_reply(reply_id):
toots = Toot.query.order_by(Toot.created_at.desc()).filter_by(in_reply_to_id=reply_id).all() toots = Toot.query.order_by(Toot.created_at.desc()).filter_by(in_reply_to_id=reply_id).all()
toots = process_toot(toots) other_toots = Other.query.order_by(Other.created_at.desc()).filter_by(in_reply_to_id=reply_id).all()
toots = process_toot(toots) + process_toot(other_toots)
for i in toots: for i in toots:
if i.in_reply_to_id != None: if i.in_reply_to_id != None:
@ -70,6 +71,8 @@ def context(toot_id):
while(in_reply_to_id != None): while(in_reply_to_id != None):
toot = [] toot = []
toot_ = Toot.query.get(toots[0].in_reply_to_id) toot_ = Toot.query.get(toots[0].in_reply_to_id)
if toot_ == None:
toot_ = Other.query.get(toots[0].in_reply_to_id)
if toot_ == None: if toot_ == None:
break break
@ -80,6 +83,17 @@ def context(toot_id):
return render_template('view.html', toots=toots,) return render_template('view.html', toots=toots,)
@app.route('/grab/<int:toot_id>', methods=['GET', 'POST'])
def grab(toot_id):
settings = Settings.query.first()
account = settings.account[1:]
username, domain = account.split("@")
url = "https://" + domain
get_context(url, toot_id)
flash('抓取完成……大概!')
return redirect(url_for('context',toot_id=toot_id))
@app.route('/settings', methods=['GET', 'POST']) @app.route('/settings', methods=['GET', 'POST'])
def settings(): def settings():
if request.method == 'POST': if request.method == 'POST':
@ -170,6 +184,8 @@ def process_toot(toots_):
toot.created_at = toot.created_at.replace(tzinfo=timezone.utc) toot.created_at = toot.created_at.replace(tzinfo=timezone.utc)
toot.created_at = toot.created_at.astimezone(user_timezone).strftime(fmt) toot.created_at = toot.created_at.astimezone(user_timezone).strftime(fmt)
if hasattr(toot, 'reblog_id'):
toot.is_myself = True
if toot.reblog_id != None: if toot.reblog_id != None:
if toot.reblog_myself: if toot.reblog_myself:
toot = Toot.query.get(toot.reblog_id) toot = Toot.query.get(toot.reblog_id)
@ -179,6 +195,8 @@ def process_toot(toots_):
toot = Other.query.get(toot.reblog_id) toot = Other.query.get(toot.reblog_id)
toot = SimpleNamespace(**toot.__dict__) toot = SimpleNamespace(**toot.__dict__)
toot.is_reblog = True toot.is_reblog = True
else:
toot.is_myself = False
if toot.media_list != "": if toot.media_list != "":
toot.medias = [] toot.medias = []