-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
216 lines (200 loc) · 8.93 KB
/
index.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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
require("dotenv").config();
const cookie = process.env.COOKIE;
const pet_base_url = process.env.PETBASEURL;
process.env["NODE_TLS_REJECT_UNAUTHORIZED"] = "0";
const axios = require("axios");
const cheerio = require("cheerio");
const mongoose = require("mongoose");
mongoose.connect(
"mongodb+srv://stl:stl@cluster0-p8kcd.mongodb.net/savethislife?authSource=admin&replicaSet=Cluster0-shard-0&readPreference=primary&appname=MongoDB%20Compass%20Community&ssl=true",
{
useNewUrlParser: true,
useUnifiedTopology: true,
}
);
mongoose.set("useFindAndModify", false);
const PetImportSchema = require("./models/pet.model");
const OwnerImportSchema = require("./models/owner.model");
const PhotoImportSchema = require("./models/photo.model");
const connection = mongoose.connection;
connection.once("open", function () {
console.log("MongoDB Database connection established Successfully.");
for (let thread = 1; thread < max_thread_num + 1; thread++) {
start_pet_import(thread);
}
});
const debug = require("./log");
const max_thread_num = 30;
const max_pet_id = 1100000;
async function start_pet_import(thread) {
await debug.log(thread, "starting thread " + thread);
for (let index = thread; index < max_pet_id; index += max_thread_num) {
const process = index;
try {
await debug.log(thread, "sending request to page " + process);
const result = await axios.get(pet_base_url + process, {
headers: {
"content-type": "application/x-www-form-urlencoded",
Cookie: cookie,
},
});
await debug.log(thread, "received data from page " + process);
const $ = cheerio.load(result.data);
const data = {
microchip: $(
"#pet-form > div > .panel-body > div:nth-child(2) > .panel-body > div:nth-child(3) > span"
).text(),
registered_at: $(
"#pet-form > div > .panel-body > div:nth-child(2) > .panel-body > div:nth-child(5) > span"
).text(),
petName: $("#petname").attr("value"),
petSpecies: $("select[name='species'] option:selected").attr(
"value"
),
petBreed: $("#breed").attr("value"),
petColor: $("input[name='color']").attr("value"),
petGender: $("select[name='gender'] option:selected").attr(
"value"
),
petBirth: $("input[name='birthdate']").attr("value"),
specialNeeds: $("textarea[name='needs']").text(),
vetInfo: $("textarea[name='veterinary']").text(),
dateRV: $("input[name='datevacc']").attr("value"),
implantedCompany: $("input[name='purchasedfrom']").attr(
"value"
),
email: $("input[name='owneremail']").attr("value"),
membership: "platinum",
ownerName: $("input[name='ownername']").attr("value"),
ownerPhone1: $("input[name='cellphone']").attr("value"),
ownerPhone2: $("input[name='secondphone']").attr("value"),
ownerPhone3: $("input[name='addphone0']").attr("value"),
ownerPhone4: $("input[name='addphone1']").attr("value"),
ownerPhone5: $("input[name='addphone2']").attr("value"),
ownerPhone6: $("input[name='addphone3']").attr("value"),
ownerPhone7: $("input[name='addphone4']").attr("value"),
ownerAddress1: $("input[name='address']").attr("value"),
ownerAddress2: "",
ownerCity: $("input[name='city']").attr("value"),
ownerState: $("input[name='state']").attr("value"),
ownerZip: $("input[name='zipcode']").attr("value"),
ownerCountry: $("select[name='country'] option:selected").attr(
"value"
),
ownerSecContact: $("input[name='secondcontact']").attr("value"),
ownerNote: "",
petMicrochip: $(
"#pet-form > div > .panel-body > div:nth-child(2) > .panel-body > div:nth-child(3) > span"
).text(),
petPhotoName: $("img.image-pet").attr("src"),
petPhotoData: $("img.image-pet").attr("src"),
};
if ($(".container > div > .panel-warning").length !== 0)
data.membership = "diamond";
if (data.petSpecies === "") data.petSpecies = "Dog";
if (data.petGender === "") data.petGender = "Male";
if (data.ownerCountry === "") data.ownerCountry = "US";
const petImport = new PetImportSchema(data);
const ownerImport = new OwnerImportSchema(data);
const photoImport = new PhotoImportSchema(data);
if (data.microchip !== "") {
await PetImportSchema.findOneAndUpdate(
{ microchip: data.microchip },
data,
async function (err, pet) {
if (err)
await debug.log(
thread,
err + " :: on page " + process
);
if (!pet) {
try {
await petImport.save();
await debug.log(
thread,
"saved new pet from page " + process
);
} catch (error) {
await debug.log(
thread,
error + " :: on page " + process
);
}
} else {
await debug.log(
thread,
"updated new pet from page " + process
);
}
}
);
}
if (data.email !== "") {
await OwnerImportSchema.findOneAndUpdate(
{ email: data.email },
data,
async function (err, owner) {
if (err)
await debug.log(
thread,
err + " :: on page " + process
);
if (!owner) {
try {
await ownerImport.save();
await debug.log(
thread,
"saved new owner from page " + process
);
} catch (error) {
await debug.log(
thread,
error + " :: on page " + process
);
}
} else {
await debug.log(
thread,
"updated new owner from page " + process
);
}
}
);
}
if (data.petPhotoData !== "") {
await PhotoImportSchema.findOneAndUpdate(
{ petMicrochip: data.petMicrochip },
data,
async function (err, photo) {
if (err)
await debug.log(
thread,
err + " :: on page " + process
);
if (!photo) {
try {
await photoImport.save();
await debug.log(
thread,
"saved new photo from page " + process
);
} catch (error) {
await debug.log(
thread,
error + " :: on page " + process
);
}
} else {
await debug.log(
thread,
"updated new photo from page " + process
);
}
}
);
}
} catch (error) {
await debug.log(thread, error + " :: on page " + process);
}
}
}