Skip to content

Instantly share code, notes, and snippets.

@gajus

gajus/mk2.js Secret

Last active January 18, 2017 13:42
Show Gist options
  • Save gajus/68f9da3b27a51a58db990ae67e9acdae to your computer and use it in GitHub Desktop.
Save gajus/68f9da3b27a51a58db990ae67e9acdae to your computer and use it in GitHub Desktop.
import _ from 'lodash';
import surgeon from 'surgeon';
import {
extractMatch,
extractDate,
extractTime,
mapSelector,
request
} from '@applaudience/showtime-scraper-tools';
export const source = {
country: 'fr',
name: 'mk2',
nid: 'mk2',
url: 'http://www.mk2.com/'
};
export const scrapeVenues = async () => {
const document = await request('get', 'http://www.mk2.com/', 'html');
const x = surgeon(document);
const venues = x({
properties: {
name: '::text()',
nid: '::attribute(href)::match("/salles/mk2-(.+)")',
url: '::attribute(href)'
},
selector: '#footer p:contains(Les salles MK2) + .item-list a[href^="/salles/"] {1,}'
});
return venues.map((venue) => {
return {
guide: {
url: 'http://www.mk2.com/salles/mk2-' + venue.url
},
result: {
name: venue.name,
nid: venue.nid,
url: 'http://www.mk2.com/salles/mk2-' + venue.url
}
};
});
};
export const scrapeMovies = async (guide) => {
const document = await request('get', guide.url, 'html');
const x = surgeon(document);
const movies = x({
properties: {
name: '.fiche-film-title',
url: 'a[href^="/films/"]::attribute(href)'
},
selector: '#seances .l-mk2-tables .l-session-table .fiche-film-info::has(.fiche-film-title) {0,}',
uniqueBy: 'url'
});
return movies.map((movie) => {
return {
guide: {
showtimeUrl: guide.url,
movieUrl: movie.url
},
result: {
name: movie.name
}
}
});
};
const scrapeLanguageAttributes = (version) => {
switch (version) {
case 'VO':
return {
audio: {
language: 'original'
}
};
case 'VF':
return {
audio: {
language: 'fr'
}
};
default:
return {};
}
};
export const scrapeShowtimes = async (guide) => {
const document = await request('get', guide.showtimeUrl, 'html');
const x = surgeon(document, {
formatters: {
extractDate,
extractTime
},
parameters: {
movieUrl: guide.movieUrl
}
});
const dates = x({
has: 'a[href="${movieUrl}"]',
properties: {
date: '.table-header .l-schedule-days::attribute(id)::extractDate(YYYYMMDD)',
events: {
properties: {
time: '::text()::extractTime(HH[h]mm)',
url: '::attribute(href)',
version: '::text()::match("(VOST|VO|VF)")'
},
selector: '.item-list a[href^="/reservation"]'
}
},
selector: '#seances .l-mk2-tables .l-session-table'
});
return _.flatten(dates.map((date) => {
return date.events.map((event) => {
return {
time: date.date + ' ' + event.time,
url: 'http://www.mk2.com' + event.url,
...scrapeLanguageAttributes(event.version)
};
});
}));
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment