Skip to content

Commit

Permalink
Add CSV support
Browse files Browse the repository at this point in the history
Running `fetch.js` now also generates `mcc-mnc-list.csv` file.

Signed-off-by: Prashanth Pai <prashanth.pai@plivo.com>
  • Loading branch information
ppai-plivo committed Aug 27, 2020
1 parent 6dc779c commit 6222406
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
16 changes: 13 additions & 3 deletions fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const fs = require('fs');
const path = require('path');
const jsdom = require('jsdom');
const { JSDOM } = jsdom;
const csvstringify = require('csv-stringify/lib/sync')

const WIKI_URL = 'https://en.wikipedia.org/wiki/Mobile_country_code';
const WIKI_URL_REGIONS = [
Expand All @@ -15,7 +16,8 @@ const WIKI_URL_REGIONS = [
'https://en.wikipedia.org/wiki/Mobile_Network_Codes_in_ITU_region_7xx_(South_America)'
];

const MCC_MNC_OUTPUT_FILE = path.join( __dirname, 'mcc-mnc-list.json');
const MCC_MNC_OUTPUT_FILE_JSON = path.join( __dirname, 'mcc-mnc-list.json');
const MCC_MNC_OUTPUT_FILE_CSV = path.join( __dirname, 'mcc-mnc-list.csv');
const STATUS_CODES_OUTPUT_FILE = path.join( __dirname, 'status-codes.json');


Expand Down Expand Up @@ -166,11 +168,19 @@ function collect (resolve, from, records, statusCodes, globals) {
}

function writeData(records, statusCodes) {
fs.writeFile( MCC_MNC_OUTPUT_FILE, JSON.stringify( records, null, 2 ), err => {
fs.writeFile( MCC_MNC_OUTPUT_FILE_JSON, JSON.stringify( records, null, 2 ), err => {
if ( err ) {
throw err;
}
console.log( 'MCC-MNC list saved to ' + MCC_MNC_OUTPUT_FILE );
console.log( 'MCC-MNC list saved to ' + MCC_MNC_OUTPUT_FILE_JSON );
console.log( 'Total ' + records.length + ' records' );
});

fs.writeFile( MCC_MNC_OUTPUT_FILE_CSV, csvstringify( records , {header: true} ), err => {
if ( err ) {
throw err;
}
console.log( 'MCC-MNC list saved to ' + MCC_MNC_OUTPUT_FILE_CSV );
console.log( 'Total ' + records.length + ' records' );
});

Expand Down
8 changes: 7 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"license": "MIT",
"devDependencies": {
"ava": "^3.10.1",
"csv-stringify": "^5.5.1",
"eslint": "^7.5.0",
"jsdom": "^16.3.0"
},
Expand Down

0 comments on commit 6222406

Please sign in to comment.