Skip to content

Commit

Permalink
添加对 Apple 原生地图 / Apple Watch 原生天气 的支持;支持显示和风天气没有覆盖到地区的 AQI
Browse files Browse the repository at this point in the history
  • Loading branch information
Hackl0us committed Dec 28, 2020
1 parent cd717b5 commit d2f467e
Showing 1 changed file with 61 additions and 14 deletions.
75 changes: 61 additions & 14 deletions Scripts/Surge/iOS_Weather_AQI_Standard.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const aqicnToken = ''
// STEP 2: 参考下方配置片段,在代理工具的配置文件中添加对应的配置。注意:script-path 后应该替换为添加 apicnToken 值后的脚本路径
/*
[Script]
AQI-US = type=http-response, pattern=https://weather-data.apple.com/v1/weather/[\w-]+/[0-9]+\.[0-9]+/[0-9]+\.[0-9]+\?include=, requires-body=true, script-path=/path/to/iOS_Weather_AQI_Standard.js
AQI-US = type=http-response, pattern=https://weather-data.apple.com/v1/weather/[\w-]+/[0-9]+\.[0-9]+/[0-9]+\.[0-9]+\?, requires-body=true, script-path=/path/to/iOS_Weather_AQI_Standard.js
[MITM]
hostname = weather-data.apple.com
Expand All @@ -26,7 +26,7 @@ const AirQualityLevel = {
HAZARDOUS: 6
}

const coordRegex = /https:\/\/weather-data\.apple\.com\/v1\/weather\/[\w-]+\/([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\?include=/
const coordRegex = /https:\/\/weather-data\.apple\.com\/v1\/weather\/[\w-]+\/([0-9]+\.[0-9]+)\/([0-9]+\.[0-9]+)\?/
const [_, lat, lng] = $request.url.match(coordRegex)

function classifyAirQualityLevel(aqiIndex) {
Expand All @@ -48,25 +48,72 @@ function classifyAirQualityLevel(aqiIndex) {
function modifyWeatherResp(weatherRespBody, aqicnRespBody) {
let weatherRespJson = JSON.parse(weatherRespBody)
let aqicnRespJson = JSON.parse(aqicnRespBody).data
weatherRespJson.air_quality = constructAirQuailityNode(aqicnRespJson)
return JSON.stringify(weatherRespJson)
}

let aqicnIndex = aqicnRespJson.aqi
weatherRespJson.air_quality.airQualityCategoryIndex = classifyAirQualityLevel(aqicnIndex)
weatherRespJson.air_quality.airQualityIndex = aqicnIndex
weatherRespJson.air_quality.airQualityScale = AirQualityStandard.US
weatherRespJson.air_quality.learnMoreURL = aqicnRespJson.city.url + '/cn'

weatherRespJson.air_quality.metadata.provider_logo = 'https://i.loli.net/2020/12/27/UqW23eZLFAIbxGV.png'
weatherRespJson.air_quality.metadata.provider_name = 'aqicn.org'
function getPrimaryPollutant(pollutant) {
switch (pollutant) {
case 'co':
return 'CO2';
case 'so2':
return 'SO2';
case 'no2':
return 'NO2';
case 'pm25':
return 'PM2.5';
case 'pm10':
return 'PM10';
case 'o3':
return 'OZONE';
default:
return "OTHER";
}
}

weatherRespJson.air_quality.primaryPollutant = aqicnRespJson.dominentpol
weatherRespJson.air_quality.source = aqicnRespJson.city.name
function constructAirQuailityNode(aqicnData) {
let airQualityNode = { "source": "", "learnMoreURL": "", "isSignificant": true, "airQualityCategoryIndex": 1, "airQualityScale": "", "airQualityIndex": 0, "pollutants": { "CO": { "name": "CO", "amount": 0, "unit": "μg/m3" }, "SO2": { "name": "SO2", "amount": 0, "unit": "μg/m3" }, "NO2": { "name": "NO2", "amount": 0, "unit": "μg/m3" }, "PM2.5": { "name": "PM2.5", "amount": 0, "unit": "μg/m3" }, "OZONE": { "name": "OZONE", "amount": 0, "unit": "μg/m3" }, "PM10": { "name": "PM10", "amount": 0, "unit": "μg/m3" } }, "metadata": { "reported_time": 0, "longitude": 0, "provider_name": "aqicn.org", "expire_time": 2, "provider_logo": "https://i.loli.net/2020/12/27/UqW23eZLFAIbxGV.png", "read_time": 2, "latitude": 0, "v": 1, "language": "", "data_source": 0 }, "name": "AirQuality", "primaryPollutant": "" }
const aqicnIndex = aqicnData.aqi
airQualityNode.source = aqicnData.city.name
airQualityNode.learnMoreURL = aqicnData.city.url + '/cn'
airQualityNode.airQualityCategoryIndex = classifyAirQualityLevel(aqicnIndex)
airQualityNode.airQualityScale = AirQualityStandard.US
airQualityNode.airQualityIndex = aqicnIndex
airQualityNode.pollutants.CO.amount = aqicnData.iaqi.co.v
airQualityNode.pollutants.SO2.amount = aqicnData.iaqi.so2.v
airQualityNode.pollutants.NO2.amount = aqicnData.iaqi.no2.v
airQualityNode.pollutants["PM2.5"].amount = aqicnData.iaqi.pm25.v
airQualityNode.pollutants.OZONE.amount = aqicnData.iaqi.o3.v
airQualityNode.pollutants.PM10.amount = aqicnData.iaqi.pm10.v
airQualityNode.metadata.latitude = aqicnData.city.geo[0]
airQualityNode.metadata.longitude = aqicnData.city.geo[1]
airQualityNode.metadata.read_time = roundHours(new Date(), 'down')
airQualityNode.metadata.expire_time = roundHours(new Date(), 'up')
airQualityNode.metadata.reported_time = aqicnData.time.v
//airQualityNode.metadata.language = $request.headers['Accept-Language']
airQualityNode.primaryPollutant = getPrimaryPollutant(aqicnData.dominentpol)
return airQualityNode
}

return JSON.stringify(weatherRespJson)
function roundHours(time, method) {
switch (method) {
case 'up':
time.setHours(time.getHours() + Math.ceil(time.getMinutes() / 60));
break;
case 'down':
time.setHours(time.getHours() + Math.floor(time.getMinutes() / 60));
break;
default:
console.log("Error rounding method");
}
time.setMinutes(2, 0, 0);
return time;
}

$httpClient.get(`https://api.waqi.info/feed/geo:${lat};${lng}/?token=${aqicnToken}`, function (error, _response, data) {
if (error) {
$done({})
let body = $response.body
$done({ body })
} else {
let body = modifyWeatherResp($response.body, data)
$done({ body })
Expand Down

0 comments on commit d2f467e

Please sign in to comment.