<jupyter_start><jupyter_text>Twitter API Data Pull Created by <NAME>, CS109 Final Project Unfortunately, Twitter data is only available for recent -- not historical -- data. Given this, we won't be able to easily find anything for our 2018 midterm election without paying through some third party site. This doesn't seem to be a possible feature for our social media model. Credit to <NAME> for setting up initial version of this python notebook to provide information on creating an API key for Twitter.<jupyter_code>from twython import Twython
import pandas as pd
keys_df = pd.read_csv("./../Keys/Twitter Keys.csv")
APP_KEY = keys_df['App Key'].iloc[0]
APP_SECRET = keys_df['App Secret'].iloc[0]
curl -u '3o9023S5Ws9GDsobaRqdUxuf5:BwEJbLJPAa8cyhwItiHBlDnQFI9nDBGieFe1zLVI745UCl487H' \
  --data 'grant_type=client_credentials' \
  'https://api.twitter.com/oauth2/token'
Bearer_token = APP_KEY + ":" + APP_SECRET
OAUTH_TOKEN = keys_df['Oauth Token'].iloc[0]
OAUTH_TOKEN_SECRET = keys_df['Oauth Token Secret'].iloc[0]
twitter = Twython(APP_KEY, APP_SECRET,
                  OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
twitter.get_application_rate_limit_status()['resources']['search']
#I want to make sure that I have all of my information correct with the API connection
twitter.verify_credentials()
query = {'q': 'liberal',
        'count': 150,
        'lang': 'en',
         'result_type': 'popular'
        }
import requests
import json

endpoint = "https://api.twitter.com/1.1/tweets/search/30day/dev.json" 

headers = {"Authorization":"Bearer MYREALLYLONGTOKENBASEDONKEYS", "Content-Type": "application/json"}  

data = '{"query":"(snow OR sleet OR hail OR (freezing rain)) has:images", "fromDate": "201802020000", "toDate": "201802240000"}'

response = requests.post(endpoint,data=data,headers=headers).json()

print(json.dumps(response, indent = 2))

import pandas as pd

# Search tweets
dict_ = {'user': [], 'date': [], 'text': [], 'favorite_count': []}
for status in twitter.search(**query)['statuses']:
    dict_['user'].append(status['user']['screen_name'])
    dict_['date'].append(status['created_at'])
    dict_['text'].append(status['text'])
    dict_['favorite_count'].append(status['favorite_count'])

# Structure data in a pandas DataFrame for easier manipulation
df = pd.DataFrame(dict_)
df.sort_values(by='favorite_count', inplace=True, ascending=False)
df

for x in df['text']:
    print(x)
from searchtweets import load_credentials

load_credentials(filename="./search_tweets_creds_example.yaml",
                 yaml_key="search_tweets_ent_example",
                 env_overwrite=False)<jupyter_output>cannot read file ./search_tweets_creds_example.yaml
Error parsing YAML file; searching for valid environment variables
Account type is not specified and cannot be inferred.
        Please check your credential file, arguments, or environment variables
        for issues. The account type must be 'premium' or 'enterprise'.
{'fraction_non_alphanumeric': 0.11934716082964977, 'fraction_numerical': 0.02074124447466848, 'mean_word_length': 4.991853360488799, 'pattern_counts': {'":': 6, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 2, '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': '30839', 'license': 'MIT', 'n_tokens_mistral': 988, 'n_tokens_neox': 926, 'n_words': 260, 'path': 'API/Twitter/Twitter API (Standard).ipynb', 'repo_name': 'jschneids13/CS109a-Group-81-Final-Project', 'size': '2954'}