-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate-event-setlist.user.js
119 lines (95 loc) · 4.42 KB
/
generate-event-setlist.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// ==UserScript==
// @name MusicBrainz: Generate event setlist
// @author Kebabpizza
// @namespace Kebabpizza
// @description musicbrainz.org: Generate event setlist from relationships
// @match *://*.musicbrainz.org/event/*/edit
// @match *://*.musicbrainz.org/event/create
// @match *://*.mbsandbox.org/event/*/edit
// @match *://*.mbsandbox.org/event/create
// @downloadURL https://github.com/Kebabpizza/musicbrainz-userscripts/raw/master/generate-event-setlist.user.js
// @updateURL https://github.com/Kebabpizza/musicbrainz-userscripts/raw/master/generate-event-setlist.user.js
// @version 2017-09-16
// @grant none
// ==/UserScript==
let allNightContainer = $("<div class='row no-label'></div>'")
let allNightLabel = $("<label for='allNight'>All night</label>")
let allNightButton = $("<input type='checkbox' id='allNight' />")
let appendContainer = $("<div class='row no-label'></div>'")
let appendLabel = $("<label for='append'>Append</label>")
let appendButton = $("<input type='checkbox' id='append' />")
let goContainer = $("<div class='row no-label buttons' style='margin-top: 0 !important; margin-bottom: 0 !important' />")
let goBtn = $("<button class='submit positive' type='button'>Generate setlist</button>")
let textarea = $("textarea")[0]
allNightButton.appendTo(allNightContainer)
allNightLabel.appendTo(allNightContainer)
appendButton.appendTo(appendContainer)
appendLabel.appendTo(appendContainer)
allNightContainer.insertAfter(textarea)
appendContainer.insertAfter(allNightContainer)
goBtn.appendTo(goContainer)
goContainer.insertAfter(appendContainer)
goBtn[0].addEventListener('click', generateEventList)
function generateEventList() {
let performances = []
let hosts = []
$("tbody tr").each(function (index) {
let th = $(this).find("th")
if (th.length == 0) {
return
}
let headerText = th.find("label").text()
// English only :(
if (headerText == "main performers:") {
$(this).find(".relationship-list > div").each(function (index) {
if ($(this).find("a").length == 0) {
return
}
let performance = {}
// _.unescape to replace & with & (etc), and replacing square brackets with html entities to prevent the link from breaking (Rebel[ut]ion, I'm looking at you)
performance.artist = _.unescape($(this).find("bdi")[0].innerHTML).replace("[", "[").replace("]", "]")
let artistLink = $(this).find("a")[0].href
performance.artistMBID = artistLink.split("/").splice(-1)[0]
let text = $(this)[0].innerHTML
let pattern = /time: (.*)\</gi
let time = pattern.exec(text)
performance.time = time ? time[1].replace("-", " – ") : ""
performances.push(performance)
})
} else if (headerText == "hosts:") {
$(this).find(".relationship-list > div").each(function (index) {
if ($(this).find("a").length == 0) {
return
}
let host = {}
host.artist = _.unescape($(this).find("bdi")[0].innerHTML)
let artistLink = $(this).find("a")[0].href
host.artistMBID = artistLink.split("/").splice(-1)[0]
hosts.push(host)
})
}
})
let isAllNight = allNightButton[0].checked
performances = performances.sort(function(a, b) {
let timea = a.time.split(" ")[0]
let timeb = b.time.split(" ")[0]
let datea = timea[0] == "0" && isAllNight ? "1/1/2" : "1/1/1"
let dateb = timeb[0] == "0" && isAllNight ? "1/1/2" : "1/1/1"
return new Date(datea + " " + timea) - new Date(dateb + " " + timeb)
})
if (!appendButton[0].checked) {
textarea.value = ""
} else {
textarea.value = textarea.value + "\n"
}
for (let performance of performances) {
let output = performance.time
? "@" + " " + performance.time + " [" + performance.artistMBID + "|" + performance.artist + "]\n"
: "@ [" + performance.artistMBID + "|" + performance.artist + "]\n"
textarea.value = textarea.value + output
}
for (let host of hosts) {
let output = "@ Hosted by [" + host.artistMBID + "|" + host.artist + "]"
textarea.value = textarea.value + output
}
}