Skip to content

Instantly share code, notes, and snippets.

@tromika
Created December 18, 2014 14:43
Show Gist options
  • Save tromika/98c42fa86b6936bfae0f to your computer and use it in GitHub Desktop.
Save tromika/98c42fa86b6936bfae0f to your computer and use it in GitHub Desktop.
Twitter connections
#install twitterapi
#pip install TwitterAPI
#Docs for twittterAPI
#https://github.com/geduldig/TwitterAPI
#Twitter credentials
#https://apps.twitter.com/
#Twitter API docs
#https://dev.twitter.com/rest/public
#Used endpoints in the script:
#https://dev.twitter.com/rest/reference/get/friends/list
#https://dev.twitter.com/rest/reference/get/followers/list
from TwitterAPI import TwitterAPI
import os
import sys
import time
consumer_key = ''
consumer_secret = ''
access_token_key = ''
access_token_secret = ''
#Bind the credentials to twitterapi
api = TwitterAPI(consumer_key, consumer_secret, access_token_key, access_token_secret)
#users included in the query
UserList = ['','','']
#write results to the following file
with open('', 'w') as f:
for TwitterUser in UserList:
print 'getting info for '+TwitterUser
#For first page result cursor is -1 or blank, last one is 0
cursor = -1
while cursor != 0:
#Check the endpoints for the possible query params, maximum count is 200
r = api.request('friends/list', {'screen_name':TwitterUser, 'count':200, 'cursor':cursor})
for item in r:
#Error handling
#https://dev.twitter.com/overview/api/response-codes
if 'users' not in item.keys() and 'message' in item.keys() and 'code' in item.keys():
if item['code'] != 88:
print 'Unhandled error' + item['message']
sys.exit()
print 'due '+item['message']+' the script sleeps 5min'
time.sleep(300)
else:
if (item != None and 'users' in item.keys() ):
for user in item['users']:
#get queried user + he/she following + name + loc +following number of people + her/his followers count + screen name
string = TwitterUser +'|following|'+user['name']+'|'+ user['location']+'|'+str(user['followers_count'])+'|'+str(user['friends_count'])+'|'+user['screen_name']
#write row
f.write(string.encode('utf8', 'replace')+os.linesep)
#print string.encode('utf8', 'replace')
#set the cursor for the next page, aka pagination
cursor = item['next_cursor']
cursor = -1
while cursor != 0:
r = api.request('followers/list', {'screen_name':TwitterUser, 'count':200, 'cursor':cursor})
for item in r:
#Error handling
#https://dev.twitter.com/overview/api/response-codes
if 'users' not in item.keys() and 'message' in item.keys() and 'code' in item.keys():
if item['code'] != 88:
print 'Unhandled error ' + item['message']
sys.exit()
print 'due '+item['message']+' the script sleeps 5min'
time.sleep(300)
else:
if (item != None and 'users' in item.keys() ):
for user in item['users']:
#get queried user + he/she followers + name + loc +following number of people + her/his followers count + screen name
string = TwitterUser +'|follower|'+user['name']+'|'+ user['location']+'|'+str(user['followers_count'])+'|'+str(user['friends_count'])+'|'+user['screen_name']
#wirte row
f.write(string.encode('utf8', 'replace')+os.linesep)
#set the cursor for the next page, aka pagination
cursor = item['next_cursor']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment