This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
80 lines (75 loc) · 2.85 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict'
const axios = require('axios')
const fs = require('fs')
const pcs = require('./pc.json')
const prettier = require('prettier')
const APIURL =
'https://www.starbucks.com.cn/api/stores'
const result = {}
const FixedCity = ['上海市', '天津市', '重庆市', '北京市', '香港', '澳門']
const searchProvince = city => {
if (FixedCity.indexOf(city) > -1) return city
if (city === 'Hong Kong') return '香港'
if (city === 'Macau') return '澳門'
if (city === 'SHANGHAI' || city === 'Shanghai') return '上海市'
if (city === 'Beijing') return '北京市'
if (city === 'Yangzhou' || city === 'Suzhou' || city === 'Nantong' || city === 'Wuxi' || city === 'Nanjing') return '江苏省'
if (city === 'Hangzhou' || city === 'Jinhua' || city === 'Huzhou' || city === 'TBD' || city === 'Ningbo') return '浙江省'
if (city === '襄樊市' || city === 'Wuhan' || city === '恩施市') return '湖北省'
if (city === 'Guangzhou' || city === 'Shenzhen') return '广东省'
if (city === 'Tin Shui Wai') return '黑龙江省'
if (city === '延吉市') return '吉林省'
if (city === 'Chengdu City' || city === '西昌市' || city === 'Luzhou' || city === 'Chengdu' || city === 'Nanchong' || city === 'Panzhihua' || city === '甘孜市') return '四川省'
if (city === '大理市' || city === '景洪市' || city === '文山市' || city === '楚雄市' || city === '蒙自市') return '云南省'
if (city === 'Jinan') return '山东省'
if (city === 'Chongqing') return '重庆市'
if (city === 'Anqing') return '安徽省'
if (city === '海南省') return '海南省'
if (city === '吉首市') return '湖南省'
if (city === '乌兰浩特市' || city === '锡林浩特市') return '内蒙古自治区'
if (city === '兴义市' || city === '都匀市' || city === '凯里市') return '贵州省'
for (const k in pcs) {
for (const x of pcs[k]) {
if (x.indexOf(city) > -1) {
return k
}
}
}
}
axios
.get(APIURL)
.then(res => {
const data = res.data.data
fs.writeFileSync('./data.json', JSON.stringify(res.data))
data.forEach(item => {
const city = item.address.city
const provice = searchProvince(city)
if (!provice) {
throw new Error(`${city} 没找到对应省份`)
}
if (!result[provice]) {
result[provice] = {}
result[provice].count = 1
} else {
result[provice].count = result[provice].count + 1
}
})
fs.writeFileSync(
'./format.json',
prettier.format(
JSON.stringify({
data: Object.keys(result).map(key => {
return {
name: key,
count: result[key].count
}
}).sort((a, b) => b.count - a.count),
total: res.data.meta.total
}),
{ parser: 'json' }
)
)
})
.catch(err => {
console.log(err)
})