diff --git a/BDSM/commands.py b/BDSM/commands.py index 6be5677..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]) @@ -193,17 +193,15 @@ 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() 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 07601ef..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) @@ -79,6 +56,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..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 +from BDSM.models import 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'] @@ -52,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 @@ -78,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'] @@ -181,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 @@ -208,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 d0a9570..1402035 100644 --- a/BDSM/views.py +++ b/BDSM/views.py @@ -5,9 +5,10 @@ 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, Other -from BDSM.toot import app_register, archive_toot, get_context +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 from datetime import timezone @@ -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" @@ -32,6 +33,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': @@ -55,8 +71,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: @@ -68,9 +82,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) @@ -81,9 +93,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) @@ -95,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) @@ -106,20 +115,18 @@ def grab(toot_id): @app.route('/settings', methods=['GET', 'POST']) def settings(): if request.method == 'POST': - account = request.form['account'] timezone = request.form['timezone'] - - if not account or len(account) > 30: - flash('无效输入') - return redirect(url_for('settings')) - settings = Settings.query.first() - if settings == None: - settings = Settings(account=account, timezone=timezone) + 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: - settings.account = account settings.timezone = timezone db.session.commit() @@ -137,13 +144,17 @@ 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 + 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) @@ -168,8 +179,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) @@ -196,20 +206,16 @@ 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 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,"