forked from DemocracyOS/bill-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetPagesData.js
162 lines (147 loc) · 4.34 KB
/
getPagesData.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var http = require("http");
var util = require("util");
var fs = require('fs')
var Entity;
var argv = require('optimist')
.usage(
'Usage1: $0 -t json_structure -h hostname [-p path] [-s start_index] [-e end_index]\n'+
'\n'+
'')
.default({ p : '', s : 0 })
.demand(['t'])
.demand(['h'])
.demand(['d'])
.demand(['b'])
.describe('t', 'Json file with the structure to parse (ie.: ./config.json)')
.describe('d', 'Database name (ie.: scrapper)')
.describe('b', 'DB Schema to write to (ie.: laws)')
.describe('h', 'Hostname to connect to (ie.: www.google.com)')
.describe('p', 'Path in the host, include "#1" to replace for an index (ie.: /docs/law#1.html)')
.describe('s', 'Starting index for auto generated urls (ie.: 200)')
.describe('e', 'End index for auto generated urls (ie.: 210)')
.describe('o', 'DB Schema to read links from (ie: links)')
.describe('a', 'DB attribute to read links from (ie: url)')
.argv;
var structure = undefined;
var mongoose = undefined;
var db= undefined;
var linkList;
main();
function connectToDB(schema){
mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/'+argv.d);
createEntity(schema);
db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function() {
processLinks(argv.s);
});
}
function createEntity(schemaLocal){
var entityTemp=JSON.parse(schemaLocal);
var entitySchema = mongoose.Schema(entityTemp);
Entity = mongoose.model(argv.b, entitySchema);
}
function writeEntity(dataToSave){
var entityInstance = new Entity(JSON.parse(dataToSave));
entityInstance.save(function(){console.log('saved')});
}
function main(){
checkParams();
structure = readStructure();
var schema = createDBSchema(structure);
connectToDB(schema);
}
function checkParams(){
//ToDo: when parameters logic is more complex, add here a validation logic to tell the user
//what's wrong with them and to check consistency
}
function createDBSchema(sourceStructure){
var jsonSchema="{";
var isFirst=true;
for(token in sourceStructure.structure){
if(isFirst){
isFirst=false;
}else{
jsonSchema+=",";
}
if(structure.structure[token].array=="true"){
jsonSchema+='"'+sourceStructure.structure[token].dbField+'"'+":";
jsonSchema+="["+'"'+sourceStructure.structure[token].dbFieldType+'"'+"]";
}else{
jsonSchema+='"'+sourceStructure.structure[token].dbField+'"'+":";
jsonSchema+='"'+sourceStructure.structure[token].dbFieldType+'"';
}
}
jsonSchema+="}";
return jsonSchema;
}
function readStructure(){
return require(argv.t);
}
function processLinks(lastUsedIndex){
if(argv.p){
if(lastUsedIndex>argv.e){
return;
}else{
var newPathname=argv.p.replace("\#1", lastUsedIndex);
var thisLink=argv.h+newPathname;
console.log("processing: "+thisLink);
lastUsedIndex++;
traerPag(argv.h, newPathname, lastUsedIndex);
}
}else{
if(linkList){
traerPag(argv.h, linkList[lastUsedIndex], lastUsedIndex);
}else{
readLinksFromDB();
}
}
}
function createiInputEntity(){
var schemaLocal='{"'+argv.a+'"}';
var entityTemp=JSON.parse(schemaLocal);
var entitySchema = mongoose.Schema(entityTemp);
inputEntity = mongoose.model(argv.o, entitySchema);
}
function readLinksFromDB(){
var originCollection=mongoose.db(argv.a);
var linkList=originCollection.find();
for(linkI in linkList){
console.log(linkList[linkI]);
}
}
function traerPag(host, path, lastUsedIndex){
var options = {
host: host,
port: 80,
path: path
};
var content="";
http.get(options, function(res) {
return manageHttpResponse(res, lastUsedIndex, content);
}).on('error', function(e) {
console.log("Got error: " + e.message);
});
}
function manageHttpResponse(res, lastUsedIndex, content){
res.setEncoding("utf8");
res.on("data", function (chunk) {
content += chunk;
});
res.on("end", function (sss) {
if(res.statusCode==200){
var localContent=getJsonFromString(content);
if(localContent instanceof Array){
for(var cont in localContent){
writeEntity(localContent[cont]);
}
}else{
writeEntity(localContent);
}
processLinks(lastUsedIndex);
}else{
console.log("Page not found");
}
});
}