SUSHMA KUMARI
SUSHMA KUMARI

Reputation: 95

Issue with the time delay for Twitter Api

I have a dataframe of twitter account, I am trying to call a photometer(https://github.com/IUNetSci/botometer-python) to fetch the details, below is the sample data and code

authors,count
generate_output,6
dismisstrump,6
luciano_ocasio,6
Jenny72166737,5
Hong18249170,5
anas_erindra,5
JayChance12,5
viralvm69,5
89nuncamais,5
ngaruman,4
sixyelcastaneda,4
Debisriprasad,4

import pandas as pd
import botometer
import nltk
from datetime import datetime
import tweepy
import time

author_details=[]
for i, row in df.iterrows():
   try:
      print(i)
      start = datetime.now()
      a_name='@'+row['authors']
      result = bom.check_account(a_name)
      a_details=(a_name, result)
      a_details=dict([(a_name, result)]) 
      author_details.append(a_details)
      now = datetime.now()
      d_time = (now - start).total_seconds()
      if int(d_time) < 900 and i<180:
        pass
      else:
        time.sleep(100)
   except Exception as e:
      print(e)
      continue

Issue: I am not to apply the pause after 15 minutes if the API calls more than 180 accounts and issue is that I am not able to store the result in a separate column. The output from a photometer is a dictionary.

Expected output

authors,count, result
generate_output,6, {}
dismisstrump,6, {}
luciano_ocasio,6, {}
Jenny72166737,5, {}
Hong18249170,5, {}
anas_erindra,5, {}
JayChance12,5, {}
viralvm69,5, {}
89nuncamais,5, {}
ngaruman,4, {}
sixyelcastaneda,4, {}
Debisriprasad,4, {}

Any suggestion to apply the time delay and store the result as separate column

Upvotes: 0

Views: 157

Answers (1)

Gautam Shahi
Gautam Shahi

Reputation: 455

You can try this code, it will catch the exception and wait for 600 seconds, you can store the result from botometer as a list. You can directly store the bot score with result['scores']['english']

err="429 Client Error: Too Many Requests for url: https://osome- 
botometer.p.rapidapi.com/2/check_account"
df['details']=''
df['bot_score']=''
for i, row in df.iterrows():
   try:
    start = datetime.now()
    a_name='@'+row['authors']
    result = bom.check_account(a_name)
    df.at[i,'det']=[result]
    df.at[i,'bot_score'] = int((result['scores']['english'])*100)
 except Exception as e:
    if e==err:
        time.sleep(600)
    continue

Upvotes: 1

Related Questions