Skip to content

Instantly share code, notes, and snippets.

@nvbn
Last active December 5, 2017 07:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save nvbn/617dd73d7b4bf35877cf5c235a8861c8 to your computer and use it in GitHub Desktop.
Save nvbn/617dd73d7b4bf35877cf5c235a8861c8 to your computer and use it in GitHub Desktop.
flights graph
from collections import Counter
import tweepy
import networkx
from matplotlib import cm, pyplot
TWITTER_CONSUMER_KEY = ''
TWITTER_CONSUMER_SECRET = ''
TWITTER_ACCESS_TOKEN = ''
TWITTER_ACCESS_TOKEN_SECRET = ''
USER_ID = ''
MARKER = '✈'
def get_tweets():
auth = tweepy.OAuthHandler(TWITTER_CONSUMER_KEY, TWITTER_CONSUMER_SECRET)
auth.set_access_token(TWITTER_ACCESS_TOKEN, TWITTER_ACCESS_TOKEN_SECRET)
api = tweepy.API(auth)
cursor = tweepy.Cursor(api.user_timeline,
user_id=USER_ID,
exclude_replies='true',
include_rts='false',
count=200)
return cursor.items()
flight_texts = (tweet.text for tweet in get_tweets()
if MARKER in tweet.text)
def get_flights(text):
parts = [part for part in text.split(' ') if len(part) == 3]
if len(parts) < 2:
return []
return zip(parts[:-1], parts[1:])
flights = [flight for text in flight_texts
for flight in get_flights(text)]
uniq_flights = list(set(flights))
airports = [airport for flight in flights
for airport in flight]
uniq_airports = list(set(airports))
graph = networkx.DiGraph()
graph.add_nodes_from(uniq_airports)
graph.add_edges_from(uniq_flights)
def get_colors(all_records, uniq_records):
counter = Counter(all_records)
max_val = max(counter.values())
return [counter[record] / max_val
for record in uniq_records]
networkx.draw(graph,
with_labels=True,
node_size=1000,
width=1.5,
pos=networkx.nx_pydot.graphviz_layout(graph, prog='neato'),
cmap=cm.get_cmap('Pastel1'),
edge_cmap=cm.get_cmap('Pastel2'),
edge_color=get_colors(flights, uniq_flights),
node_color=get_colors(airports, uniq_airports))
pyplot.draw()
pyplot.show()
tweepy
networkx
matplotlib
pydotplus
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment