Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

First push

  • Loading branch information...
commit a7d524820d5d7b6a94e42f77d72a50de9d2d06bc 1 parent beb142f
flybyme authored
Showing with 193 additions and 0 deletions.
  1. +11 −0 .project
  2. +139 −0 lib/btn.js
  3. +12 −0 lib/utils.js
  4. +18 −0 package.json
  5. +13 −0 test/test.js
View
11 .project
@@ -0,0 +1,11 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<projectDescription>
+ <name>btn-api</name>
+ <comment></comment>
+ <projects>
+ </projects>
+ <buildSpec>
+ </buildSpec>
+ <natures>
+ </natures>
+</projectDescription>
View
139 lib/btn.js
@@ -0,0 +1,139 @@
+var http = require('http');
+var qs = require('querystring');
+var events = require('events');
+var util = require('util');
+/***
+ * uuid
+ */
+var uuid = require('./utils').uuid
+/***
+ * btn api client
+ */
+var Btn = module.exports = function(key) {
+ events.EventEmitter.call(this);
+ this.key = key
+
+}
+/***
+ * Make it an event
+ */
+util.inherits(Btn, events.EventEmitter);
+/***
+ *
+ */
+Btn.prototype.getChangelog = function(callBack) {
+ this.query({
+ method : 'getChangelog',
+ params : [],
+ id : uuid()
+ }, function(err, data) {
+ if (err)
+ callBack(err);
+ else
+ callBack(null, data);
+ });
+};
+
+var methods = ['userInfo', 'getChangelog', 'getNews', 'getNewsById', 'getBlog', 'getBlogById', 'getTVNews', 'getTVNewsById', 'getInbox', 'getInboxConversation', 'sendInboxConversation', 'getSchedule', 'getNewSeries', 'getTorrentsBrowse', 'getTorrentsSearch', 'getTorrentsUrl', 'getForumsIndex', 'getForumsPage', 'getUserSubscriptions', 'getUserSnatchlist']
+methods.forEach(function(method) {
+
+ Btn.prototype[method] = function() {
+ var self = this
+ var args = Array.prototype.slice.call(arguments);
+
+ var callBack = args.pop()
+ this.query({
+ method : method,
+ params : [self.key].concat(args),
+ id : uuid()
+ }, function(err, data) {
+ if (err)
+ callBack(err);
+ else
+ callBack(null, data.result);
+ });
+ }
+})
+/***
+ *
+ */
+Btn.prototype.search = function() {
+ var self = this
+ var query = {}
+ var limit = 2500
+ var result = {
+ limit : function(l) {
+ limit = l;
+ return result
+ },
+ run : function(callBack) {
+ self.query({
+ method : 'getTorrentsSearch',
+ params : [self.key, query, limit],
+ id : uuid()
+ }, function(err, data) {
+ if (err)
+ callBack(err);
+ else
+ callBack(null, data.result);
+ });
+ }
+ };
+ ['id', 'series', 'name', 'search', 'codec', 'container', 'source', 'resolution', 'origin'].forEach(function(key) {
+ result[key] = function(val) {
+ query[key] = val
+ return result
+ }
+ })
+ return result
+};
+/***
+ *
+ */
+Btn.prototype.query = function(data, callBack) {
+ data = JSON.stringify(data) + '\n'
+
+ var req = http.request({
+ host : 'api.btnapps.net',
+ port : 80,
+ method : 'POST',
+ path : '/',
+ headers : {
+ 'content-length' : data.length,
+ 'content-type' : 'application/json',
+ 'xtype' : 'node-btn'
+ }
+ }, function(res) {
+
+ res.setEncoding('utf8');
+
+ var json = [];
+
+ res.on('data', function(chunk) {
+ json.push(chunk)
+ }).on('end', function(chunk) {
+ var code = res.statusCode;
+ if (code === 200) {
+ json = JSON.parse(json.join(''))
+
+ callBack(null, json)
+ } else if (code === 400) {
+ callBack(new Error('Bad Request: invalid parameters'))
+ } else if (code === 401) {
+ callBack(new Error('Unauthorized: invalid api key'))
+ } else if (code === 404) {
+ callBack(new Error('Not Found: the method was not found'))
+ } else if (code === 503) {
+ callBack(new Error('Service Unavailable: invalid API Key, or you have hit the API too much'))
+ } else {
+ callBack(new Error('Something is wrong.'))
+ }
+ });
+ });
+
+ req.on('error', function(e) {
+ callBack(e)
+ });
+ req.write(data);
+ req.end()
+};
View
12 lib/utils.js
@@ -0,0 +1,12 @@
+/***
+ *
+ *
+ */
+var exports = module.exports = {};
+//
+var S4 = function() {
+ return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
+}
+var uuid = module.exports.uuid = function() {
+ return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
+}
View
18 package.json
@@ -0,0 +1,18 @@
+{
+ "name": "btn-api",
+ "version": "0.0.0",
+ "description": "btn-api =======",
+ "main": "index.js",
+ "directories": {
+ "test": "test"
+ },
+ "scripts": {
+ "test": "echo \"Error: no test specified\" && exit 1"
+ },
+ "repository": {
+ "type": "git",
+ "url": "https://github.com/FLYBYME/btn-api.git"
+ },
+ "author": "",
+ "license": "BSD"
+}
View
13 test/test.js
@@ -0,0 +1,13 @@
+var Btn = require('../lib/btn')
+
+var btn = new Btn('')
+
+btn.search().limit(1).container('mp4').resolution('sd').run(console.log)
+
+btn.getUserSnatchlist(50, console.log)
+
+btn.userInfo(console.log)
+
+btn.getChangelog(console.log)
+
+btn.getInbox(console.log)
Please sign in to comment.
Something went wrong with that request. Please try again.