UNPKG

sstapi

Version:
305 lines (276 loc) 9.14 kB
(function() { var crypto, fs, request, sstapi, sstutil, util; util = require('util'); request = require('superagent'); fs = require('fs'); crypto = require('crypto'); sstutil = require('./util'); sstapi = (function() { function sstapi(devicename) { this.devicename = devicename; this.version_name = '5.3.1'; this.platform = 'android'; this.apiUrl = 'http://diantai.ifeng.com/api/sstApi.php'; this.headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'User-Agent': 'Apache-HttpClient/UNAVAILABLE (java 1.4)' }; this.request = request; this.formSignture = function(form, signlist) { var common_key, i, preText, rtime, sign, signTemplate, signText, _i, _len; common_key = 'sstifengcom'; signTemplate = 'rtime=%splatform=%sv=%s%s'; preText = ''; for (_i = 0, _len = signlist.length; _i < _len; _i++) { i = signlist[_i]; preText += "" + i + "=" + form[i]; } rtime = new Date().getTime(); signText = preText + util.format(signTemplate, rtime, this.platform, this.version_name, common_key); sign = crypto.createHash('md5').update(signText).digest('hex'); form.platform = this.platform; form.v = this.version_name; form.rtime = rtime; form.sign = sign; return form; }; this.postRequest = function(action, form, signlist, callback) { form = this.formSignture(form, signlist); return request.post(this.apiUrl).set(this.headers).query({ "action": "action", action: action }).send(form).end(this.callbackWrap(callback)); }; this.defaultCallback = function(bodyObj) { return fs.writeFile('./_lastcallback.json', JSON.stringify(bodyObj), function(err) { if (err) { return console.log(err); } else { return console.log("_lastcallback saved"); } }); }; this.callbackWrap = (function(_this) { return function(callback) { if (callback == null) { callback = _this.defaultCallback; } return function(res) { var obj; obj = {}; if (res.status === 200) { obj = JSON.parse(res.text); _this.filepathDecrypt(obj); } return callback(obj); }; }; })(this); this.filepathDecrypt = function(obj) { return sstutil.objectLoop(obj); }; } sstapi.prototype.getIndex = function(callback) { var action, form, signlist; action = 'getIndex'; form = { devicename: this.devicename }; signlist = ["devicename"]; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getNewProgramList = function(channelId, categoryid, page, perCount, callback) { var action, form, signlist; action = 'getNewProgramList5.3'; form = { devicename: this.devicename, channelid: channelId, categoryid: categoryid, page: page, percount: perCount, tags: 2 }; signlist = ["devicename"]; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getProgramAudio = function(programId, page, perCount, order, needinfo, callback) { var action, form, signlist; action = 'getProgramAudio5.3'; form = { devicename: this.devicename, programid: programId, page: page, percount: perCount, sid: '', order: order, needinfo: needinfo }; signlist = ["devicename"]; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getCategoryList = function(categoryId, page, perCount, callback) { var action, form, signlist; action = 'getCategoryList'; form = { categoryid: categoryId, page: page, percount: perCount, source: 'index' }; signlist = ['categoryid', 'page']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getAudioBooks = function(channelId, page, perCount, callback) { var action, form, signlist; action = 'getAudioBooks'; form = { channelid: channelId, page: page, percount: perCount, tags: 2 }; signlist = ['channelid', 'page', 'percount']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getOpenClassList = function(channelId, page, perCount, callback) { var action, form, signlist; action = "getOpenClassList"; form = { channelid: channelId, page: page, percount: perCount, tags: 2 }; signlist = ['channelid', 'page', 'percount']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getMusicCategory = function(categoryId, callback) { var action, form, signlist; action = "getMusicCategory"; form = { categoryid: categoryId }; signlist = ['categoryid']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getSingerList = function(categoryId, callback) { var action, form, signlist; action = "getSingerList"; form = { categoryid: categoryId }; signlist = ['categoryid']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getSongList = function(type, id, page, perCount, callback) { var action, form, signlist; action = "getSongList"; form = { type: type, id: id, page: page, percount: perCount }; signlist = ['type', 'id']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getSongByThemeId = function(themeId, callback) { var action, form, signlist; action = "getSongBythemeid"; form = { themeid: themeId }; signlist = ['themeid']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getTvList = function(type, page, perCount, callback) { var action, form, signlist; action = "getTvList"; form = { devicename: this.devicename, type: type, page: page, percount: perCount }; signlist = ['devicename']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getTvEpgList = function(tvid, callback) { var action, form, signlist; action = "getTvEpgList"; form = { devicename: this.devicename, tvid: tvid }; signlist = ['devicename']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getTvNowEpg = function(tvid, callback) { var action, form, signlist; action = "getTvNowEpg"; form = { devicename: this.devicename, tvid: tvid }; signlist = ['devicename']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getTvRelevantList = function(type, tvId, showNum, callback) { var action, form, signlist; action = "getTvRelevantList"; form = { devicename: this.devicename, type: type, tvid: tvId, shownum: showNum }; signlist = ['devicename']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getTvCatAndPos = function(callback) { var action, form, signlist; action = "getTvCatAndPos"; form = { devicename: this.devicename }; signlist = ['devicename']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getRankingByCategoryId = function(categoryId, perCount, callback) { var action, form, signlist; action = "getRankingByCategoryid"; form = { devicename: this.devicename, categoryid: categoryId, percount: perCount }; signlist = ['devicename']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getSpecialList = function(page, perCount, callback) { var action, form, signlist; action = "getSpecialList"; form = { devicename: this.devicename, page: page, percount: perCount }; signlist = ['devicename']; return this.postRequest(action, form, signlist, callback); }; sstapi.prototype.getSpecialAudioList = function(themeId, page, perCount, callback) { var action, form, signlist; action = "getSpecialAudioList"; form = { devicename: this.devicename, themeid: themeId, page: page, perCount: perCount }; signlist = ['devicename']; return this.postRequest(action, form, signlist, callback); }; return sstapi; })(); module.exports = sstapi; }).call(this);