1616@cross_origin ()
1717@login_exempt
1818def 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>' )
4398def add (id ):
0 commit comments