Skip to content

Instantly share code, notes, and snippets.

@flyingnn
Forked from mashihua/README.md
Created January 11, 2012 13:26
Show Gist options
  • Save flyingnn/1594639 to your computer and use it in GitHub Desktop.
Save flyingnn/1594639 to your computer and use it in GitHub Desktop.
Query tickets in china

Run it your own risk.

Query tickets in china

How to

  1. Copy ticket.js and run it on your browser
  2. Install node.js
  3. Put package.json and server.js into a folder
  4. Copy a mp3 to the same forder and rename it to "alert.mp3"
  5. Open terminal and install dependences by command "npm install"
  6. Aftering finished, type command "npm start"

Enjoy it :-)

{
"name" : "local-notification",
"version" : "0.0.3",
"description" : "local notification through growl",
"author" : "Shihua Ma",
"engines" : {
"node" : ">=0.6"
},
"scripts" : {
"start" : "node server.js > /dev/null &"
},
"dependencies" : {
"growl" : "1.x.x",
"email" : ">=0.2.4",
"express": ">=2.4"
}
}
#!/usr/bin/env node
var exec = require('child_process').exec;
var express = require('express');
var growl = require('growl');
var Email = require('email').Email;
var app = express.createServer();
var child;
app.get('/ticket', function(req, res){
//if you have growl software
growl.notify('Congratulation. You got tickets.' , { title: 'Hurry up',sticky: true });
child = exec('open '+ __dirname + '/alert.mp3',function(error, stdout, stderr){});
var mail = new Email({
from : 'ticket@12306.cn',
to: "to@me.com",
subject: "Order ticket",
body: "Congratulation. You got tickets."
});
mail.send();
res.send('Send ticket information');
});
app.listen(8080);
/*
this is a client run in browser
*/
(function(){
//ticket query days
var due = ['2012-01-15','2012-01-16','2012-01-17','2012-01-18','2012-01-19'];
//form station
var from = '北京西';
//to station
var to = '陇西';
//the train number.if set null,query all matched train
var train = null;
//how many tick
var many = 1;
//notification url
var url = 'http://localhost:8080/ticket';
//query durition
var durition = 30;
//form soft berth, superior soft berth is 8;
var form_set = 9;
//to hard berth
var to_set = 11;
//submit the order form
var submited = true;
window.sid = null;
var win = document.getElementsByTagName('iframe')[0].contentWindow;
var days = function (){
var i = 0;
return function(){
var val = due[i++];
if(!val){
i = 0 ;
val = due[i++];
}
return val;
}
}();
Date.prototype.availabe = function(){
var hours = this.getHours(),
minute = this.getMinutes();
if(hours === 6 ){
return minute > 5;
}
return hours >= 6 && hours < 23
};
setup();
dispatch();
//setup input
function setup(){
win.$.each(win.array_cities,function(i,v){
if(v[1] === from){
win.$('#fromStation').val(v[2]);
}
if(v[1] === to){
win.$('#toStation').val(v[2]);
}
});
win.$('#fromStationText').val(from);
win.$('#toStationText').val(to);
train && win.$('#trainCodeText').val(train.toUpperCase());
}
//for safira
if(win.$.browser.webkit){
win.WdatePicker=function(){};
win.$("#startdatepicker").attr('readonly','');
}
var _load = win.removeLoadMsg;
win.removeLoadMsg = function(){
_load();
var trs = win.$('#gridbox table.obj tr:gt(0)');
var get = false;
//for each row
trs.each(function(i,tr){
tr = $(tr);
var eles = tr.children().slice(9,11);
var train = tr.children().slice(0,1).text()
var input = tr.find('input');
eles.each(function(ind,ele){
//we have get ticket.
if(get){
return;
}
var text = $(ele).text();
if( ~text.indexOf('有') || parseInt(text) >= many ){
if(submited) {
get = true;
};
//send notification
if(url){
var img = new Image;
img.src = url + '?submit=' + (submited ? 1 : 0) + '&train='+ train + '&many=' + text;
}
submited && input.click();
}
});
});
!get && (window.sid = setTimeout(dispatch , durition * 1000));
}
function _method(){
//'confirmPassengerAction.do';
return win.location.href.indexOf('querySingleAction.do') ?
'query' :
'order';
};
function dispatch(){
if(!(new Date).availabe()){
var img = new Image;
img.src = 'https://dynamic.12306.cn/otsweb/';
window.sid = setTimeout(dispatch, 3 * 60 * 1000);
}else{
var method = _method();
if(method === 'query'){
setup();
query()
}else{
order()
}
}
};
function order(){
//win.$('#showPassengerFilter input').click();
};
function query(){
//setup start day.
win.$("#startdatepicker").val(days());
win.$('#submitQuery').click();
};
//remove annoying alert dialog
String.prototype.replaceAll = win.String.prototype.replaceAll;
win.loadData = function() {
var gridbox = win.$("#gridbox");
win.showLoadMsg(gridbox);
//异步请求站名
win.$.ajax( {
url : win.ctx+'/order/querySingleAction.do?method=queryLeftTicket',
type : "GET",
dataType:'text',
data:{
'orderRequest.train_date' : win.$('#startdatepicker').val(),
'orderRequest.from_station_telecode' : win.$('#fromStation').val(),
'orderRequest.to_station_telecode' : win.$('#toStation').val(),
'orderRequest.train_no' : win.$('#trainCode').val(),
'trainPassType' : win.getTrainPassType(),
'trainClass' : win.getTrainClassString(),
'includeStudent' : win.getIncludeStudent(),
'seatTypeAndNum' : win.getSeanTypeAndNum(),
'orderRequest.start_time_str' : win.$('#startTime').val()
},
success : function(data, textStatus) {
if(data == "-10"){
console && console.log('recive redirect');
return;
}
if(data == "-1") {
console && console.log('server busy.');
data = "";
} else if(data !="undefine" &&data.split(",")[0]=="-2"){
alert(data.split(",")[1]);
data="";
}else {
data=data.replaceAll("\\\\n",String.fromCharCode(10));
}
win.mygrid.clearAll();
win.mygrid.startFastOperations();
win.mygrid.parse(data,"csv");
win.mygrid.stopFastOperations();
win.dealwithQueryInfo(win.mygrid);
win.removeLoadMsg();
},
error : function(e) {
console && console.error('loading error');
win.removeLoadMsg();
win.validQueryButton();
if(win.isStudentTicketDateValid()){
win.stu_validQueryButton();
}
if(clickBuyStudentTicket=='N'){
renameButton('research_u');
}else{
stu_renameButton('research_u');
}
}
});
}
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment