Created
January 3, 2015 18:11
-
-
Save james-see/4b47837e5b68124e6876 to your computer and use it in GitHub Desktop.
Python Command Line Interface Twitter Reporter tool
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#python twitter example | |
#last updated December 24 2014 | |
#James Campbell http://www.jamescampbell.us | |
# @jamescampbell on twitter | |
# | |
# | |
# twitter items available: | |
#contributors, truncated, text, in_reply_to_status_id, id, favorite_count, source, retweeted, | |
#coordinates, entities, in_reply_to_screen_name, in_reply_to_user_id, retweet_count, id_str, favorited, | |
#retweeted_status, user, geo, in_reply_to_user_id_str, lang, created_at, in_reply_to_status_id_str, place, metadata | |
# | |
# all user related data available: | |
#{u'follow_request_sent': False, u'profile_use_background_image': True, u'profile_text_color': u'333333', | |
#u'default_profile_image': False, u'id': 571096081, | |
#u'profile_background_image_url_https': u'https://pbs.twimg.com/profile_background_images/594858416/gbmhsyzd9flv2fbvpfk0.jpeg', | |
#u'verified': False, u'profile_location': None, u'profile_image_url_https': u'https://pbs.twimg.com/profile_images/378800000076640246/a3f859682860dbefd2e4b21d357cc304_normal.jpeg', | |
#u'profile_sidebar_fill_color': u'DDEEF6', u'entities': {u'description': {u'urls': []}}, u'followers_count': 216, | |
#u'profile_sidebar_border_color': u'C0DEED', u'id_str': u'571096081', u'profile_background_color': u'C0DEED', u'listed_count': 1, | |
#u'is_translation_enabled': False, u'utc_offset': -18000, u'statuses_count': 1566, | |
#u'description': u'Only talking about the important stuff. Stoolie', u'friends_count': 277, u'location': u'', | |
#u'profile_link_color': u'0084B4', u'profile_image_url': u'http://pbs.twimg.com/profile_images/378800000076640246/a3f859682860dbefd2e4b21d357cc304_normal.jpeg', | |
#u'following': False, u'geo_enabled': True, u'profile_banner_url': u'https://pbs.twimg.com/profile_banners/571096081/1407226364', | |
#u'profile_background_image_url': u'http://pbs.twimg.com/profile_background_images/594858416/gbmhsyzd9flv2fbvpfk0.jpeg', | |
#u'name': u'Devin Brosnan', u'lang': u'en', u'profile_background_tile': True, u'favourites_count': 1412, | |
#u'screen_name': u'DevinMBrosnan', u'notifications': False, u'url': None, u'created_at': u'Fri May 04 19:36:11 +0000 2012', | |
#u'contributors_enabled': False, u'time_zone': u'Eastern Time (US & Canada)', u'protected': False, u'default_profile': False, u'is_translator': False} | |
from TwitterAPI import TwitterAPI | |
import sys | |
import csv | |
import codecs | |
from twitter import * # package used to retrieve all tweets for a user (easier implementation than TwitterAPI) | |
from twitterkeys import * # twitter keys files for OAUTH for TwitterAPI package and twitter package | |
t = Twitter(auth=OAuth(a_key, a_secret, c_key, c_secret)) | |
# set colors in the command line output | |
class color: | |
PURPLE = '\033[95m' | |
CYAN = '\033[96m' | |
DARKCYAN = '\033[36m' | |
BLUE = '\033[94m' | |
GREEN = '\033[92m' | |
YELLOW = '\033[93m' | |
RED = '\033[91m' | |
BOLD = '\033[1m' | |
UNDERLINE = '\033[4m' | |
END = '\033[0m' | |
# If you are behind a firewall you may need to provide proxy server | |
# authentication. | |
proxy_url = None # Example: 'https://USERNAME:PASSWORD@PROXYSERVER:PORT' | |
#global defaults | |
tsearch = 'Hello' | |
fquery = '' | |
uname = 'jamescampbell' | |
# functions ---------- | |
# this function asks the user what type of twitter query they want to run | |
def firstquestion(): | |
fquery = raw_input('What would you like to do: (press number and return key) \n 1: User report \n 2: Twitter search \n 3: List of Following for user \n 4: All tweets for a user \n ?: ') | |
return fquery | |
# this function fixes unicode/ascii issues VERY IMPORTANT | |
def normalize(s): | |
if type(s) == unicode: | |
return s.encode('utf8', 'ignore') | |
else: | |
return str(s) | |
# this function gets the search term from an input | |
def get_search_term(): | |
tsearch = raw_input('Enter a search term: ') | |
return tsearch | |
# this funcion gets the username for the report | |
def get_username(): | |
uname = raw_input('Enter username (no @ sign / leave blank to exit) \n ?: ') | |
return uname | |
# this function spits out the user ids for any twitter user that the user is following (twitter calls it friends) | |
def get_user_friends(): | |
uname = get_username() | |
r = api.request('friends/ids', {'screen_name': uname}) | |
i = 0 | |
resultfilename = uname + '_friends.csv' | |
resultfile = open(resultfilename,'wb') | |
wr = csv.writer(resultfile, delimiter=',',lineterminator='\n') | |
for item in r: | |
for id in item['ids']: | |
print (id) | |
wr.writerow(id) | |
i = i + 1 | |
print('\nQUOTA: %s' % r.get_rest_quota()['remaining'] + ' remaining') | |
exit('printed all %s ids' % i) | |
# this function gets all available user data for specific user and prints it as a report and CSV | |
def get_user_report(u): | |
r = api.request('users/show', {'screen_name': u}) | |
for item in r: | |
resultfilename = u + '.csv' | |
resultfile = open(resultfilename,'wb') | |
wr = csv.writer(resultfile, delimiter=',',lineterminator='\n') | |
#print (item) # prints all raw array data for user | |
followers = item['followers_count'] | |
location = item['profile_location'] | |
website = item['url'] | |
description = normalize(item['description']) # to normalize in case of encoding issues | |
created = item['created_at'] | |
geod = item['geo_enabled'] | |
realname = normalize(item['name']) | |
timezone = item['time_zone'] | |
defaultprofile = item['default_profile'] | |
defaultimage = item['default_profile_image'] | |
protected = item['protected'] | |
lang = item['lang'] | |
backgroundimage = item['profile_use_background_image'] | |
listed = item['listed_count'] | |
favorites = item['favourites_count'] | |
statuses = item['statuses_count'] | |
friends = item['friends_count'] | |
print (color.CYAN + '\n TWITTER REPORT FOR @%s \n' % u + color.END) | |
print ('Real Name: %s' % realname) | |
wr.writerow(['Real Name: %s' % realname]) | |
print ('Geo turned on? %s' % geod) | |
wr.writerow(['Geo turned on? %s' % geod]) | |
print ('Protected profile? %s' % protected) | |
wr.writerow(['Protected profile? %s' % protected]) | |
print ('Language in profile: %s' % lang) | |
wr.writerow(['Language in profile: %s' % lang]) | |
print ('Followers: %d' % followers) | |
wr.writerow(['Followers: %d' % followers]) | |
print ('Following: %s' % friends) | |
wr.writerow(['Following: %s' % friends]) | |
print ('Total tweets: %s' % statuses) | |
wr.writerow(['Total tweets: %s' % statuses]) | |
print ('Total Favs: %s' % favorites) | |
wr.writerow(['Total Favs: %s' % favorites]) | |
print ('Total lists: %s' % listed) | |
wr.writerow(['Total lists: %s' % listed]) | |
print ('Location: %s' % location) | |
wr.writerow(['Location: %s' % location]) | |
print ('Website: %s' % website) | |
wr.writerow(['Website: %s' % website]) | |
print ('Description: %s' % description) | |
wr.writerow(['Description: %s' % description]) | |
print ('Created: %s' % created) | |
wr.writerow(['Created: %s' % created]) | |
print ('Timezone: %s' % timezone) | |
wr.writerow(['Timezone: %s' % timezone]) | |
print ('Default Profile? %s' % defaultprofile) | |
wr.writerow(['Default Profile? %s' % defaultprofile]) | |
print ('Default Image? %s' % defaultimage) | |
wr.writerow(['Default Image? %s' % defaultimage]) | |
print ('Has Background Image set? %s' % backgroundimage) | |
wr.writerow(['Has Background Image set? %s' % backgroundimage]) | |
print (color.CYAN + '\n report saved as csv for %s \n exiting now... \n' % u + color.END) | |
exit() | |
def iterated(maxid,u): | |
resultfilename = u + '_tweets.csv' | |
resultfile = open(resultfilename,'a') | |
wr = csv.writer(resultfile, delimiter=',',lineterminator='\n') | |
rr = t.statuses.user_timeline(screen_name=u,count="200",max_id=maxid) | |
for item in rr: | |
tweet = normalize(item['text']) | |
dated = normalize(item['created_at']) | |
tweetid = item['id'] | |
wr.writerow([tweet,dated,tweetid]) | |
maxid = tweetid | |
print tweet | |
return maxid | |
# this function is for option 4, get a user's tweets | |
def get_user_tweets(): | |
uname = get_username() | |
countset = '200' | |
maxid = '' | |
rr = t.statuses.user_timeline(screen_name=uname,count=countset) | |
#print [s.text for s in statuses] | |
#r = api.request('statuses/user_timeline', {'screen_name': uname}, {'count': countset} ) | |
i = 0 | |
resultfilename = uname + '_tweets.csv' | |
resultfile = open(resultfilename,'wb') | |
wr = csv.writer(resultfile, delimiter=',',lineterminator='\n') | |
tweetid = 0 | |
for item in rr: | |
tweet = normalize(item['text']) | |
dated = normalize(item['created_at']) | |
tweetid = item['id'] | |
wr.writerow([tweet,dated,tweetid]) | |
maxid = tweetid | |
#print tweet | |
print maxid | |
while i < 17: | |
maxid = iterated(maxid, uname) | |
i = i + 1 | |
print('\nQUOTA: %s' % r.get_rest_quota()['remaining'] + ' remaining') | |
exit('only returned 200 max, will be working fully soon') | |
# this function returns tweets for a specific search term | |
def perform_search(searchterm): | |
resultfile = open("tweetoutput.csv",'wb') | |
wr = csv.writer(resultfile, delimiter=',',lineterminator='\n') | |
#stringed = (words.text_content()).encode('utf-8') | |
r = api.request('search/tweets', {'q': searchterm}) | |
for item in r: | |
tweet = item['text'] | |
user = item['user']['geo_enabled'] | |
tweet = normalize(tweet) | |
#print ', '.join(tweet) | |
#exit() | |
print (tweet, user) | |
wr.writerow([tweet]) | |
#sys.stdout.write('') | |
print('\nQUOTA: %s' % r.get_rest_quota()) | |
exit() | |
# Using OAuth 1.0 to authenticate you have access all Twitter endpoints. | |
# Using OAuth 2.0 to authenticate you lose access to user specific endpoints (ex. statuses/update), | |
# but you get higher rate limits. | |
api = TwitterAPI( | |
CONSUMER_KEY, | |
CONSUMER_SECRET, | |
ACCESS_TOKEN_KEY, | |
ACCESS_TOKEN_SECRET, | |
auth_type='oAuth1', | |
proxy_url=proxy_url) | |
#api = TwitterAPI(CONSUMER_KEY, CONSUMER_SECRET, auth_type='oAuth2', proxy_url=proxy_url) | |
r = api.request('application/rate_limit_status') | |
# Print HTTP status code (=200 when no errors). | |
tstatus = r.status_code | |
print ('current status %s' % tstatus) | |
#main start of code | |
if tstatus == 200: | |
fquery = firstquestion() | |
if fquery == '1': | |
print (color.RED + '\nYou selected User Report' + color.END) | |
uname = get_username() | |
if uname != '': | |
get_user_report(uname) | |
else: | |
print (color.BOLD + 'username blank, exiting now dingus... \n' + color.END) | |
elif fquery == '2': | |
tsearch = get_search_term() | |
perform_search(tsearch) | |
elif fquery == '3': | |
print (color.RED + '\nYou selected Get User\'s Friends' + color.END) | |
get_user_friends() | |
elif fquery == '4': | |
print (color.RED + '\nYou selected Get User\'s Tweets' + color.END) | |
get_user_tweets() | |
elif tstatus != 200: | |
exit('hit API limit or other error, sorry') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Just simply download and run "python TWEEPORTER.py" in your terminal / command line and it will ask you what you want to do next. It will automatically save user reports and all of a user's tweets to CSV files based on the options you choose. Have fun!