Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.58 KB | None | 0 0
  1. #!/usr/bin/python
  2.  
  3. #-----------------------------------------------------------------------
  4. # twitter-search-geo
  5. #  - performs a search for tweets close to New Cross, London,
  6. #    and outputs them to a CSV file.
  7. #-----------------------------------------------------------------------
  8.  
  9. from twitter import *
  10.  
  11. import sys
  12. import csv
  13.  
  14. latitude = 51.474144    # geographical centre of search
  15. longitude = -0.035401   # geographical centre of search
  16. max_range = 1           # search range in kilometres
  17. num_results = 50        # minimum results to obtain
  18. outfile = "output.csv"
  19.  
  20. #-----------------------------------------------------------------------
  21. # load our API credentials
  22. #-----------------------------------------------------------------------
  23. #config = {}
  24. #execfile("config.py", config)
  25.  
  26. #-----------------------------------------------------------------------
  27. # create twitter API object
  28. #-----------------------------------------------------------------------
  29. twitter = Twitter(
  30.                 auth = OAuth("","", "", "")
  31.  
  32. #-----------------------------------------------------------------------
  33. # open a file to write (mode "w"), and create a CSV writer object
  34. #-----------------------------------------------------------------------
  35.  
  36. csvfile = file(outfile.csv, "w")
  37. csvwriter = csv.writer(csvfile)
  38.  
  39. #-----------------------------------------------------------------------
  40. # add headings to our CSV file
  41. #-----------------------------------------------------------------------
  42. row = [ "user", "text", "latitude", "longitude" ]
  43. csvwriter.writerow(row)
  44.  
  45. #-----------------------------------------------------------------------
  46. # the twitter API only allows us to query up to 100 tweets at a time.
  47. # to search for more, we will break our search up into 10 "pages", each
  48. # of which will include 100 matching tweets.
  49. #-----------------------------------------------------------------------
  50. result_count = 0
  51. last_id = None
  52. while result_count <  num_results:
  53.     #-----------------------------------------------------------------------
  54.     # perform a search based on latitude and longitude
  55.     # twitter API docs: https://dev.twitter.com/rest/reference/get/search/tweets
  56.     #-----------------------------------------------------------------------
  57.     query = twitter.search.tweets(q = "cool", geocode = "%f,%f,%dkm" % (latitude, longitude, max_range), count = 100, max_id = last_id)
  58.  
  59.     for result in query["statuses"]:
  60.         #-----------------------------------------------------------------------
  61.         # only process a result if it has a geolocation
  62.         #-----------------------------------------------------------------------
  63.         if result["geo"]:
  64.             user = result["user"]["screen_name"]
  65.             text = result["text"]
  66.             text = text.encode('ascii', 'replace')
  67.             latitude = result["geo"]["coordinates"][0]
  68.             longitude = result["geo"]["coordinates"][1]
  69.  
  70.             #-----------------------------------------------------------------------
  71.             # now write this row to our CSV file
  72.             #-----------------------------------------------------------------------
  73.             row = [ user, text, latitude, longitude ]
  74.             csvwriter.writerow(row)
  75.             result_count += 1
  76.         last_id = result["id"]
  77.  
  78.     #-----------------------------------------------------------------------
  79.     # let the user know where we're up to
  80.     #-----------------------------------------------------------------------
  81.     print "got %d results" % result_count
  82.  
  83. #-----------------------------------------------------------------------
  84. # we're all finished, clean up and go home.
  85. #-----------------------------------------------------------------------
  86. csvfile.close()
  87.  
  88. print "written to %s" % outfile
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement