-
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathmastodon_api.py
More file actions
1123 lines (977 loc) · 37.5 KB
/
mastodon_api.py
File metadata and controls
1123 lines (977 loc) · 37.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import datetime
from mastodon import Mastodon, MastodonError
import streaming
import application
from version import APP_NAME, APP_VERSION
import threading
from GUI import main, misc
import config
import timeline
import speak
from GUI.ask import *
from GUI.platform_dialog import select_platform, get_bluesky_credentials
import webbrowser
import platform
import os
import sys
import wx
from models import UserCache
from platforms.mastodon import MastodonAccount
# Get logger for API operations
try:
from logging_config import get_logger
_logger = get_logger('api')
except ImportError:
_logger = None
class AccountSetupCancelled(Exception):
"""Raised when user cancels account setup."""
pass
def _exit_app():
"""Safely exit the application from within wxPython context."""
# Raise exception to stop current code execution, then schedule exit
raise AccountSetupCancelled()
class mastodon(object):
"""Multi-platform account wrapper. Despite the name, supports both Mastodon and Bluesky."""
def __init__(self, app, index):
self.app = app
# Folder index for the on-disk account{N} directory. Tracked explicitly
# so removal/renumbering can find the right account regardless of the
# order in which parallel-loaded accounts landed in app.accounts.
self.folder_index = index
self.ready = False
self.timelines = []
self.currentTimeline = None
self.currentIndex = 0
self.currentStatus = None
self.confpath = ""
# Initialize streaming-related attributes early
self._pending_initial_loads = 0
self._initial_loads_lock = threading.Lock()
self._stream_lock = threading.Lock() # Prevents multiple stream connections
self.stream_listener = None
self.stream_thread = None
self.stream = None
self._stream_started = False
# In portable mode, don't add FastSM prefix (userdata is already app-specific)
if config.is_portable_mode():
self.prefs = config.Config(name="account"+str(index), autosave=True)
self.confpath = self.prefs._user_config_home+"/account"+str(index)
else:
self.prefs = config.Config(name="FastSM/account"+str(index), autosave=True)
self.confpath = self.prefs._user_config_home+"/FastSM/account"+str(index)
# Platform backend (initialized after authentication)
self._platform = None
# Check platform type - this determines which auth flow to use
self.prefs.platform_type = self.prefs.get("platform_type", "")
# Timeline preferences (shared across platforms)
self.prefs.user_timelines = self.prefs.get("user_timelines", [])
self.prefs.list_timelines = self.prefs.get("list_timelines", [])
self.prefs.search_timelines = self.prefs.get("search_timelines", [])
self.prefs.custom_timelines = self.prefs.get("custom_timelines", [])
self.prefs.instance_timelines = self.prefs.get("instance_timelines", [])
self.prefs.remote_user_timelines = self.prefs.get("remote_user_timelines", [])
# Built-in timeline order (list of timeline types in desired order)
self.prefs.timeline_order = self.prefs.get("timeline_order", [])
# Remote API instances for instance timelines (unauthenticated)
self.remote_apis = {}
self.prefs.footer = self.prefs.get("footer", "")
self.prefs.soundpack = self.prefs.get("soundpack", "default")
self.prefs.soundpan = self.prefs.get("soundpan", 0)
self.prefs.soundpack_volume = self.prefs.get("soundpack_volume", 1.0) # Per-account soundpack volume
self.prefs.mentions_in_notifications = self.prefs.get("mentions_in_notifications", False)
# Local position sync for notifications/mentions timelines
self.prefs.last_notifications_id = self.prefs.get("last_notifications_id", None)
self.prefs.last_mentions_id = self.prefs.get("last_mentions_id", None)
# User aliases - maps user ID to custom display name
self.prefs.aliases = self.prefs.get("aliases", {})
# Determine platform type if not set
if self.prefs.platform_type == "":
# New account - ask user which platform
selected = select_platform(main.window)
if selected is None:
_exit_app()
self.prefs.platform_type = selected
# Initialize based on platform type
if self.prefs.platform_type == "bluesky":
self._init_bluesky(index)
else:
self.prefs.platform_type = "mastodon"
self._init_mastodon(index)
def _init_mastodon(self, index):
"""Initialize Mastodon account."""
# Mastodon-specific config
self.prefs.instance_url = self.prefs.get("instance_url", "")
self.prefs.access_token = self.prefs.get("access_token", "")
is_new_signin = self.prefs.access_token == "" # Track if this is a new sign-in
self.prefs.client_id = self.prefs.get("client_id", "")
self.prefs.client_secret = self.prefs.get("client_secret", "")
# Get instance URL if not set or invalid
def is_valid_instance_url(url):
"""Check if URL is a valid instance URL with a host."""
if not url or not url.strip():
return False
# Must have a host after the protocol
if url.startswith("https://"):
host = url[8:]
elif url.startswith("http://"):
host = url[7:]
else:
host = url
# Host must have at least one dot or be localhost
return bool(host) and ('.' in host or host.startswith('localhost'))
if not is_valid_instance_url(self.prefs.instance_url):
# Clear credentials if instance URL is invalid (they're tied to the instance)
self.prefs.client_id = ""
self.prefs.client_secret = ""
self.prefs.access_token = ""
self.prefs.instance_url = ask(caption="Mastodon Instance",
message="Enter your Mastodon instance URL (e.g., mastodon.social, fosstodon.org):")
if self.prefs.instance_url is None or not self.prefs.instance_url.strip():
_exit_app()
self.prefs.instance_url = self.prefs.instance_url.strip()
# Ensure https://
if not self.prefs.instance_url.startswith("https://") and not self.prefs.instance_url.startswith("http://"):
self.prefs.instance_url = "https://" + self.prefs.instance_url
# Register app if needed
if self.prefs.client_id == "" or self.prefs.client_secret == "":
try:
client_id, client_secret = Mastodon.create_app(
"FastSM",
scopes=['read', 'write', 'follow', 'push'],
redirect_uris='urn:ietf:wg:oauth:2.0:oob',
api_base_url=self.prefs.instance_url,
user_agent=f"{APP_NAME}/{APP_VERSION}"
)
self.prefs.client_id = client_id
self.prefs.client_secret = client_secret
except MastodonError as e:
speak.speak("Error registering app: " + str(e))
_exit_app()
# Authenticate if needed
if self.prefs.access_token == "":
try:
temp_api = Mastodon(
client_id=self.prefs.client_id,
client_secret=self.prefs.client_secret,
api_base_url=self.prefs.instance_url,
user_agent=f"{APP_NAME}/{APP_VERSION}"
)
auth_url = temp_api.auth_request_url(
scopes=['read', 'write', 'follow', 'push'],
redirect_uris='urn:ietf:wg:oauth:2.0:oob'
)
webbrowser.open(auth_url)
auth_code = ask(caption="Authorization Code",
message="Enter the authorization code from your browser:")
if auth_code is None:
_exit_app()
access_token = temp_api.log_in(code=auth_code, scopes=['read', 'write', 'follow', 'push'])
self.prefs.access_token = access_token
except MastodonError as e:
speak.speak("Error during authentication: " + str(e))
_exit_app()
# Initialize the API
self.api = Mastodon(
client_id=self.prefs.client_id,
client_secret=self.prefs.client_secret,
access_token=self.prefs.access_token,
api_base_url=self.prefs.instance_url,
user_agent=f"{APP_NAME}/{APP_VERSION}"
)
# Verify credentials and get user info
try:
self.me = self.api.account_verify_credentials()
except MastodonError as e:
# Don't clear credentials on transient failures (server offline, network issues)
# Only exit - credentials will be preserved for next startup
speak.speak("Error connecting to server: " + str(e))
_exit_app()
# Prompt to follow FastSM account on new sign-in
if is_new_signin:
self._prompt_follow_fastsm()
# Get instance info for character limit (always fetch, fall back to cache)
try:
instance_info = self.api.instance()
if hasattr(instance_info, 'configuration') and hasattr(instance_info.configuration, 'statuses'):
self.max_chars = instance_info.configuration.statuses.max_characters
else:
self.max_chars = 500
# Update cache
self.prefs.cached_max_chars = self.max_chars
except:
cached_max_chars = self.prefs.get("cached_max_chars", 0)
if cached_max_chars > 0:
self.max_chars = cached_max_chars
else:
self.max_chars = 500
# Get default visibility from user info (already fetched)
self.default_visibility = getattr(self.me, 'source', {}).get('privacy', 'public')
# Initialize platform backend with user cache
self._platform = MastodonAccount(self.app, index, self.api, self.me, self.confpath, self.max_chars, self.prefs)
# Migrate global user cache to per-account if this is the first run
self._migrate_user_cache()
self._finish_init(index)
# Create built-in timelines in user's preferred order
self._create_builtin_timelines()
# Restore saved timelines (avoid API calls during startup for speed)
for ut_entry in list(self.prefs.user_timelines):
try:
# Handle both string and dict entries (dict has username and optional filter)
if isinstance(ut_entry, dict):
username = ut_entry.get('username', '')
user_filter = ut_entry.get('filter')
filter_labels = {
'posts_no_replies': 'Posts Only',
'posts_with_media': 'Media',
'posts_and_author_threads': 'Threads',
'posts_with_video': 'Videos',
'posts_no_boosts': 'No Boosts',
}
tl_name = username + "'s Timeline"
if user_filter and user_filter in filter_labels:
tl_name = f"{username}'s {filter_labels[user_filter]}"
self.timelines.append(timeline.timeline(self, name=tl_name, type="user", data=ut_entry, user=None, silent=True))
else:
username = ut_entry
self.timelines.append(timeline.timeline(self, name=username + "'s Timeline", type="user", data=username, user=None, silent=True))
except:
self.prefs.user_timelines.remove(ut_entry)
for list_data in list(self.prefs.list_timelines):
try:
if not isinstance(list_data, dict):
self.prefs.list_timelines.remove(list_data)
continue
list_id = list_data.get('id')
list_name = list_data.get('name', 'List')
self.timelines.append(timeline.timeline(self, name=list_name + " List", type="list", data=list_id, silent=True))
except:
self.prefs.list_timelines.remove(list_data)
for q in list(self.prefs.search_timelines):
try:
self.timelines.append(timeline.timeline(self, name=q + " Search", type="search", data=q, silent=True))
except:
self.prefs.search_timelines.remove(q)
# Restore custom timelines (local, federated, favourites, bookmarks)
for ct in list(self.prefs.custom_timelines):
try:
tl_type = ct.get('type', '')
tl_id = ct.get('id', '')
tl_name = ct.get('name', tl_type.title())
if tl_type in ('local', 'federated', 'favourites', 'bookmarks'):
self.timelines.append(timeline.timeline(self, name=tl_name, type=tl_type, data=tl_id, silent=True))
except:
self.prefs.custom_timelines.remove(ct)
# Restore instance timelines (remote instance local timelines)
for inst in list(self.prefs.instance_timelines):
try:
inst_url = inst.get('url', '')
inst_name = inst.get('name', inst_url + ' Local')
if inst_url:
self.timelines.append(timeline.timeline(self, name=inst_name, type="instance", data=inst_url, silent=True))
except:
self.prefs.instance_timelines.remove(inst)
# Restore remote user timelines
for rut in list(self.prefs.remote_user_timelines):
try:
inst_url = rut.get('url', '')
username = rut.get('username', '')
rut_filter = rut.get('filter')
# Build timeline name based on filter
filter_labels = {
'posts_no_replies': 'Posts Only',
'posts_with_media': 'Media',
'posts_no_boosts': 'No Boosts',
}
instance_domain = inst_url.replace('https://', '')
rut_name = f"@{username}@{instance_domain}"
if rut_filter and rut_filter in filter_labels:
rut_name = f"@{username}@{instance_domain}'s {filter_labels[rut_filter]}"
# Use saved name if available
if rut.get('name'):
rut_name = rut.get('name')
if inst_url and username:
data = {'url': inst_url, 'username': username}
if rut_filter:
data['filter'] = rut_filter
self.timelines.append(timeline.timeline(self, name=rut_name, type="remote_user", data=data, silent=True))
except:
self.prefs.remote_user_timelines.remove(rut)
# Only reset stream state if not already streaming
# (this can be called during re-init while stream is running)
if not (self.stream_thread is not None and self.stream_thread.is_alive()):
self.stream_listener = None
self.stream = None
self._stream_started = False
# Don't start streaming yet - wait for initial timeline loads to complete
# Streaming will be started by _check_initial_loads_complete()
self._finish_timeline_init()
def _prompt_follow_fastsm(self):
"""Prompt user to follow mewproj account after new Mastodon sign-in."""
result = wx.MessageBox(
"Would you like to follow mewproj@fwoof.space to get updates about the app?",
"Follow mewproj",
wx.YES_NO | wx.ICON_QUESTION
)
if result == wx.YES:
try:
# Look up the mewproj account via search
results = self.api.account_search(q="mewproj@fwoof.space", limit=1)
if results and len(results) > 0:
self.api.account_follow(id=results[0].id)
speak.speak("Now following mewproj")
else:
speak.speak("Could not find mewproj account")
except Exception as e:
speak.speak(f"Could not follow: {e}")
def _init_bluesky(self, index):
"""Initialize Bluesky account."""
from atproto import Client
from atproto.exceptions import AtProtocolError
from platforms.bluesky import BlueskyAccount, bluesky_profile_to_universal
# Bluesky-specific config
self.prefs.bluesky_handle = self.prefs.get("bluesky_handle", "")
self.prefs.bluesky_password = self.prefs.get("bluesky_password", "")
self.prefs.bluesky_service = self.prefs.get("bluesky_service", "https://bsky.social")
# Get credentials if not set
if self.prefs.bluesky_handle == "" or self.prefs.bluesky_password == "":
creds = get_bluesky_credentials(main.window)
if creds is None:
_exit_app()
self.prefs.bluesky_handle = creds['handle']
self.prefs.bluesky_password = creds['password']
self.prefs.bluesky_service = creds['service_url']
# Initialize the client and login
try:
self.api = Client(base_url=self.prefs.bluesky_service)
raw_profile = self.api.login(self.prefs.bluesky_handle, self.prefs.bluesky_password)
self.me = bluesky_profile_to_universal(raw_profile)
except AtProtocolError as e:
speak.speak("Error logging into Bluesky: " + str(e))
# Clear credentials
self.prefs.bluesky_handle = ""
self.prefs.bluesky_password = ""
_exit_app()
except Exception as e:
speak.speak("Error connecting to Bluesky: " + str(e))
_exit_app()
# Set platform properties
self.max_chars = 300 # Bluesky character limit
self.default_visibility = 'public' # Bluesky only has public posts
# Initialize platform backend (pass raw profile, it converts internally)
self._platform = BlueskyAccount(self.app, index, self.api, raw_profile, self.confpath, self.prefs)
self._finish_init(index)
# Create built-in timelines in user's preferred order
self._create_builtin_timelines()
# Restore saved user timelines and searches (no lists for Bluesky)
# Avoid API calls during startup for speed
for ut_entry in list(self.prefs.user_timelines):
try:
# Handle both string and dict entries (dict has username and optional filter)
if isinstance(ut_entry, dict):
username = ut_entry.get('username', '')
user_filter = ut_entry.get('filter')
filter_labels = {
'posts_no_replies': 'Posts Only',
'posts_with_media': 'Media',
'posts_and_author_threads': 'Threads',
'posts_with_video': 'Videos',
'posts_no_boosts': 'No Boosts',
}
tl_name = username + "'s Timeline"
if user_filter and user_filter in filter_labels:
tl_name = f"{username}'s {filter_labels[user_filter]}"
self.timelines.append(timeline.timeline(self, name=tl_name, type="user", data=ut_entry, user=None, silent=True))
else:
username = ut_entry
self.timelines.append(timeline.timeline(self, name=username + "'s Timeline", type="user", data=username, user=None, silent=True))
except:
self.prefs.user_timelines.remove(ut_entry)
for q in list(self.prefs.search_timelines):
try:
self.timelines.append(timeline.timeline(self, name=q + " Search", type="search", data=q, silent=True))
except:
self.prefs.search_timelines.remove(q)
# Restore custom timelines (feeds for Bluesky)
for ct in list(self.prefs.custom_timelines):
try:
tl_type = ct.get('type', '')
tl_id = ct.get('id', '')
tl_name = ct.get('name', 'Feed')
if tl_type in ('feed', 'favourites', 'bookmarks'):
self.timelines.append(timeline.timeline(self, name=tl_name, type=tl_type, data=tl_id, silent=True))
except:
self.prefs.custom_timelines.remove(ct)
# No streaming for Bluesky
self.stream_listener = None
self.stream = None
self._stream_started = False
self._finish_timeline_init()
def _finish_init(self, index):
"""Common initialization after platform-specific setup."""
import wx
if self.app.currentAccount is None:
self.app.currentAccount = self
# Get display name - both platforms now use UniversalUser with acct
acct = getattr(self.me, 'acct', str(self.me))
# Use CallAfter for thread safety
wx.CallAfter(main.window.SetLabel, acct + " - " + application.name + " " + application.version)
def _finish_timeline_init(self):
"""Finish timeline initialization (common to all platforms)."""
import wx
if self.app.currentAccount == self:
# Use CallAfter for thread safety - must refresh timeline list before selecting
wx.CallAfter(main.window.refreshTimelines)
wx.CallAfter(main.window.list.SetSelection, 0)
wx.CallAfter(main.window.on_list_change, None)
# Track pending initial loads - streaming starts after all complete
self._pending_initial_loads = len([t for t in self.timelines if t.initial and not t.hide])
self._initial_loads_lock = threading.Lock()
threading.Thread(target=timeline.timelineThread, args=[self,], daemon=True).start()
def _on_timeline_initial_load_complete(self):
"""Called when a timeline finishes its initial load. Starts streaming when all are done."""
with self._initial_loads_lock:
self._pending_initial_loads -= 1
if self._pending_initial_loads <= 0 and self.app.prefs.streaming:
# All timelines loaded, start streaming
self.start_stream()
def get_timeline_by_type(self, timeline_type):
"""Find a timeline by its type (e.g., 'home', 'notifications', 'mentions').
Returns the first matching timeline, or None if not found.
"""
for tl in self.timelines:
if tl.type == timeline_type:
return tl
return None
def get_first_timeline(self):
"""Get the first timeline (fallback when current timeline is removed).
Returns the first timeline in the list, or None if no timelines exist.
"""
return self.timelines[0] if self.timelines else None
def _create_builtin_timelines(self):
"""Create built-in timelines in the user's preferred order.
Respects the timeline_order preference if set, otherwise uses default order.
"""
# Define available built-in timelines for each platform
if self.prefs.platform_type == "bluesky":
available = {
"home": ("Home", "home", None, None),
"notifications": ("Notifications", "notifications", None, None),
"mentions": ("Mentions", "mentions", None, None),
"sent": ("Sent", "user", self.me.acct, self.me),
}
default_order = ["home", "notifications", "mentions", "sent"]
else:
# Mastodon
available = {
"home": ("Home", "home", None, None),
"notifications": ("Notifications", "notifications", None, None),
"mentions": ("Mentions", "mentions", None, None),
"conversations": ("Conversations", "conversations", None, None),
"sent": ("Sent", "user", self.me.acct, self.me),
}
default_order = ["home", "notifications", "mentions", "conversations", "sent"]
# Use saved order if available, otherwise use default
order = self.prefs.timeline_order if self.prefs.timeline_order else default_order
# Ensure all available timelines are included (in case new ones were added)
for tl_key in default_order:
if tl_key not in order:
order.append(tl_key)
# Create timelines in the specified order
for tl_key in order:
if tl_key in available:
name, tl_type, data, user = available[tl_key]
timeline.add(self, name, tl_type, data, user)
def start_stream(self):
# Bluesky doesn't support streaming
if self.prefs.platform_type == "bluesky":
return
# Use lock to prevent race condition where multiple threads try to start stream
with self._stream_lock:
# Check if stream is already running or starting
if hasattr(self, '_stream_started') and self._stream_started:
return
if self.stream_thread is not None and self.stream_thread.is_alive():
return # Stream already running
# Mark as started before creating thread
self._stream_started = True
self.stream_thread = threading.Thread(
target=self._run_stream,
daemon=True
)
self.stream_thread.start()
def _run_stream(self):
# Bluesky doesn't support streaming
if self.prefs.platform_type == "bluesky":
return
import time
import threading
import requests
import json
thread_id = threading.current_thread().ident
consecutive_errors = 0
base_delay = 5 # seconds
max_delay = 300 # 5 minutes max
# Create listener once
self.stream_listener = streaming.MastodonStreamListener(self)
while True:
try:
# Ensure API is available before attempting stream
if not hasattr(self, 'api') or self.api is None:
time.sleep(5)
continue
# Check if we're still the active stream thread
if self.stream_thread is None or self.stream_thread.ident != thread_id:
return
# Use our own SSE implementation instead of Mastodon.py's buggy one
stream_url = f"{self.prefs.instance_url}/api/v1/streaming/user"
headers = {
"Authorization": f"Bearer {self.prefs.access_token}",
"Accept": "text/event-stream",
}
with requests.get(stream_url, headers=headers, stream=True, timeout=300) as response:
response.raise_for_status()
consecutive_errors = 0 # Reset on successful connect
event_type = None
data_lines = []
for line in response.iter_lines():
# Check if we should stop
if self.stream_thread is None or self.stream_thread.ident != thread_id:
return
if line:
line = line.decode('utf-8')
if line.startswith('event:'):
event_type = line[6:].strip()
elif line.startswith('data:'):
data_lines.append(line[5:].strip())
else:
# Empty line = end of event
if event_type and data_lines:
data_str = '\n'.join(data_lines)
try:
data = json.loads(data_str)
self._handle_stream_event(event_type, data)
except json.JSONDecodeError:
pass # Ignore malformed JSON
event_type = None
data_lines = []
except requests.exceptions.Timeout:
time.sleep(2)
continue
except Exception as e:
error_str = str(e).lower()
transient_errors = [
"connection", "timeout", "reset", "refused", "unreachable",
"network", "socket", "eof", "broken pipe", "ssl", "certificate"
]
if any(err in error_str for err in transient_errors):
time.sleep(2)
continue
consecutive_errors += 1
if consecutive_errors >= 5:
speak.speak("Stream connection lost")
consecutive_errors = 0
delay = min(base_delay * (2 ** (consecutive_errors - 1)), max_delay)
time.sleep(delay)
def _handle_stream_event(self, event_type, data):
"""Handle a streaming event by dispatching to the listener."""
# Guard: check listener exists
if self.stream_listener is None:
return
from mastodon import AttribAccessDict
def convert_to_attrib_dict(obj):
"""Recursively convert dicts to AttribAccessDict for attribute access."""
if isinstance(obj, dict):
converted = {k: convert_to_attrib_dict(v) for k, v in obj.items()}
return AttribAccessDict(**converted)
elif isinstance(obj, list):
return [convert_to_attrib_dict(item) for item in obj]
return obj
try:
if event_type == 'update':
status = convert_to_attrib_dict(data)
self.stream_listener.on_update(status)
elif event_type == 'notification':
notification = convert_to_attrib_dict(data)
self.stream_listener.on_notification(notification)
elif event_type == 'delete':
self.stream_listener.on_delete(data)
elif event_type == 'status.update':
status = convert_to_attrib_dict(data)
self.stream_listener.on_status_update(status)
elif event_type == 'conversation':
conversation = convert_to_attrib_dict(data)
self.stream_listener.on_conversation(conversation)
except Exception:
pass # Silently ignore stream handler errors
def followers(self, id):
# Use platform backend if available
if hasattr(self, '_platform') and self._platform:
return self._platform.get_followers(id, limit=80, max_pages=self.app.prefs.user_limit)
count = 0
followers = []
try:
page = self.api.account_followers(id=id, limit=80)
except MastodonError as err:
self.app.handle_error(err, "followers")
return []
followers.extend(page)
count += 1
while page and count < self.app.prefs.user_limit:
try:
page = self.api.fetch_next(page)
if page:
followers.extend(page)
count += 1
except MastodonError as err:
self.app.handle_error(err, "followers")
break
return followers
def following(self, id):
# Use platform backend if available
if hasattr(self, '_platform') and self._platform:
return self._platform.get_following(id, limit=80, max_pages=self.app.prefs.user_limit)
count = 0
following = []
try:
page = self.api.account_following(id=id, limit=80)
except MastodonError as err:
self.app.handle_error(err, "following")
return []
following.extend(page)
count += 1
while page and count < self.app.prefs.user_limit:
try:
page = self.api.fetch_next(page)
if page:
following.extend(page)
count += 1
except MastodonError as err:
self.app.handle_error(err, "following")
break
return following
def mutual_following(self):
followers = self.followers(self.me.id)
following = self.following(self.me.id)
users = []
follower_ids = {f.id for f in followers}
for i in following:
if i.id in follower_ids:
users.append(i)
return users
def not_following(self):
followers = self.followers(self.me.id)
following = self.following(self.me.id)
following_ids = {f.id for f in following}
users = []
for i in followers:
if i.id not in following_ids:
users.append(i)
return users
def not_following_me(self):
followers = self.followers(self.me.id)
following = self.following(self.me.id)
follower_ids = {f.id for f in followers}
users = []
for i in following:
if i.id not in follower_ids:
users.append(i)
return users
def havent_posted(self):
following = self.following(self.me.id)
users = []
for i in following:
if hasattr(i, "last_status_at") and i.last_status_at:
if i.last_status_at.year < datetime.datetime.now().year - 1:
users.append(i)
return users
def list_timelines(self, hidden=False):
tl = []
for i in self.timelines:
if i.hide == hidden:
tl.append(i)
return tl
def post(self, text, id=None, visibility=None, spoiler_text=None, **kwargs):
"""Post a new status or reply"""
try:
# Use platform backend if available
if hasattr(self, '_platform') and self._platform:
return self._platform.post(
text=text,
reply_to_id=id,
visibility=visibility,
spoiler_text=spoiler_text,
**kwargs
)
if visibility is None:
visibility = self.default_visibility
post_kwargs = {
'status': text,
'visibility': visibility
}
if spoiler_text:
post_kwargs['spoiler_text'] = spoiler_text
if id is not None:
post_kwargs['in_reply_to_id'] = id
# Merge any additional kwargs
post_kwargs.update(kwargs)
return self.api.status_post(**post_kwargs)
except Exception as e:
speak.speak(str(e))
return False
def boost(self, id):
"""Boost (reblog) a status"""
# Use platform backend if available
if hasattr(self, '_platform') and self._platform:
return self._platform.boost(id)
self.api.status_reblog(id=id)
def unboost(self, id):
"""Unboost (unreblog) a status"""
# Use platform backend if available
if hasattr(self, '_platform') and self._platform:
return self._platform.unboost(id)
self.api.status_unreblog(id=id)
def quote(self, status, text, visibility=None, language=None, content_type=None):
"""Quote a status - try native quote, fallback to URL"""
# Use platform backend if available
if hasattr(self, '_platform') and self._platform:
return self._platform.quote(
status, text,
visibility=visibility,
language=language,
content_type=content_type,
)
if visibility is None:
visibility = self.default_visibility
status_id = str(status.id)
result = None
quote_succeeded = False
# Method 1: Try native Mastodon 4.5+ quoting via direct API call
try:
params = {
'status': text,
'visibility': visibility,
'quoted_status_id': status_id
}
if language:
params['language'] = language
result = self.api._Mastodon__api_request('POST', '/api/v1/statuses', params)
# Verify the quote was actually attached
if result and ('quote' in result or 'quote_id' in result):
quote_succeeded = True
except:
pass
# Method 2: Try Mastodon.py's quote_id (for Fedibird/compatible servers)
if not quote_succeeded:
result = None
try:
result = self.api.status_post(status=text, quote_id=status_id, visibility=visibility, language=language)
if result and (hasattr(result, 'quote') and result.quote):
quote_succeeded = True
except:
pass
# Method 3: Fallback - include link to original post
if not quote_succeeded:
original_url = getattr(status, 'url', None)
if not original_url:
original_url = f"{self.prefs.instance_url}/@{status.account.acct}/{status_id}"
result = self.api.status_post(status=f"{text}\n\n{original_url}", visibility=visibility, language=language)
return result
def edit(self, status_id, text, visibility=None, spoiler_text=None, media_ids=None, language=None, **kwargs):
"""Edit an existing status"""
# Use platform backend if available
if hasattr(self, '_platform') and self._platform:
return self._platform.edit(
status_id=status_id,
text=text,
visibility=visibility,
spoiler_text=spoiler_text,
media_ids=media_ids,
language=language,
**kwargs
)
edit_kwargs = {
'id': status_id,
'status': text,
}
if spoiler_text:
edit_kwargs['spoiler_text'] = spoiler_text
if media_ids:
edit_kwargs['media_ids'] = media_ids
# Note: language is not supported by Mastodon's status_update API
return self.api.status_update(**edit_kwargs)
def favourite(self, id):
"""Favourite a status"""
# Use platform backend if available
if hasattr(self, '_platform') and self._platform:
return self._platform.favourite(id)
self.api.status_favourite(id=id)
def unfavourite(self, id):
"""Unfavourite a status"""
# Use platform backend if available
if hasattr(self, '_platform') and self._platform:
return self._platform.unfavourite(id)
self.api.status_unfavourite(id=id)
def follow(self, user_id):
"""Follow a user by ID or acct"""
if isinstance(user_id, str) and not user_id.isdigit():
# It's an acct/username, look up the user
user = self.app.lookup_user_name(self, user_id.lstrip('@'))
if user and user != -1:
user_id = user.id
else:
speak.speak("User not found")
return
# Use platform backend if available
if hasattr(self, '_platform') and self._platform:
return self._platform.follow(user_id)
self.api.account_follow(id=user_id)
def unfollow(self, user_id):
"""Unfollow a user by ID or acct"""
if isinstance(user_id, str) and not user_id.isdigit():
user = self.app.lookup_user_name(self, user_id.lstrip('@'))
if user and user != -1:
user_id = user.id
else:
speak.speak("User not found")
return
# Use platform backend if available
if hasattr(self, '_platform') and self._platform:
return self._platform.unfollow(user_id)
self.api.account_unfollow(id=user_id)
def block(self, user_id):
"""Block a user by ID or acct"""
if isinstance(user_id, str) and not user_id.isdigit():
user = self.app.lookup_user_name(self, user_id.lstrip('@'))
if user and user != -1:
user_id = user.id
else:
speak.speak("User not found")
return
# Use platform backend if available
if hasattr(self, '_platform') and self._platform:
return self._platform.block(user_id)
self.api.account_block(id=user_id)
def unblock(self, user_id):
"""Unblock a user by ID or acct"""
if isinstance(user_id, str) and not user_id.isdigit():
user = self.app.lookup_user_name(self, user_id.lstrip('@'))
if user and user != -1:
user_id = user.id
else:
speak.speak("User not found")
return
# Use platform backend if available
if hasattr(self, '_platform') and self._platform:
return self._platform.unblock(user_id)
self.api.account_unblock(id=user_id)
def mute(self, user_id):
"""Mute a user by ID or acct"""
if isinstance(user_id, str) and not user_id.isdigit():
user = self.app.lookup_user_name(self, user_id.lstrip('@'))
if user and user != -1: