|
|
|
|
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, reject) => {
|
|
|
|
|
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 {
|
|
|
|
|
const res = 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);
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
|
event_url,
|
|
|
|
|
get_city_name,
|
|
|
|
|
get_event_host,
|
|
|
|
|
get_past_events,
|
|
|
|
|
get_upcoming_events,
|
|
|
|
|
merge_edges,
|
|
|
|
|
parse_args,
|
|
|
|
|
save_images,
|
|
|
|
|
};
|