Skip to content

Instantly share code, notes, and snippets.

@nullren
Created April 29, 2013 06:16
Show Gist options
  • Select an option

  • Save nullren/5479963 to your computer and use it in GitHub Desktop.

Select an option

Save nullren/5479963 to your computer and use it in GitHub Desktop.
i am trying to count how many recent tweets contain 'bitcoin' in their text... i don't think this is right.
#!/usr/bin/python
import json
import urllib.request as r
import urllib.parse as p
from datetime import datetime, timedelta
import pytz
now = pytz.UTC.localize(datetime.utcnow())
delta = timedelta(minutes=3)
def grab(url):
f = r.urlopen(url)
doc = f.read().decode()
f.close()
return doc
def twitter_search(query):
return 'http://search.twitter.com/search.json{}'.format(query)
def datetime_recent(dt):
#now = datetime.utcnow() - timedelta(minutes=3)
#print('{} is less than {}: {}'.format(now - dt, delta, now - dt < delta))
return now - dt < delta
def twitter_cmpdate(j):
# 'created_at': 'Mon, 29 Apr 2013 05:09:26 +0000'
#print('is {} okay?'.format(j['created_at']))
dt = datetime.strptime(j['created_at'], '%a, %d %b %Y %H:%M:%S %z')
return datetime_recent(dt)
def twitter_count_tweets(term):
query = '?{}'.format(p.urlencode({'q':term}))
total = 0
while True:
j = json.loads(grab(twitter_search(query)))
r = len([x for x in j['results'] if twitter_cmpdate(x)])
t = len(j['results'])
total += r
print('read {}/{} results on page {}'.format(r,t,j['page']))
if r==t and 'next_page' in j:
query = j['next_page']
else:
break
return total
print("{}".format(twitter_count_tweets('bitcoin')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment