Did you know? Programmers convert coffee to code.

If you like my articles, sponsor me a coffee.

At the other end of the link I post some results of my other hobbies, like playing bass. It is worth checking it out ;)

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.

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[&quot;woeid&quot;] != 1:<br />
        countries.update([&quot;%s&quot;%(location[&quot;country&quot;].encode('ascii', 'replace'))])</p>
<p>print &quot;%d contries known currently&quot;%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[&quot;name&quot;].encode('ascii', 'replace')<br />
    country = location[&quot;country&quot;].encode('ascii', 'replace')<br />
    print &quot;(%d) %s, %s&quot; % (location[&quot;woeid&quot;], 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[0]['trends']])<br />
munich_trends = twitter_api.trends.place(_id=676757)<br />
munich_trend_set = set([trend['name'] for trend in munich_trends[0]['trends']])</p>
<p>common_trends = frankfurt_trend_set.intersection(munich_trend_set)</p>
<p>print &quot;Common trends in Frankfurt and Munich&quot;<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.

Share the knowledge!

Senior developer, consultant, author, mentor, apprentice. I love to share my knowledge and insights what I achieve through my daily work which is not trivial -- at least not for me.

Click Here to Leave a Comment Below

%d bloggers like this: