|
|
|
|
@ -1,16 +1,7 @@
|
|
|
|
|
const { |
|
|
|
|
eqBy, |
|
|
|
|
hasPath, |
|
|
|
|
maxBy, |
|
|
|
|
pathOr, |
|
|
|
|
prop, |
|
|
|
|
props, |
|
|
|
|
unionWith, |
|
|
|
|
} = require('ramda'); |
|
|
|
|
const { hasPath, pathOr, props } = 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 { graphql_endpoint } = require('./constants'); |
|
|
|
|
@ -18,10 +9,6 @@ const { graphql_endpoint } = require('./constants');
|
|
|
|
|
const fs = require('fs').promises; |
|
|
|
|
const filesystem = require('fs'); |
|
|
|
|
|
|
|
|
|
const path = require('path'); |
|
|
|
|
|
|
|
|
|
const gm = require('gm').subClass({ imageMagick: true }); |
|
|
|
|
|
|
|
|
|
const puppeteer = require('puppeteer'); |
|
|
|
|
|
|
|
|
|
const flatten_string = (page_id) => { |
|
|
|
|
@ -70,10 +57,6 @@ const parse_args = (args) => {
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const output = parse_output(argv); |
|
|
|
|
const images = pathOr(false, ['images'], argv) || pathOr(false, ['i'], argv); |
|
|
|
|
const image_directory = flatten_string( |
|
|
|
|
pathOr('./img', ['image-directory'], argv), |
|
|
|
|
); |
|
|
|
|
|
|
|
|
|
const get_upcoming_events = !pathOr(false, ['skip-upcoming-events'], argv); |
|
|
|
|
const get_past_events = pathOr(false, ['past-events'], argv); |
|
|
|
|
@ -87,8 +70,6 @@ const parse_args = (args) => {
|
|
|
|
|
], |
|
|
|
|
events, |
|
|
|
|
output, |
|
|
|
|
images, |
|
|
|
|
image_directory, |
|
|
|
|
get_upcoming_events, |
|
|
|
|
get_past_events, |
|
|
|
|
headless, |
|
|
|
|
@ -112,85 +93,12 @@ const merge_edges = (acc, current) => {
|
|
|
|
|
]; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
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, image_directory) => { |
|
|
|
|
if (image === null) { |
|
|
|
|
return []; |
|
|
|
|
} |
|
|
|
|
const original_path = `${image_directory}/${event_id}.jpg`; |
|
|
|
|
const resized_path = `${image_directory}/${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) => { |
|
|
|
|
if (images_directory === null || images_directory === undefined) { |
|
|
|
|
return Promise.reject('Image path was not set'); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!filesystem.existsSync(images_directory)) { |
|
|
|
|
return fs.mkdir(images_directory, { recursive: true }).catch(console.error); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return Promise.resolve(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const read_previous_events = (path) => { |
|
|
|
|
if (path !== null) { |
|
|
|
|
if (filesystem.existsSync(path)) { |
|
|
|
|
@ -206,36 +114,6 @@ const read_previous_events = (path) => {
|
|
|
|
|
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); |
|
|
|
|
return {}; |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const map_event = ({ node: event }) => { |
|
|
|
|
const ticket_url = pathOr('', ['event_buy_ticket_url'], event); |
|
|
|
|
const city = get_city_name(event); |
|
|
|
|
@ -391,12 +269,9 @@ const get_page_events = async (
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
module.exports = { |
|
|
|
|
create_images_directory, |
|
|
|
|
get_page_events, |
|
|
|
|
load_event, |
|
|
|
|
merge_edges, |
|
|
|
|
open_browser, |
|
|
|
|
parse_args, |
|
|
|
|
read_previous_events, |
|
|
|
|
save_images, |
|
|
|
|
}; |
|
|
|
|
|