Problems running 'botometer-python' script over multiple user accounts & saving to CSV

Question:

I’m new to python, having mostly used R, but I’m attempting to use the code below to run around 90 twitter accounts/handles (saved as a one-column csv file called ‘1’ in the code below) through the Botometer V4 API. The API github says that you can run through a sequence of accounts with ‘check_accounts_in’ without upgrading to the paid-for BotometerLite.

However, I’m stuck on how to loop through all the accounts/handles in the spreadsheet and then save the individual results to a new csv. Any help or suggestions much appreciated.

import botometer
import csv
import pandas as pd

rapidapi_key = "xxxxx"
twitter_app_auth = {
    'consumer_key': 'xxxxx',
    'consumer_secret': 'xxxxx',
    'access_token': 'xxxxx',
    'access_token_secret': 'xxxxx',
  }
bom = botometer.Botometer(wait_on_ratelimit=True,
                          rapidapi_key=rapidapi_key,
                          **twitter_app_auth)

#read in csv of account names with pandas
data = pd.read_csv("1.csv")

for screen_name, result in bom.check_accounts_in(data):

#add output to csv
    with open('output.csv', 'w') as csvfile:
        csvwriter = csv.writer(csvfile)
        csvwriter.writerow(['Account Name','Astroturf Score', 'Fake Follower Score']),
        csvwriter.writerow([
        result['user']['user_data']['screen_name'],
        result['display_scores']['universal']['astroturf'],
        result['display_scores']['universal']['fake_follower']
        ])
Asked By: gzpach

||

Answers:

Im not sure what the API returns, but you need to loop through your CSV data and send each item to the API. with the returned results you can append the CSV. You can loop through the csv without pandas, but it kept that in place because you are already using it.

added a dummy function to demonstrate the some returned data saved to a csv.

CSV I used:

names
name1
name2
name3
name4


import pandas as pd
import csv

def sample(x):
    return x + " Some new Data"   

df = pd.read_csv("1.csv", header=0)

output = open('NewCSV.csv', 'w+')

for name in df['names'].values:
    api_data = sample(name)
    csvfile = csv.writer(output)
    csvfile.writerow([api_data])

output.close()



to read the one column CSV directly without pandas. you may need to adjust based on your CSV


with open('1.csv', 'r') as csv:
    content = csv.readlines()

for name in content[1:]: # skips the header row - remove [1:] if the file doesn have one
    api_data = sample(name.replace('n', ""))

Making some assumptions about your API. This may work:

This assumes the API is returning a dictionary:

{"cap": 
    {
    "english": 0.8018818614025648,
    "universal": 0.5557322218336633
    }

import pandas as pd
import csv

df = pd.read_csv("1.csv", header=0)

output = open('NewCSV.csv', 'w+')
for name in df['names'].values:
    api_data = bom.check_accounts_in(name)
    csvfile = csv.writer(output)
    csvfile.writerow([api_data['cap']['english'],api_data['cap']['universal']])

output.close()

Answered By: KJDII

I’m also new to Python and desperately trying to get the cap score of my list of Twitter users (csv file), then save it to a new csv file. I’ve tried so many scripts but nothing seems to work. If someone could provide a working code I would be extremely grateful

Answered By: ranam1976
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.