<jupyter_start><jupyter_text>Twitter API rate limits<jupyter_code>### checking rate limit - friends list
limit = api.rate_limit_status()
limit['resources']['friends']['/friends/list']['remaining']
limit['resources']['friends']['/friends/list']<jupyter_output><empty_output><jupyter_text>Calculating remaining time (in mins) before api limits resetI find it helpful to check how many more minutes I have to wait before trying again<jupyter_code>import datetime as dt
given_date =dt.datetime.fromtimestamp(
        int(limit['resources']['friends']['/friends/list']['reset'])
    ).strftime('%Y-%m-%d %H:%M:%S')
difference =  dt.datetime.strptime(given_date, "%Y-%m-%d %H:%M:%S")-dt.datetime.today()
min=difference.seconds/60
print ("Minute(s) remaining: ",min)<jupyter_output>Minute(s) remaining:  15.083333333333334<jupyter_text>Key function(s) of tweepy wrapperThese fields are are stored in Neo4j (as properties) against a Person node.<jupyter_code># getting user details of a Person aka <NAME>
# any other Person details can be fetched as a starting point 
suser = api.get_user('narendramodi') # this returns a User model
print (suser.screen_name)
print (suser.followers_count)
print (suser.friends_count)
print(suser.location)
print(suser.created_at)
# get <NAME>'s friends i.e. whome NM is following
friends = api.friends('narendramodi')
# defining a driver
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "welcome123"))
# initiating a server
session = driver.session()

#deleting ALL existing records
session.run("MATCH (n) DETACH DELETE n")

#adding a unique constraint; this ensures that same Person is not added twice
session.run("CREATE CONSTRAINT ON (a:Person) ASSERT a.screen_name IS UNIQUE")

session.close()
# defining a driver
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "welcome123"))
session = driver.session()

#adding narnendra modi details
label =suser.screen_name
session.run("CREATE (label:Person {screen_name: {screen_name}, name: {name}, followers_count: {followers_count}, friends_count:{friends_count},location:{location}})",
              {"name": suser.name, "screen_name": suser.screen_name, "followers_count":suser.followers_count,"friends_count": suser.friends_count,"location":suser.location }
               )
session.close()
# defining a driver
driver = GraphDatabase.driver("bolt://localhost:7687", auth=basic_auth("neo4j", "welcome123"))
session = driver.session()

i =0


#adding all persons - whom <NAME> is following
for user in friends:
    #checking if the user already exists
    output = session.run("MATCH (a:Person) WHERE a.screen_name ={check_name} return a.screen_name",
                             {"check_name":user.screen_name})
    exists =''
    for exists in output:
            True
    label =user.screen_name
    #adding the user if doesnt exist
    if exists=='':
        session.run("CREATE (label:Person {screen_name: {screen_name}, name: {name}, followers_count: {followers_count}, friends_count:{friends_count},location:{location}})",
              {"name": user.name, "screen_name": user.screen_name, "followers_count":user.followers_count,"friends_count": user.friends_count,"location":user.location }
               )
    else:
            print('Person already exists: ',user.screen_name)
            
    session.run("MATCH (a:Person),(b:Person) WHERE a.screen_name = {a_screen_name} AND b.screen_name = {b_screen_name} CREATE (a)-[r:FOLLOWING]->(b)",
               {"a_screen_name":suser.screen_name, "b_screen_name":user.screen_name })
    
    #temporary; ending the loop after 6 runs - dont exhaust twitter api limits
    i=i+1
    if i==6:
        break
    
    #finding friends of Modi's friends
    print ('sub_friends: '+ user.screen_name) #debugging why rate limits are getting exhausted
    sub_friends = api.friends(user.screen_name)
    #adding the friends of friends of Modi
    for sub_user in sub_friends:
        #checking if the sub_user already exists
        output = session.run("MATCH (a:Person) WHERE a.screen_name ={check_name} return a.screen_name",
                             {"check_name":sub_user.screen_name})
        exists =''
        for exists in output:
            True
        label =sub_user.screen_name
        #adding the user if doesnt exist
        if exists=='':
            
            session.run("CREATE (label:Person {screen_name: {screen_name}, name: {name}, followers_count: {followers_count}, friends_count:{friends_count},location:{location}})",
              {"name": sub_user.name, "screen_name": sub_user.screen_name, "followers_count":sub_user.followers_count,"friends_count": sub_user.friends_count,"location":sub_user.location }
               )
        else:
            print('Person already exists: ',sub_user.screen_name)
        session.run("MATCH (a:Person),(b:Person) WHERE a.screen_name = {a_screen_name} AND b.screen_name = {b_screen_name} CREATE (a)-[r:FOLLOWING]->(b)",
               {"a_screen_name":user.screen_name, "b_screen_name":sub_user.screen_name })
        

session.close()<jupyter_output>sub_friends: blind_cricket
sub_friends: antoniocostapm
Person already exists:  narendramodi
sub_friends: DuttYogi
Person already exists:  klrahul11
sub_friends: klrahul11
Person already exists:  anilkumble1074
Person already exists:  SDhawan25
Person already exists:  BCCI
Person already exists:  karun126
sub_friends: karun126
Person already exists:  narendramodi
Person already exists:  coach_rsridhar
Person already exists:  VVSLaxman281
Person already exists:  anilkumble1074
Person already exists:  cheteshwar1
{'fraction_non_alphanumeric': 0.11953041622198506, 'fraction_numerical': 0.012806830309498399, 'mean_word_length': 4.380861244019139, 'pattern_counts': {'":': 21, '<': 14, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, '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': '131000', 'license': 'MIT', 'n_tokens_mistral': 1815, 'n_tokens_neox': 1701, 'n_words': 436, 'path': 'Twitter analysis using Neo4j v2.ipynb', 'repo_name': 'saideepchandg/twitter-relationship-using-neo4j', 'size': '5643'}