Skip to content

Commit ca18b52

Browse files
committed
more HTTP status code check
add subtitle upload
1 parent 942fc17 commit ca18b52

File tree

4 files changed

+88
-8
lines changed

4 files changed

+88
-8
lines changed

lib/endpoints.js

Lines changed: 61 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,15 @@
77

88
var querystring = require('querystring'),
99
http = require('http'),
10-
fs = require('fs');
10+
url = require('url'),
11+
fs = require('fs'),
12+
FormData = require('form-data'),
13+
clone = require('clone');
1114

1215
var ep = module.exports = function() {
1316

1417
this.options = {
15-
//'hostname': 'sandbox.thesubdb.com',
18+
//'hostname': 'sandbox.thesubdb.com', // buggy
1619
//'hostname': 'permita.se',
1720
'hostname': 'api.thesubdb.com',
1821
'agent': false,
@@ -26,6 +29,7 @@ var ep = module.exports = function() {
2629
ep.prototype.get = function(action, params, cb) {
2730

2831
params.action = action;
32+
this.options.method = 'GET';
2933
this.options.path = "/?"+querystring.stringify(params);
3034

3135
//console.log(this.options);
@@ -43,12 +47,46 @@ ep.prototype.get = function(action, params, cb) {
4347
}).end();
4448
};
4549

50+
ep.prototype.post = function(action, file, params, cb) {
51+
52+
var opt = clone(this.options);
53+
opt.method = 'POST';
54+
opt.path = "/?"+querystring.stringify({action:action});
55+
56+
var form = new FormData();
57+
form.append('file', fs.createReadStream(file));
58+
for(var i in params) {
59+
form.append(i, params[i]);
60+
}
61+
var headers = form.getHeaders();
62+
for(var i in headers) {
63+
opt.headers[i] = headers[i];
64+
}
65+
66+
var req = http.request(opt);
67+
form.pipe(req);
68+
69+
var data = '';
70+
req.on('response', function (response) {
71+
response.on('data', function (chunk) {
72+
data += chunk;
73+
}).on('end', function(){
74+
cb(null, this.statusCode, data);
75+
});
76+
}).on('error', function(e) {
77+
cb(e);
78+
});
79+
req.end();
80+
};
81+
4682

4783
/** protocol methods */
4884
ep.prototype.available_languages = function(cb){
4985
this.get('languages', {}, function(err, status, res) {
5086
if(err) return cb(err);
5187

88+
if(status == 400) return cb('Bad Request');
89+
5290
cb(null, res.split(','));
5391
});
5492
};
@@ -60,6 +98,9 @@ ep.prototype.search_subtitles = function(hash, versions, cb){
6098
this.get('search', params, function(err, status, res){
6199
if(err) return cb(err);
62100

101+
if(status == 400) return cb('Bad Request');
102+
if(status == 404) return cb(null, ''); // no subtitle found
103+
63104
cb(null, res.split(','));
64105
});
65106
};
@@ -69,6 +110,9 @@ ep.prototype.download_subtitle = function(hash, lang, path, cb){
69110
this.get('download', params, function(err, status, res){
70111
if(err) return cb(err);
71112

113+
if(status == 400) return cb('Bad Request');
114+
if(status == 404) return cb(null, ''); // no subtitle found
115+
72116
fs.writeFile(path, res, function(err) {
73117
if(err) return cb(err);
74118

@@ -77,9 +121,22 @@ ep.prototype.download_subtitle = function(hash, lang, path, cb){
77121
});
78122
};
79123
ep.prototype.upload_subtitle = function(hash, file, cb){
80-
this.post('upload', function(err, status, res){
124+
var params = {hash:hash}
125+
this.post('upload', file, params, function(err, status, res){
81126
if(err) return cb(err);
82127

83-
cb(null, res);
128+
/*
129+
'Uploaded': (HTTP/1.1 201 Created)
130+
If everything was OK, the HTTP status code 201 will be returned.
131+
132+
*/
133+
134+
135+
if(status == 403) return cb('Forbidden : subtitle already exists');
136+
if(status == 415) return cb('Unsupported Media Type');
137+
if(status == 400) return cb('Bad Request');
138+
139+
140+
cb(null, status);
84141
});
85142
};

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@
1212
},
1313
"scripts": {
1414
"test": "mocha"
15-
}, "main": "lib/subdb.js",
15+
},"main": "lib/subdb.js",
1616
"dependencies": {
17-
17+
"form-data": "0.0.x",
18+
"clone": "0.1.x"
1819
},
1920
"devDependencies": {
20-
"mocha":"1.6.0"
21+
"mocha":"1.6.x"
2122
},
2223
"optionalDependencies": {},
2324
"engines": {

test/justified.srt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
1
2+
00:00:05,000 --> 00:00:15,000
3+
Attention: Ceci est une l�gende de test.
4+
5+
2
6+
00:00:25,000 --> 00:00:40,000
7+
SubDB - une base de donn�es libre de sous-titres.
8+
http://thesubdb.com
9+

test/test.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe('Subdb', function() {
5252
subdb.api.available_languages(function(err, res){
5353
if(err) return done(err);
5454

55-
assert.equal(res.join(','), ["af","cs","da","de","en","es","fi","fr","hu","id","it","la","nl","no","oc","pl","pt","ro","ru","sl","sr","sv","tr"].join(','));
55+
assert.equal(res.join(','), ["en","es","fr","it","nl","pl","pt","ro","sv","tr"].join(','));
5656
done();
5757
});
5858
});
@@ -89,6 +89,19 @@ describe('Subdb', function() {
8989
});
9090
});
9191
});
92+
93+
describe('#upload_subtitle', function(){
94+
it('should upload subtitle', function(done) {
95+
var path = process.cwd()+'/test/justified.srt';
96+
var subdb = new SubDb();
97+
subdb.api.upload_subtitle('edc1981d6459c6111fe36205b4aff6c2', path, function(err, res){
98+
if(err) return done(err);
99+
100+
assert.equal(res, 200);
101+
done();
102+
});
103+
});
104+
});
92105
});
93106
});
94107

0 commit comments

Comments
 (0)