-
-
Notifications
You must be signed in to change notification settings - Fork 9
Cinemas #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Cinemas #38
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
76fe4a0
chore(deps): update all deps
bartholomej b6364ef
feat(cinemas): parse cinemas [kickoff]
bartholomej 659e7d9
feat(cinemas): parse cinemas
bartholomej 99aa00d
reafactor(cinemas): temp
bartholomej b50d7ea
fix(cinemas): url + coords
bartholomej 4f44ec0
test: fix tests
bartholomej 0de1a31
feat(server): add cinemas
bartholomej 362b178
test: add basic tests for cinemas
bartholomej 1f4c8b7
test: add cinema tests
bartholomej 5248be4
test: ignore testing express server
bartholomej a0d0672
fix: some small fixes
bartholomej 2a75d04
fix: small type fixes
bartholomej File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
|
@@ -85,4 +85,4 @@ | |
"lint-staged": { | ||
"*.ts": "eslint --cache --fix" | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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,107 @@ | ||
import { | ||
CSFDCinemaGroupedFilmsByDate, | ||
CSFDCinemaMeta, | ||
CSFDCinemaMovie | ||
} from 'interfaces/cinema.interface'; | ||
import { HTMLElement } from 'node-html-parser'; | ||
import { CSFDColorRating } from '../interfaces/global'; | ||
import { Colors } from '../interfaces/user-ratings.interface'; | ||
import { parseColor, parseIdFromUrl } from './global.helper'; | ||
|
||
export const getColorRating = (el: HTMLElement): CSFDColorRating => { | ||
return parseColor(el?.classNames.split(' ').pop() as Colors); | ||
}; | ||
|
||
export const getCinemaId = (el: HTMLElement | null): number => { | ||
const id = el?.id?.split('-')[1]; | ||
return +id; | ||
}; | ||
|
||
export const getId = (url: string): number => { | ||
if (url) { | ||
return parseIdFromUrl(url); | ||
} | ||
return null; | ||
bartholomej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
export const getCoords = (el: HTMLElement | null): { lat: number; lng: number } => { | ||
bartholomej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
||
bartholomej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const linkMapsEl = el.querySelector('a[href*="q="]'); | ||
if (!linkMapsEl) return null; | ||
|
||
const linkMaps = linkMapsEl.getAttribute('href'); | ||
bartholomej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const [_, latLng] = linkMaps.split('q='); | ||
|
||
const coords = latLng.split(','); | ||
if (coords.length !== 2) return null; | ||
|
||
const lat = Number(coords[0]); | ||
const lng = Number(coords[1]); | ||
if (Number.isFinite(lat) && Number.isFinite(lng)) { | ||
return { lat, lng }; | ||
} | ||
return null; | ||
}; | ||
|
||
export const getCinemaUrl = (el: HTMLElement | null): string => { | ||
bartholomej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return el.querySelector('a[title="Přejít na webovou stránku kina"]')?.attributes.href ?? ''; | ||
}; | ||
|
||
export const parseCinema = (el: HTMLElement | null): { city: string; name: string } => { | ||
const title = el.querySelector('.box-header h2').innerText.trim(); | ||
const [city, name] = title.split(' - '); | ||
return { city, name }; | ||
bartholomej marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; | ||
|
||
export const getGroupedFilmsByDate = (el: HTMLElement | null): CSFDCinemaGroupedFilmsByDate[] => { | ||
const divs = el.querySelectorAll(':scope > div'); | ||
const getDatesAndFilms = divs | ||
.map((_, index) => index) | ||
.filter((index) => index % 2 === 0) | ||
.map((index) => { | ||
const [date, films] = divs.slice(index, index + 2); | ||
const dateText = date?.firstChild?.textContent?.trim() ?? ""; | ||
bartholomej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
return { date: dateText, films: getFilms('', films) }; | ||
}); | ||
|
||
return getDatesAndFilms; | ||
}; | ||
|
||
export const getFilms = (date: string, el: HTMLElement | null): CSFDCinemaMovie[] => { | ||
const filmNodes = el.querySelectorAll('.cinema-table tr'); | ||
|
||
const films = filmNodes.map((filmNode) => { | ||
const url = filmNode.querySelector('td.name h3 a')?.attributes.href; | ||
const id = getId(url); | ||
const title = filmNode.querySelector('.name h3')?.text.trim(); | ||
const colorRating = getColorRating(filmNode.querySelector('.name .icon')); | ||
const showTimes = filmNode.querySelectorAll('.td-time')?.map((x) => x.textContent.trim()); | ||
const meta = filmNode.querySelectorAll('.td-title span')?.map((x) => x.text.trim()); | ||
|
||
return { | ||
id, | ||
title, | ||
url, | ||
colorRating, | ||
showTimes, | ||
meta: parseMeta(meta) | ||
}; | ||
}); | ||
return films; | ||
}; | ||
|
||
export const parseMeta = (meta: string[]): CSFDCinemaMeta[] => { | ||
const metaConvert: CSFDCinemaMeta[] = []; | ||
|
||
for (const element of meta) { | ||
if (element === 'T') { | ||
metaConvert.push('subtitles'); | ||
} else if (element === 'D') { | ||
metaConvert.push('dubbing'); | ||
} else { | ||
metaConvert.push(element); | ||
} | ||
} | ||
|
||
return metaConvert; | ||
}; |
This file contains hidden or 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 hidden or 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,25 @@ | ||
import { CSFDMovieListItem } from './movie.interface'; | ||
|
||
export interface CSFDCinema { | ||
id: number; | ||
name: string; | ||
city: string; | ||
url: string; | ||
coords: { lat: number; lng: number }; | ||
region?: string; | ||
screenings: CSFDCinemaGroupedFilmsByDate[]; | ||
} | ||
|
||
export interface CSFDCinemaGroupedFilmsByDate { | ||
date: string; | ||
films: CSFDCinemaMovie[]; | ||
} | ||
|
||
export interface CSFDCinemaMovie extends CSFDMovieListItem { | ||
meta: CSFDCinemaMeta[]; | ||
showTimes: string[]; | ||
} | ||
|
||
export type CSFDCinemaMeta = 'dubbing' | '3D' | 'subtitles' | string; | ||
|
||
export type CSFDCinemaPeriod = 'today' | 'weekend' | 'week' | 'tomorrow' | 'month'; |
This file contains hidden or 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,48 @@ | ||
import { HTMLElement, parse } from 'node-html-parser'; | ||
import { fetchPage } from '../fetchers'; | ||
import { CSFDCinema, CSFDCinemaPeriod } from '../interfaces/cinema.interface'; | ||
import { cinemasUrl } from '../vars'; | ||
import { | ||
getCinemaId, | ||
getCinemaUrl, | ||
getCoords, | ||
getGroupedFilmsByDate, | ||
parseCinema | ||
} from './../helpers/cinema.helper'; | ||
|
||
export class CinemaScraper { | ||
private cinema: CSFDCinema[]; | ||
|
||
public async cinemas( | ||
district: number = 1, | ||
period: CSFDCinemaPeriod = 'today' | ||
): Promise<CSFDCinema[]> { | ||
const url = cinemasUrl(district, period); | ||
const response = await fetchPage(url); | ||
|
||
const cinemasHtml = parse(response); | ||
|
||
const contentNode = cinemasHtml.querySelectorAll('#snippet--cinemas section.box'); | ||
|
||
this.buildCinemas(contentNode); | ||
return this.cinema; | ||
} | ||
|
||
private buildCinemas(contentNode: HTMLElement[]) { | ||
const cinemas: CSFDCinema[] = []; | ||
|
||
contentNode.map((x) => { | ||
bartholomej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const cinema: CSFDCinema = { | ||
id: getCinemaId(x), | ||
name: parseCinema(x)?.name, | ||
city: parseCinema(x)?.city, | ||
bartholomej marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
url: getCinemaUrl(x), | ||
coords: getCoords(x), | ||
screenings: getGroupedFilmsByDate(x) | ||
}; | ||
cinemas.push(cinema); | ||
}); | ||
|
||
this.cinema = cinemas; | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.