<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Basic Eventador Report
Goal
Step1:  Simple graph analytics for the Twitter stream
For this first step we want
Step2:  Building the directed graph
We build the retweet graph, where an edge is from the original tweeter to the retweeter. We add node weights corresponding to how much each node was retweeted
Step3:  Most retweeted users
Step4:  Top 10 Pageranked users
Note - these are the 'archetypal retweeters' of the graph (well, not exactly. see https
Step5:  Basic network viz
size of nodes is number of retweets
color of nodes is pagerank
we only label the top 10 pageranked users
<ASSISTANT_TASK:>
Python Code:
from kafka import KafkaConsumer
import uuid
import json
consumer = KafkaConsumer(bootstrap_servers='', 
                         value_deserializer=lambda s: json.loads(s, encoding='utf-8'), 
                         auto_offset_reset='smallest', 
                         group_id=uuid.uuid4()) 
consumer.subscribe(['tweets'])
limit = 500
consumer.poll(max_records=limit)
count = 0
data = []
for msg in consumer:
    data.append(msg.value)
    count += 1
    if count >= limit:
        break
len(data)
Explanation: Basic Eventador Report
Goal: to set up a simple eventador report with basic indicators on tweets. Should be the base for the later, more complicated analytics pipeline.
Example Eventador NB from @asragab
End of explanation
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
%matplotlib inline
import networkx as nx
Explanation: Simple graph analytics for the Twitter stream
For this first step we want:
- top 10 retweeted users
- top 10 PageRanked users
- basic matplotlib viz
End of explanation
graph = nx.DiGraph()
for tweet in data:
    if tweet.get('retweet') == 'Y':
        name = tweet.get('name')
        original_name = tweet.get('original_name')
        followers = tweet.get('followers')
        if name not in graph: graph.add_node(name, retweets = 0)
        if original_name not in graph: 
            graph.add_node(original_name, retweets = 1)
        else:
            graph.node[original_name]['retweets'] = graph.node[original_name]['retweets'] +1
        graph.add_edge(original_name, name)    
Explanation: Building the directed graph
We build the retweet graph, where an edge is from the original tweeter to the retweeter. We add node weights corresponding to how much each node was retweeted
End of explanation
top10_retweets = sorted([(node,graph.node[node]['retweets']) for node in graph.nodes()], key = lambda x: -x[1])[0:10]
top10_retweets
Explanation: Most retweeted users
End of explanation
pr = nx.pagerank(graph)
colors = [pr[node] for node in graph.nodes()]
top10_pr = sorted([(k,v) for k,v in pr.items()], key = lambda x: x[1])[0:10]
label_dict = dict([(k[0],k[0]) for k in top10_pr])
top10_pr
Explanation: Top 10 Pageranked users
Note - these are the 'archetypal retweeters' of the graph (well, not exactly. see https://en.wikipedia.org/wiki/PageRank)
End of explanation
plt.figure(figsize=(11,11))
plt.axis('off')
weights = [10*(graph.node[node]['retweets'] + 1) for node in graph.nodes()]
nx.draw_networkx(graph, node_size = weights,  width = .1, linewidths = .1, with_labels=True,
                 node_color = colors, cmap = 'RdYlBu', 
                 labels = label_dict)
consumer.close()
Explanation: Basic network viz
size of nodes is number of retweets
color of nodes is pagerank
we only label the top 10 pageranked users
End of explanation
<END_TASK>