Skip to content

Instantly share code, notes, and snippets.

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 = ''
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.