@@ -100,10 +100,11 @@ function _lastList() {
100100 } ) ;
101101
102102 // regex patterns to match
103- let regexElement = / d a t a - y o u t u b e - i d = [ ^ > ] * > / gmi;
104- let regexYoutube = / d a t a - y o u t u b e - i d = \" ( [ ^ \" ] * ) \" / gmi;
105- let regexTitle = / d a t a - t r a c k - n a m e = \" ( [ ^ \" ] * ) \" / gmi;
106- let regexArtist = / d a t a - a r t i s t - n a m e = \" ( [ ^ \" ] * ) \" / gmi;
103+ let regexElement = / < t r \s ( ( .| \n ) * ?) c h a r t l i s t - l o v e - b u t t o n ( ( .| \n ) * ?) < \/ t r > / gmi;
104+ let regexYoutube = / d a t a - y o u t u b e - i d = \" ( .* ?) \" / gmi;
105+ let regexTitle = / d a t a - t r a c k - n a m e = \" ( .* ?) \" / gmi;
106+ let regexArtist = / d a t a - a r t i s t - n a m e = \" ( .* ?) \" / gmi;
107+ let regexFallBack = / h r e f = \" \/ m u s i c \/ ( [ ^ \/ ] + ) \/ _ \/ ( [ ^ \" ] + ) \" / gmi;
107108
108109 // create playlist
109110 let playlist = plman . FindOrCreatePlaylist ( playlistName , false ) ;
@@ -127,29 +128,44 @@ function _lastList() {
127128 let matches = [ ...content . matchAll ( regexElement ) ] ;
128129 this . log ( `${ matches . length } matches found` ) ;
129130 matches . every ( ( match ) => {
130-
131+ // get track info from youtube data element
131132 let youtube = [ ...match [ 0 ] . matchAll ( regexYoutube ) ] ;
132133 let title = [ ...match [ 0 ] . matchAll ( regexTitle ) ] ;
133134 let artist = [ ...match [ 0 ] . matchAll ( regexArtist ) ] ;
134135
135- // TODO what if only youtube doesn't exist but have track in library?
136- if ( title . length == 0 || artist . length == 0 || youtube . length == 0 ) {
136+ if ( title . length && artist . length ) {
137+ // clean strings
138+ title = this . cleanString ( decodeURI ( title [ 0 ] [ 1 ] ) ) ;
139+ artist = this . cleanString ( decodeURI ( artist [ 0 ] [ 1 ] ) ) ;
140+ } else { // fallback to href if youtube data element is not available
141+ let fallbackData = [ ...match [ 0 ] . matchAll ( regexFallBack ) ] ;
142+ if ( ! fallbackData . length ) {
143+ return true ;
144+ }
145+ // clean strings
146+ artist = decodeURIComponent ( fallbackData [ 0 ] [ 1 ] ) . replace ( / \+ / g, " " ) ;
147+ title = decodeURIComponent ( fallbackData [ 0 ] [ 2 ] ) . replace ( / \+ / g, " " ) ;
148+ }
149+
150+ // if no title or artist, skip
151+ if ( ! title . length || ! artist . length ) {
137152 return true ;
138153 }
139154
140- youtube = youtube [ 0 ] [ 1 ] ;
141- title = this . cleanString ( decodeURI ( title [ 0 ] [ 1 ] ) ) ;
142- artist = this . cleanString ( decodeURI ( artist [ 0 ] [ 1 ] ) ) ;
155+ // get file from library
156+ let file = indexedLibrary [ `${ artist . toLowerCase ( ) } - ${ title . toLowerCase ( ) } ` ] ;
157+ // if no file and no youtube link, skip
158+ if ( ! file && ! youtube . length ) {
159+ return true ;
160+ }
143161
144162 // add to items to add
145- if ( artist . length && title . length ) {
146- itemsToAdd . push ( {
147- youtube : youtube ,
148- title : title ,
149- artist : artist ,
150- file : indexedLibrary [ `${ artist . toLowerCase ( ) } - ${ title . toLowerCase ( ) } ` ]
151- } ) ;
152- }
163+ itemsToAdd . push ( {
164+ youtube : youtube . length ? youtube [ 0 ] [ 1 ] : null ,
165+ title : title ,
166+ artist : artist ,
167+ file : file
168+ } ) ;
153169
154170 return true ;
155171 } ) ;
0 commit comments