Skip to content

Instantly share code, notes, and snippets.

@philipcristiano
Created October 12, 2012 00:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save philipcristiano/3876716 to your computer and use it in GitHub Desktop.
Save philipcristiano/3876716 to your computer and use it in GitHub Desktop.
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