-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
189 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
import { DateTime } from 'luxon' | ||
import Xray from 'x-ray' | ||
|
||
import { logger as parentLogger } from '../powertools' | ||
import { Screening } from '../types' | ||
import xRayPuppeteer from '../xRayPuppeteer' | ||
import { guessYear } from './utils/guessYear' | ||
import { monthToNumber } from './utils/monthToNumber' | ||
import { splitTime } from './utils/splitTime' | ||
|
||
const logger = parentLogger.createChild({ | ||
persistentLogAttributes: { | ||
scraper: 'focusarnhem', | ||
}, | ||
}) | ||
|
||
const trim = (value) => (typeof value === 'string' ? value.trim() : value) | ||
|
||
const cleanTitle = (value) => | ||
typeof value === 'string' ? value.replace(/^Expat Cinema: /gi, '') : value | ||
|
||
const toLowerCase = (value) => | ||
typeof value === 'string' ? value.toLowerCase() : value | ||
|
||
const replaceNoBreakSpace = (value) => | ||
typeof value === 'string' ? value.replace(/\u00a0/g, ' ') : value | ||
|
||
const normalizeWhitespace = (value) => | ||
typeof value === 'string' ? value.replace(/\s+/g, ' ') : value | ||
|
||
const xray = Xray({ | ||
filters: { | ||
cleanTitle, | ||
replaceNoBreakSpace, | ||
toLowerCase, | ||
trim, | ||
normalizeWhitespace, | ||
}, | ||
}) | ||
.driver( | ||
xRayPuppeteer({ | ||
logger, | ||
waitForOptions: { timeout: 60_000, waitUntil: 'networkidle2' }, | ||
}), | ||
) | ||
.concurrency(10) | ||
.throttle(10, 300) | ||
|
||
type XRayFromMoviePage = { | ||
title: string | ||
metadata: string | ||
screenings: { | ||
date: string | ||
times: string[] | ||
}[] | ||
} | ||
|
||
type XRayFromMainPage = { | ||
title: string | ||
url: string | ||
} | ||
|
||
const hasEnglishSubtitles = (movie: XRayFromMoviePage) => { | ||
return /ondertiteling: english/i.test(movie.metadata) | ||
} | ||
|
||
const splitDate = (date: string) => { | ||
if (date === 'Vandaag') { | ||
const { day, month, year } = DateTime.now() | ||
return { day, month, year } | ||
} else if (date === 'Morgen') { | ||
const { day, month, year } = DateTime.now().plus({ days: 1 }) | ||
return { day, month, year } | ||
} else { | ||
const [dayString, monthString] = date | ||
.split(/\s+/) // ['Woensdag 8 mei'] => ['Woensdag', '8', 'mei'] | ||
.slice(1) // ['Woensdag', '8', 'mei'] => ['8', 'mei'] | ||
|
||
const day = Number(dayString) | ||
const month = monthToNumber(monthString) | ||
|
||
const year = guessYear({ | ||
day, | ||
month, | ||
}) | ||
|
||
return { day, month, year } | ||
} | ||
} | ||
|
||
const extractFromMoviePage = async (url: string): Promise<Screening[]> => { | ||
logger.info('extracting', { url }) | ||
|
||
const movie: XRayFromMoviePage = await xray(url, { | ||
title: 'h1 | trim | cleanTitle', | ||
metadata: '#credits | normalizeWhitespace | trim', | ||
screenings: xray('#movie-times li', [ | ||
{ | ||
date: '.date | trim', | ||
times: ['a .text | trim'], | ||
}, | ||
]), | ||
}) | ||
|
||
logger.info('extractFromMoviePage', { movie }) | ||
|
||
if (!hasEnglishSubtitles(movie)) { | ||
logger.warn('extractFromMoviePage without english subtitles', { | ||
url, | ||
title: movie.title, | ||
}) | ||
return [] | ||
} | ||
|
||
const screenings: Screening[] = movie.screenings.flatMap( | ||
({ date, times }) => { | ||
const { day, month, year } = splitDate(date) | ||
|
||
return times.map((time) => { | ||
const [hour, minute] = splitTime(time) | ||
|
||
return { | ||
title: movie.title, | ||
url, | ||
cinema: 'Focus Arnhem', | ||
date: DateTime.fromObject({ | ||
day, | ||
month, | ||
year, | ||
hour, | ||
minute, | ||
}).toJSDate(), | ||
} | ||
}) | ||
}, | ||
) | ||
|
||
return screenings | ||
} | ||
|
||
const extractFromMainPage = async () => { | ||
const url = 'https://www.focusarnhem.nl/special/focus-expat-cinema/' | ||
|
||
const movies: XRayFromMainPage[] = await xray( | ||
url, | ||
'#special-films .movie-block', | ||
[ | ||
{ | ||
title: '@title | trim | cleanTitle', | ||
url: '@href', | ||
}, | ||
], | ||
) | ||
|
||
logger.info('movies', { movies }) | ||
|
||
const screenings = ( | ||
await Promise.all(movies.map(({ url }) => extractFromMoviePage(url))) | ||
).flat() | ||
|
||
logger.info('extractFromMainPage', { screenings }) | ||
|
||
return screenings | ||
} | ||
|
||
if (require.main === module) { | ||
extractFromMainPage() | ||
.then((x) => JSON.stringify(x, null, 2)) | ||
.then(console.log) | ||
|
||
// extractFromMoviePage( | ||
// // 'https://www.focusarnhem.nl/agenda/un-metier-serieux/', | ||
// 'https://www.focusarnhem.nl/agenda/expat-cinema-the-peasants/', | ||
// ) | ||
// .then((x) => JSON.stringify(x, null, 2)) | ||
// .then(console.log) | ||
} | ||
|
||
export default extractFromMainPage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.