-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwp-poster.js
55 lines (54 loc) · 1.99 KB
/
wp-poster.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
const request = require('request');
module.exports = {
sendToWordpress: function (res) {
let itemPrice = '';
for (var i = 0; res.length > i; i += 1) {
// check if title is a link
if (this.requirementsToPass(res[i].title)) {
// look for price
if (res[i].price === undefined || res[i].price === null || res[i].price === '') {
itemPrice = this.extractPrice(res[i].description);
} else {
itemPrice = res[i].price;
}
itemPrice = itemPrice.replace('€', '');
console.log(itemPrice);
request({
headers: {
'Content-Type': 'application/json',
'Authorization': 'Basic bWtzb2xlbW46TTFuZDN4NHNAMTk5MQ=='
},
uri: 'http://livinmalta.com/wp-json/wp/v2/posts',
body: JSON.stringify({
title: res[i].title,
content: res[i].description,
status: 'publish',
fields: {
item_price: itemPrice,
phone_number: this.extractPhone(res[i].description)
}
}),
method: 'POST'
})
}
}
},
requirementsToPass: function (title) {
if (title === undefined || title === null || title === '') {
return false;
}
return true;
},
extractPhone: function (description) {
const regex = /(\+?\d+)\d+\d+\d+\d+\d+\d+/g;
if (description.match(regex) !== null) {
return description.match(regex)[0];
}
},
extractPrice: function (description) {
const regex = /[\$\£\€](\d+(?:\.\d{1,2})?)/;
if (description.match(regex) !== null) {
return description.match(regex)[0];
}
}
}