node脚本下载geo数据

var fs = require("fs");
var path = require("path");
var request = require("request");

//创建文件夹目录
var dirPath = path.join(__dirname, "geos");
if (!fs.existsSync(dirPath)) {
    fs.mkdirSync(dirPath);
    console.log("文件夹创建成功");
} else {
    console.log("文件夹已存在");
}
const  url = 'https://geo.datav.aliyun.com/areas_v2/bound/';
function downFile(fileName){	
	request(url + fileName,function(err, response, body){
		console.log(typeof body,"type")
		let data = JSON.stringify(JSON.parse((body)))
		if(fileName === 'all.json'){
			JSON.parse(body).map(item=>{
				if(item.level == 'country' || item.level=="province")
				{
					downFile(item.adcode+'_full.json')
				}
			})
			data =  JSON.stringify( JSON.parse(body).filter(item => item.level =='country' || item.level =='province'))
		}
		 let stream = fs.createWriteStream(path.join(dirPath, fileName));
		 stream.write(data)
	})
}
downFile('all.json')
posted @ 2020-07-31 19:12  橙云生  阅读(130)  评论(0编辑  收藏  举报