From e079552833617fafc2fb815a32da584fb20c2dc1 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Fri, 17 Feb 2023 12:33:34 +0800 Subject: [PATCH 1/3] refactor/settings --- BDSM/commands.py | 5 ++--- BDSM/models.py | 3 ++- BDSM/templates/settings.html | 17 ++++++++--------- BDSM/toot.py | 7 ++++--- BDSM/views.py | 37 ++++++++++++++++++++++++++---------- 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/BDSM/commands.py b/BDSM/commands.py index 6be5677..b291fe0 100644 --- a/BDSM/commands.py +++ b/BDSM/commands.py @@ -193,10 +193,9 @@ def renderfile(): def graball(): """Grab all toots context""" settings = Settings.query.first() - account = settings.account[1:] - username, domain = account.split("@") + domain = settings.domain url = "https://" + domain - mastodon, user = app_login(url) + mastodon, _ = app_login(url) acct = mastodon.me().acct toots = Toot.query.filter(Toot.in_reply_to_id.isnot(None)).all() diff --git a/BDSM/models.py b/BDSM/models.py index 07601ef..596e5ba 100644 --- a/BDSM/models.py +++ b/BDSM/models.py @@ -79,6 +79,7 @@ class Poll(db.Model): options = db.Column(db.Text) class Settings(db.Model): - account = db.Column(db.Text, primary_key=True) + domain = db.Column(db.Text, primary_key=True) + account = db.Column(db.Text) timezone = db.Column(db.Text) setup = db.Column(db.Boolean) diff --git a/BDSM/templates/settings.html b/BDSM/templates/settings.html index 483fb3b..2e48400 100644 --- a/BDSM/templates/settings.html +++ b/BDSM/templates/settings.html @@ -4,13 +4,12 @@

设置

{% if settings != None %} -
- -
- -
-
+
+

站点地址:

+

{{ settings.domain }}

+
+

用户名:

+

{{ settings.account }}

