{"id":167,"date":"2012-04-06T09:48:44","date_gmt":"2012-04-06T09:48:44","guid":{"rendered":"http:\/\/technicalelvis.com\/blog\/?p=167"},"modified":"2012-04-06T09:48:44","modified_gmt":"2012-04-06T09:48:44","slug":"twitter-mining-by-geolocation","status":"publish","type":"post","link":"https:\/\/technicalelvis.com\/blog\/2012\/04\/06\/twitter-mining-by-geolocation\/","title":{"rendered":"twitter mining by geolocation"},"content":{"rendered":"<p>Twitter's streaming api permits filtering tweets by geolocation. According to the <a href=\"https:\/\/dev.twitter.com\/docs\/streaming-api\/methods#locations\" title=\"streaming api doc\">api documentation<\/a>, only tweets that are created using the Geotagging API can be filtered. The code below uses tweepy to filter tweets for the San Francisco area.<\/p>\n<pre class=\"brush: python; collapse: false; title: ; wrap-lines: false; notranslate\" title=\"\">\r\n#!\/usr\/bin\/env python\r\nimport tweepy\r\nimport ConfigParser\r\nimport os, sys\r\n\r\nclass Listener(tweepy.StreamListener):\r\n    def on_status(self, status):\r\n        print &quot;screen_name='%s' tweet='%s'&quot;%(status.author.screen_name, status.text)\r\n\r\ndef login(config):\r\n    &quot;&quot;&quot;Tweepy oauth dance\r\n    The config file should contain:\r\n\r\n    &#x5B;auth]\r\n    CONSUMER_KEY = ...\r\n    CONSUMER_SECRET = ...\r\n    ACCESS_TOKEN = ...\r\n    ACCESS_TOKEN_SECRET = ...\r\n    &quot;&quot;&quot;     \r\n    CONSUMER_KEY = config.get('auth','CONSUMER_KEY')\r\n    CONSUMER_SECRET = config.get('auth','CONSUMER_SECRET')\r\n    ACCESS_TOKEN = config.get('auth','ACCESS_TOKEN')\r\n    ACCESS_TOKEN_SECRET = config.get('auth','ACCESS_TOKEN_SECRET')\r\n    \r\n    auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)\r\n    auth.set_access_token(ACCESS_TOKEN, ACCESS_TOKEN_SECRET)\r\n    return auth\r\n\r\n\r\nfn=sys.argv&#x5B;1]\r\nconfig = ConfigParser.RawConfigParser()\r\nconfig.read(fn)\r\ntry:\r\n    auth = login(config)\r\n    streaming_api = tweepy.streaming.Stream(auth, Listener(), timeout=60)\r\n    # San Francisco area.\r\n    streaming_api.filter(follow=None, locations=&#x5B;-122.75,36.8,-121.75,37.8]) \r\nexcept KeyboardInterrupt:\r\n    print &quot;got keyboardinterrupt&quot;\r\n<\/pre>\n<p>Find the complete codebase on github at: <a title=\"twitter_mining github\" href=\"https:\/\/github.com\/telvis07\/twitter_mining\">https:\/\/github.com\/telvis07\/twitter_mining<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Twitter&#8217;s streaming api permits filtering tweets by geolocation. According to the api documentation, only tweets that are created using the Geotagging API can be filtered. The code below uses tweepy to filter tweets for the San Francisco area. #!\/usr\/bin\/env python import tweepy import ConfigParser import os, sys class Listener(tweepy.StreamListener): def on_status(self, status): print &quot;screen_name=&#8217;%s&#8217; tweet=&#8217;%s&#8217;&quot;%(status.author.screen_name, &hellip; <a href=\"https:\/\/technicalelvis.com\/blog\/2012\/04\/06\/twitter-mining-by-geolocation\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">twitter mining by geolocation<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[7,11,12],"tags":[],"class_list":["post-167","post","type-post","status-publish","format-standard","hentry","category-python","category-tech","category-twitter_mining"],"_links":{"self":[{"href":"https:\/\/technicalelvis.com\/blog\/wp-json\/wp\/v2\/posts\/167","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/technicalelvis.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/technicalelvis.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/technicalelvis.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/technicalelvis.com\/blog\/wp-json\/wp\/v2\/comments?post=167"}],"version-history":[{"count":6,"href":"https:\/\/technicalelvis.com\/blog\/wp-json\/wp\/v2\/posts\/167\/revisions"}],"predecessor-version":[{"id":196,"href":"https:\/\/technicalelvis.com\/blog\/wp-json\/wp\/v2\/posts\/167\/revisions\/196"}],"wp:attachment":[{"href":"https:\/\/technicalelvis.com\/blog\/wp-json\/wp\/v2\/media?parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/technicalelvis.com\/blog\/wp-json\/wp\/v2\/categories?post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/technicalelvis.com\/blog\/wp-json\/wp\/v2\/tags?post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}