3 changed files with 138 additions and 37 deletions
@ -0,0 +1,98 @@ |
|||||||
|
import fetch from 'node-fetch'; |
||||||
|
|
||||||
|
process.stdin.resume(); |
||||||
|
process.stdin.setEncoding('utf8'); |
||||||
|
let input = []; |
||||||
|
process.stdin.on('data', (data) => { |
||||||
|
input.push(data); |
||||||
|
}); |
||||||
|
|
||||||
|
const token = |
||||||
|
'1234567812345678123456781234567812345678123456781234567812345678'; |
||||||
|
|
||||||
|
const api = 'http://localhost:3333'; |
||||||
|
const headers = { 'Content-Type': 'application/json' }; |
||||||
|
|
||||||
|
const updated = (oldEvent, scrapedEvent) => { |
||||||
|
let keys = [ |
||||||
|
'canceled', |
||||||
|
'end', |
||||||
|
'start', |
||||||
|
'draft', |
||||||
|
'facebook_id', |
||||||
|
'place_id', |
||||||
|
'name', |
||||||
|
'ticket_url', |
||||||
|
'id' |
||||||
|
]; |
||||||
|
for (let key of keys) { |
||||||
|
if (oldEvent[key] != scrapedEvent[key]) { |
||||||
|
return true; |
||||||
|
} |
||||||
|
} |
||||||
|
return false; |
||||||
|
}; |
||||||
|
|
||||||
|
process.stdin.on('end', async () => { |
||||||
|
for (let event_line of input.join('').split('\n')) { |
||||||
|
const [ |
||||||
|
facebook_id = '', |
||||||
|
location_name = '', |
||||||
|
place_id = '', |
||||||
|
name = '', |
||||||
|
start = '', |
||||||
|
ticket_url = '' |
||||||
|
] = event_line.split('¤'); |
||||||
|
let payload = { |
||||||
|
draft: false, |
||||||
|
canceled: false, |
||||||
|
facebook_id, |
||||||
|
location_name, |
||||||
|
place_id: Number(place_id), |
||||||
|
name, |
||||||
|
start: Number(start), |
||||||
|
ticket_url |
||||||
|
}; |
||||||
|
|
||||||
|
let search = await fetch( |
||||||
|
`${api}/search/events/?facebook_id=${payload.facebook_id}&token=${token}` |
||||||
|
); |
||||||
|
if (!search.ok) { |
||||||
|
console.log(await search.text()); |
||||||
|
continue; |
||||||
|
} |
||||||
|
search = await search.json(); |
||||||
|
let new_event = search.length === 0; |
||||||
|
let old_event; |
||||||
|
if (!new_event) { |
||||||
|
old_event = search[0]; |
||||||
|
} |
||||||
|
let res; |
||||||
|
if (new_event) { |
||||||
|
res = await fetch(`${api}/events/?token=${token}`, { |
||||||
|
method: 'POST', |
||||||
|
body: JSON.stringify(payload), |
||||||
|
headers |
||||||
|
}); |
||||||
|
console.log(res.status, 'Insert', payload.name); |
||||||
|
} else if (old_event && updated(old_event, payload)) { |
||||||
|
payload.id = old_event.id; |
||||||
|
if (old_event.ticket_url.length > 0 && payload.ticket_url.length == 0) { |
||||||
|
payload.ticket_url = old_event.ticket_url; |
||||||
|
} |
||||||
|
if (updated(old_event, payload)) { |
||||||
|
res = await fetch(`${api}/events/${payload.id}/?token=${token}`, { |
||||||
|
method: 'PATCH', |
||||||
|
body: JSON.stringify(payload), |
||||||
|
headers |
||||||
|
}); |
||||||
|
console.log(res.status, 'Update', payload.name); |
||||||
|
} else { |
||||||
|
console.log(201, 'Skip', payload.name); |
||||||
|
} |
||||||
|
} else { |
||||||
|
console.log(201, 'Skip', payload.name); |
||||||
|
} |
||||||
|
console.log(res.status, await res.text()); |
||||||
|
} |
||||||
|
}); |
||||||
Loading…
Reference in new issue