@@ -24,9 +23,9 @@
{% else %}
- +
- +
diff --git a/BDSM/toot.py b/BDSM/toot.py index c0efba5..bcc8e06 100644 --- a/BDSM/toot.py +++ b/BDSM/toot.py @@ -3,7 +3,7 @@ from mastodon import Mastodon from tenacity import * from BDSM import db -from BDSM.models import Other, Toot, Tag, Media, Emoji, Poll +from BDSM.models import Other, Toot, Tag, Media, Emoji, Poll, Settings import sys import dateutil.parser @@ -40,8 +40,9 @@ def app_login(url): return mastodon, user def get_context(url, toot_id): - mastodon, user = app_login(url) - acct = mastodon.me().acct + mastodon, _ = app_login(url) + settings = Settings.query.first() + acct = settings.account context = mastodon.status_context(toot_id) statuses = [] statuses= context['ancestors'] + context['descendants'] diff --git a/BDSM/views.py b/BDSM/views.py index d0a9570..7969828 100644 --- a/BDSM/views.py +++ b/BDSM/views.py @@ -7,7 +7,7 @@ from flask import render_template, request, url_for, redirect, flash, abort from flask_sqlalchemy import Pagination from BDSM import app, db from BDSM.models import Media, Poll, Settings, Toot, Emoji, Other -from BDSM.toot import app_register, archive_toot, get_context +from BDSM.toot import app_login, app_register, archive_toot, get_context from mastodon import Mastodon from types import SimpleNamespace from datetime import timezone @@ -32,6 +32,21 @@ def index(): return render_template('view.html', toots=toots, pagination=toots_, path=path) +@app.route('/favourited', methods=['GET', 'POST']) +def favourited(): + settings = Settings.query.first() + if settings == None: + return redirect(url_for('settings')) + else: + page = request.args.get('page', 1, type=int) + toots_ = Toot.query.order_by(Toot.created_at.desc()).paginate(page=page, per_page=50) + toots = process_toot(toots_) + path=SimpleNamespace() + path.path = "favourited" + path.args = {} + + return render_template('view.html', toots=toots, pagination=toots_, path=path) + @app.route('/search', methods=['GET', 'POST']) def search(): if request.method == 'POST': @@ -106,20 +121,19 @@ def grab(toot_id): @app.route('/settings', methods=['GET', 'POST']) def settings(): if request.method == 'POST': - account = request.form['account'] + domain = request.form['domain'] timezone = request.form['timezone'] - if not account or len(account) > 30: + if not domain or len(domain) > 50: flash('无效输入') return redirect(url_for('settings')) settings = Settings.query.first() if settings == None: - settings = Settings(account=account, timezone=timezone) + settings = Settings(domain=domain, timezone=timezone) db.session.add(settings) else: - settings.account = account settings.timezone = timezone db.session.commit() @@ -137,11 +151,10 @@ def settings(): def register(): settings = Settings.query.first() if settings == None: - flash('请先输入用户名!') + flash('请先输入站点地址!') return redirect(url_for('settings')) else: - account = settings.account[1:] - username, domain = account.split("@") + domain = settings.domain url = "https://" + domain if request.method == 'POST': @@ -149,6 +162,11 @@ def register(): mastodon = Mastodon(client_id='pyBDSM_clientcred.secret', api_base_url=url) mastodon.log_in(code=token, to_file='user.secret', scopes=['read']) + mastodon, _ = app_login(url) + account = mastodon.me().acct + settings.account = account + db.session.commit() + if os.path.isfile('user.secret'): flash('应用已授权!') return redirect(url_for('settings')) @@ -168,8 +186,7 @@ def archive(): settings = Settings.query.first() if request.method == 'POST': archive_match = request.form.getlist("archive_match") - account = settings.account[1:] - username, domain = account.split("@") + domain = settings.domain url = "https://" + domain archive_toot(url, archive_match) -- 2.45.2 From fbfd8a1505687127d4cfc1596adc1dc19aacfb47 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Fri, 17 Feb 2023 12:48:44 +0800 Subject: [PATCH 2/3] refactor/remove Other model --- BDSM/commands.py | 11 +++++------ BDSM/models.py | 23 ----------------------- BDSM/toot.py | 15 +++------------ BDSM/views.py | 25 +++++++------------------ 4 files changed, 15 insertions(+), 59 deletions(-) diff --git a/BDSM/commands.py b/BDSM/commands.py index b291fe0..91cdd86 100644 --- a/BDSM/commands.py +++ b/BDSM/commands.py @@ -2,7 +2,7 @@ import click from BDSM import app, db -from BDSM.models import Settings, Toot, Other +from BDSM.models import Settings, Toot from BDSM.toot import app_login, toot_process from mastodon import MastodonNotFoundError @@ -19,7 +19,7 @@ def initdb(drop): @app.cli.command() def analysis(): """Analysis current Year""" - from BDSM.models import Toot, Other + from BDSM.models import Toot from sqlalchemy.sql import extract from sqlalchemy import func from sqlalchemy import desc @@ -42,8 +42,8 @@ def analysis(): ) print("2022 年互动最多帐号排名" + - str(db.session.query(Other.acct.label('count'),func.count('count') - ).filter(extract('year', Other.created_at) == 2022 + str(db.session.query(Toot.acct.label('count'),func.count('count') + ).filter(extract('year', Toot.created_at) == 2022 ).group_by('count' ).order_by(desc(func.count('count')) ).all()[:3]) @@ -201,8 +201,7 @@ def graball(): 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): + if (Toot.query.get(i.in_reply_to_id) != None): continue #context api excluding itself toots_id.append(i.id) diff --git a/BDSM/models.py b/BDSM/models.py index 596e5ba..5a48936 100644 --- a/BDSM/models.py +++ b/BDSM/models.py @@ -26,29 +26,6 @@ class Toot(db.Model): favourites_count = db.Column(db.Integer) language = db.Column(db.Text) -class Other(db.Model): - id = db.Column(db.Integer, primary_key=True) - acct = db.Column(db.Text) - url = db.Column(db.Text) - created_at = db.Column(db.DateTime) - edited_at = db.Column(db.DateTime) - in_reply_to_id = db.Column(db.Integer) - in_reply_to_account_id = db.Column(db.Integer) - content = db.Column(db.Text) - media_list = db.Column(db.Text) - emoji_list = db.Column(db.Text) - spoiler_text = db.Column(db.Text) - poll_id = db.Column(db.Integer) - visibility = db.Column(db.Text) - reblogged = db.Column(db.Boolean) - favourited = db.Column(db.Boolean) - bookmarked = db.Column(db.Boolean) - sensitive = db.Column(db.Boolean) - replies_count = db.Column(db.Integer) - reblogs_count = db.Column(db.Integer) - favourites_count = db.Column(db.Integer) - language = db.Column(db.Text) - class Tag(db.Model): __table_args__ = {'sqlite_autoincrement': True} tag_id = db.Column(db.Integer, primary_key=True) diff --git a/BDSM/toot.py b/BDSM/toot.py index bcc8e06..4e0e49e 100644 --- a/BDSM/toot.py +++ b/BDSM/toot.py @@ -3,7 +3,7 @@ from mastodon import Mastodon from tenacity import * from BDSM import db -from BDSM.models import Other, Toot, Tag, Media, Emoji, Poll, Settings +from BDSM.models import Toot, Tag, Media, Emoji, Poll, Settings import sys import dateutil.parser @@ -53,7 +53,6 @@ def get_context(url, toot_id): def toot_process(statuses, my_acct, duplicates_counter=0): for status in statuses: is_reblog = False - is_myself = False if status['reblog'] != None: if my_acct == status['reblog']['account']['acct']: reblog_myself = True @@ -79,11 +78,6 @@ def toot_process(statuses, my_acct, duplicates_counter=0): 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'] @@ -182,10 +176,7 @@ def toot_process(statuses, my_acct, duplicates_counter=0): favourites_count = status['favourites_count'] language = status['language'] - if is_reblog or not is_myself: - table = Other() - else: - table = Toot() + table = Toot() table.id=id table.acct = acct @@ -209,7 +200,7 @@ def toot_process(statuses, my_acct, duplicates_counter=0): table.favourites_count=favourites_count table.language=language - if Toot.query.get(id) != None or Other.query.get(id) != None: + if Toot.query.get(id) != None: duplicates_counter += 1 db.session.merge(table) diff --git a/BDSM/views.py b/BDSM/views.py index 7969828..2721668 100644 --- a/BDSM/views.py +++ b/BDSM/views.py @@ -6,7 +6,7 @@ import json from flask import render_template, request, url_for, redirect, flash, abort from flask_sqlalchemy import Pagination from BDSM import app, db -from BDSM.models import Media, Poll, Settings, Toot, Emoji, Other +from BDSM.models import Media, Poll, Settings, Toot, Emoji from BDSM.toot import app_login, app_register, archive_toot, get_context from mastodon import Mastodon from types import SimpleNamespace @@ -70,8 +70,6 @@ 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() - 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: @@ -83,9 +81,7 @@ def context(toot_id): toot_ = Toot.query.get(toot_id) if toot_ == None: - toot_ = Other.query.get(toot_id) - if toot_ == None: - abort(404) + abort(404) toots.append(toot_) toots = process_toot(toots) @@ -96,9 +92,7 @@ def context(toot_id): 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 + break toot.append(toot_) toot = process_toot(toot) @@ -213,17 +207,12 @@ 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'): + if toot.acct == settings.account: toot.is_myself = True if toot.reblog_id != None: - if toot.reblog_myself: - toot = Toot.query.get(toot.reblog_id) - toot = SimpleNamespace(**toot.__dict__) - toot.is_reblog = True - else: - toot = Other.query.get(toot.reblog_id) - toot = SimpleNamespace(**toot.__dict__) - toot.is_reblog = True + toot = Toot.query.get(toot.reblog_id) + toot = SimpleNamespace(**toot.__dict__) + toot.is_reblog = True else: toot.is_myself = False -- 2.45.2 From 5c57d884ee4568507fc25ac3710b531c59fb3ef8 Mon Sep 17 00:00:00 2001 From: SouthFox Date: Fri, 17 Feb 2023 13:24:11 +0800 Subject: [PATCH 3/3] refactor/done, i think? --- BDSM/views.py | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/BDSM/views.py b/BDSM/views.py index 2721668..1402035 100644 --- a/BDSM/views.py +++ b/BDSM/views.py @@ -5,6 +5,7 @@ import json from flask import render_template, request, url_for, redirect, flash, abort from flask_sqlalchemy import Pagination +from sqlalchemy import or_ from BDSM import app, db from BDSM.models import Media, Poll, Settings, Toot, Emoji from BDSM.toot import app_login, app_register, archive_toot, get_context @@ -24,7 +25,7 @@ def index(): return redirect(url_for('settings')) else: page = request.args.get('page', 1, type=int) - toots_ = Toot.query.order_by(Toot.created_at.desc()).paginate(page=page, per_page=50) + toots_ = Toot.query.order_by(Toot.created_at.desc()).filter(or_(Toot.acct==settings.account, Toot.reblog_id!=None)).paginate(page=page, per_page=50) toots = process_toot(toots_) path=SimpleNamespace() path.path = "index" @@ -104,8 +105,7 @@ def context(toot_id): @app.route('/grab/', methods=['GET', 'POST']) def grab(toot_id): settings = Settings.query.first() - account = settings.account[1:] - username, domain = account.split("@") + domain = settings.domain url = "https://" + domain get_context(url, toot_id) @@ -115,16 +115,15 @@ def grab(toot_id): @app.route('/settings', methods=['GET', 'POST']) def settings(): if request.method == 'POST': - domain = request.form['domain'] timezone = request.form['timezone'] - - if not domain or len(domain) > 50: - flash('无效输入') - return redirect(url_for('settings')) - settings = Settings.query.first() - if settings == None: + domain = request.form['domain'] + + if not domain or len(domain) > 50: + flash('无效输入') + return redirect(url_for('settings')) + settings = Settings(domain=domain, timezone=timezone) db.session.add(settings) else: @@ -151,16 +150,16 @@ def register(): domain = settings.domain url = "https://" + domain + mastodon, _ = app_login(url) + account = mastodon.me().acct + settings.account = account + db.session.commit() + if request.method == 'POST': token = request.form['token'].rstrip() mastodon = Mastodon(client_id='pyBDSM_clientcred.secret', api_base_url=url) mastodon.log_in(code=token, to_file='user.secret', scopes=['read']) - mastodon, _ = app_login(url) - account = mastodon.me().acct - settings.account = account - db.session.commit() - if os.path.isfile('user.secret'): flash('应用已授权!') return redirect(url_for('settings')) @@ -209,13 +208,14 @@ def process_toot(toots_): if toot.acct == settings.account: toot.is_myself = True - if toot.reblog_id != None: - toot = Toot.query.get(toot.reblog_id) - toot = SimpleNamespace(**toot.__dict__) - toot.is_reblog = True else: toot.is_myself = False + if toot.reblog_id != None: + toot = Toot.query.get(toot.reblog_id) + toot = SimpleNamespace(**toot.__dict__) + toot.is_reblog = True + if toot.media_list != "": toot.medias = [] #media_list "1111,2222,333," -- 2.45.2