花几分钟实现一个下班公交的邮件订阅吧(抓包+node.js)

前言

最近暑期实习,公司上下班比较自由不需要打卡,有时候比较轻松下午五点半就可以回家。有时候等公交时间觉得有点浪费,所以想自己实现一个程序,如果公交到站时间大概十分钟的时候自动发邮件提醒我(上班的时候手机一般开勿扰,自己设置了只有类似电话、短信、邮件才会手机提醒),这样就可以直接搜索东西回家。

实现步骤

抓包

抓包工具(ios):Steam/HTTPS抓包

试用了高德地图、百度地图、珠海公交公众号,发现高德、百度抓到的数据包加密了(小白的我还没有完全看懂hhh),珠海公交公众号直接把数据暴露了出来,因为想要节省时间快速实现,暂时采用第三种,后续有时间完善再补充。

花几分钟实现一个下班公交的邮件订阅吧(抓包+node.js)_第1张图片 花几分钟实现一个下班公交的邮件订阅吧(抓包+node.js)_第2张图片

接口测试

导出抓包的cURL并到Apifox一键导入生成快捷测试接口

花几分钟实现一个下班公交的邮件订阅吧(抓包+node.js)_第3张图片

顺便生成axios请求代码

花几分钟实现一个下班公交的邮件订阅吧(抓包+node.js)_第4张图片
const axios = require('axios');
const positionData = { "subrouteid": xxx, "segmentid": xxx, "stationname": "xxx" };
const config = {method: 'post',url: 'https://xxx',headers: {'User-Agent': 'apifox/1.0.0 (https://www.apifox.cn)','content-type': 'application/json'},data: positionData
};
axios(config).then(function (response) {}).catch(function (error) {console.log(error);}); 

创建node服务

npm init -y
//创建index.js
npm install axios 

载入自动发送邮件功能

//https://www.npmjs.com/package/nodemailer
npm install nodemailer 

编写mail函数

const nodemailer = require('nodemailer')
function mail(diff, time) {const transporter = nodemailer.createTransport({service: 'qq', port: 465, secureConnection: true, auth: {user: '[email protected]',pass: 'xxx',},})const mailOptions = {from: '[email protected]', to: '[email protected]', subject: 'xxx', html: `

当前K3距离港湾一号有

${diff + 1}站

预计到达时间

${time}分钟`, }transporter.sendMail(mailOptions, (error, info) => {if (error) {return console.log(error)}console.log(`发送邮件成功`)}) }

载入node定时任务

//https://github.com/node-schedule/node-schedule
npm install node-schedule 
const schedule = require('node-schedule');
const rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [ 1, 2, 3, 4, 5]
rule.hour = 17
rule.minute = [new schedule.Range(30,59)]
rule.second = [new schedule.Range(30,40)];
const job = schedule.scheduleJob(rule, () => {getBusInfo()
})
if (nowTime == '00:00:00') {job.cancel({ reschedule: true })
} 

最后

这样就一个下班回家公交订阅就完成了,快速实现的东西写得不太好,但是足够使用了,非常方便。

花几分钟实现一个下班公交的邮件订阅吧(抓包+node.js)_第5张图片

代码

const axios = require('axios');
const nodemailer = require('nodemailer')
const schedule = require('node-schedule');

const positionData = { "subrouteid": 参数, "segmentid": 参数, "stationname": "参数" };
const config = {method: 'post',url: '请求地址',headers: {'User-Agent': 'apifox/1.0.0 (https://www.apifox.cn)','content-type': 'application/json'},data: positionData
};

const nowHours = new Date().getHours()
const nowMinutes = new Date().getMinutes()
const nowSeconds = new Date().getSeconds()
const nowTime = `${nowHours}:${nowMinutes}:${nowSeconds}`

function getBusInfo() {axios(config).then(function (response) {const { diff, time } = response.data.dataif (diff == 5 || time == 10) {mail(diff, time)}}).catch(function (error) {console.log(error);});
}

function mail(diff, time) {const transporter = nodemailer.createTransport({service: 'qq', port: 465, secureConnection: true, auth: {user: '邮箱',pass: '授权码',},})const mailOptions = {from: '', to: '', subject: '', html: ``,}transporter.sendMail(mailOptions, (error, info) => {if (error) {return console.log(error)}console.log(`发送邮件成功`)})
}

const rule = new schedule.RecurrenceRule();
rule.dayOfWeek = [ 1, 2, 3, 4, 5]
rule.hour = 17
rule.minute = [new schedule.Range(30,59)]
rule.second = [new schedule.Range(30,40)];

const job = schedule.scheduleJob(rule, () => {getBusInfo()
})

if (nowTime == '00:00:00') {job.cancel({ reschedule: true })
} 

你可能感兴趣的:(node.js,前端,javascript)