<jupyter_start><jupyter_text><jupyter_code>import tweepy as tw
import datetime
import pandas as pd
print(tw.__version__)
class TweetMiner(object):
  result_limit = 20
  data = []
  api = False
  search_words = "#coronavirus"

  twitter_keys = {
      'consumer_key' : '<KEY>',
      'consumer_secret' : '<KEY>',
      'access_token' : '<KEY>',
      'access_token_secret' : '<KEY>'
  }

  def __init__(self, keys_dict=twitter_keys, api=api, result_limit=result_limit):
    self.twitter_keys = keys_dict
    
    auth = tw.OAuthHandler(keys_dict['consumer_key'], keys_dict['consumer_secret'])
    auth.set_access_token(keys_dict['access_token'], keys_dict['access_token_secret'])

    self.api = tw.API(auth, wait_on_rate_limit=True)
    self.twitter_keys = keys_dict
    self.result_limit = result_limit
    # self.api.update_status(status='Test')

  def mine_tweets(self, mine_retweets=True, tweet_count=1000):
    data = []
    last_tweet_id = True

    statuses = tw.Cursor(self.api.search,
                             q=self.search_words, 
                             count=self.result_limit,
                             tweet_mode='extended',
                             included_retweets=True,
                             lang="en").items(tweet_count)
    # mined = [[tweet.id, 
    #           tweet.user.name, 
    #           tweet.user.screen_name, 
    #           tweet.full_text,
    #           datetime.datetime.now(),
    #           tweet.created_at,
    #           tweet.favourite_count,
    #           tweet.entities['hashtags'],
    #           tweet.user.statuses_count,
    #           tweet.place,
    #           tweet.source,
    #           tweet.user.verified] for tweet in statuses]    

    for item in statuses:

      mined = {
        'tweet_id': item.id,
        'name': item.user.name,
        'screen_name': item.user.screen_name,
        'retweet_count': item.retweet_count,
        'text': item.full_text,
        'mined_at': datetime.datetime.now(),
        'created_at': item.created_at.strftime("%d-%b-%Y"),
        'favourite_count': item.favorite_count,
        'hashtags': item.entities['hashtags'],
        'urls' : item.entities['urls'],
        'user_mentions' : item.entities['user_mentions'],
        'status_count': item.user.statuses_count,
        'location': item.user.location,
        'source_device': item.source,
        'verfied' : item.user.verified,
        'source_url' : item.source_url
        }                     
      
      try:
        mined['retweet_text'] = item.retweeted_status.full_text
      except:
        mined['retweet_text'] = "None"
      try:
        mined['quote_text'] = item.quoted_status.full_text
        mined['quote_screen_name'] = status.quoted_status.user.screen_name
        # mined['urls'] = item.entities['urls']
        # mined['user_mentions'] = item.entities['user_mentions']
      except:
        mined['quote_text'] = 'None'
        mined['quote_screen_name'] = 'None'
        # mined['urls'] = "None"
        # mined['user_mentions'] = "None"

      data.append(mined)

    return data

miner = TweetMiner(result_limit=20)
mined_tweets = miner.mine_tweets(tweet_count=1800)
print(mined_tweets)
tweet_text = pd.DataFrame(mined_tweets)
tweet_text.to_csv("file_name.csv")
df = pd.read_csv("/content/file_name.csv")
df<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12891674127126232, 'fraction_numerical': 0.0035810205908683975, 'mean_word_length': 2.4030456852791877, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '314422', 'license': 'Apache-2.0', 'n_tokens_mistral': 1144, 'n_tokens_neox': 1121, 'n_words': 155, 'path': 'TweeetMiner.ipynb', 'repo_name': 'TheBoy-WhoCode/fake-news-detection', 'size': '3381'}