From 8b8055c409bb0b9573c386a5e8caffabf0f3f1a1 Mon Sep 17 00:00:00 2001 From: Claromes Date: Sat, 4 Nov 2023 13:05:37 -0300 Subject: [PATCH] fix json excepts and fix pagination --- README.md | 9 +++------ app.py | 49 ++++++++++++++++++++++--------------------------- 2 files changed, 25 insertions(+), 33 deletions(-) diff --git a/README.md b/README.md index 3d2432a..9851feb 100644 --- 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 5062ed5..9fa91e1 100644 --- 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': -- 2.34.1