Advertisement
Guest User

quickie tweet plottin’

a guest
Nov 19th, 2011
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rails 1.27 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import json
  4. import codecs
  5. import Image
  6. from ImageOps import invert
  7. from math import sqrt, pi, sin
  8. from sys import argv
  9. import calendar
  10. import time
  11.  
  12.  
  13. latrange = [45, 46]
  14. lonrange = [-123.5, -122]
  15.  
  16. latgrain = 1/500.0/sqrt(2) # heehee
  17. longrain = 1/500.0
  18.  
  19. width = int(round((lonrange[1]-lonrange[0]) * 1/longrain))
  20. height = int(round((latrange[1]-latrange[0]) * 1/latgrain))
  21.  
  22. i = Image.new('RGB', (width, height))
  23. pixels = i.load()
  24.  
  25. colorincrement = 100
  26.  
  27.  
  28. def kz1(v):
  29.     v = v * pi
  30.     r = sin(v)
  31.     g = sin(pi * 2.0/3.0 + v)
  32.     b = sin(pi * 4.0/3.0 + v)
  33.     return (int(r*r*255), int(g*g*255), int(b*b*255))
  34.  
  35.  
  36. input = codecs.open(argv[1], 'r', 'utf-8')
  37.  
  38. for line in input.readlines():
  39.     try: tweet = json.loads(line)
  40.     except: continue
  41.    
  42.     if tweet['geo']:
  43.         #if tweet['geo']['type'] == 'Point':
  44.         when = calendar.timegm(time.strptime(tweet['created_at'], '%a %b %d %H:%M:%S +0000 %Y'))
  45.         #print when
  46.        
  47.         ago = time.time() - when
  48.        
  49.         lat, lon = tweet['geo']['coordinates']
  50.         y = int((lat-latrange[0]) * 1/latgrain)
  51.         x = int((lon-lonrange[0]) * 1/longrain)
  52.         y = height-y
  53.        
  54.         try:
  55.             pixels[x, y] = kz1(ago/(60*60*24) + 0.5)
  56.             #pixels[x, y] += colorincrement
  57.         except:
  58.             print when, x, y
  59.             #pass
  60.  
  61. i = invert(i)
  62. i.save(argv[2])
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement