携程火车票余票提醒插件

2017-01-11 15:24:06来源:segmentfault作者:AlfieriChou人点击

前言

年底了,好多人都没有抢到回家的火车票,在这里我这种回家的可以秀出一丝优越。喔喔~~


插件功能

获取携程火车票余票


用阿里大鱼短信提醒

运用的技术

node.js


javascript

实现

通过npm新建一个package.json文件
在需要建立这个项目的文件夹下用docs输入npm init,(因为我这安装了Git,所以我就直接用右键打开Git Bash Here,然在输入的npm init的)然后输入项目名,什么的一堆的东西,这些东西你们都是会的,我就不一一展开了。

安装相关依赖


npm install alidayu-node iconv-lite request --save


开始实现main.js
1 创建一个main.js文件
2 携程的获取火车信息是通过ajax实现的,所以这里要得到携程火车票搜索的URL,还有获取的数据信息
3 引入阿里大鱼的短信提醒
4 因为文中没有给定短信的号码,所以你应该将104行的your phone number改成你的电话号码,这样你就能接收到短信


下面就是实现后的代码


var request = require('request');
var iconv = require('iconv-lite');
var fs = require('fs');
var from = 10;
var end = 16;var App = require('alidayu-node');
var app = new App('App Key','App-Secret');var url = 'http://trains.ctrip.com/TrainBooking/Ajax/SearchListHandler.ashx?Action=getSearchList';var postData = {
"IsBus": false,
"Filter": "0",
"Catalog": "",
"IsGaoTie": false,
"IsDongChe": false,
"CatalogName": "",
"DepartureCity": "beijing",
"ArrivalCity": "changsha",
"HubCity": "",
"DepartureCityName": "北京",
"ArrivalCityName": "长沙",
"DepartureDate": "2017-01-24",
"DepartureDateReturn": "2017-01-26",
"ArrivalDate": "",
"TrainNumber": ""
};var options = {
encoding: null,
method: 'POST',
url: url,
form: {
value: JSON.stringify(postData)
},
headers: {
'Content-Type': 'application/x-www-form-urlencoded:charset=gb2312',
}
};function main() {
console.log('Search From:', new Date().toString() + '/n');
fs.appendFile('log.txt', new Date().toString() + '/n');
request.post(options, callback);
setInterval(function() {
console.log('Search From:', new Date().toString() + '/n');
fs.appendFile('log.txt', new Date().toString() + '/n');
request.post(options, callback);
}, 30000);
}function callback(error, response, body) {
console.log('/n');
var list = JSON.parse(iconv.decode(body, "gb2312")).TrainItemsList;
var newList = parseList(list);
showList(newList);
}function parseList(list) {
var newList = [];
for (var i = 0; i < list.length; i++) {
var start = Number(list[i].StratTime.split(':')[0]);
var Inventory = list[i].SeatBookingItem[0].Inventory;
if (start > from && start < end && Inventory === 0) {
newList.push(list[i]);
}
}
return newList;
}function showList(list) {
if (list.length === 0) {
console.log('No data found/n');
fs.appendFile('log.txt', 'No data found/n');
} else {
sendSMS(list);
for (var i = 0; i < list.length; i++) {
var TrainName = list[i].TrainName;
var StartStationName = list[i].StartStationName;
var EndStationName = list[i].EndStationName;
var StratTime = list[i].StratTime;
var EndTime = list[i].EndTime;
var Inventory = list[i].SeatBookingItem[0].Inventory;
var str = '车次:' + TrainName + ' 开始:' + StartStationName + ' 到达:' + EndStationName + ' 发出时间:' + StratTime + ' 到达时间:' + EndTime + ' 余票:' + Inventory + '/n';
console.log(str);
fs.appendFile('log.txt', str);
}
}
}function sendSMS(list) {
var trainNames = list[0].TrainName;
var numbers = list[0].SeatBookingItem[0].Inventory;
var message = JSON.stringify({
"name": "lrh",
"trainName": trainNames,
"number": numbers
});var smsOptions = {
sms_free_sign_name: '提示信息',
sms_param: message,
rec_num: 'your phone number',
sms_template_code: 'SMS_39010188'
};
app.smsSend(options);
}main();

项目地址:https://github.com/AlfieriCho...感兴趣的兄弟给个star~~~~

最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台