Skip to content

Instantly share code, notes, and snippets.

@4xx
Forked from gdsaxton/twitter-user-data.py
Created October 18, 2015 14:27
Show Gist options
  • Save 4xx/140f7381e1ea69cb6c91 to your computer and use it in GitHub Desktop.
Save 4xx/140f7381e1ea69cb6c91 to your computer and use it in GitHub Desktop.
Twitter User Information
#!/usr/bin/env python
"""
Use Twitter API to grab user information from list of organizations; export text file
USES TWYTHON AND OAUTH
"""
import sys
import urllib
import string
import simplejson
import datetime
now = datetime.datetime.now()
day=int(now.day)
month=int(now.month)
year=int(now.year)
hour=int(now.hour)
from twython import Twython
t = Twython(app_key='APP_KEY', #REPLACE 'APP_KEY' WITH YOUR APP KEY, ETC., IN THE NEXT 4 LINES
app_secret='APP_SECRET',
oauth_token='OAUTH_TOKEN',
oauth_token_secret='OAUTH_TOKEN_SECRET')
#REPLACE WITH YOUR IDS
ids = "4816,9715012,13023422, 13393052, 14226882, 14235041, 14292458, 14335586, 14730894,\
15029174, 15474846, 15634728, 15689319, 15782399, 15946841, 16116519, 16148677, 16223542,\
16315120, 16566133, 16686673, 16801671, 41900627, 42645839, 42731742, 44157002, 44988185,\
48073289, 48827616, 49702654, 50310311, 50361094,"
##### NEW VERSION OF TWYTHON (6/26/2013) REQUIRES THE FOLLOWING:
users = t.lookup_user(user_id = ids)
outfn = "friend_followers_twython_%i.%i.%i.txt" % (now.month, now.day, now.year)
fields = "id screen_name name created_at url followers_count friends_count statuses_count \
favourites_count listed_count \
contributors_enabled description protected location lang expanded_url".split()
outfp = open(outfn, "w")
outfp.write(string.join(fields, "\t") + "\n") # header
for entry in users:
screen_name = entry['screen_name']
followers_count = entry['followers_count']
friends_count = entry['friends_count']
name = entry['name']
created_at = entry['created_at']
id = entry['id']
url = entry['url']
statuses_count = entry['statuses_count']
favourites_count = entry['favourites_count']
listed_count = entry['listed_count']
contributors_enabled = entry['contributors_enabled']
description = entry['description']
protected = entry['protected']
location = entry['location']
lang = entry['lang']
if 'url' in entry['entities']:
expanded_url = entry['entities']['url']['urls'][0]['expanded_url']
else:
expanded_url = ''
print contributors_enabled
print description
print protected
print location
print lang
print expanded_url
print "ON TO NEXT USER..............."
r = {}
for f in fields:
r[f] = ""
r['id'] = entry['id']
r['screen_name'] = entry['screen_name']
r['name'] = entry['name']
r['created_at'] = entry['created_at']
r['url'] = entry['url']
r['followers_count'] = entry['followers_count']
r['friends_count'] = entry['friends_count']
r['statuses_count'] = entry['statuses_count']
r['favourites_count'] = entry['favourites_count']
r['listed_count'] = entry['listed_count']
r['contributors_enabled'] = entry['contributors_enabled']
r['description'] = entry['description']
r['protected'] = entry['protected']
r['location'] = entry['location']
r['lang'] = entry['lang']
if 'url' in entry['entities']:
r['expanded_url'] = entry['entities']['url']['urls'][0]['expanded_url']
else:
r['expanded_url'] = ''
print r
lst = []
for f in fields:
lst.append(unicode(r[f]).replace("\/", "/"))
# PRINT lst
outfp.write(string.join(lst, "\t").encode("utf-8") + "\n")
outfp.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment