fix json excepts and fix pagination
authorClaromes <claromes@hey.com>
Sat, 4 Nov 2023 16:05:37 +0000 (13:05 -0300)
committerClaromes <claromes@hey.com>
Sat, 4 Nov 2023 16:05:37 +0000 (13:05 -0300)
README.md
app.py

index 3d2432a30f3f1f9342e81a0a40adef2bb150b6aa..9851feb543c879749042870e3e48c0131fc27ed1 100644 (file)
--- a/README.md
+++ b/README.md
@@ -5,14 +5,12 @@
 
 Tool that displays, via [Wayback CDX Server API](https://github.com/internetarchive/wayback/tree/master/wayback-cdx-server), multiple archived tweets on Wayback Machine to avoid opening each link manually. The app is a prototype written in Python with Streamlit and hosted at Streamlit Cloud.
 
-*Thanks Tristan Lee for the idea.*
-
-## Features
-
 - Tweets per page defined by user
 - Filter by years
 - Filter by only deleted tweets
 
+*Thanks Tristan Lee for the idea.*
+
 ## Development
 
 ### Requirement
@@ -34,9 +32,9 @@ Streamlit will be served at http://localhost:8501
 ## Bugs
 
 - [ ] "web.archive.org took too long to respond."
-- [ ] Pagination: set session variable on first click
 - [ ] Timeout error
 - [x] `only_deleted` checkbox selected for handles without deleted tweets
+- [x] Pagination: set session variable on first click
 - [x] Pagination: scroll to top
 - [x] `IndexError`
 
@@ -59,7 +57,6 @@ Streamlit will be served at http://localhost:8501
 
 >"A tool to quickly view tweets saved on archive.org." — [Irina_Tech_Tips Newsletter #3](https://irinatechtips.substack.com/p/irina_tech_tips-newsletter-3-2023#%C2%A7wayback-tweets)
 
-
 ## Contributing
 
 PRs are welcome. Please, check the bug topic above, the [roadmap](docs/ROADMAP.md) or add a new feature.
diff --git a/app.py b/app.py
index 5062ed5156d91481139c87bd3f7749a04dd19adf..9fa91e104cbf8b6f4c089a34e55e296c6b377e78 100644 (file)
--- a/app.py
+++ b/app.py
@@ -50,9 +50,6 @@ hide_streamlit_style = '''
 
 st.markdown(hide_streamlit_style, unsafe_allow_html=True)
 
-if 'current_query' not in st.session_state:
-    st.session_state.current_query = ''
-
 if 'current_handle' not in st.session_state:
     st.session_state.current_handle = ''
 
@@ -229,12 +226,10 @@ only_deleted = st.checkbox('Only deleted tweets')
 
 query = st.button('Query', type='primary', use_container_width=True)
 
-if query or st.session_state.count:
-    if handle != st.session_state.current_handle:
-        st.session_state.offset = 0
+if handle != st.session_state.current_handle:
+    st.session_state.offset = 0
 
-    if query != st.session_state.current_query:
-        st.session_state.offset = 0
+if query or st.session_state.count:
 
     st.session_state.count = tweets_count(handle, st.session_state.saved_at)
 
@@ -258,7 +253,6 @@ if query or st.session_state.count:
             st.divider()
 
             st.session_state.current_handle = handle
-            st.session_state.current_query = query
 
             return_none_count = 0
 
@@ -275,30 +269,31 @@ if query or st.session_state.count:
                     st.error('Tweet has been deleted.')
                     try:
                         response_json = requests.get(link)
+
+                        if response_json.status_code == 200:
+                            json_data = response_json.json()
+
+                            if 'data' in json_data:
+                                if 'text' in json_data['data']:
+                                    json_text = json_data['data']['text']
+                                else:
+                                    json_text = json_data['data']
+                            else:
+                                if 'text' in json_data:
+                                    json_text = json_data['text']
+                                else:
+                                    json_text = json_data
+
+                            st.code(json_text)
+                            st.json(json_data, expanded=False)
+                        else:
+                            st.error(response_json.status_code)
                     except requests.exceptions.Timeout:
                         st.error('Connection to web.archive.org timed out.')
                     except requests.exceptions.ConnectionError:
                         st.error('Failed to establish a new connection with web.archive.org.')
                     except UnboundLocalError:
                         st.empty()
-                    if response_json.status_code == 200:
-                        json_data = response_json.json()
-
-                        if 'data' in json_data:
-                            if 'text' in json_data['data']:
-                                json_text = json_data['data']['text']
-                            else:
-                                json_text = json_data['data']
-                        else:
-                            if 'text' in json_data:
-                                json_text = json_data['text']
-                            else:
-                                json_text = json_data
-
-                        st.code(json_text)
-                        st.json(json_data, expanded=False)
-                    else:
-                        st.error(response_json.status_code)
 
                     st.divider()
                 if mimetype[i] == 'text/html':