Skip to content

Commit

Permalink
Added script for country numeric codes
Browse files Browse the repository at this point in the history
  • Loading branch information
Fayder Florez committed Sep 17, 2016
1 parent decd80e commit 698552e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 6 deletions.
2 changes: 1 addition & 1 deletion python/countries-input.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion python/countries-output.json

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions python/crawlers/numeric_codes.py
@@ -0,0 +1,43 @@
# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup
import urllib2

URL_ISO_COUNTRIES = 'https://en.wikipedia.org/wiki/ISO_3166-1'


def update(countriesJSON):
html = __getHTML(URL_ISO_COUNTRIES)
soup = BeautifulSoup(html, "html.parser")
data = __extractData(soup)
return __updateJSON(countriesJSON, data)


def __getHTML(url):
usock = urllib2.urlopen(url)
data = usock.read()
usock.close()
return data

def __extractData(html):
data = {}
table = html.find('table', class_='wikitable')
for row in table:
nameLink = row.find('a')
if nameLink != -1:
countryName = nameLink['title'].encode('utf-8')
for idx,element in enumerate(row):
if idx == 7:
numericCode = element.text
data[countryName] = numericCode

return data

def __updateJSON(countriesJSON, data):
for country in data:
for countryJSON in countriesJSON:
if country == countryJSON['name'].encode('utf-8'):
print(country, data[country])
countryJSON['numericCode'] = data[country]

return countriesJSON

10 changes: 6 additions & 4 deletions python/updater.py
Expand Up @@ -10,6 +10,7 @@
from crawlers import gini
from crawlers import currencies
from crawlers import languages
from crawlers import numeric_codes


def getCountriesJSON():
Expand All @@ -26,8 +27,9 @@ def saveCountriesJSON(countriesJSON):
json.dump(countriesJSON, outputJsonFile)

countriesJSON = getCountriesJSON()
countriesJSON = population.update(countriesJSON)
countriesJSON = gini.update(countriesJSON)
countriesJSON = currencies.update(countriesJSON)
countriesJSON = languages.update(countriesJSON)
#countriesJSON = population.update(countriesJSON)
#countriesJSON = gini.update(countriesJSON)
#countriesJSON = currencies.update(countriesJSON)
#countriesJSON = languages.update(countriesJSON)
countriesJSON = numeric_codes.update(countriesJSON)
saveCountriesJSON(countriesJSON)

0 comments on commit 698552e

Please sign in to comment.