feat/show other account replys
This commit is contained in:
parent
6793fda0c8
commit
670e7d42ba
3 changed files with 216 additions and 166 deletions
|
@ -77,7 +77,9 @@
|
|||
{% endif %}
|
||||
</div>
|
||||
<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
|
||||
href="{{ url_for('context', toot_id=toot.id) }}" target=" _blank">上下文</a></span>
|
||||
{% endif %}
|
||||
|
|
52
BDSM/toot.py
52
BDSM/toot.py
|
@ -15,7 +15,7 @@ def app_register(url):
|
|||
scopes=["read"]
|
||||
)
|
||||
|
||||
def archive_toot(url):
|
||||
def app_login(url):
|
||||
mastodon = Mastodon(
|
||||
client_id='pyBDSM_clientcred.secret',
|
||||
access_token='user.secret',
|
||||
|
@ -36,22 +36,26 @@ def archive_toot(url):
|
|||
# exit in either case
|
||||
sys.exit(1)
|
||||
|
||||
return mastodon, user
|
||||
|
||||
def get_context(url, toot_id):
|
||||
mastodon, user = app_login(url)
|
||||
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)
|
||||
# xx = statuses['created_at'].astimezone(tz_cn)
|
||||
# pprint(xx.strftime("%m/%d/%Y, %H:%M:%S"))
|
||||
# pprint(statuses)
|
||||
db.session.commit()
|
||||
|
||||
happy_counter = 20
|
||||
duplicates_counter = 0
|
||||
|
||||
while(True):
|
||||
|
||||
def toot_process(statuses, my_acct, duplicates_counter=0):
|
||||
for status in statuses:
|
||||
is_reblog = False
|
||||
is_myself = False
|
||||
if status['reblog'] != None:
|
||||
if acct == status['reblog']['account']['acct']:
|
||||
if my_acct == status['reblog']['account']['acct']:
|
||||
reblog_myself = True
|
||||
else:
|
||||
reblog_myself = False
|
||||
|
@ -73,7 +77,13 @@ def archive_toot(url):
|
|||
status = status['reblog']
|
||||
|
||||
id = status['id']
|
||||
|
||||
acct = status['account']['acct']
|
||||
if my_acct == acct:
|
||||
is_myself = True
|
||||
else:
|
||||
is_myself = False
|
||||
|
||||
url = status['url']
|
||||
created_at = status['created_at']
|
||||
|
||||
|
@ -166,7 +176,11 @@ def archive_toot(url):
|
|||
favourites_count = status['favourites_count']
|
||||
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.acct = acct
|
||||
table.url=url
|
||||
|
@ -199,7 +213,23 @@ def archive_toot(url):
|
|||
# VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)'''
|
||||
# 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))
|
||||
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()
|
||||
print(str(happy_counter) + ' / ' + statuses_count)
|
||||
happy_counter += 20
|
||||
|
|
|
@ -6,7 +6,7 @@ from flask import render_template, request, url_for, redirect, flash
|
|||
from flask_sqlalchemy import Pagination
|
||||
from BDSM import app, db
|
||||
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 types import SimpleNamespace
|
||||
from datetime import timezone
|
||||
|
@ -53,7 +53,8 @@ def search():
|
|||
def context(toot_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 = 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:
|
||||
if i.in_reply_to_id != None:
|
||||
|
@ -70,6 +71,8 @@ def context(toot_id):
|
|||
while(in_reply_to_id != None):
|
||||
toot = []
|
||||
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:
|
||||
break
|
||||
|
||||
|
@ -80,6 +83,17 @@ def context(toot_id):
|
|||
|
||||
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'])
|
||||
def settings():
|
||||
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.astimezone(user_timezone).strftime(fmt)
|
||||
|
||||
if hasattr(toot, 'reblog_id'):
|
||||
toot.is_myself = True
|
||||
if toot.reblog_id != None:
|
||||
if toot.reblog_myself:
|
||||
toot = Toot.query.get(toot.reblog_id)
|
||||
|
@ -179,6 +195,8 @@ def process_toot(toots_):
|
|||
toot = Other.query.get(toot.reblog_id)
|
||||
toot = SimpleNamespace(**toot.__dict__)
|
||||
toot.is_reblog = True
|
||||
else:
|
||||
toot.is_myself = False
|
||||
|
||||
if toot.media_list != "":
|
||||
toot.medias = []
|
||||
|
|
Loading…
Reference in a new issue