Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

city

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

city - npm Package Compare versions

Comparing version 0.1.2 to 1.0.0

76

index.js

@@ -1,50 +0,30 @@

#!/usr/bin/env node
var commander = require('commander');
var version = require('./package.json').version;
const axios = require('axios')
const api = 'https://city-grabber.herokuapp.com/api/v1'
commander
.version(version)
.option('-l --level <n>','输出指定级别行政区数据',parseInt)
.option('-o --output [value]','输出文件')
.option('-j --js [value]','以指定全局对象输出js文件')
.option('-a --amd','以AMD规范输出js文件')
.option('-m --cmd','以CMD规范输出js文件')
.option('-p --pretty','格式化输出')
.option('-s --overseas','包含海外地区')
.option('-y --pinyin','输出pinyin')
.option('-i --ignore [value]','忽略 \'省|市|区|地区|县\'')
.option('-c --code','包含地区编码(身份证前6位)')
.option('-u --update','重新抓取原始数据,或在citydata.json丢失的情况下使用')
.option('-k --key [value]','对象键名 name,children,code,pinyin,顺序不能改变,例如n,s,c,p')
.option('-f --flat [value]','扁平化输出,可以指定parentId参数名称')
.option('--closed','地区树默认为关闭状态')
.option('--father [value]','转出指定父节点的所有子节点,不含父节点')
.option('-r --reverse','转出以子节点为根的树,便于倒查父节点')
// .option('-t --type','输出类型,array/object')
// .option('-z --zipcode','包含邮编')
.parse(process.argv);
if(commander.update){
require('./lib/update').update();
}else{
var options = {
level:commander.level,
code:commander.code,
overseas:commander.overseas,
output:commander.output,
js:commander.js,
amd:commander.amd,
cmd:commander.cmd,
pretty:commander.pretty,
keys:commander.key,
pinyin:commander.pinyin,
ignore:commander.ignore,
flat:commander.flat,
closed:commander.closed,
father:commander.father,
reverse:commander.reverse
};
require('./lib/generator.js').generate(options);
}
module.exports = {
/**
* This will return the City and Country from GPS coordinates
* @param {Number} latitude The latitude value
* @param {Number} longitude The longitude value
* @param {Number | Null} size The city size value
* @param {String} apiKey The API key
* @return {Object}
*/
get: async( latitude, longitude, size = null, apiKey ) => {
const route = '/cities/nearest'
let url = api + route + '?latitude=' + latitude + '&longitude=' + longitude
if ( size !== null ) url += '&size=' + size
const headers = {}
if ( apiKey ) headers[ 'x-api-key' ] = apiKey
const response = await axios(url, {
method: 'GET',
headers
}).catch( e => {
return Promise.reject( e.response.data )
})
return Promise.resolve( response.data )
}
}
{
"name": "city",
"description": "中国国家行政区划数据",
"version": "0.1.2",
"author": "basecss <i@basecss.net>",
"contributors": [
{
"name": "TooBug",
"email": "i@toobug.net",
"url": "http://www.toobug.net/"
}
],
"dependencies": {
"cheerio": "^0.17.0",
"commander": "~2.2.0",
"pinyin": "^2.6.2",
"restler": "^3.2.2"
},
"devDependencies": {},
"version": "1.0.0",
"description": "TheCity API returns the city from a latitude and longitude using spatial mapping and data anlysis.",
"main": "index.js",
"bin": {
"city": "index.js"
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"repository": {
"type": "git",
"url": "https://github.com/TooBug/city.git"
},
"keywords": [
"city",
"城市数据",
"国家行政区划数据"
"City",
"Location",
"Latitude",
"Longitude",
"Country",
"GPS",
"API"
],
"scripts": {
"test": "echo no test"
},
"engines": {
"node": ">= 0.8.0"
},
"license": "MIT"
"author": "WilliamDaniels1995@gmail.com",
"license": "ISC",
"dependencies": {
"axios": "^0.21.1"
}
}

@@ -1,96 +0,89 @@

city
========
# City Grabber
This is a wrapper for the City Grabber API which gets a city or closest city from a latitude and longitude.
一款用于生成行政区划联动数据的小工具,如
City grabber uses an algorithim that utilises spatial mapping and sophisticated data analysis to give you a blazing fast and accurate response from over 150,000 cities from every country across the globe.
```json
[{
"name":"北京市",
"children":[{
"name":"东城区"
},{
"name":"西城区"
}]
},{
"name":"天津市",
"children":[{
"name":"和平区"
}]
}]
```
This package requires an API key which can be generated [Here](https://city.rocket-solutions.co.uk/key).
### 特色
<img src="https://city.rocket-solutions.co.uk/img/logo2.6271016e.png" alt="logo" width="100" height="100" />
- 数据权威,来自国家统计局,每年更新
- 自由选择是否包含海外国家列表数据(来自维基百科)
- 自由选择生成一级(省)、二级(省市)、三级(省市县)数据
- 可自定义数据对象键名(key)
- 支持输出JSON、原生js文件、AMD/CMD规范js文件
- 支持输出压缩后的代码及格式化后的代码
- 支持输出地区代码(身份证前6位)
- 支持拼音输出
- 支持自定义去除 省|市|区|地区|县 等后缀
## Installation
`npm i city-grabber --save`
## Usage
### 使用方法
使用npm安装
```sh
npm install -g city
```
const cityGrabber = require( 'city-grabber' )
使用:
```sh
city
//Using Promises
cityGrabber.get( latitude, longitude, size, apiKey )
.then( response => {
const { city, country, population, distance } = response
})
.catch( e => console.log( e ))
//Async await
const { city, country, population, distance } = await cityGrabber.get( latitude, longitude, size, apiKey )
```
支持参数:
## City Sizes
| Size | Description
| ----------- | -------------------------- |
|`NULL` | This will search through every city of every size
|`0` | Major Cities, 1m+ population
|`1` | Big Cities, 250k - 1m population
|`2` | Medium Cities, 10k - 250k population
|`3` | Small cities, under 10k population
- `-V` `--version` 输出版本号
- `-h` `--help` 输出菜单
- `-l` `--level level` 输出指定级别行政区数据,取值1-3
- `-k` `--key name,children,code` 自定义输出格式中的对象键名(必须包含三个值)
- `-o` `--output fileName` 输出文件路径
- `-j` `--js varibleName` 以指定变量名输出js文件(配合`--output`选项写入文件)
- `-a` `--amd` 以AMD规范输出js文件
- `-m` `--cmd` 以CMD规范输出js文件
- `-p` `--pretty` 格式化输出
- `-s` `--overseas` 包含海外地区
- `-c` `--code` 包含地区编码(身份证前6位)
- `-u` `--update` 重新抓取原始数据
- `-y` `--pinyin` 输出pinyin
- `-i` `--ignore '省|市|区|地区|县'` 忽略 省|市|区|地区|县
- `-k` `--key name,children,code,pinyin` 导出的键名(顺序不能改变,例如n,s,c,p)
- `-f` `--flat 'parentId'` 扁平化输出,可以指定parentId参数名称
- `--closed` 地区树结点默认为关闭状态
- `--father` 转出指定父节点的所有子节点,不含父节点
- `-r` `--reserve` 转出以子节点为根的树,便于倒查父节点
## Demos
示例:
1. 嵌套方式列出所有地区,name改为n,children改为s,code改为c,并带上地区编码
```sh
city -k n,s,c -o ./area.json -c
```
cityGrabber.get( 35.4526317, 139.4546863, 0, apiKey )
//Response
{
city: 'Yokohama'.
country: 'JP',
population: 3574443,
distance: 17.823109463333083
}
2. 平面方式列出所有地区,name改为n,children改为s,code改为c,parentId为p,并带上地区编码
```sh
city -k n,s,c -o ./flat-area.json -c -f p
cityGrabber.get( 42.34133262, -83.05576000, 1, apiKey )
//Response
{
city: 'Detroit'.
country: 'US',
population: 677116,
distance: 1.3745663787473823
}
```
3. 倒树方式列出所有地区,子地区的code为key,val为父地区的{code:code, name:name};
```sh
city -k n,s,c -o ./reverse-area.js -j reverse_area -r
```
## Response
| Property | Description
| ----------- | -------------------- |
|`city`| The city name.
|`country` | The 2 letter country code which the city is located.
|`population` | The last updated population of that city.
|`distance` | The distance between the user and city center in km.
## Errors
| Status | Error | Description
| ----------- | -------------------- | ---------------------------------- |
|`422` |`API KEY NOT FOUND` | You need to pass your API key as a parameter. You can get one [Here](https://city.rocket-solutions.co.uk/key).
|`422` |`INVALID API KEY` | Your key is invalid or badly formatted.
|`422` |`INVALID SIZE` | Your size parameter is invalid.
|`402` |`NO CREDITS REMAINING` | You need to purchase more credits for your key. You can do that [Here](https://city.rocket-solutions.co.uk/key).| Icons related to the business type
|`422` |`INVALID COORDINATES` | Missing or invalid latitude/ longitude.
### Todo
## Documentation
This is a brand new API launced in February 2021. Full Documentation will be here soon.
- [ ] 海外地区支持按拼音排序<https://github.com/TooBug/city/issues/1>
- [ ] 支持更多格式的输出<https://github.com/TooBug/city/issues/2>
<s>Click [Here](https://city.rocket-solutions.co.uk/documentation) to see the full documentation</s>
### Changelog
2020.06.12
- 更新内置 `citydata.json` 数据
- 移除 `市辖区` 数据项
## Use cases
- Location based social media
- Dating services
- Profiles
- Location based games
- Environmental evaluation
- Map and directions services
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc