Created
October 12, 2012 00:51
-
-
Save philipcristiano/3876716 to your computer and use it in GitHub Desktop.
Twitter Search to Graphite
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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