File size: 1,951 Bytes
e03530d
 
071cb1b
 
e03530d
f4db971
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
910b282
f4db971
86f79dd
f4db971
dc1bf57
86f79dd
dc2ae70
86f79dd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
071cb1b
 
360bf8a
 
 
 
071cb1b
360bf8a
 
 
86f79dd
 
 
071cb1b
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import streamlit as st
import pandas as pd
import twint 
import nest_asyncio

import multiprocessing.pool
import functools

# https://stackoverflow.com/questions/492519/timeout-on-a-function-call
def timeout(max_timeout):
    """Timeout decorator, parameter in seconds."""
    def timeout_decorator(item):
        """Wrap the original function."""
        @functools.wraps(item)
        def func_wrapper(*args, **kwargs):
            """Closure for function."""
            pool = multiprocessing.pool.ThreadPool(processes=1)
            async_result = pool.apply_async(item, args, kwargs)
            # raises a TimeoutError if execution exceeds max_timeout
            return async_result.get(max_timeout)
        return func_wrapper
    return timeout_decorator

# nest_asyncio.apply()

# Function to make this easy for ourselves:
@st.cache
@timeout(120.0) 
def get_tweets(username, limit=500, save_name=None):
  #nest_asyncio.apply() # Helps avoid RuntimeError: This event loop is already running

  # Setup config
  c = twint.Config() # Create a config object to store our settings
  c.Limit = limit # Max number of tweets to fetch (increments of 20)
  c.Username = username # User of interest
  c.Pandas = True # Store tweets in a dataframe
  c.Hide_output = True # Avoid printing out tweets

  # Run the seearch
  twint.run.Search(c)

  # Get the results and optionally save to a file as well
  df = twint.storage.panda.Tweets_df
  if save_name != None:
    df.to_csv(save_name)
  return df

st.title('Test')

with st.form("my_form"):
  st.write("Inside the form")
  user = st.text_input("Twitter Username")
  n_tweets = st.slider('How Many Tweets', 20, 2000, 20)

  # Every form must have a submit button.
  submitted = st.form_submit_button("Submit")
  if submitted:
    st.write("Fetching user", user, "n_tweets", n_tweets)
    tweets = get_tweets(user, limit=n_tweets)
    st.write(st.dataframe(tweets.head()))

st.write("Outside the form")