@@ -10,19 +10,44 @@ import { RecipeMarker } from '../helpers/RecipeMarker';
1010import { HanayamaHuzzle } from './HanayamaHuzzle' ;
1111import { HanayamaHuzzlesRecipeSettings } from './settings/HanayamaHuzzlesRecipeSettings' ;
1212
13+ interface HanayamaHuzzlesScrapeDataSource {
14+ url : string ;
15+ level ?: string ;
16+ }
17+
1318export class HanayamaHuzzlesRecipe implements Recipe {
1419 static readonly NAME = 'Hanayama Huzzles' ;
1520 static readonly WEBPAGE = 'https://hanayama-toys.com/product-category/puzzles/huzzle' ;
16- static readonly CHESS_PUZZLES_WEBPAGE = 'https://hanayama-toys.com/product-category/puzzles/huzzle/chess-puzzle' ;
21+ static readonly CHESS_PUZZLES_DATA_SOURCE : HanayamaHuzzlesScrapeDataSource = {
22+ url : 'https://hanayama-toys.com/product-category/puzzles/huzzle/chess-puzzle'
23+ } ;
1724
1825 static readonly #HEADERS: readonly string [ ] = [ 'Level' , 'Index' , 'Name' , 'Picture' , 'Status' ] ;
19- static readonly #SCRAPE_URLS: readonly string [ ] = [
20- 'https://hanayama-toys.com/product-category/puzzles/huzzle/level-1-fun' ,
21- 'https://hanayama-toys.com/product-category/puzzles/huzzle/level-2-easy' ,
22- 'https://hanayama-toys.com/product-category/puzzles/huzzle/level-3-normal' ,
23- 'https://hanayama-toys.com/product-category/puzzles/huzzle/level-4-hard' ,
24- 'https://hanayama-toys.com/product-category/puzzles/huzzle/level-5-expert' ,
25- 'https://hanayama-toys.com/product-category/puzzles/huzzle/level-6-grand-master'
26+ static readonly #SCRAPE_DATA_SOURCES: readonly HanayamaHuzzlesScrapeDataSource [ ] = [
27+ {
28+ url : 'https://hanayama-toys.com/product-category/puzzles/huzzle/level-1-fun' ,
29+ level : '1' ,
30+ } ,
31+ {
32+ url : 'https://hanayama-toys.com/product-category/puzzles/huzzle/level-2-easy' ,
33+ level : '2' ,
34+ } ,
35+ {
36+ url : 'https://hanayama-toys.com/product-category/puzzles/huzzle/level-3-normal' ,
37+ level : '3' ,
38+ } ,
39+ {
40+ url : 'https://hanayama-toys.com/product-category/puzzles/huzzle/level-4-hard' ,
41+ level : '4' ,
42+ } ,
43+ {
44+ url : 'https://hanayama-toys.com/product-category/puzzles/huzzle/level-5-expert' ,
45+ level : '5' ,
46+ } ,
47+ {
48+ url : 'https://hanayama-toys.com/product-category/puzzles/huzzle/level-6-grand-master' ,
49+ level : '6' ,
50+ }
2651 ] ;
2752
2853 #marker = new RecipeMarker ( HanayamaHuzzlesRecipe . NAME ) ;
@@ -52,24 +77,29 @@ export class HanayamaHuzzlesRecipe implements Recipe {
5277 }
5378
5479 async #scrapeHuzzles( ) : Promise < HanayamaHuzzle [ ] > {
55- const metadataRegex = new RegExp ( / \w + [ ] (?< level > \d + ) - (?< index > \d + ) [ ] (?< name > .+ ) / ) ; // https://regex101.com/r/1vGzHd/2
80+ const metadataRegex = new RegExp ( / \w + [ ] \d + - (?< index > \d + ) [ ] (?< name > .+ ) / ) ; // https://regex101.com/r/1vGzHd/3
5681
57- const urls = [ ...HanayamaHuzzlesRecipe . #SCRAPE_URLS ] ;
82+ const dataSources = [ ...HanayamaHuzzlesRecipe . #SCRAPE_DATA_SOURCES ] ;
5883 if ( this . settings . includeChessPuzzles ) {
59- urls . push ( HanayamaHuzzlesRecipe . CHESS_PUZZLES_WEBPAGE ) ;
84+ dataSources . push ( HanayamaHuzzlesRecipe . CHESS_PUZZLES_DATA_SOURCE ) ;
6085 }
61- const scraper = new WebsiteScraper ( urls ) ;
86+ const scraper = new WebsiteScraper (
87+ dataSources . map ( dataSource => ( {
88+ url : dataSource . url ,
89+ context : dataSource . level
90+ } ) )
91+ ) ;
6292
6393 return await scraper . scrape (
6494 content => {
6595 return Array . from ( content . querySelectorAll ( '#main > .products > .product' ) ) ;
6696 } ,
67- product => {
97+ ( product , sourceLevel ) => {
6898 const title = product . querySelector ( '.product-info > .product-title > a' ) ?. textContent || '' ;
6999 const titleMatch = title . match ( metadataRegex ) ;
70100 const titleGroups = titleMatch ?. groups ;
71101
72- const level = titleGroups != null ? titleGroups . level : 'N/A' ;
102+ const level = sourceLevel ?? 'N/A' ;
73103 const index = titleGroups != null ? titleGroups . index : 'N/A' ;
74104 const name = titleGroups != null ? titleGroups . name : title ;
75105
0 commit comments