|
|
|
|
const { pathOr, hasPath, props, prop, unionWith, eqBy } = require('ramda');
|
|
|
|
|
const parseArgs = require('minimist');
|
|
|
|
|
const process = require('process');
|
|
|
|
|
|
|
|
|
|
const event_url = (event_id) => `https://www.facebook.com/events/${event_id}`;
|
|
|
|
|
const page_url = (page_id) => `https://www.facebook.com/${page_id}`;
|
|
|
|
|
const page_events_url = (page_id) => page_url(page_id) + '/events/';
|
|
|
|
|
|
|
|
|
|
const fs = require('fs').promises;
|
|
|
|
|
const gm = require('gm').subClass({ imageMagick: true });
|
|
|
|
|
|
|
|
|
|
const parse_output = (argv) => {
|
|
|
|
|
const [res = null] = props(['output', 'o'], argv).filter(
|
|
|
|
|
(item) => item !== undefined,
|
|
|
|
|
);
|
|
|
|
|
return res;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const parse_args = (args) => {
|
|
|
|
|
const argv = parseArgs(args);
|
|
|
|
|
|
|
|
|
|
const has_help_param =
|
|
|
|
|
hasPath(['h'], argv) || hasPath(['help'], argv) || hasPath(['?'], argv);
|
|
|
|
|
if (has_help_param) {
|
|
|
|
|
process.exit(1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const away_empty_strings = (str) => str.length !== 0;
|
|
|
|
|
const page_id_to_page_events_url = page_events_url;
|
|
|
|
|
const parse_param = (param) =>
|
|
|
|
|
pathOr('', [param], argv)
|
|
|
|
|
.split(',')
|
|
|
|
|
.filter(away_empty_strings)
|
|
|
|
|
.map(page_id_to_page_events_url);
|
|
|
|
|
|
|
|
|
|
const events = pathOr(null, ['events'], argv);
|
|
|
|
|
const output = parse_output(argv);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
page_ids: [
|
|
|
|
|
...parse_param('page'),
|
|
|
|
|
...parse_param('p'),
|
|
|
|
|
...parse_param('pages'),
|
|
|
|
|
],
|
|
|
|
|
events,
|
|
|
|
|
output,
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const get_upcoming_events = pathOr(
|
|
|
|
|
null,
|
|
|
|
|
'data.page.upcoming_events'.split('.'),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const get_past_events = pathOr(null, 'data.page.past_events'.split('.'));
|
|
|
|
|
const merge_edges = unionWith(eqBy(prop('event_id')));
|
|
|
|
|
|
|
|
|
|
const write_image = (path, image) =>
|
|
|
|
|
fs.writeFile(path, image, { encoding: null });
|
|
|
|
|
|
|
|
|
|
const gm_write = (image, path) => {
|
|
|
|
|
return new Promise((resolve, reject) =>
|
|
|
|
|
image.write(path, (err) => (!err ? resolve() : reject(err))),
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const write_resized = async (image_path, original) => {
|
|
|
|
|
const image = gm(original);
|
|
|
|
|
const size = await new Promise((resolve) => {
|
|
|
|
|
image.size((err, value) => (!err ? resolve(value) : resolve(null)));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if (size === null) {
|
|
|
|
|
throw new Error('Could not get image.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let { height: y, width: x } = size;
|
|
|
|
|
|
|
|
|
|
if (y % 2 === 1) {
|
|
|
|
|
y = y + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (x % 2 === 1) {
|
|
|
|
|
x = x + 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
image.resize(x, y);
|
|
|
|
|
|
|
|
|
|
if (y > x) {
|
|
|
|
|
const z = (y - x) / 2;
|
|
|
|
|
image.crop(x, x, 0, z);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (y < x) {
|
|
|
|
|
const z = (x - y) / 2;
|
|
|
|
|
image.crop(y, y, z, 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return gm_write(image, image_path);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const save_images = async ({ image = null, event_id }) => {
|
|
|
|
|
if (image === null) {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
const original_path = `./img/${event_id}.jpg`;
|
|
|
|
|
const resized_path = `./img/${event_id}-square.jpg`;
|
|
|
|
|
const original = write_image(original_path, image);
|
|
|
|
|
const resized_square = write_resized(resized_path, image);
|
|
|
|
|
try {
|
|
|
|
|
await Promise.all([original, resized_square]);
|
|
|
|
|
return { original: original_path, square: resized_path };
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
return { original: null };
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const get_city_name = (event) =>
|
|
|
|
|
pathOr('', 'event_place.city.contextual_name'.split('.'), event);
|
|
|
|
|
|
|
|
|
|
const get_event_host = (event) =>
|
|
|
|
|
pathOr('', 'event_place.contextual_name'.split('.'), event);
|
|
|
|
|
|
|
|
|
|
const create_images_directory = (images_directory) =>
|
|
|
|
|
fs.mkdir(images_directory, { recursive: true }).catch(console.error);
|
|
|
|
|
|
|
|
|
|
const read_previous_events = (path) => {
|
|
|
|
|
if (path !== null) {
|
|
|
|
|
if (filesystem.existsSync(path)) {
|
|
|
|
|
return fs
|
|
|
|
|
.readFile(path, { encoding: 'utf-8' })
|
|
|
|
|
.then((content) => JSON.parse(content))
|
|
|
|
|
.catch(console.error);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return Promise.resolve([]);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const load_event = async (page, event_id) => {
|
|
|
|
|
try {
|
|
|
|
|
const image_data = new Promise((resolve) => {
|
|
|
|
|
const images = [];
|
|
|
|
|
page.on('response', async (response) => {
|
|
|
|
|
const response_url = response.request().url();
|
|
|
|
|
const { pathname } = new URL(response_url);
|
|
|
|
|
const ext = path.extname(pathname);
|
|
|
|
|
if (ext === '.jpg') {
|
|
|
|
|
const image = await response.buffer();
|
|
|
|
|
images.push(image);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
page.on('domcontentloaded', async () => {
|
|
|
|
|
resolve(images);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
await page.goto(event_url(event_id));
|
|
|
|
|
const images = await image_data;
|
|
|
|
|
const image = images.reduce((res, image) =>
|
|
|
|
|
maxBy((item) => item.length, res, image),
|
|
|
|
|
);
|
|
|
|
|
return { image };
|
|
|
|
|
} catch (e) {
|
|
|
|
|
console.error(e);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const map_event = ({ node: event }) => {
|
|
|
|
|
const ticket_url = pathOr('', ['event_buy_ticket_url'], event);
|
|
|
|
|
const city = get_city_name(event);
|
|
|
|
|
const host = get_event_host(event);
|
|
|
|
|
return {
|
|
|
|
|
date: event.time_range,
|
|
|
|
|
name: event.name,
|
|
|
|
|
event_id: event.id,
|
|
|
|
|
ticket_url,
|
|
|
|
|
location: {
|
|
|
|
|
host: host,
|
|
|
|
|
location: city,
|
|
|
|
|
},
|
|
|
|
|
};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const open_browser = async () => {
|
|
|
|
|
const browser = await puppeteer.launch({
|
|
|
|
|
headless: true,
|
|
|
|
|
args: ['--disable-dev-shm-usage'],
|
|
|
|
|
});
|
|
|
|
|
return browser;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const register_upcoming_events_listener = (endpoint, page) => {
|
|
|
|
|
let responses = [];
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
page.on('response', async (response) => {
|
|
|
|
|
if (endpoint === response.request().url()) {
|
|
|
|
|
try {
|
|
|
|
|
const json = await response.json();
|
|
|
|
|
const upcoming_events = get_upcoming_events(json);
|
|
|
|
|
if (upcoming_events !== null) {
|
|
|
|
|
responses = [upcoming_events, ...responses];
|
|
|
|
|
if (!upcoming_events.page_info.has_next_page) {
|
|
|
|
|
resolve(responses);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const past_events = get_past_events(json);
|
|
|
|
|
if (past_events !== null) {
|
|
|
|
|
if (!past_events.page_info.has_next_page) {
|
|
|
|
|
resolve(responses);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
reject(err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
module.exports = { parse_args, create_images_directory };
|