@@ -10,19 +10,44 @@ import { RecipeMarker } from '../helpers/RecipeMarker';
10
10
import { HanayamaHuzzle } from './HanayamaHuzzle' ;
11
11
import { HanayamaHuzzlesRecipeSettings } from './settings/HanayamaHuzzlesRecipeSettings' ;
12
12
13
+ interface HanayamaHuzzlesScrapeDataSource {
14
+ url : string ;
15
+ level ?: string ;
16
+ }
17
+
13
18
export class HanayamaHuzzlesRecipe implements Recipe {
14
19
static readonly NAME = 'Hanayama Huzzles' ;
15
20
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
+ } ;
17
24
18
25
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
+ }
26
51
] ;
27
52
28
53
#marker = new RecipeMarker ( HanayamaHuzzlesRecipe . NAME ) ;
@@ -52,24 +77,29 @@ export class HanayamaHuzzlesRecipe implements Recipe {
52
77
}
53
78
54
79
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
56
81
57
- const urls = [ ...HanayamaHuzzlesRecipe . #SCRAPE_URLS ] ;
82
+ const dataSources = [ ...HanayamaHuzzlesRecipe . #SCRAPE_DATA_SOURCES ] ;
58
83
if ( this . settings . includeChessPuzzles ) {
59
- urls . push ( HanayamaHuzzlesRecipe . CHESS_PUZZLES_WEBPAGE ) ;
84
+ dataSources . push ( HanayamaHuzzlesRecipe . CHESS_PUZZLES_DATA_SOURCE ) ;
60
85
}
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
+ ) ;
62
92
63
93
return await scraper . scrape (
64
94
content => {
65
95
return Array . from ( content . querySelectorAll ( '#main > .products > .product' ) ) ;
66
96
} ,
67
- product => {
97
+ ( product , sourceLevel ) => {
68
98
const title = product . querySelector ( '.product-info > .product-title > a' ) ?. textContent || '' ;
69
99
const titleMatch = title . match ( metadataRegex ) ;
70
100
const titleGroups = titleMatch ?. groups ;
71
101
72
- const level = titleGroups != null ? titleGroups . level : 'N/A' ;
102
+ const level = sourceLevel ?? 'N/A' ;
73
103
const index = titleGroups != null ? titleGroups . index : 'N/A' ;
74
104
const name = titleGroups != null ? titleGroups . name : title ;
75
105
0 commit comments