77
88var  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
1215var  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() {
2629ep . 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 */ 
4884ep . 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} ; 
79123ep . 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} ; 
0 commit comments