@@ -34,17 +34,19 @@ ep.prototype.get = function(action, params, cb) {
34
34
35
35
//console.log(this.options);
36
36
37
- var data = '' ;
37
+ var data = [ ] ;
38
38
var req = http . request ( this . options ) ;
39
39
req . on ( 'response' , function ( response ) {
40
40
41
41
//to avoid problems with subtitle with a chatset different than utf-8
42
- response . setEncoding ( 'binary' ) ;
43
42
44
43
response . on ( 'data' , function ( chunk ) {
45
- data += chunk ;
44
+ if ( ! ! chunk && chunk . length > 0 ) {
45
+ data . push ( chunk ) ;
46
+ }
46
47
} ) . on ( 'end' , function ( ) {
47
- cb ( null , this . statusCode , data ) ;
48
+ debugger ;
49
+ cb ( null , this . statusCode , Buffer . concat ( data ) ) ;
48
50
} ) ;
49
51
} ) . on ( 'error' , function ( e ) {
50
52
cb ( e ) ;
@@ -77,17 +79,19 @@ ep.prototype.post = function(action, file, params, cb) {
77
79
} ) . on ( 'end' , function ( ) {
78
80
cb ( null , this . statusCode , data ) ;
79
81
} ) ;
82
+ req . end ( ) ;
80
83
} ) . on ( 'error' , function ( e ) {
81
84
cb ( e ) ;
85
+ req . end ( ) ;
82
86
} ) ;
83
- req . end ( ) ;
84
87
} ;
85
88
86
89
87
90
/** protocol methods */
88
91
ep . prototype . available_languages = function ( cb ) {
89
92
this . get ( 'languages' , { } , function ( err , status , res ) {
90
93
if ( err ) return cb ( err ) ;
94
+ res = res . toString ( ) ;
91
95
92
96
if ( status == 400 ) return cb ( 'Bad Request' ) ;
93
97
@@ -101,6 +105,7 @@ ep.prototype.search_subtitles = function(hash, versions, cb){
101
105
102
106
this . get ( 'search' , params , function ( err , status , res ) {
103
107
if ( err ) return cb ( err ) ;
108
+ res = res . toString ( ) ;
104
109
105
110
if ( status == 400 ) return cb ( 'Bad Request' ) ;
106
111
if ( status == 404 ) return cb ( null , '' ) ; // no subtitle found
@@ -117,7 +122,7 @@ ep.prototype.download_subtitle = function(hash, lang, path, cb){
117
122
if ( status == 400 ) return cb ( 'Bad Request' ) ;
118
123
if ( status == 404 ) return cb ( null , '' ) ; // no subtitle found
119
124
120
- fs . writeFile ( path , res , function ( err ) {
125
+ fs . writeFile ( path , res , { encoding : 'binary' } , function ( err ) {
121
126
if ( err ) return cb ( err ) ;
122
127
123
128
cb ( null , path ) ;
0 commit comments