Twit-twit but not in Hungary — because Twitter does not know all countries
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.
<br /> import twitter<br /> import json</p> <p>CONSUMER_KEY = 'value of API key'<br /> CONSUMER_SECRET = 'value of API secret'<br /> OAUTH_TOKEN = 'value of Access token'<br /> OAUTH_TOKEN_SECRET = 'value of Access token secret'</p> <p>auth = twitter.oauth.OAuth(OAUTH_TOKEN, OAUTH_TOKEN_SECRET, CONSUMER_KEY, CONSUMER_SECRET)<br /> twitter_api = twitter.Twitter(auth=auth)<br />
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:
<br /> countries = set()</p> <p>results = twitter_api.trends.available()<br /> for location in results:<br /> if location["woeid"] != 1:<br /> countries.update(["%s"%(location["country"].encode('ascii', 'replace'))])</p> <p>print "%d contries known currently"%len(countries)<br /> for country in sorted(countries):<br /> print country<br />
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:
<br /> 62 contries known currently<br /> Algeria<br /> Argentina<br /> Australia<br /> Austria<br /> Bahrain<br /> Belarus<br /> Belgium<br /> Brazil<br /> Canada<br /> Chile<br /> Colombia<br /> Denmark<br /> Dominican Republic<br /> Ecuador<br /> Egypt<br /> France<br /> Germany<br /> Ghana<br /> Greece<br /> Guatemala<br /> India<br /> Indonesia<br /> Ireland<br /> Israel<br /> Italy<br /> Japan<br /> Jordan<br /> Kenya<br /> Korea<br /> Kuwait<br /> Latvia<br /> Lebanon<br /> Malaysia<br /> Mexico<br /> Netherlands<br /> New Zealand<br /> Nigeria<br /> Norway<br /> Oman<br /> Pakistan<br /> Panama<br /> Peru<br /> Philippines<br /> Poland<br /> Portugal<br /> Puerto Rico<br /> Qatar<br /> Russia<br /> Saudi Arabia<br /> Singapore<br /> South Africa<br /> Spain<br /> Sweden<br /> Switzerland<br /> Thailand<br /> Turkey<br /> Ukraine<br /> United Arab Emirates<br /> United Kingdom<br /> United States<br /> Venezuela<br /> Vietnam<br />
This is not much. To get the available cities or places by ID you can alter the script above to output each element directly:
<br /> for location in results:<br /> name = location["name"].encode('ascii', 'replace')<br /> country = location["country"].encode('ascii', 'replace')<br /> print "(%d) %s, %s" % (location["woeid"], name, country)<br />
And as mentioned previously let’s examine the current trends of two cities in Germany: Frankfurt and Munich.
<br /> frankfurt_trends = twitter_api.trends.place(_id=650272)<br /> frankfurt_trend_set = set([trend['name'] for trend in frankfurt_trends['trends']])<br /> munich_trends = twitter_api.trends.place(_id=676757)<br /> munich_trend_set = set([trend['name'] for trend in munich_trends['trends']])</p> <p>common_trends = frankfurt_trend_set.intersection(munich_trend_set)</p> <p>print "Common trends in Frankfurt and Munich"<br /> for trend in common_trends:<br /> print trend<br />
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:
<br /> Common trends in Frankfurt and Munich<br /> #ichsehedich<br /> Temperaturen<br /> Unwetter<br /> #bibinator<br /> #deichbrand<br /> Liam and Sophia<br /> #askbibi<br />
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.