Skip to content

Commit

Permalink
fix: lantarenvenster (29 feb next year edge case)
Browse files Browse the repository at this point in the history
  • Loading branch information
ckuijjer committed Dec 6, 2023
1 parent cd7a941 commit 34dbaef
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 95 deletions.
14 changes: 6 additions & 8 deletions cloud/scrapers/filmhuislumen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,12 @@ const extractFromMainPage = async () => {

const [hour, minute] = splitTimeDot(time)

const year = guessYear(
DateTime.fromObject({
day,
month,
hour,
minute,
}),
)
const year = guessYear({
day,
month,
hour,
minute,
})

return {
title,
Expand Down
14 changes: 6 additions & 8 deletions cloud/scrapers/forumgroningen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,12 @@ const extractFromMoviePage = async ({
const [startTime, endTime] = time.split(/ tot /)
const [hour, minute] = splitTime(startTime)

const year = guessYear(
DateTime.fromObject({
day,
month,
hour,
minute,
}),
)
const year = guessYear({
day,
month,
hour,
minute,
})

return {
title: scrapeResult.title,
Expand Down
14 changes: 6 additions & 8 deletions cloud/scrapers/hartlooper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,12 @@ const extractFromMoviePage = async ({ url }: { url: string }) => {
const month = fullMonthToNumberDutch(date.month)
const [hour, minute] = splitTime(time)

const year = guessYear(
DateTime.fromObject({
day,
month,
hour,
minute,
}),
)
const year = guessYear({
day,
month,
hour,
minute,
})

return {
title: cleanTitle(movie.title),
Expand Down
14 changes: 6 additions & 8 deletions cloud/scrapers/lab111.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,12 @@ const extractFromMainPage = async () => {
const day = Number(dayString)
const month = shortMonthToNumberEnglish(monthString)
const [hour, minute] = splitTime(time)
const year = guessYear(
DateTime.fromObject({
day,
month,
hour,
minute,
}),
)
const year = guessYear({
day,
month,
hour,
minute,
})

logger.debug('extracted date', {
dateString: date,
Expand Down
16 changes: 8 additions & 8 deletions cloud/scrapers/lantarenvenster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,16 @@ export const extractFromMoviePage = async (
.map((time) => {
const [dayOfWeek, dayString, monthString] = date.split(' ')
const day = Number(dayString)

const month = shortMonthToNumberDutch(monthString)
const [hour, minute] = splitTime(time)
const year = guessYear(
DateTime.fromObject({
day,
month,
hour,
minute,
}),
)

const year = guessYear({
day,
month,
hour,
minute,
})

return {
title: cleanTitle(movie.title),
Expand Down
10 changes: 4 additions & 6 deletions cloud/scrapers/lumiere.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,10 @@ const extractFromMoviePage = async (url: string): Promise<Screening[]> => {
const day = Number(dayString)
const month = shortMonthToNumberDutch(monthString)

const year = guessYear(
DateTime.fromObject({
day,
month,
}),
)
const year = guessYear({
day,
month,
})

return times.map((time) => {
const [hour, minute] = splitTime(time)
Expand Down
23 changes: 16 additions & 7 deletions cloud/scrapers/rialto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import { DateTime } from 'luxon'

import guessYear from './utils/guessYear'
import { logger as parentLogger } from '../powertools'
import { fullMonthToNumberEnglish } from './utils/monthToNumber'
import splitTime from './utils/splitTime'

const logger = parentLogger.createChild({
persistentLogAttributes: {
Expand Down Expand Up @@ -53,13 +55,20 @@ const extractFromMoviePage = async ({
([cinema, dates]) => {
return Object.entries(dates).flatMap(([dateString, times]) => {
return times.map(({ time, link, text }) => {
let date = DateTime.fromFormat(
`${dateString} ${time}`,
'EEEE d MMMM H:mm',
)

const year = guessYear(date)
date = date.set({ year })
const [dayOfWeek, dayString, monthString] = dateString.split(/\s+/)
const day = Number(dayString)
const month = fullMonthToNumberEnglish(monthString)
const [hour, minute] = splitTime(time)

const year = guessYear({ day, month, hour, minute })

const date = DateTime.fromObject({
year,
day,
month,
hour,
minute,
})

return {
title,
Expand Down
14 changes: 6 additions & 8 deletions cloud/scrapers/schuur.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ const extractFromMainPage = async () => {
const month = shortMonthToNumberDutch(monthString)
const [hour, minute] = splitTime(time)

const year = guessYear(
DateTime.fromObject({
day,
month,
hour,
minute,
}),
)
const year = guessYear({
day,
month,
hour,
minute,
})

return {
title,
Expand Down
14 changes: 6 additions & 8 deletions cloud/scrapers/slachtstraat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,12 @@ const extractFromMoviePage = async ({ url }: { url: string }) => {
const month = fullMonthToNumberDutch(date.month)
const [hour, minute] = splitTime(time)

const year = guessYear(
DateTime.fromObject({
day,
month,
hour,
minute,
}),
)
const year = guessYear({
day,
month,
hour,
minute,
})

return {
title: cleanTitle(movie.title),
Expand Down
14 changes: 6 additions & 8 deletions cloud/scrapers/springhaver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,12 @@ const extractFromMoviePage = async ({ url }: { url: string }) => {
const month = fullMonthToNumberDutch(date.month)
const [hour, minute] = splitTime(time)

const year = guessYear(
DateTime.fromObject({
day,
month,
hour,
minute,
}),
)
const year = guessYear({
day,
month,
hour,
minute,
})

return {
title: cleanTitle(movie.title),
Expand Down
14 changes: 6 additions & 8 deletions cloud/scrapers/studiok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,12 @@ const extractFromMoviePage = async (url: string) => {
})
.map(({ time, title }) => {
const [hour, minute] = splitTime(time)
const year = guessYear(
DateTime.fromObject({
day,
month,
hour,
minute,
}),
)
const year = guessYear({
day,
month,
hour,
minute,
})

return {
title: cleanTitle(title),
Expand Down
14 changes: 6 additions & 8 deletions cloud/scrapers/themovies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,12 @@ const extractFromMainPage = async () => {
.split(':')
.map(Number)

const year = guessYear(
DateTime.fromObject({
day,
month,
hour,
minute,
}),
)
const year = guessYear({
day,
month,
hour,
minute,
})

return {
title,
Expand Down
12 changes: 10 additions & 2 deletions cloud/scrapers/utils/guessYear.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { DateTime } from 'luxon'
import { DateTime, DateObjectUnits } from 'luxon'

// if the date is over half a year in the past, it's likely one belonging to next year
const guessYear = (date: DateTime) => {
const guessYear = (dateObjects: DateObjectUnits) => {
const now = DateTime.local()

let date = DateTime.fromObject(dateObjects)

// handle edge case where you're looking for 29 feb in a leap year that's next year
if (!date.isValid && date.invalidReason === 'unit out of range') {
date = DateTime.fromObject({ ...dateObjects, year: now.year + 1 })
}

if (date < now.minus({ months: 6 })) {
return date.year + 1
} else {
Expand Down
26 changes: 26 additions & 0 deletions cloud/scrapers/utils/monthToNumber.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,32 @@ export const shortMonthToNumberDutch = (month: string) => {
return monthNumber
}

export const fullMonthToNumberEnglish = (month: string) => {
const monthNumber =
[
'january',
'february',
'march',
'april',
'may',
'june',
'july',
'august',
'september',
'october',
'november',
'december',
].indexOf(month.toLowerCase()) + 1

if (monthNumber === 0) {
throw new Error(
`invalid month ${month}, might you need the Dutch version of this function?`,
)
}

return monthNumber
}

export const shortMonthToNumberEnglish = (month: string) => {
const monthNumber =
[
Expand Down

0 comments on commit 34dbaef

Please sign in to comment.