Instantly share code, notes, and snippets.

Embed
What would you like to do?
Twitter Search to Graphite
import calendar
import json
import socket
import time
import requests
from dateutil.parser import parse
graphite_server = 'localhost'
graphite_port = 2003
while True:
url = 'http://search.twitter.com/search.json?q=seatgeek'
resp = requests.get(url)
content = json.loads(resp.content)
counters = {}
for result in content['results']:
dt = parse(result['created_at'])
dt = dt.replace(second=0)
timestamp = calendar.timegm(dt.utctimetuple())
counters[timestamp] = counters.get(timestamp, 0) + 1
sock = socket.socket()
sock.connect((graphite_server, graphite_port))
template = 'stats.twitter {} {}\n'
for key, value in counters.iteritems():
metric = template.format(value, key)
print metric
sock.sendall(metric)
sock.close()
time.sleep(300)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment