Skip to content

Commit fba727e

Browse files
author
Ivo Barros
committed
add regex matching for a fall back
1 parent bc1573f commit fba727e

File tree

1 file changed

+34
-18
lines changed

1 file changed

+34
-18
lines changed

main/last_list/last_list.js

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,11 @@ function _lastList() {
100100
});
101101

102102
// regex patterns to match
103-
let regexElement = /data-youtube-id=[^>]*>/gmi;
104-
let regexYoutube = /data-youtube-id=\"([^\"]*)\"/gmi;
105-
let regexTitle = /data-track-name=\"([^\"]*)\"/gmi;
106-
let regexArtist = /data-artist-name=\"([^\"]*)\"/gmi;
103+
let regexElement = /<tr\s((.|\n)*?)chartlist-love-button((.|\n)*?)<\/tr>/gmi;
104+
let regexYoutube = /data-youtube-id=\"(.*?)\"/gmi;
105+
let regexTitle = /data-track-name=\"(.*?)\"/gmi;
106+
let regexArtist = /data-artist-name=\"(.*?)\"/gmi;
107+
let regexFallBack = /href=\"\/music\/([^\/]+)\/_\/([^\"]+)\"/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

Comments
 (0)