1
+ const RENT = 'rent' ;
2
+ const SALE = 'sale' ;
3
+
1
4
const textOrNull = textElement => {
2
5
if ( textElement === null ) {
3
6
return null ;
@@ -6,57 +9,91 @@ const textOrNull = textElement => {
6
9
}
7
10
} ;
8
11
9
- export const siteHosts = {
12
+ const sites = {
10
13
SREALITY : {
11
- hostString : 'sreality.cz'
14
+ id : 'sreality' ,
12
15
} ,
13
16
BEZREALITKY : {
14
- hostString : 'bezrealitky.cz'
17
+ id : 'bezrealitky' ,
15
18
} ,
16
19
MAXIREALITY : {
17
- hostString : 'maxirealitypraha.cz'
20
+ id : 'maxirealitypraha' ,
18
21
} ,
19
22
REALITY_IDNES : {
20
- hostString : 'reality. idnes.cz'
23
+ id : 'idnes' ,
21
24
}
22
25
} ;
23
26
24
- /**
25
- *
26
- * @param {{ hostString: String } } hostId
27
- * @param {String } host
28
- * @return {Number|undefined }
29
- */
30
- export const isCurrentHost = ( hostId , host ) => host . includes ( hostId . hostString ) ;
31
-
32
27
const priceAreaGuard = ( price , area ) => ( area && ! isNaN ( area ) && ( price && ! isNaN ( price ) ) ) && price / area ;
33
28
29
+ const getHostPredicate = ( locationHost ) => ( siteHost ) => locationHost . includes ( siteHost ) ;
30
+
31
+ const containsBoxWords = ( selector , words ) => {
32
+ const containsNodeWord = ( node , word ) => node . textContent . includes ( word ) ;
33
+
34
+ const node = document . querySelector ( selector ) ;
35
+ if ( ! node || ! words . length ) {
36
+ return false ;
37
+ }
38
+
39
+ const mapWords = ( word ) => containsNodeWord ( node , word ) ;
40
+ // ['foo'] => [true]
41
+ // ['foo', 'bar', 'baz'] => [true, false, true] => false
42
+ return words . map ( mapWords ) . filter ( Boolean ) . length === words . length ;
43
+ } ;
44
+
45
+ // this is business logic, so it may contain site specific settings/params
46
+ // underneath it should only call some generic functions
47
+ const extractAdType = ( locationHost ) => {
48
+ const verify = getHostPredicate ( locationHost ) ;
49
+
50
+ if ( verify ( sites . SREALITY . id ) || verify ( sites . SREALITY . id ) || verify ( sites . MAXIREALITY . id ) ) {
51
+ if ( / p r o n a j e m / i. test ( location . pathname ) ) {
52
+ return RENT ;
53
+ }
54
+ return SALE ;
55
+ }
56
+
57
+ if ( verify ( 'bezrealitky' ) ) {
58
+ const selector = '.box-params.col-1' ;
59
+ return containsBoxWords ( selector , [ 'typ' , 'nabídky' , 'Pronájem' ] ) ? RENT : SALE ;
60
+ }
61
+ } ;
62
+
34
63
// TODO add extractor's methods for sites dynamically
35
64
export const extractors = {
36
- getAddress ( host ) {
37
- if ( isCurrentHost ( siteHosts . SREALITY , host ) ) {
65
+ getAddress ( ) {
66
+ const verify = getHostPredicate ( window . location . host ) ;
67
+ if ( verify ( sites . SREALITY . id ) ) {
38
68
return textOrNull ( document . querySelector ( '.location-text' ) ) ;
39
69
}
40
70
41
- if ( isCurrentHost ( siteHosts . BEZREALITKY , host ) ) {
71
+ if ( verify ( sites . BEZREALITKY . id ) ) {
42
72
return textOrNull ( document . querySelector ( 'header h2' ) ) ;
43
73
}
44
74
45
- if ( isCurrentHost ( siteHosts . MAXIREALITY , host ) ) {
75
+ if ( verify ( sites . MAXIREALITY . id ) ) {
46
76
const addressRow = Array . from ( document . querySelectorAll ( 'tr' ) )
47
77
. filter ( node => node . textContent . includes ( 'Adresa' ) ) [ 0 ] ;
48
78
return addressRow && addressRow . querySelector ( 'td' ) . innerHTML . replace ( / < b r > / g, ' ' ) . trim ( ) ;
49
79
}
50
80
51
- if ( isCurrentHost ( siteHosts . REALITY_IDNES , host ) ) {
81
+ if ( verify ( sites . REALITY_IDNES . id ) ) {
52
82
return textOrNull ( document . querySelector ( '.realAddress' ) ) ;
53
83
}
54
84
55
85
RR . logError ( 'cannot parse address on page: ' , window . location ) ;
56
86
return null ;
57
87
} ,
58
- getPrices ( host ) {
59
- if ( isCurrentHost ( siteHosts . SREALITY , host ) ) {
88
+ extractSquarePrice ( ) {
89
+ const adType = extractAdType ( window . location . host ) ;
90
+ const verify = getHostPredicate ( window . location . host ) ;
91
+
92
+ if ( adType === RENT ) {
93
+ return ;
94
+ }
95
+
96
+ if ( verify ( sites . SREALITY . id ) ) {
60
97
const propertyParams = Array . from ( document . querySelectorAll ( '.params li' ) ) ;
61
98
const priceRow = propertyParams . filter ( p => p . innerHTML . includes ( 'Celková cena' ) ) [ 0 ] ;
62
99
const areaRow = propertyParams . filter ( p => p . innerHTML . includes ( 'Užitná' ) ) [ 0 ] ;
@@ -66,7 +103,7 @@ export const extractors = {
66
103
return priceAreaGuard ( price , area ) ;
67
104
}
68
105
69
- if ( isCurrentHost ( siteHosts . BEZREALITKY , host ) ) {
106
+ if ( verify ( sites . BEZREALITKY . id ) ) {
70
107
const propertyParams = Array . from ( document . querySelectorAll ( '.box-params .row' ) ) ;
71
108
const areaRow = propertyParams . filter ( item => item . innerHTML . includes ( 'plocha' ) ) [ 0 ] ; // returns DOM node
72
109
const priceRow = propertyParams . filter ( item => item . innerHTML . includes ( 'cena' ) ) [ 0 ] ; // returns DOM node
@@ -80,7 +117,7 @@ export const extractors = {
80
117
return priceAreaGuard ( price , area ) ;
81
118
}
82
119
83
- if ( isCurrentHost ( siteHosts . MAXIREALITY , host ) ) {
120
+ if ( verify ( sites . MAXIREALITY . id ) ) {
84
121
const areaRow = Array . from ( document . querySelectorAll ( '#makler_zaklad > table tr' ) )
85
122
. filter ( node => node . innerHTML . includes ( 'Užitná plocha' ) ) [ 0 ] ;
86
123
const priceNode = document . querySelector ( '.two.price' ) ;
@@ -90,12 +127,12 @@ export const extractors = {
90
127
return priceAreaGuard ( price , area ) ;
91
128
}
92
129
93
- if ( isCurrentHost ( siteHosts . REALITY_IDNES , host ) ) {
130
+ if ( verify ( sites . REALITY_IDNES . id ) ) {
94
131
const areaText = $ ( '.parameters .leftCol dt:contains("Užitná plocha")' ) . next ( ) . text ( ) ;
95
132
const area = Number . parseInt ( areaText ) ; // eg. when text is "34 m2" Number.parseInt can strip text parts and parse it as just 34
96
133
97
- const priceText = document . querySelectorAll ( '.priceBox strong' ) [ 0 ] . innerHTML ;
98
- const price = Number . parseInt ( priceText . replace ( / & n b s p ; / gi, '' ) ) ;
134
+ const priceText = document . querySelectorAll ( '.priceBox strong' ) [ 0 ] . innerHTML ;
135
+ const price = Number . parseInt ( priceText . replace ( / & n b s p ; / gi, '' ) ) ;
99
136
100
137
return priceAreaGuard ( price , area ) ;
101
138
}
0 commit comments