You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
126 lines
3.5 KiB
126 lines
3.5 KiB
import { get_page_events } from '../src/facebook/get-page-events.mjs'; |
|
import send from '../src/signal/send.mjs'; |
|
import fetch from 'node-fetch'; |
|
|
|
const api = 'http://localhost:3333'; |
|
const token = |
|
// '831411806230c7e950c4eeb226499ef92bb6bdc4157797929a0e16d133dc13a8'; |
|
'1234567812345678123456781234567812345678123456781234567812345678'; |
|
const headers = { 'Content-Type': 'application/json' }; |
|
|
|
const scrape = async (pageID) => { |
|
try { |
|
const res = await get_page_events({ |
|
pageID, |
|
get_upcoming_events: true, |
|
get_past_events: false |
|
}); |
|
return res; |
|
} catch (e) { |
|
console.error(e); |
|
} |
|
return []; |
|
}; |
|
const unix = (a) => parseInt(new Date(a).valueOf() / 1000, 10); |
|
const sleep = (ms) => new Promise((res) => setTimeout(res, ms)); |
|
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; |
|
}; |
|
|
|
(async () => { |
|
let resp = await fetch(`${api}/places/?token=${token}`); |
|
let places = await resp.json(); |
|
places = places.filter((place) => { |
|
const scrape = place.scraper == 'facebook'; |
|
if (!scrape) { |
|
console.log( |
|
100, |
|
`Skipping #${place.id}$ {place.name}. Reason: Scraper is ${place.scraper}` |
|
); |
|
} |
|
return scrape; |
|
}); |
|
|
|
for (let place of places) { |
|
if (place.id < 13) { |
|
console.log(100, `Skipping #${place.id} ${place.name}`); |
|
continue; |
|
} else { |
|
console.log(100, `Scraping #${place.id} ${place.name}`); |
|
} |
|
const events = await scrape(place.facebook_id); |
|
let payloads = []; |
|
for (let event of events) { |
|
payloads.push({ |
|
canceled: false, |
|
end: -1, |
|
start: unix(new Date(event.time_range.start)), |
|
draft: false, |
|
facebook_id: event.id, |
|
place_id: place.id, |
|
name: event.name ?? '', |
|
ticket_url: event.event_buy_ticket_url ?? '' |
|
}); |
|
} |
|
for (let payload of payloads) { |
|
let search = await fetch( |
|
`${api}/search/events/?facebook_id=${payload.facebook_id}&token=${token}` |
|
); |
|
if (!search.ok) { |
|
await sleep(200); |
|
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', place.name, payload.name); |
|
let newEvent = await res.text(); |
|
let msg = await send(newEvent, place); |
|
console.log(res.status, 'Signal', msg); |
|
} 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', place.name, payload.name); |
|
} else { |
|
console.log(201, 'Skip', place.name, payload.name); |
|
} |
|
} else { |
|
console.log(201, 'Skip', place.name, payload.name); |
|
} |
|
} |
|
} |
|
})();
|
|
|