Spaces:
Runtime error
Runtime error
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,65 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import twint
|
3 |
+
import pandas as pd
|
4 |
+
|
5 |
+
|
6 |
+
# add banner image
|
7 |
+
st.header("Data Scraper App")
|
8 |
+
st.image('images/twitter.jpg')
|
9 |
+
st.subheader('''
|
10 |
+
A simple app to scrap data from Twitter.
|
11 |
+
''')
|
12 |
+
|
13 |
+
# form to collect searcy query and other conditions
|
14 |
+
my_form = st.form(key='Twitter_form')
|
15 |
+
search_query = my_form.text_input('Input your search query')
|
16 |
+
data_limit = my_form.slider('How many tweets do you want to get?',
|
17 |
+
10,
|
18 |
+
3000,
|
19 |
+
value= 100,
|
20 |
+
step=10)
|
21 |
+
|
22 |
+
output_csv = my_form.radio('Save data to a CSV file?',
|
23 |
+
['Yes', 'No'])
|
24 |
+
file_name = my_form.text_input('Name the CSV file:')
|
25 |
+
submit = my_form.form_submit_button(label='Search')
|
26 |
+
|
27 |
+
# function to show output in pandas dataframe with specific folumns
|
28 |
+
def twint_to_pd(columns):
|
29 |
+
return twint.output.panda.Tweets_df[columns]
|
30 |
+
|
31 |
+
# configure twint to serach the query
|
32 |
+
if submit:
|
33 |
+
config = twint.Config()
|
34 |
+
config.Search = search_query
|
35 |
+
config.Limit = data_limit
|
36 |
+
config.Pandas = True
|
37 |
+
if output_csv == "Yes":
|
38 |
+
config.Store_csv = True
|
39 |
+
config.Output = 'data/{}.csv'.format(file_name)
|
40 |
+
twint.run.Search(config)
|
41 |
+
|
42 |
+
st.subheader("Results: Sample Data")
|
43 |
+
if output_csv == "Yes":
|
44 |
+
# show data in pandas dataframe
|
45 |
+
data = pd.read_csv('data/{}.csv'.format(file_name),usecols=['date','username','tweet'])
|
46 |
+
st.table(data)
|
47 |
+
else:
|
48 |
+
data = twint_to_pd(["date","username","tweet"])
|
49 |
+
st.table(data)
|
50 |
+
|
51 |
+
|
52 |
+
#download the dataframe
|
53 |
+
@st.cache
|
54 |
+
def convert_df(df):
|
55 |
+
# IMPORTANT: Cache the conversion to prevent computation on every rerun
|
56 |
+
return df.to_csv().encode('utf-8')
|
57 |
+
|
58 |
+
csv = convert_df(data)
|
59 |
+
|
60 |
+
st.download_button(
|
61 |
+
label="Download scrapped data as CSV",
|
62 |
+
data=csv,
|
63 |
+
file_name='{}.csv'.format(file_name),
|
64 |
+
mime='text/csv',
|
65 |
+
)
|