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.

181 lines
4.8 KiB

4 years ago
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));
4 years ago
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));
4 years ago
const headers = { 'Content-Type': 'application/json' };
4 years ago
4 years ago
(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,
4 years ago
end: -1,
4 years ago
start: unix(new Date(event.time_range.start)),
4 years ago
draft: false,
facebook_id: event.id,
host: get_place(event),
location: get_city(event),
4 years ago
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());
4 years ago
await sleep(200);
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),
4 years ago
headers
});
console.error(137, 'Updated');
} else {
res = await fetch(`${hostname}/events/?token=${token}`, {
4 years ago
method: 'POST',
body: JSON.stringify(event),
4 years ago
headers
});
let newEvent = await res.text();
console.error(145, newEvent);
try {
4 years ago
const format = (d) => {
return (
d.getFullYear() +
'-' +
('0' + (d.getMonth() + 1)).slice(-2) +
'-' +
('0' + d.getDate()).slice(-2) +
' ' +
('0' + d.getHours()).slice(-2) +
':' +
('0' + d.getMinutes()).slice(-2)
);
};
newEvent = JSON.parse(newEvent);
4 years ago
let startTime = `${format(new Date(newEvent.start * 1000))}`;
let message = `${startTime} ${newEvent.host}: ${newEvent.name}`;
4 years ago
let s = await fetch(`http://localhost:8080/v2/send`, {
body: JSON.stringify({
4 years ago
message,
number: '+4793478353',
4 years ago
recipients: ['+4793478353']
}),
method: 'POST',
4 years ago
headers
});
4 years ago
console.error(await s.text());
} catch (e) {
console.error(e);
}
}
console.error(161, res.status);
4 years ago
}
})();