-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrape.js
123 lines (89 loc) · 3.53 KB
/
scrape.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
const axios = require('axios');
const { getCode } = require('country-list');
class Client {
constructor() { }
async getPrices() {
let responses = await Promise.all([
this.getAiralo(),
this.getHolafly(),
this.getNomad()
]);
return responses;
}
async getAiralo() {
let { data } = await axios.get('https://www.airalo.com/api/v2/countries');
let countries = await Promise.all(data.map(c => this.getAiraloPricesByCountry(c.slug)));
return {
site: 'airalo.com',
countries
};
}
async getAiraloPricesByCountry(slug) {
let { data } = await axios.get('https://www.airalo.com/api/v2/countries/' + slug);
let items = data.packages.map(p => {
return {
data: p.data,
duration: p.validity,
price: p.price
};
});
let countryCode = getCode(data.title);
return { country: data.title, countryCode, items };
}
async getHolafly() {
let { data } = await axios.get('https://esim.holafly.com/shop/');
let regex = /<\/a><a href="https:\/\/esim.holafly.com\/([^/]+)\/" data-quantity="\d+" class="button product_type_variable add_to_cart_button" data-product_id="\d+" data-product_sku="[^"]+" aria-label="Select options for “([^\&]+)”"/;
let matches = data.match(new RegExp(regex, 'g'));
let countries = await Promise.all(matches.map(m => {
let [, slug, country] = m.match(regex);
return this.getHolaflyPricesByCountry(slug, country);
}));
return {
site: 'esim.holafly.com',
countries
};
}
async getHolaflyPricesByCountry(slug, country) {
let { data } = await axios.get(`https://esim.holafly.com/${slug}/`);
let regex = /var productVariations = ([^;]+);\n/;
let products = JSON.parse(data.match(regex)[1]);
let items = products.map(p => {
let [d1, d2, d3, d4] = p.name.split(' ');
let data = [d3, d4].join(' ');
let duration = [d1, d2].join(' ');
return {
data,
duration,
price: p.price
};
});
let countryCode = getCode(country);
return { country, countryCode, items };
}
async getNomad() {
let { data } = await axios.get('https://www.getnomad.app/shop/all/index.pageContext.json');
let countries = [];
for (let plan of data.apiCache['apis-getProducts-[]'].plans) {
let [name, duration, data] = plan.name.split(' - ');
let isLocal = name.split(' ')[0] === 'Local';
if (!isLocal) continue;
let countryName = name.substring(name.indexOf(' ') + 1); // Get the full country name
let item = {
data,
duration,
price: plan.price
};
let country = countries.find(c => c.country === countryName);
if (country) {
country.items.push(item);
} else {
let countryCode = getCode(countryName);
let flagUrl = `https://www.countryflags.io/${countryCode}/flat/64.png`;
countries.push({ country: countryName, flagUrl, items: [item] });
}
}
countries.sort((a, b) => a.country.localeCompare(b.country));
return { site: 'getnomad.app', countries };
}
}
module.exports = { Client };