podsnigame commited on
Commit
b702048
·
1 Parent(s): 4594580
Files changed (2) hide show
  1. app.py +16 -7
  2. requirements.txt +2 -0
app.py CHANGED
@@ -2,7 +2,8 @@ import streamlit as st
2
  import snscrape.modules.twitter as sntwitter
3
  import pandas as pd
4
  import plotly.express as px
5
- import os
 
6
 
7
  st.set_page_config(page_title="Scraping Twitter")
8
 
@@ -69,9 +70,17 @@ if st.button('Scrape Tweets'):
69
  st.write(f'Tweets involving @{selected_username}:')
70
  st.write(tweets_df[tweets_df['Username'] == selected_username])
71
 
72
- # Save tweets as CSV file in data folder
73
- if not os.path.exists("data"):
74
- os.mkdir("data")
75
- file_name = f"data/tweets_{query.replace(' ', '_')}.csv"
76
- tweets_df.to_csv(file_name, index=False)
77
- st.write(f"Tweets saved as {file_name}")
 
 
 
 
 
 
 
 
 
2
  import snscrape.modules.twitter as sntwitter
3
  import pandas as pd
4
  import plotly.express as px
5
+ import base64
6
+
7
 
8
  st.set_page_config(page_title="Scraping Twitter")
9
 
 
70
  st.write(f'Tweets involving @{selected_username}:')
71
  st.write(tweets_df[tweets_df['Username'] == selected_username])
72
 
73
+ # Download CSV
74
+ if len(tweets_df) > 0:
75
+ csv = tweets_df.to_csv(index=False)
76
+ b64 = base64.b64encode(csv.encode()).decode()
77
+ filename = f'tweets_{query.replace(" ", "_")}.csv'
78
+ href = f'<a href="data:file/csv;base64,{b64}" download="{filename}">Download CSV</a>'
79
+ st.markdown(href, unsafe_allow_html=True)
80
+
81
+ if len(tweets_df) > 0:
82
+ txt = "\n\n".join(tweets_df['Text'].tolist())
83
+ b64 = base64.b64encode(txt.encode()).decode()
84
+ txt_filename = f"{query}_tweets.txt"
85
+ txt_href = f'<a href="data:file/txt;base64,{b64}" download="{txt_filename}">Download TXT</a>'
86
+ st.markdown(txt_href, unsafe_allow_html=True)
requirements.txt CHANGED
@@ -3,3 +3,5 @@ snscrape
3
  plotly
4
  seaborn
5
  pandas
 
 
 
3
  plotly
4
  seaborn
5
  pandas
6
+ base64
7
+