There is a feature of Twitter where you can ask for the trends among the tweets by location. Twitter is proud to have over 160 locations where you can browse the trends (see here) unfortunately there are only 62 known countries as of writing this article. And Hungary is not among these 62…
The trend’s location query is based on the Yahoo! WOE-IDs (Where On Earth) — and you can query them by the IDs to get the most popular themes in the Twitter world. The ID 1 is worldwide. So if you are not keen about a specific country you can use 1 to get the most popular trends. However, as you could read above, Hungary is not among those 62 countries getting into the trends.
For the places: currently there are 460+ places where you can choose the threads of — for example you could compare two German cities what they’re tweeting about. But let’s get to the coding part because this is the interesting part. I won’t go into detail how to get Twitter keys. For an application to access the Twitter API it has to identify itself via oAuth. You can get your credentials creating an application at dev.twitter.com. You can find all 4 after clicking the “manage API keys”. And as for the access tokens are mine and I take the responsibility for them you don’t get to see them — sorry 😦
But you can add your application’s tokens to the places mentioned.
import twitter import json CONSUMER_KEY = 'value of API key' CONSUMER_SECRET = 'value of API secret' OAUTH_TOKEN = 'value of Access token' OAUTH_TOKEN_SECRET = 'value of Access token secret' auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET) twitter_api = twitter.Twitter(auth=auth)
The code snippet above is the main part which I’ll use in the examples. This creates the Twitter API connection. Prerequisite is the Twitter Python API, installable with: pip install twitter. Now let’s take a look at the countries. The example code is right below:
countries = set() results = twitter_api.trends.available() for location in results: if location["woeid"] != 1: countries.update(["%s"%(location["country"].encode('ascii', 'replace'))]) print "%d contries known currently"%len(countries) for country in sorted(countries): print country
You can see I remove the “Worldwide” (with ID 1) because it is not really a country. If you execute the script you get something like this list:
62 contries known currently Algeria Argentina Australia Austria Bahrain Belarus Belgium Brazil Canada Chile Colombia Denmark Dominican Republic Ecuador Egypt France Germany Ghana Greece Guatemala India Indonesia Ireland Israel Italy Japan Jordan Kenya Korea Kuwait Latvia Lebanon Malaysia Mexico Netherlands New Zealand Nigeria Norway Oman Pakistan Panama Peru Philippines Poland Portugal Puerto Rico Qatar Russia Saudi Arabia Singapore South Africa Spain Sweden Switzerland Thailand Turkey Ukraine United Arab Emirates United Kingdom United States Venezuela Vietnam
This is not much. To get the available cities or places by ID you can alter the script above to output each element directly:
for location in results: name = location["name"].encode('ascii', 'replace') country = location["country"].encode('ascii', 'replace') print "(%d) %s, %s" % (location["woeid"], name, country)
And as mentioned previously let’s examine the current trends of two cities in Germany: Frankfurt and Munich.
frankfurt_trends = twitter_api.trends.place(_id=650272) frankfurt_trend_set = set([trend['name'] for trend in frankfurt_trends['trends']]) munich_trends = twitter_api.trends.place(_id=676757) munich_trend_set = set([trend['name'] for trend in munich_trends['trends']]) common_trends = frankfurt_trend_set.intersection(munich_trend_set) print "Common trends in Frankfurt and Munich" for trend in common_trends: print trend
For the sources the first part is the same: you have to create the twitter api as above. A sample result of the current common trends could be:
Common trends in Frankfurt and Munich #ichsehedich Temperaturen Unwetter #bibinator #deichbrand Liam and Sophia #askbibi
If you want to change the display to another city simply change the for loop with the results of the city — and for another city get a WOE-ID from the list above. And as a good practice: change the name of the variable according to your city 😉
For this article the source code is not at GitHub because my application’s tokens. However if you have any questions just write me a comment or a mail and I’ll get back on you.