Skip to content

Instantly share code, notes, and snippets.

@macoj
Last active October 10, 2017 23:43
Show Gist options
  • Save macoj/0809afb719c2f807307d to your computer and use it in GitHub Desktop.
Save macoj/0809afb719c2f807307d to your computer and use it in GitHub Desktop.
Just a hello to Twitter API
"""
this code is here: http://tinyurl.com/hello-network
Twitter libraries:
https://dev.twitter.com/overview/api/twitter-libraries
Twitter Python library:
https://github.com/sixohsix/twitter
$ sudo pip2.7 install twitter
igraph library: http://igraph.org/python/
$ sudo pip2.7 install igraph
Some links:
http://www.learnpython.org/
"""
import igraph
import time
from twitter import *
# Create an app (https://apps.twitter.com/) using your Twitter account
consumer_key = ''
consumer_secret = ''
access_token_key = ''
access_token_secret = ''
#### SEARCH API
api = Twitter(auth=OAuth(access_token_key, access_token_secret, consumer_key, consumer_secret))
status = api.statuses.update(status="In Python we trust")
tweets_search = api.search.tweets(q="floridatech", count=100) # https://dev.twitter.com/rest/reference/get/search/tweets
for tweet in tweets_search['statuses']: # https://dev.twitter.com/docs/platform-objects/tweets
print tweet['user']['screen_name'] + " said '" + tweet['text']
# The location is preferentially taking from the Geotagging API, but will fall back to their Twitter profile.
# The parameter value is specified by "latitude,longitude,radius", where radius units must be specified as
# either "mi" (miles) or "km" (kilometers).
# You can use GeoPy to get coordinates: https://geopy.readthedocs.org
# or you can get coordinates manually here: http://nominatim.openstreetmap.org/search.php
tweets_search = api.search.tweets(q='Trump', geo="28.0655722,-80.6247762,1mi", count=100)
for tweet in tweets_search['statuses']:
print tweet['user']['screen_name'] + " said '" + tweet['text'] + "'"
from geopy.geocoders import Nominatim
geolocator = Nominatim()
location = geolocator.geocode("Florida Institute of Technology")
print (location.latitude, location.longitude)
#### STREAM API
api_stream = TwitterStream(auth=OAuth(access_token_key, access_token_secret, consumer_key, consumer_secret))
for tweet in api_stream.statuses.sample(): # https://dev.twitter.com/streaming/reference/get/statuses/sample
if 'user' in tweet and 'text' in tweet:
print tweet['user']['screen_name'] + ' said "' + tweet['text'] + '"'
time.sleep(1)
for tweet in api_stream.statuses.filter(track="floridatech,complex networks"):
if 'user' in tweet and 'text' in tweet:
print tweet['user']['screen_name'] + ' said "' + tweet['text'] + '"'
break
# https://dev.twitter.com/streaming/overview/request-parameters#locations
# A comma-separated list of longitude,latitude pairs specifying a set of bounding boxes to filter Tweets by. Only
# geolocated Tweets falling within the requested bounding boxes will be included.
# > Get bound locations here: http://boundingbox.klokantech.com/
for tweet in api_stream.statuses.filter(locations="-81.0595,27.7319,-80.2834,28.4518"):
if 'user' in tweet and 'text' in tweet:
print tweet['user']['screen_name'] + ' said "' + tweet['text'] + '"'
break
# An attempt to create a network of friends using REST API.
screen_name_ex = 'ronaldomenezes'
friends_ids = api.friends.ids(screen_name=screen_name_ex, count=5)
followers_ids = api.followers.ids(screen_name=screen_name_ex, count=5) # maximum count is 200!
friends = api.friends.list(screen_name=screen_name_ex, count=5)
followers = api.followers.list(screen_name=screen_name_ex, count=5) # maximum count is 200!
for friend in friends['users']:
print friend['screen_name'] + ' is a friend of ' + screen_name_ex
for follower in followers['users']:
print follower['screen_name'] + ' follows ' + screen_name_ex
## CREATING a network
# getting the followers that follows
followers_followers = {}
for follower in followers['users']:
follower_screen_name = follower['screen_name']
print "Getting followers of '"+follower_screen_name+"'"
while True:
try:
followers_followers[follower_screen_name] = api.followers.list(screen_name=follower_screen_name)
break
except TwitterError as e:
if 'errors' in e.response_data:
if e.response_data['errors'][0]['code'] == 88:
print e.response_data['errors'][0]['message']
time.sleep(60)
else:
break
followers_friends = {}
for follower in followers['users']:
follower_screen_name = follower['screen_name']
print "Getting friends of '"+follower_screen_name+"'"
while True:
try:
followers_friends[follower_screen_name] = api.friends.list(user_id=follower_screen_name)
break
except TwitterError as e:
if 'errors' in e.response_data:
if e.response_data['errors'][0]['code'] == 88:
print e.response_data['errors'][0]['message']
time.sleep(60)
else:
break
for follower in followers_friends:
for follower_friend in followers_friends[follower]['users']:
print follower + " is friend of " + follower_friend['screen_name']
for follower_follower in followers_followers[follower]['users']:
print follower_follower['screen_name'] + " is friend of " + follower
# just an example to visualize and to analyze this data
# Remember 2-step process:
# 1- Collect data
# 2- Construct the network
# Intensive traffic/volume, try MongoDB, CSV, etc, OTHERWISE YOU CAN LOSE DATA! :(
# creating a list with all users:
users_list = []
for follower in followers_friends:
users_list.append(follower)
if follower in followers_friends:
for follower_friend in followers_friends[follower]['users']:
users_list.append(follower_friend['screen_name'])
if follower in followers_followers:
for follower_follower in followers_followers[follower]['users']:
users_list.append(follower_follower['screen_name'])
for friend in friends['users']:
users_list.append(friend['screen_name'])
users_list = list(set(users_list)) # we need a list with unique elements
# create a graph
graph = igraph.Graph(directed=True)
# let's add the vertices
for user in users_list:
graph.add_vertex(user)
# let's connect the vertices:
for friend in friends['users']:
graph.add_edge(friend['screen_name'], screen_name_ex)
for follower in followers['users']:
graph.add_edge(screen_name_ex, follower['screen_name'])
for follower in followers_friends:
if follower in followers_friends:
for follower_friend in followers_friends[follower]['users']:
graph.add_edge(follower, follower_friend['screen_name'])
if follower in followers_followers:
for follower_follower in followers_followers[follower]['users']:
graph.add_edge(follower_follower['screen_name'], follower)
# now we have a graph and we can analyze it...
closeness_measured = graph.closeness()
degrees = graph.degree()
# etc...
### JUST A (REALLY) GLIMPSE of numpy
import numpy as np
degrees = graph.degree()
np.mean(degrees)
### matplotlib
import matplotlib.pyplot as plt
plt.hist(degrees)
plt.show()
# EXPORTING!
graph.write_graphml("/home/marcos/twitter_.graphml")
screen_name_ex = 'BarackObama'
followers = api.followers.ids(screen_name=screen_name_ex)
followers_ids = []
while followers['next_cursor'] != 0:
print followers['ids']
followers_ids += followers['ids']
followers = api.followers.ids(screen_name=screen_name_ex, cursor=followers['next_cursor'])
output_file = open("/Users/moliveira/PycharmProjects/hellopy/network_one", 'w')
for follower in followers:
output_file.write(screen_name_ex+','+follower.screen_name+'\n')
for friend in friends:
output_file.write(friend.screen_name+','+screen_name_ex+'\n')
output_file.close()
input_file = open("/Users/moliveira/PycharmProjects/hellopy/network_one", 'r')
for edge in input_file:
i, j = edge.strip().split(",")
input_file.close()
# igraph only likes node with id
# let's just export in a plain text file
output_file = open("/User/PycharmProjects/hellopy/network", 'w')
for follower in followers:
output_file.write(screen_name_ex+','+follower.screen_name)
for follower_follower in followers_followers[follower.screen_name]:
output_file.write(follower_follower.screen_name+','+follower.screen_name+'\n')
for follower_friend in followers_friends[follower.screen_name]:
output_file.write(follower_friend.screen_name+","+follower_friend.screen_name+'\n')
for friend in friends:
output_file.write(friend+','+screen_name_ex)
@jefferyansah
Copy link

This is awesome

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment