|
|
|
|
import fetch from 'node-fetch';
|
|
|
|
|
import { promises as fs } from 'fs';
|
|
|
|
|
const eventFiles = process.argv.slice(3);
|
|
|
|
|
|
|
|
|
|
const later = (delay, value) =>
|
|
|
|
|
new Promise((resolve) => setTimeout(resolve, delay, value));
|
|
|
|
|
|
|
|
|
|
const unix = (a) => parseInt(new Date(a).valueOf() / 1000, 10);
|
|
|
|
|
const token =
|
|
|
|
|
'831411806230c7e950c4eeb226499ef92bb6bdc4157797929a0e16d133dc13a8';
|
|
|
|
|
|
|
|
|
|
const hostname = 'https://dansbart.no';
|
|
|
|
|
// const hostname = 'http://localhost:3333';
|
|
|
|
|
const get_city = (event) => {
|
|
|
|
|
let trondheim =
|
|
|
|
|
event.event_place?.city?.id === '110429825645017' ||
|
|
|
|
|
event.place?.city?.id === '110429825645017';
|
|
|
|
|
|
|
|
|
|
if (trondheim) {
|
|
|
|
|
return 'Trondheim, Norway';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let city = event.event_place?.city?.name ?? null;
|
|
|
|
|
|
|
|
|
|
if (city === null) {
|
|
|
|
|
city = event.event_place?.city?.contextual_name ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (city === null) {
|
|
|
|
|
city = event.place?.city?.name ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (city === null) {
|
|
|
|
|
city = event.place?.city?.contextual_name ?? null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (city !== null) {
|
|
|
|
|
return city;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 'Trondheim, Norway';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const get_place = (event) => {
|
|
|
|
|
let place, address, page;
|
|
|
|
|
|
|
|
|
|
address =
|
|
|
|
|
event.event_place?.__typename === 'FreeformPlace' ||
|
|
|
|
|
event.place?.__typename === 'FreeformPlace';
|
|
|
|
|
page =
|
|
|
|
|
event.event_place?.__typename === 'Page' ||
|
|
|
|
|
event.place?.__typename === 'Page';
|
|
|
|
|
|
|
|
|
|
if (address) {
|
|
|
|
|
place = event?.event_place?.contextual_name ?? null;
|
|
|
|
|
if (place === null) {
|
|
|
|
|
place = event?.place?.contextual_name ?? null;
|
|
|
|
|
}
|
|
|
|
|
if (place == null) {
|
|
|
|
|
place = '';
|
|
|
|
|
}
|
|
|
|
|
} else if (page) {
|
|
|
|
|
place = event?.event_place?.name ?? null;
|
|
|
|
|
if (place === null) {
|
|
|
|
|
place = event?.place?.name ?? null;
|
|
|
|
|
}
|
|
|
|
|
if (place === null) {
|
|
|
|
|
place = event?.event_place?.contextual_name ?? null;
|
|
|
|
|
}
|
|
|
|
|
if (place === null) {
|
|
|
|
|
place = event?.place?.contextual_name ?? null;
|
|
|
|
|
}
|
|
|
|
|
if (place == null) {
|
|
|
|
|
place == '';
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
place = '';
|
|
|
|
|
}
|
|
|
|
|
if (place == null) {
|
|
|
|
|
place = '';
|
|
|
|
|
}
|
|
|
|
|
return place;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const sleep = (ms) => new Promise((res) => setTimeout(res, ms));
|
|
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
|
const root = '/home/zalox/src/acne/lib/derma';
|
|
|
|
|
let requests = [];
|
|
|
|
|
const promises = eventFiles.map(async (filename) => {
|
|
|
|
|
const path = `${root}/${filename}`;
|
|
|
|
|
const file_content = await fs.readFile(path, { encoding: 'utf-8' });
|
|
|
|
|
const j = JSON.parse(file_content);
|
|
|
|
|
for (let i = 0; i < j.length; i++) {
|
|
|
|
|
let event = j[i];
|
|
|
|
|
const payload = {
|
|
|
|
|
canceled: false,
|
|
|
|
|
end: -1,
|
|
|
|
|
start: unix(new Date(event.time_range.start)),
|
|
|
|
|
draft: false,
|
|
|
|
|
facebook_id: event.id,
|
|
|
|
|
host: get_place(event),
|
|
|
|
|
location: get_city(event),
|
|
|
|
|
name: event.name ?? 'NAME_MISSING',
|
|
|
|
|
ticket_url: event.event_buy_ticket_url ?? ''
|
|
|
|
|
};
|
|
|
|
|
requests.push(payload);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
await Promise.all(promises);
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < requests.length; i++) {
|
|
|
|
|
let event = requests[i];
|
|
|
|
|
let search = await fetch(
|
|
|
|
|
`${hostname}/search/events/?facebook_id=${event.facebook_id}&token=${token}`
|
|
|
|
|
);
|
|
|
|
|
if (!search.ok) {
|
|
|
|
|
console.error(118, 'search not ok');
|
|
|
|
|
console.error(119, await search.text());
|
|
|
|
|
await sleep(1000);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
search = await search.json();
|
|
|
|
|
let update = search.length === 1;
|
|
|
|
|
let res;
|
|
|
|
|
if (update) {
|
|
|
|
|
let old_event = search[0];
|
|
|
|
|
event.id = old_event.id;
|
|
|
|
|
if (old_event.ticket_url.length > 0 && event.ticket_url.length == 0) {
|
|
|
|
|
delete event.ticket_url;
|
|
|
|
|
}
|
|
|
|
|
res = await fetch(`${hostname}/events/${event.id}/?token=${token}`, {
|
|
|
|
|
method: 'PATCH',
|
|
|
|
|
body: JSON.stringify(event),
|
|
|
|
|
headers: { 'Content-Type': 'application/json' }
|
|
|
|
|
});
|
|
|
|
|
console.error(137, 'Updated');
|
|
|
|
|
} else {
|
|
|
|
|
res = await fetch(`${hostname}/events/?token=${token}`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
body: JSON.stringify(event),
|
|
|
|
|
headers: { 'Content-Type': 'application/json' }
|
|
|
|
|
});
|
|
|
|
|
let newEvent = await res.text();
|
|
|
|
|
console.error(145, newEvent);
|
|
|
|
|
try {
|
|
|
|
|
newEvent = JSON.parse(newEvent);
|
|
|
|
|
await fetch(`http://localhost:8080/v2/send`, {
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
message: `${new Date(newEvent.start * 1000)} ${newEvent.name}`,
|
|
|
|
|
number: '+4793478353',
|
|
|
|
|
recipients: ['dansbart.dev']
|
|
|
|
|
}),
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: { 'Content-Type': 'application/json' }
|
|
|
|
|
});
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
console.error(161, res.status);
|
|
|
|
|
}
|
|
|
|
|
})();
|