@@ -85,7 +85,9 @@ export default class ShazamSource {
8585 }
8686
8787 const extractArtworkFromImgAlt = ( ) => {
88- const ogImage = html . match ( / < m e t a p r o p e r t y = " o g : i m a g e " c o n t e n t = " ( [ ^ " ] + ) " / )
88+ const ogImage = html . match (
89+ / < m e t a p r o p e r t y = " o g : i m a g e " c o n t e n t = " ( [ ^ " ] + ) " /
90+ )
8991 if ( ogImage ) return ogImage [ 1 ]
9092
9193 let altIdx = html . indexOf ( 'alt="album cover"' )
@@ -114,6 +116,76 @@ export default class ShazamSource {
114116 'href="https://www.shazam.com/applemusic/song/'
115117 )
116118
119+ const durationMs = ( ( ) => {
120+ const findIso = ( ) => {
121+ const needles = [
122+ '"duration":"PT' ,
123+ '"duration": "PT' ,
124+ '\\"duration\\":\\"PT'
125+ ]
126+ for ( let i = 0 ; i < needles . length ; i ++ ) {
127+ const n = needles [ i ]
128+ const at = html . indexOf ( n )
129+ if ( at === - 1 ) continue
130+
131+ const start = at + n . length - 2
132+ const end =
133+ n [ 0 ] === '\\'
134+ ? html . indexOf ( '\\"' , start )
135+ : html . indexOf ( '"' , start )
136+ return end === - 1 ? null : html . slice ( start , end )
137+ }
138+ return null
139+ }
140+
141+ const parseIsoMs = ( iso ) => {
142+ if ( ! iso ) return 0
143+ const t = iso . indexOf ( 'T' )
144+ if ( t === - 1 ) return 0
145+
146+ let ms = 0
147+ let num = 0
148+ let frac = 0
149+ let fracDiv = 1
150+ let inFrac = false
151+
152+ for ( let i = t + 1 ; i < iso . length ; i ++ ) {
153+ const c = iso . charCodeAt ( i )
154+
155+ if ( c >= 48 && c <= 57 ) {
156+ const d = c - 48
157+ if ( inFrac ) {
158+ frac = frac * 10 + d
159+ fracDiv *= 10
160+ } else {
161+ num = num * 10 + d
162+ }
163+ continue
164+ }
165+
166+ if ( c === 46 ) {
167+ inFrac = true
168+ continue
169+ }
170+
171+ const val = inFrac ? num + frac / fracDiv : num
172+ if ( c === 72 ) ms += val * 3600000
173+ else if ( c === 77 ) ms += val * 60000
174+ else if ( c === 83 ) ms += val * 1000
175+ else break
176+
177+ num = 0
178+ frac = 0
179+ fracDiv = 1
180+ inFrac = false
181+ }
182+
183+ return ms ? Math . round ( ms ) : 0
184+ }
185+
186+ return parseIsoMs ( findIso ( ) )
187+ } ) ( )
188+
117189 const title =
118190 extractTextAfterClass ( 'NewTrackPageHeader_trackTitle__' ) || 'Unknown'
119191 const artist =
@@ -132,7 +204,7 @@ export default class ShazamSource {
132204 identifier,
133205 isSeekable : true ,
134206 author : artist ,
135- length : 0 ,
207+ length : durationMs || 0 ,
136208 isStream : false ,
137209 position : 0 ,
138210 title,
@@ -231,4 +303,4 @@ export default class ShazamSource {
231303 . replace ( '{w}' , artworkData . width )
232304 . replace ( '{h}' , artworkData . height )
233305 }
234- }
306+ }
0 commit comments