Skip to content

Commit e0a485c

Browse files
authored
Merge pull request #2 from L3v3L/feature-fallback-to-url
2 parents c571801 + b8b009e commit e0a485c

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

.github/release.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1+
# .github/release.yml
12

3+
changelog:
4+
exclude:
5+
labels:
6+
- ignore-for-release
7+
authors:
8+
- octocat
9+
categories:
10+
- title: Changes
11+
labels:
12+
- "*"

foo_last_list_smp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ include('main\\last_list\\last_list_button.js');
66
window.DefineScript("Last List",
77
{
88
author: "Ivo Barros",
9-
version: "0.4",
9+
version: "0.5",
1010
});
1111

1212
const lastList = new _lastList();

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)