@@ -22,64 +22,37 @@ const createGetData = fetch => async (url, opts) => {
22
22
const embed = parse ( text )
23
23
24
24
const scripts = embed
25
- . filter ( e => e . tagName === 'html' ) [ 0 ]
26
- . children . filter ( e => e . tagName === 'body' ) [ 0 ]
27
- . children . filter ( e => e . tagName === 'script' )
25
+ . find ( el => el . tagName === 'html' )
26
+ . children . find ( el => el . tagName === 'body' )
27
+ . children . filter ( ( { tagName } ) => tagName === 'script' )
28
28
29
- const resourceScriptOld = scripts . filter (
30
- e => e . attributes . findIndex ( a => a . value === 'resource' ) !== - 1
29
+ let script = scripts . find ( script =>
30
+ script . attributes . some ( ( { value } ) => value === 'resource' )
31
31
)
32
32
33
- if ( resourceScriptOld . length > 0 ) {
33
+ if ( script !== undefined ) {
34
34
// found data in the older embed style
35
- return JSON . parse (
36
- decodeURIComponent ( resourceScriptOld [ 0 ] . children [ 0 ] . content )
37
- )
35
+ return normalizeData ( {
36
+ data : JSON . parse ( decodeURIComponent ( script . children [ 0 ] . content ) )
37
+ } )
38
38
}
39
39
40
- const resourceScriptNew = scripts . filter (
41
- e => e . attributes . findIndex ( a => a . value === 'initial-state' ) !== - 1
40
+ script = scripts . find ( script =>
41
+ script . attributes . some ( ( { value } ) => value === 'initial-state' )
42
42
)
43
43
44
- if ( resourceScriptNew . length > 0 ) {
44
+ if ( script !== undefined ) {
45
45
// found data in the new embed style
46
46
const data = JSON . parse (
47
- Buffer . from (
48
- decodeURIComponent ( resourceScriptNew [ 0 ] . children [ 0 ] . content ) ,
49
- 'base64'
50
- )
47
+ Buffer . from ( decodeURIComponent ( script . children [ 0 ] . content ) , 'base64' )
51
48
) . data . entity
52
49
// they removed/renamed some things, which for backwards compatibility we need to add back
53
50
data . external_urls = {
54
51
spotify : 'https://open.spotify.com/track/' + data . uri
55
52
}
56
53
data . release_date = data . releaseDate . isoString
57
54
data . audio = data . audioPreview . url
58
- return data
59
- }
60
-
61
- const hydrateScript = scripts . filter (
62
- e => e . children [ 0 ] && / % 2 2 d a t a % 2 2 % | " d a t a " : / . test ( e . children [ 0 ] . content )
63
- )
64
-
65
- if ( hydrateScript . length > 0 ) {
66
- // found hydration data
67
- // parsing via looking for { to be a little bit resistant to code changes
68
- const scriptContent = hydrateScript [ 0 ] . children [ 0 ] . content . includes (
69
- '%22data%22%'
70
- )
71
- ? decodeURIComponent ( hydrateScript [ 0 ] . children [ 0 ] . content )
72
- : hydrateScript [ 0 ] . children [ 0 ] . content
73
- return normalizeData (
74
- JSON . parse (
75
- '{' +
76
- scriptContent
77
- . split ( '{' )
78
- . slice ( 1 )
79
- . join ( '{' )
80
- . trim ( )
81
- )
82
- )
55
+ return normalizeData ( { data } )
83
56
}
84
57
85
58
throw new Error (
0 commit comments