fix: use Mastodon character counter

This commit is contained in:
2026-08-12 09:56:50 -06:00
parent 5261563745
commit 0612f85b21
2 changed files with 6 additions and 17 deletions
+1 -1
View File
@@ -15,6 +15,7 @@ decorator==5.3.1
demoji==2.0.0 demoji==2.0.0
deepl==1.30.0 deepl==1.30.0
future==1.0.0 future==1.0.0
grapheme==0.6.0
idna==3.18 idna==3.18
importlib-metadata==9.0.0 importlib-metadata==9.0.0
iniconfig==2.3.0 iniconfig==2.3.0
@@ -48,7 +49,6 @@ six==1.17.0
sniffio==1.3.1 sniffio==1.3.1
sound_lib @ git+https://github.com/accessibleapps/sound_lib@a439f0943fb95ee7b6ba24f51a686f47c4ad66b2 sound_lib @ git+https://github.com/accessibleapps/sound_lib@a439f0943fb95ee7b6ba24f51a686f47c4ad66b2
sqlitedict==2.1.0 sqlitedict==2.1.0
twitter-text-parser==3.0.0
types-python-dateutil==2.9.0.20260807 types-python-dateutil==2.9.0.20260807
urllib3==2.7.0 urllib3==2.7.0
win-inet-pton==1.1.0 win-inet-pton==1.1.0
+5 -16
View File
@@ -1,14 +1,12 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
import os import os
import re
import wx import wx
import logging import logging
import widgetUtils import widgetUtils
import config import config
import output import output
import languageHandler import languageHandler
from twitter_text import parse_tweet, config from mastodon import Mastodon, MastodonError
from mastodon import MastodonError
from controller import messages from controller import messages
from sessions.mastodon import templates, utils from sessions.mastodon import templates, utils
from wxUI.dialogs.mastodon import postDialogs from wxUI.dialogs.mastodon import postDialogs
@@ -17,18 +15,9 @@ from . import userList
log = logging.getLogger("controller.mastodon.messages") log = logging.getLogger("controller.mastodon.messages")
def character_count(post_text, post_cw, character_limit=500): def character_count(post_text, post_cw):
# We will use text for counting character limit only. """Return the Mastodon-compatible length of a status and its content warning."""
full_text = post_text+post_cw return Mastodon.get_status_length(post_text, post_cw)
# find remote users as Mastodon doesn't count the domain in char limit.
users = re.findall("@[\w\.-]+@[\w\.-]+", full_text)
for user in users:
domain = user.split("@")[-1]
full_text = full_text.replace("@"+domain, "")
options = config.config.get("defaults")
options.update(max_weighted_tweet_length=character_limit, default_weight=100)
parsed = parse_tweet(full_text, options=options)
return parsed.weightedLength
class post(messages.basicMessage): class post(messages.basicMessage):
def __init__(self, session, title, caption, text="", *args, **kwargs): def __init__(self, session, title, caption, text="", *args, **kwargs):
@@ -121,7 +110,7 @@ class post(messages.basicMessage):
def text_processor(self, *args, **kwargs): def text_processor(self, *args, **kwargs):
text = self.message.text.GetValue() text = self.message.text.GetValue()
cw = self.message.spoiler.GetValue() cw = self.message.spoiler.GetValue()
results = character_count(text, cw, character_limit=self.max) results = character_count(text, cw)
self.message.SetTitle(_("%s - %s of %d characters") % (self.title, results, self.max)) self.message.SetTitle(_("%s - %s of %d characters") % (self.title, results, self.max))
if results > self.max: if results > self.max:
self.session.sound.play("max_length.ogg") self.session.sound.play("max_length.ogg")