@@ -21,68 +21,12 @@ var fourChanService = {
21
21
categories . splice ( - 23 , 23 ) ;
22
22
callback ( categories ) ;
23
23
} ) ;
24
-
25
-
26
- /*var noodle = require('noodlejs');
27
- noodle.query({
28
- url: 'http://www.4chan.org/',
29
- type: 'html',
30
- selector: 'a.boardlink',
31
- extract: 'href'
32
- })
33
- .then(function (data) {
34
- console.log(typeof(data.results));
35
- //data.results.splice(3 , 5, 'hola' );
36
- console.log('========');
37
- console.log(data.results);
38
- console.log('========');
39
- var keys = Object.keys(data.results);
40
- console.log(keys);
41
- });*/
42
-
43
-
44
- /*var Xray = require('x-ray');
45
- var x = Xray();
46
-
47
- x('https://4chan.org/', {title:'.boardlink'})
48
- (function (err, result) {
49
- console.log(result)
50
- });*/
51
-
52
- /*[{
53
- 'title': '.boardlink a@title ',
54
- 'href': '.boardlink a@href',
55
- }]*/
56
-
57
-
58
- /* var request = require("request");
59
- var cheerio = require("cheerio");
60
- var url = 'http://4chan.org/';
61
-
62
- request(url, function (error, response, body) {
63
- if (!error) {
64
- var $ = cheerio.load(body),
65
- temperature = $('.boardlink').html();
66
-
67
- console.log("It’s " + temperature + " degrees Fahrenheit.");
68
- } else {
69
- console.log("We’ve encountered an error: " + error);
70
- }
71
- });*/
72
-
73
- /*console.log('this.categories');
74
- console.log(this.categories);
75
- var data = {
76
- categories : ['uno', 'dos']
77
- };
78
- return data;*/
79
24
} ,
80
25
81
26
getBoard : function ( boardName , callback ) {
82
27
83
28
// Proccess to scrap
84
29
var osmosis = require ( 'osmosis' ) ;
85
- console . log ( 'http://boards.4chan.org/' + boardName + '/catalog' ) ;
86
30
osmosis . get ( 'http://boards.4chan.org/' + boardName + '/catalog' )
87
31
. set ( {
88
32
'threads' : [ 'script' ]
@@ -127,6 +71,7 @@ var fourChanService = {
127
71
getThread : function ( boardName , id , surl , callback ) {
128
72
129
73
var self = this ; // Saving the reference to the main object
74
+
130
75
// Proccess to scrap
131
76
var osmosis = require ( 'osmosis' ) ;
132
77
var url = 'http://boards.4chan.org/' + boardName + '/thread/' + id + '/' + surl ;
@@ -137,6 +82,7 @@ var fourChanService = {
137
82
. data ( function ( results ) {
138
83
139
84
var resources = results . resources ;
85
+ console . log ( 'There going to be downloaded ' + resources . length + ' resources.' ) ;
140
86
for ( var cont = 0 ; cont < resources . length ; cont ++ )
141
87
{
142
88
var url = 'http:' + resources [ cont ] ;
@@ -148,17 +94,11 @@ var fourChanService = {
148
94
// We set the full path '.files/boardName/semanticURL/filename'
149
95
var path = './files/' + boardName + '/' + surl + '/' + filename ;
150
96
151
- console . log ( url + '::' + path ) ;
152
-
153
97
// We download the file
98
+ var fileCounter = cont + 1 ;
154
99
self . dowloadResource ( url , boardName , surl , path , function ( ) {
155
- console . log ( ' File downloaded') ;
100
+ console . log ( fileCounter + ':: File ' + path + ' downloaded') ;
156
101
} ) ;
157
-
158
- /*
159
- self.dowloadResource(url, path, function () {
160
- console.log('Dowload completed');
161
- });*/
162
102
}
163
103
// After all, we call the callback
164
104
callback ( resources ) ;
@@ -178,22 +118,38 @@ var fourChanService = {
178
118
// We check for the directories and we create them if they dont exists
179
119
var boardDirectory = './files/' + boardName
180
120
var surlDirectory = './files/' + boardName + '/' + surl ;
181
- if ( ! fs . existsSync ( boardDirectory ) ) {
182
-
121
+ if ( ! fs . existsSync ( boardDirectory ) ) {
122
+
183
123
fs . mkdirSync ( boardDirectory ) ;
124
+ }
125
+ else {
126
+
184
127
if ( ! fs . existsSync ( surlDirectory ) ) {
185
-
186
128
fs . mkdirSync ( surlDirectory ) ;
187
129
}
188
130
}
189
131
190
- // We download the file
191
- console . log ( '=== Dowloading::' + uri ) ;
192
- request . head ( uri , function ( err , res , body ) {
193
- // console.log('content-type:', res.headers['content-type']);
194
- // console.log('content-length:', res.headers['content-length']);
195
- request ( uri ) . pipe ( fs . createWriteStream ( filename ) ) . on ( 'close' , callback ) ;
196
- } ) ;
132
+ if ( ! fs . existsSync ( filename ) )
133
+ {
134
+ // We download the file
135
+ console . log ( '=== Dowloading::' + uri ) ;
136
+ /*request.head(uri, function(err, res, body) {
137
+ // console.log('content-type:', res.headers['content-type']);
138
+ // console.log('content-length:', res.headers['content-length']);
139
+ request(uri).pipe(fs.createWriteStream(filename)).on('close', callback);
140
+ });*/
141
+
142
+ request ( uri )
143
+ . pipe ( fs . createWriteStream ( filename ) )
144
+ . on ( 'close' , function ( ) {
145
+ console . log ( filename + ' downloaded.' ) ;
146
+ } )
147
+ . on ( 'error' , function ( err ) {
148
+ console . log ( err )
149
+ } ) ;
150
+
151
+ }
152
+
197
153
}
198
154
199
155
0 commit comments