Skip to content

Commit 64c38a7

Browse files
committed
combine tweets & toots
1 parent 062957e commit 64c38a7

3 files changed

Lines changed: 60 additions & 4 deletions

File tree

‎api/modules/toots.py‎

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,36 @@
1616
@cross_origin()
1717
@login_exempt
1818
def root():
19+
# Read the current tweets from the API or other source
20+
current_tweets = readTootsApiJson()
21+
22+
# Read additional tweets from file_tweets.json
23+
try:
24+
with open('data/file_tweets.json', 'r') as file:
25+
file_tweets_data = json.load(file)
26+
# Check if the data is a dictionary
27+
if isinstance(file_tweets_data, dict):
28+
# If the current tweets are also a dictionary, update it
29+
if isinstance(current_tweets, dict):
30+
current_tweets.update(file_tweets_data)
31+
# If the current tweets are a list, convert the dictionary to a list and extend it
32+
elif isinstance(current_tweets, list):
33+
current_tweets.extend(file_tweets_data.values())
34+
else:
35+
raise ValueError("The current tweets are neither a list nor a dictionary.")
36+
else:
37+
raise ValueError("The contents of file_tweets.json are not a dictionary")
38+
except FileNotFoundError:
39+
logging.error("The file file_tweets.json was not found.")
40+
except json.JSONDecodeError:
41+
logging.error("The file file_tweets.json does not contain valid JSON.")
42+
except ValueError as e:
43+
logging.error(e)
44+
45+
# Return the combined data as a JSON response
1946
return Response(json.dumps({
2047
'date': int(datetime.timestamp(datetime.now())),
21-
'tweets': readTootsApiJson()
48+
'tweets': current_tweets
2249
}), mimetype='application/json')
2350

2451

@@ -37,7 +64,35 @@ def forest_create():
3764
logging.warning('Manual invocation of creating forest!')
3865
forest = TootForest.fromFolder()
3966
forest.saveApiJson()
40-
return Response(json.dumps(readTootsApiJson()), mimetype='application/json')
67+
68+
# Read the current tweets from the API or other source
69+
current_tweets = readTootsApiJson()
70+
71+
# Read additional tweets from file_tweets.json
72+
try:
73+
with open('data/file_tweets.json', 'r') as file:
74+
file_tweets_data = json.load(file)
75+
# Check if the data is a dictionary
76+
if isinstance(file_tweets_data, dict):
77+
# If the current tweets are also a dictionary, update it
78+
if isinstance(current_tweets, dict):
79+
current_tweets.update(file_tweets_data)
80+
# If the current tweets are a list, convert the dictionary to a list and extend it
81+
elif isinstance(current_tweets, list):
82+
current_tweets.extend(file_tweets_data.values())
83+
else:
84+
raise ValueError("The current tweets are neither a list nor a dictionary.")
85+
else:
86+
raise ValueError("The contents of file_tweets.json are not a dictionary")
87+
except FileNotFoundError:
88+
logging.error("The file file_tweets.json was not found.")
89+
except json.JSONDecodeError:
90+
logging.error("The file file_tweets.json does not contain valid JSON.")
91+
except ValueError as e:
92+
logging.error(e)
93+
94+
# Return the combined data as a JSON response
95+
return Response(json.dumps(current_tweets), mimetype='application/json')
4196

4297
@bp.route('/add/<int:id>')
4398
def add(id):

‎lib/Toot.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ def asApiDict(self, add = {}):
5959
'display_name': self.getUserScreenName(),
6060
'avatar': self.getUserImageHttps(),
6161
'media': self.getMedia(),
62+
'source': 'mastodon.social',
6263
**add
6364
}
6465

‎runListener.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,12 @@ def stream_with_reconnection(self):
4949
while retry_count < max_retries:
5050
try:
5151
self.mastodon.stream_public(self)
52+
retry_count = 0
5253
break
5354
except Exception as e:
5455
self.receivedHeartbeat = False
5556
logging.warning("Error: ", e)
56-
logging.info("Trying to reconnect after " + str(retry_delay) + "seconds.")
57+
logging.info("Trying to reconnect after " + str(retry_delay) + " seconds.")
5758
retry_count += 1
5859
time.sleep(retry_delay)
5960

@@ -88,7 +89,6 @@ def handle_heartbeat(self):
8889
if not self.receivedHeartbeat:
8990
logging.info("Connected to server. Listening.")
9091
self.receivedHeartbeat = True
91-
retry_count = 0
9292

9393
def on_abort(self, status_code):
9494
logging.warning('Listener got error, status code: {}'.format(status_code))

0 commit comments

Comments
 (0)