265 lines
4.7 KiB
JavaScript
265 lines
4.7 KiB
JavaScript
const nodes = {
|
|
gchange: [
|
|
'http://data.gchange.fr'
|
|
]
|
|
}
|
|
|
|
|
|
async function fetchShippable (n) {
|
|
|
|
// var uri = '/market/record/_search?size='+ n +'&_source=title,description&q=description:envoi+possible%20type=offer'
|
|
|
|
var uri = '/market/record/_search'
|
|
|
|
var query = {
|
|
size: n
|
|
, _source: [
|
|
'title'
|
|
, 'description'
|
|
, 'id'
|
|
]
|
|
, query: {
|
|
bool: {
|
|
must: [
|
|
{
|
|
match: {
|
|
'description': {
|
|
|
|
query: 'envoi possible',
|
|
operator: 'and'
|
|
}
|
|
}
|
|
}
|
|
]
|
|
, must_not : [
|
|
{term : {stock: 0} }
|
|
]
|
|
, filter: [
|
|
{term: {type: 'offer'}}
|
|
]
|
|
}
|
|
}
|
|
, sort: [
|
|
{ 'time': 'desc'}
|
|
]
|
|
}
|
|
|
|
var fetchOpts = {
|
|
method: 'POST',
|
|
headers: {
|
|
'Accept': 'application/json'
|
|
},
|
|
body: JSON.stringify(query)
|
|
}
|
|
|
|
const r = await fetch(nodes['gchange'][0] + uri, fetchOpts)
|
|
|
|
if (r.ok === true) {
|
|
|
|
return r.json()
|
|
}
|
|
|
|
throw new Error(r.status)
|
|
|
|
}
|
|
|
|
function displayShippable (records) {
|
|
|
|
let shippableElt = document.getElementById('shippable');
|
|
|
|
for (record of records) {
|
|
|
|
offerLi = document.createElement('li')
|
|
|
|
offerLink = document.createElement('a')
|
|
|
|
offerLink.innerHTML = record._source.title
|
|
offerLink.href = 'https://www.gchange.fr/#/app/market/view/' + record._id + '/'
|
|
|
|
offerLi.append(offerLink)
|
|
shippableElt.append(offerLi)
|
|
}
|
|
}
|
|
|
|
|
|
async function fetchImmaterial (n) {
|
|
|
|
// var uri = '/market/record/_search?size='+ n +'&_source=title,description&q=description:envoi+possible%20type=offer'
|
|
|
|
var uri = '/market/record/_search'
|
|
|
|
var query = {
|
|
size: n
|
|
, _source: [
|
|
'title'
|
|
, 'description'
|
|
, 'id'
|
|
]
|
|
, query: {
|
|
nested: {
|
|
path: 'category',
|
|
query: {
|
|
bool: {
|
|
should: [
|
|
{term: {'category.parent': 'cat31'}}
|
|
, {term: {'category.id': 'cat31'}}
|
|
, {term: {'category.parent': 'cat74'}}
|
|
, {term: {'category.id': 'cat74'}}
|
|
]
|
|
,
|
|
must_not: [
|
|
{term: {'category.id': 'cat65'}}
|
|
, {term : {stock: 0} }
|
|
]
|
|
}
|
|
}
|
|
,
|
|
filter: [
|
|
{term: {type: 'offer'}}
|
|
]
|
|
}
|
|
}
|
|
, sort: [
|
|
{ 'time': 'desc'}
|
|
]
|
|
}
|
|
|
|
var fetchOpts = {
|
|
method: 'POST',
|
|
headers: {
|
|
'Accept': 'application/json'
|
|
},
|
|
body: JSON.stringify(query)
|
|
}
|
|
|
|
const r = await fetch(nodes['gchange'][0] + uri, fetchOpts)
|
|
|
|
if (r.ok === true) {
|
|
|
|
var obj = r.json()
|
|
storeAtFreds(obj)
|
|
return obj
|
|
}
|
|
|
|
throw new Error(r.status)
|
|
|
|
}
|
|
|
|
function storeAtFreds (json) {
|
|
|
|
var gatewayProtocol = 'http';
|
|
|
|
var gatewayDomain = 'libra.copylaradio.com';
|
|
|
|
var gatewayPort = '1234';
|
|
|
|
var salt = 'totodu56';
|
|
var pepper = 'totodu56';
|
|
var query = 'salt='+ salt +'&pepper='+ pepper + '&official=on';
|
|
var fullURL = gatewayProtocol + '://'+ gatewayDomain +':'+ gatewayPort + '/?' + query;
|
|
console.log(fullURL)
|
|
|
|
const controller = new AbortController()
|
|
const timeoutId = setTimeout( () => {
|
|
controller.abort()
|
|
}, 15000)
|
|
var fetchOpts = {
|
|
method: 'GET',
|
|
headers: {
|
|
// 'Accept': 'txt/html'
|
|
},
|
|
signal: controller.signal
|
|
}
|
|
|
|
fetch(fullURL, fetchOpts)
|
|
.then(reponse => {
|
|
return reponse.text()
|
|
})
|
|
.then(html => {
|
|
// console.log(html)
|
|
|
|
var regex = /url='([^']+)/i;
|
|
var redirectURL = html.match(regex)[1]
|
|
|
|
return redirectURL
|
|
})
|
|
.then(url => {
|
|
|
|
console.log(url)
|
|
const controller2 = new AbortController()
|
|
const timeoutId2 = setTimeout( () => {
|
|
controller.abort()
|
|
}, 15000)
|
|
var fetchOpts2 = {
|
|
method: 'GET',
|
|
headers: {
|
|
'Accept': 'text/html',
|
|
'Content-Type': 'text/html',
|
|
// 'Access-Control-Allow-Origin': '*',
|
|
'Origin': 'la-bureautique',
|
|
'Referrer-Policy': 'unsafe-url'
|
|
},
|
|
mode: 'cors',
|
|
redirect: 'follow',
|
|
signal: controller2.signal
|
|
}
|
|
fetch(url, fetchOpts2)
|
|
.then(html => {
|
|
|
|
console.log(html)
|
|
/*
|
|
var regex = /url='.*\/ipns\/([^']+)/isU;
|
|
var ipnsKey = html.match(regex)[1]
|
|
|
|
return ipnsKey
|
|
*/
|
|
}).catch(err => {
|
|
console.error(err)
|
|
})
|
|
})
|
|
|
|
// JSON.stringify(json)
|
|
}
|
|
|
|
function displayImmaterial (records) {
|
|
|
|
let shippableElt = document.getElementById('immaterial');
|
|
|
|
for (record of records) {
|
|
|
|
offerLi = document.createElement('li')
|
|
|
|
offerLink = document.createElement('a')
|
|
|
|
offerLink.innerHTML = record._source.title
|
|
offerLink.href = 'https://www.gchange.fr/#/app/market/view/' + record._id + '/'
|
|
|
|
offerLi.append(offerLink)
|
|
shippableElt.append(offerLi)
|
|
}
|
|
}
|
|
|
|
fetchImmaterial(5)
|
|
.then(records => {
|
|
|
|
displayImmaterial(records.hits.hits)
|
|
})
|
|
.catch(error => {
|
|
if (error == 'Error: 400')
|
|
console.error('Mauvaise requête')
|
|
else
|
|
console.error(error)
|
|
})
|
|
|
|
fetchShippable(5)
|
|
.then(records => {
|
|
|
|
displayShippable(records.hits.hits)
|
|
})
|
|
.catch(error => {
|
|
if (error == 'Error: 400')
|
|
console.error('Mauvaise requête')
|
|
else
|
|
console.error(error)
|
|
})
|