<jupyter_start><jupyter_text>If the previous cell ran without error and printed out a dict corresponding to a[Twitter User](https://developer.twitter.com/en/docs/tweets/data-dictionary/overview/user-object),then you're good. The authorized token pair is like a username/password and should be protected as such.<jupyter_code>twitter.get_application_rate_limit_status()['resources']['search']['/search/tweets']['remaining']
def get_remaining():
  return twitter.get_application_rate_limit_status()['resources']['search']['/search/tweets']['remaining']
import itertools
from time import sleep

SEARCH_QUOTE = 'Ukraine War Russia'
NUM_TWEETS_TO_FETCH = 5000
CURSOR_COUNT = 100
FIVETEEN_MINUTES = 900

fetched_tweets = []
dates = ['2022-02-11',
         '2022-02-12',
         '2022-02-13',
         '2022-02-14',
         '2022-02-15',
         '2022-02-16',
         '2022-02-17']

for date in range(len(dates)):
    cursor = twitter.cursor(twitter.search, q=SEARCH_QUOTE, count=CURSOR_COUNT, result_type='mixed', until=dates[date])
    remaining = get_remaining()
    while remaining <= CURSOR_COUNT:
      sleep(FIVETEEN_MINUTES)
      remaining = get_remaining()
    search_tweets = list(itertools.islice(cursor, NUM_TWEETS_TO_FETCH))
    fetched_tweets.append(search_tweets)

print(fetched_tweets)
from datetime import datetime
import pandas as pd

dict_ = {'user': [],
         'date': [],
         'text': [],
         'location': [],
         'retweeter_usr':[]}

for tweets in fetched_tweets:
    retweets = [tweet for tweet in tweets if 'retweeted_status' in tweet and tweet['user']['location']]
    for tweet in retweets:
        dict_['user'].append(tweet['user']['screen_name'])
        dict_['date'].append(tweet['created_at'])
        dict_['text'].append(tweet['text'])
        dict_['location'].append(tweet['user']['location'])
        retweeted_status = tweet['retweeted_status']
        dict_['retweeter_usr'] = retweeted_status['user']['screen_name']
# Structure data in a pandas DataFrame for easier manipulation
df = pd.DataFrame(dict_)
df.drop_duplicates(inplace=True)
df.to_csv('data.csv', sep=';', index=False)

df.head()
df['retweeter_usr'].unique()
import networkx as nx

D = nx.DiGraph()

for index, row in df.iterrows():
    retweeted_sn = (row['user'], row['location'])
    retweeter_sn = (row['retweeter_usr'], row['location'])
    # Edge direction: retweeted_sn -> retweeter_sn
    if D.has_edge(retweeter_sn, retweeted_sn):
        D.edges[retweeter_sn, retweeted_sn]['weight'] += 1
    else:
        D.add_edge(retweeter_sn, retweeted_sn, weight=1)
max(D.nodes, key=D.out_degree)
from operator import itemgetter

sorted(D.out_degree(), key=itemgetter(1), reverse=True)[:10]
sorted(D.out_degree(weight='weight'), key=itemgetter(1), reverse=True)[:5]
sorted(D.in_degree(weight='weight'), key=itemgetter(1), reverse=True)[:5]
nx.is_weakly_connected(D)
nx.number_weakly_connected_components(D)
nx.write_graphml(D,'russia_war_ukraine.graphml')
import matplotlib.pyplot as plt

FIGSIZE = (16, 16)

fig, ax = plt.subplots(1, 1, figsize=FIGSIZE)
nx.draw_networkx(D, ax=ax, with_labels=False)
plt.axis("off")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14848675914249684, 'fraction_numerical': 0.0255359394703657, 'mean_word_length': 4.919776119402985, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '337162', 'license': 'MIT', 'n_tokens_mistral': 1189, 'n_tokens_neox': 1117, 'n_words': 209, 'path': 'Twitter.ipynb', 'repo_name': 'Cogitus/Ucrania-war-twitter-analysis', 'size': '3172'}