<jupyter_start><jupyter_text>Cashtag CountsThe following script reveals cashtag mentions on Twitter over time. Outcome * Results folder that contains a .csv file for each cashtag* Charts folder that contains a graph for each cashtag<jupyter_code>import os
import sys
import yaml
import json
import requests
from requests_oauthlib import OAuth1
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.pylab as pylab
import matplotlib.dates as mdates
import seaborn as sns<jupyter_output><empty_output><jupyter_text>In the root directory, create a new file entitled `credentials.yaml` and populate the information (as exemplified in `credentials.yaml.template`). The credentials must be tied to Twitter's Search API (Full Archive) at enterprise access.<jupyter_code>with open(r'./credentials.yaml') as file:
    credentials = yaml.load(file, Loader=yaml.FullLoader)
    
    username = credentials['enterprise_full_archive_search_credentials']['username']
    password = credentials['enterprise_full_archive_search_credentials']['password']
    account_name = credentials['enterprise_full_archive_search_credentials']['account_name']
    env_label = credentials['enterprise_full_archive_search_credentials']['env_label']
endpoint = f'https://gnip-api.twitter.com/search/fullarchive/accounts/{account_name}/{env_label}/counts.json'<jupyter_output><empty_output><jupyter_text>The following cell is where you can specify the data you want to get back: * What stock you want to track (e.g. TWTTR) * What time period you'd like to pull data for (format: YYYYMMddHHmm)* What time bucket you want to use (options: 'day', 'hour', 'minute')<jupyter_code>stock = 'TWTR'
fromDate = '202001010000'
toDate = '202103040000'
bucket = 'hour'

# Only change parameters above this line 

body = {
    'query': f'${stock}', 
    'fromDate': f'{fromDate}',
    'toDate': f'{toDate}',
    'bucket': f'{bucket}'
}
response = requests.post(url=endpoint, auth=(username, password), json=body)
data = response.json()

timePeriods = []
counts = []
if 'error' in data:
    print(data)
else:
    totalCount = data['totalCount']
    print("Activity count:", totalCount)
    for result in data['results']:
        timePeriods.append(result['timePeriod'])
        counts.append(result['count'])
request_count = 1
while 'next' in data.keys():
    next_token = data['next']
    body.update(next=next_token)
    try: 
        response = requests.post(url=endpoint, auth=(username, password), json=body)
        data = response.json()
        print("Activity count:", data['totalCount'])
        totalCount += data['totalCount']
        for result in data['results']:
            timePeriods.append(result['timePeriod'])
            counts.append(result['count'])
    except requests.exceptions.RequestException as e:
        print(e)
    request_count += 1
    data = response.json()

print(f'Job complete: {request_count} request(s) in total')
print('Total activity count:', totalCount)
df = pd.DataFrame({'Time period':timePeriods, 'Count':counts})
df['Time period'] = pd.to_datetime(df['Time period'])
df.head()
df.sort_values(by='Time period')
df.to_csv(f'Results/{stock}_counts.csv')
df = df.sort_values(by='Time period')

params = {
    'axes.labelsize' : 16,
    'xtick.labelsize' : 12,
    'ytick.labelsize' : 12,
    'figure.figsize' : (20,10)
}
pylab.rcParams.update(params)

fig, ax = plt.subplots()
ax.plot(df['Time period'], df['Count'], lw=1, ls='-', c='#1DA1F2', marker='o', ms=0)
fig.autofmt_xdate()

# For smaller datasets - display labels on x axis by day
# ax.xaxis.set_major_formatter(mdates.DateFormatter('%d/%m/%y')) 
# ax.xaxis.set_major_locator(mdates.DayLocator(interval=5))

# For larger datasets - display labels on x axis by month
ax.xaxis.set_major_formatter(mdates.DateFormatter('%m/%y')) 
ax.xaxis.set_major_locator(mdates.MonthLocator(interval=1))

ax.set_xlabel("Time period")
ax.set_ylabel("Count")

plt.savefig(f'Charts/{stock}', dpi=300)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13175, 'fraction_numerical': 0.01175, 'mean_word_length': 5.164869029275809, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, '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': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '243414', 'license': 'Apache-2.0', 'n_tokens_mistral': 1324, 'n_tokens_neox': 1283, 'n_words': 320, 'path': 'Cashtag Counts.ipynb', 'repo_name': 'twitterdev/cashtag-counts', 'size': '4000'}