| | |
| | | var jsonData = '';//存储天气信息
|
| | | // 定义规则
|
| | | let rule = new schedule.RecurrenceRule();
|
| | | rule.minute = [1,6,11,16,21,26,31,36,41,46,51,56]
|
| | | rule.minute = [15,30,45,55]
|
| | | //rule.second = [30, 60]; // 每隔 10 秒执行一次
|
| | |
|
| | |
|
| | |
| | |
|
| | | schedule.scheduleJob(rule, async () => {//每小时定时取天气数据
|
| | | //https://devapi.heweather.net/v7/weather/now?location=101021700&key=495b433d21c84986a99c4ac52d42fd3b
|
| | | let urlstr = "https://devapi.heweather.net/v7/weather/now?location=location_code&key=495b433d21c84986a99c4ac52d42fd3b";
|
| | | let urlstr = "https://devapi.heweather.net/v7/weather/now?location=location_code&key=fca708c817034474b6969579ab61a380";
|
| | | sqlstr = `select * from t_s_weather_city order by code`;
|
| | | let data_rets = await mysql.query_data(sqlstr, 'master', null)
|
| | | for (let index = 0; index < data_rets.length; index++) {
|
| | |
| | | // 检测当前是否已有天气数据
|
| | | let sqlstr = `select count(1) as counter from t_d_weather_hour where citycode='`+element.code+`' and TO_DAYS(now())=TO_DAYS(posttime) and HOUR(now())=hour(posttime) `;
|
| | | let lastdata_times = await mysql.query_data(sqlstr, 'master', null);
|
| | | if( lastdata_times[0].counter > 0 ) { continue ;} |
| | | console.log(lastdata_times[0].counter);
|
| | | console.log(lastdata_times[0].counter > 0)
|
| | | if( lastdata_times[0].counter > 0 ) { continue ;}
|
| | | // 若当前无天气数据,重新抓取
|
| | | axios.get(urlstr.replace('location_code', element.code)).then(response => {
|
| | | console.log(response.data)
|
| | | if(response.data.now) {
|
| | | let w = response.data.now;
|
| | | let posttime_ = w.obsTime.replace('T',' ').substr(0,13)+ ':00:00';
|
| | | let sql_ = `insert into t_d_weather_hour (type,citycode,cityname,posttime,temp,feelsLike,icon,text,wind360,windDir,windScale,windSpeed,humidity,precip,pressure,vis,cloud,dew,responsedata) |
| | | values ('${element.type}','${element.code}','${element.name}','${posttime_}','${w.temp}','${w.feelsLike}','${w.icon}','${w.text}','${w.wind360}',
|
| | | '${w.windDir}','${w.windScale}','${w.windSpeed}','${w.humidity}','${w.precip}','${w.pressure}','${w.vis}','${w.cloud}','${w.dew}','${JSON.stringify(response.data)}'
|
| | | )`;
|
| | | mysql.query_data(sql_, 'master', null)
|
| | | let currHours = (new Date()).getHours();
|
| | | let postHours = (new Date(posttime_)).getHours();
|
| | | if(postHours == currHours) {
|
| | | let sql_ = `insert into t_d_weather_hour (type,citycode,cityname,posttime,temp,feelsLike,icon,text,wind360,windDir,windScale,windSpeed,humidity,precip,pressure,vis,cloud,dew,responsedata) |
| | | values ('${element.type}','${element.code}','${element.name}','${posttime_}','${w.temp}','${w.feelsLike}','${w.icon}','${w.text}','${w.wind360}',
|
| | | '${w.windDir}','${w.windScale}','${w.windSpeed}','${w.humidity}','${w.precip}','${w.pressure}','${w.vis}','${w.cloud}','${w.dew}','${JSON.stringify(response.data)}'
|
| | | )`;
|
| | | mysql.query_data(sql_, 'master', null)
|
| | | }
|
| | | }
|
| | | }).catch(error => {
|
| | | console.log(error);
|
| | |
| | | schedule.scheduleJob(rule, async () => {//每天定时更新天气数据
|
| | | //https://devapi.qweather.com/v7/weather/3d?location=101021700&key=495b433d21c84986a99c4ac52d42fd3b
|
| | |
|
| | | let urlstr = "https://devapi.qweather.com/v7/weather/3d?location=location_code&key=495b433d21c84986a99c4ac52d42fd3b";
|
| | | let urlstr = "https://devapi.qweather.com/v7/weather/3d?location=location_code&key=fca708c817034474b6969579ab61a380";
|
| | | sqlstr = `select * from t_s_weather_city order by code`;
|
| | | let data_rets = await mysql.query_data(sqlstr, 'master', null)
|
| | | for (let index = 0; index < data_rets.length; index++) {
|
| | |
| | | if(response.data.daily) {
|
| | | let w = response.data.daily[0];
|
| | | let posttime_ = w.fxDate+ ' 00:00:00';
|
| | | let sql_ = `insert into t_d_weather_day (type,citycode,cityname,posttime,iconNight,iconDay,humidity,precip,pressure,vis,cloud,
|
| | | moonPhase,moonPhaseIcon,moonrise,moonset,sunrise,sunset,tempMax,tempMin,textDay,textNight,uvIndex,
|
| | | wind360Day,windDirDay,wind360Night,windDirNight,windScaleDay,windScaleNight,windSpeedDay,windSpeedNight,responsedata) |
| | | values ('${element.type}','${element.code}','${element.name}','${posttime_}','${w.iconNight}','${w.iconDay}','${w.humidity}','${w.precip}','${w.pressure}','${w.vis}',
|
| | | '${w.cloud}','${w.moonPhase}','${w.moonPhaseIcon}','${w.moonrise}','${w.moonset}','${w.sunrise}','${w.sunset}','${w.tempMax}','${w.tempMin}','${w.textDay}','${w.textNight}','${w.uvIndex}',
|
| | | '${w.wind360Day}','${w.windDirDay}','${w.wind360Night}','${w.windDirNight}','${w.windScaleDay}','${w.windScaleNight}','${w.windSpeedDay}','${w.windSpeedNight}',
|
| | | '${JSON.stringify(response.data)}'
|
| | | )`;
|
| | | mysql.query_data(sql_, 'master', null)
|
| | | let currdate = (new Date()).getDate();
|
| | | let postdate = (new Date(posttime_)).getDate();
|
| | | if (currdate == postdate) { |
| | | let sql_ = `insert into t_d_weather_day (type,citycode,cityname,posttime,iconNight,iconDay,humidity,precip,pressure,vis,cloud,
|
| | | moonPhase,moonPhaseIcon,moonrise,moonset,sunrise,sunset,tempMax,tempMin,textDay,textNight,uvIndex,
|
| | | wind360Day,windDirDay,wind360Night,windDirNight,windScaleDay,windScaleNight,windSpeedDay,windSpeedNight,responsedata) |
| | | values ('${element.type}','${element.code}','${element.name}','${posttime_}','${w.iconNight}','${w.iconDay}','${w.humidity}','${w.precip}','${w.pressure}','${w.vis}',
|
| | | '${w.cloud}','${w.moonPhase}','${w.moonPhaseIcon}','${w.moonrise}','${w.moonset}','${w.sunrise}','${w.sunset}','${w.tempMax}','${w.tempMin}','${w.textDay}','${w.textNight}','${w.uvIndex}',
|
| | | '${w.wind360Day}','${w.windDirDay}','${w.wind360Night}','${w.windDirNight}','${w.windScaleDay}','${w.windScaleNight}','${w.windSpeedDay}','${w.windSpeedNight}',
|
| | | '${JSON.stringify(response.data)}'
|
| | | )`;
|
| | | mysql.query_data(sql_, 'master', null)
|
| | | }
|
| | | }
|
| | | }).catch(error => {
|
| | | console.log(error);
|