-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBdMissingShowsHelper.user.js
453 lines (377 loc) · 17.4 KB
/
BdMissingShowsHelper.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
// ==UserScript==
// @name Bierdopje missende series helper
// @namespace http://www.bierdopje.com
// @version 0.06
// @description Toont welke series nog niet zijn toegevoegd op bierdopje
// @grant GM_setClipboard
// @grant GM_addStyle
// @grant unsafeWindow
// @match http://www.bierdopje.com/forum/geachte-redactie/topic/1883-missende-series-in-bd-wel-in-tvdb/*
// @require http://code.jquery.com/jquery-2.1.4.min.js
// @require http://momentjs.com/downloads/moment-with-locales.min.js
// @copyright 2016+, Robin Houtevelts
// @run-at document-start
// ==/UserScript==
if (window.top != window.self)
return;
GM_addStyle('.twbs.red { background-color: #F3D4D4 } .twbs.green { background-color: #DFF5E2}');
var BD_API_URL = 'https://bierdopje-api.houtevelts.com';
var TVDB_API_URL = 'https://tvdb-api.houtevelts.com';
var CSS_URL = 'https://bierdopje-api.houtevelts.com/scoped-twbs.css';
var DEBUG = true;
var COMMENT_TXT = '<tr id="replyheader-1337" name="replyheader"><td width="50" align="left" valign="middle" class="defbg postbuts" height="20"><a href="#1337"><img src="http://cdn.bierdopje.eu/g/if/forum/icon_minipost.gif" width="12" height="9" alt="Bericht" border="0"></a> <span class="postname"><a href="#" class="user user">BD MissingShowFinder</a></span> </td><td nowrap="nowrap" valign="middle" align="right" class="defbg postbuts" height="20"><p align="left"> </p></td><td nowrap="nowrap" valign="middle" align="right" class="defbg postbuts" height="20"> </td></tr><tr id="replyside-1337" name="replyside"><td width="150" align="left" valign="top" class="userinfo"><span class="postdetails"><br><img src="http://eih.bz/s1/2015123000.png"><br><br><br><br><br><br></span><br></td><td class="posttext" valign="top" colspan="2"><span class="postbody" id="BD__MissingShowFinder__postbody"><div class="twbs container-fluid">Dag {{username}}!</div></span><span class="gensmall"></span></td></tr>';
var NO_REQUESTS_TXT = '<p class="postcontent">Er zijn geen aanvragen gevonden door mij op deze pagina.</p>';
var MISSING_SHOWS_TXT = '<p class="postcontent">Op deze pagina heb ik <code>{{nr_missing_shows}}</code> shows gevonden die niet op Bierdopje staan! <br />Ik heb ze in een mooi overzicht gegoten voor jou:</p><table class="twbs table table-bordered table-hover table-striped"><tr><th>Show</th><th>TvDbId</th><th>Requested by</th></tr></table>';
var MISSING_SHOWS_ROW_TXT = '<tr><td><a href="{{url}}">{{showname}}</a></td><td><code>{{tvDbId}}</code><button data-tvDbId="{{tvDbId}}" type="button" class="twbs pull-right btn btn-info btn-sm" title="Kopier id naar klembord"><span class="glyphicon glyphicon-paperclip" aria-hidden="true"></span></button></td><td></td></tr>';
var AVAILABLE_SHOWS_TXT = '<p class="postcontent">Er zijn op deze pagina <code>{{nr_available_shows}}</code> shows die wél op Bierdopje staan!</p><div class="twbs container-fluid"><table class="twbs table table-bordered table-hover table-striped"><tr><th>Show</th><th>Url</th><th>Requested by</th></tr></table></div>';
var AVAILABLE_SHOWS_ROW_TXT = '<tr><td><a href="{{url}}">{{showname}}</a></td><td><code>{{url_slug}}</code><button data-url="{{url}}" type="button" class="twbs pull-right btn btn-info btn-sm" title="Kopier url naar klembord"><span class="glyphicon glyphicon-paperclip" aria-hidden="true"></span></button></td><td></td></tr>';
var USER_REQUEST_TXT = '{{username}}<sup><a href="{{permalink}}" title="{{time}}">comment</a></sup><br />';
var windowLocation = window.location.href.split("#")[0];
var tvDbId_comments = {};
$(function () {
/* Inject bootstrap */
//https://raw.githubusercontent.com/homeyer/scoped-twbs/
var pageHead = document.getElementsByTagName("HEAD")[0];
var link = window.document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = CSS_URL;
pageHead.insertBefore(link, pageHead.lastChild);
moment.locale('nl');
$(document).on('click', 'button[data-tvdbid]', function (e) {
var button = $(this);
var tvDbId = button.attr('data-tvdbid');
GM_setClipboard(tvDbId);
});
$(document).on('click', 'button[data-url]', function (e) {
var button = $(this);
var url = button.attr('data-url');
GM_setClipboard(url);
});
$(document).on('bierdopje.missingShowsFinder.finished', function (e, shows) {
var missingShows = [];
var availableShows = [];
var comments_shows = {}; // so we can sort by request-date
shows.map(function (show) {
if (show.isAvailable) {
var comments = tvDbId_comments[show.tvdbId];
comments.map(function (comment) {
var date = comment.date.format('x');
if (!comments_shows.hasOwnProperty(date))
comments_shows[date] = [];
comments_shows[date].push(show);
});
} else {
missingShows.push(show);
}
});
shows = null;
delete shows;
// order missing shows by name
missingShows.sort(function (a, b) {
var nameA = a.name.toLowerCase();
var nameB = b.name.toLowerCase();
if (nameA > nameB)
return 1;
else if (nameA < nameB)
return -1;
return 0;
});
// order available shows by commentdate
Object.keys(comments_shows).sort().map(function (key) {
availableShows = availableShows.concat(comments_shows[key]);
});
availableShows = $.unique(availableShows);
comments_shows = null;
delete comments_shows;
var getRequests = function (show, cb) {
var comments = tvDbId_comments[parseInt(show.tvDbId)];
comments.sort(function (a, b) {
var dateA = a.date.format('x');
var dateB = b.date.format('x');
if (dateA > dateB)
return 1;
else if (dateA < dateB)
return -1;
return 0;
});
comments.map(function (comment) {
var request = USER_REQUEST_TXT.replace(/{{username}}/g, comment.user.name);
request = request.replace(/{{permalink}}/g, comment.permaLink);
request = request.replace(/{{time}}/g, comment.date.fromNow());
cb(request);
});
};
var comment = COMMENT_TXT.replace(/{{username}}/g, unsafeWindow.UserName);
comment = $(comment);
if (missingShows.length > 0) {
var body = MISSING_SHOWS_TXT.replace(/{{nr_missing_shows}}/g, missingShows.length);
body = $(body);
var table = $(body[1]);
missingShows.map(function (show) {
var row = MISSING_SHOWS_ROW_TXT.replace(/{{showname}}/g, show.name);
row = row.replace(/{{tvDbId}}/g, show.tvDbId);
row = row.replace(/{{url}}/g, 'http://thetvdb.com/?tab=series&id=' + show.tvDbId + '&lid=13');
row = $(row);
getRequests(show, function (request) {
$('td', row).last().append(request);
});
table.append(row);
});
$('.container-fluid', comment).append(body);
}
if (availableShows.length > 0) {
var body = AVAILABLE_SHOWS_TXT.replace(/{{nr_available_shows}}/g, availableShows.length);
body = $(body);
var table = $('table', body);
availableShows.map(function (show) {
var row = AVAILABLE_SHOWS_ROW_TXT.replace(/{{showname}}/g, show.name);
row = row.replace(/{{url}}/g, show.link);
row = row.replace(/{{url_slug}}/g, show.link.replace(unsafeWindow.BaseURL, ''));
row = $(row);
getRequests(show, function (request) {
$('td', row).last().append(request);
});
table.append(row);
});
$('.container-fluid', comment).append(body);
}
if (availableShows.length == 0 && missingShows.length == 0) {
var body = NO_REQUESTS_TXT;
$('.container-fluid', comment).append(body);
}
$('.content.go-wide table.forumline tr[id^="replyside"]').last().after(comment);
var consoleMsg = 'There are ' + missingShows.length + ' shows missing';
if (missingShows.length <= 0) {
console.info(consoleMsg);
} else {
console.warn(consoleMsg);
console.table(missingShows);
}
});
log('Starting');
var forumContentTable = $('#page .maincontent .content .forumline');
getCommentsOnPage(forumContentTable, function (comments) {
getTvDbIdsFromComments(comments, function (tvDbIds) {
tvDbId_comments = $.extend({}, tvDbIds); // make a copy of the
var todo = Object.keys(tvDbIds).length;
var shows = [];
$.each(tvDbIds, function (tvDbId, comments) {
checkIfExists(tvDbId, function (show) {
todo--;
if (!show) { // invalid tvDbId
if (todo == 0)
$(document).trigger('bierdopje.missingShowsFinder.finished', [shows]);
return true;
}
show.tvDbId = tvDbId;
shows.push(show);
// set background color on comments, displaying the status.
// green means it's on BD, red means it's not.
var color = show.isAvailable ? 'green' : 'red';
comments.map(function (comment) {
var element = $('td.posttext', comment.bodyel);
// In case of multiple tvdbId's in a comment
// when a comment is set to red, don't make it green
if (element.hasClass('red'))
return true;
if (color == 'red')
element.removeClass('green');
element.addClass('twbs ' + color);
});
if (todo == 0)
$(document).trigger('bierdopje.missingShowsFinder.finished', [shows]);
});
});
});
});
});
function getCommentsOnPage(forum, callback) {
var comments = {};
$('tr[id^="reply"]', forum).each(function () {
var element = $(this);
var elementType = element.attr('name');
var commentId = parseInt(element.attr('id').split('-')[1]);
var comment = {};
if (comments.hasOwnProperty(commentId))
comment = comments[commentId];
if (elementType == 'replyheader') {
var header = parseCommentHeader(element);
if (foundComment(header))
comment = mergeCommentContent(comment, header);
} else if (elementType == 'replyside') {
var body = parseCommentBody(element);
if (foundComment(body))
comment = mergeCommentContent(comment, body);
}
comments[commentId] = comment;
});
// validate comments in a crappy way
var validComments = [];
Object.keys(comments).map(function (id) {
var comment = comments[id];
var isValid = comment.hasOwnProperty('id')
&& comment.hasOwnProperty('permaLink')
&& comment.hasOwnProperty('user')
&& comment.user.hasOwnProperty('name')
&& comment.user.hasOwnProperty('link')
&& comment.hasOwnProperty('date')
&& comment.hasOwnProperty('headerel')
&& comment.hasOwnProperty('body')
&& comment.hasOwnProperty('bodyel');
if (isValid)
validComments.push(comment);
});
comments = validComments;
log('Found ' + comments.length + ' comments');
callback(comments);
}
function foundComment(commentContent) {
return commentContent.hasOwnProperty('id');
}
function mergeCommentContent(oldContent, newContent) {
// Merge oldContent and newContent
return jQuery.extend({}, oldContent, newContent, true);
}
function parseCommentHeader(element) {
var commentId = parseInt(element.attr('id').split('-')[1]);
var permaLink = windowLocation + '#' + commentId;
var userHref = $('.postname a.user', element);
var user = {};
user.name = userHref.text();
user.link = 'http://www.bierdopje.com' + userHref.attr('href');
var dateString = $('td:eq(1) p', element).text(); //Geplaatst op woensdag 26 augustus 2015 20:01
dateString = /(\d+ .*?)$/g.exec(dateString)[1]; // 26 augustus 2015 20:01
var date = moment(dateString, 'DD MMMM YYYY HH:mm');
return {
id: commentId,
permaLink: permaLink,
user: user,
date: date,
headerel: element
};
}
function parseCommentBody(element) {
var commentId = parseInt(element.attr('id').split('-')[1]);
var body = $('span[id^=post].postbody', element);
return {
id: commentId,
body: body.html(),
bodyel: element
};
}
function getTvDbIdsFromComments(comments, callback) {
var tvDbIds = {};
// Ignore Xandecs, he doesn't make requests
var ignoredUsers = ['xandecs']; // lowercase
log('Extracting TvDbId\'s from comments');
function executeRegex(regex, body, comment, processCb) {
var match;
while ((match = regex.exec(body)) !== null) {
processCb(match[1], function (TVDBId) {
if (!TVDBId) {
return;
}
log('Found tvDbId: "' + TVDBId + '"');
if (!tvDbIds.hasOwnProperty(TVDBId))
tvDbIds[TVDBId] = [];
tvDbIds[TVDBId].push(comment);
});
}
}
var promises = [];
comments.map(function (comment) {
if (ignoredUsers.indexOf(comment.user.name.toLowerCase()) >= 0)
return true; //continue
var body = comment.body;
var regexOptions = {
fromUrl: /thetvdb.com\/.*?id=(\d+)/gi,
fromSlug: /thetvdb.com\/series\/([\w-_]+)/gi,
raw: /tvdb.*?(\d+)/gi
};
// First try to find id's based on slugs
var regex = regexOptions['fromSlug'];
if (regex.test(body)) {
regex.lastIndex = 0;
promises.push(new Promise(function (resolve, reject) {
executeRegex(regex, body, comment, function (slug, cb) {
console.log('Getting tvdbId from slug: "' + slug + '"');
getSerieInfoFromTvDbSlug(slug, function (show) {
if (Object.keys(show).length <= 0) {
console.log('Slug "' + slug + '" does not exist.');
cb(false);
}
console.log('Slug "' + slug + '" is tvdb "' + show.id + '"');
cb(parseInt(show.id));
resolve();
});
});
}));
}
// Look for an url to thetvdb by default
regex = regexOptions['fromUrl'];
if (!regex.test(body)) { // If no url is found, try a less subtle search
regex = regexOptions['raw'];
}
regex.lastIndex = 0; // reset internal pointer
executeRegex(regex, body, comment, function (id, cb) {
promises.push(new Promise(function (resolve, reject) {
cb(parseInt(id));
resolve();
}));
});
});
Promise.all(promises).then(function () {
log('Found ' + Object.keys(tvDbIds).length + ' tvDbId\'s');
callback(tvDbIds);
});
}
function checkIfExists(tvDbId, callback) {
log('Checking if show with tvDbId ' + tvDbId + ' exist on Bierdopje');
checkIfAvailableOnBierdopje(tvDbId, function (show) {
var isAvailable = Object.keys(show).length > 0;
log('Show with tvDbId ' + tvDbId + ' does' + (isAvailable ? '' : ' not') + ' exist on Bierdopje');
if (!isAvailable) {
log('Trying to get showName from TvDb');
getSerieInfoFromTvDb(tvDbId, function (show) {
// Check if TvDbId returned info.
if (Object.keys(show).length <= 0) {
log('TvDbId "' + tvDbId + '" is invalid');
callback(null);
return false;
}
log('Got showName of tvdbId ' + tvDbId + ', "' + show.name + '"');
show.isAvailable = isAvailable;
callback(show);
});
} else {
show.isAvailable = isAvailable;
callback(show);
}
});
}
function checkIfAvailableOnBierdopje(TVDBId, callback) {
$.getJSON(BD_API_URL + '/GetShowByTVDBID/' + TVDBId, function (show) {
callback(show);
}).fail(function () {
callback({});
});
}
function getSerieInfoFromTvDb(TVDBId, callback) {
$.getJSON(TVDB_API_URL + '/show/' + TVDBId, function (show) {
callback(show);
}).fail(function () {
callback({});
});
}
function getSerieInfoFromTvDbSlug(slug, callback) {
$.getJSON(TVDB_API_URL + '/show/search/' + slug, function (show) {
callback(show);
}).fail(function () {
callback({});
});
}
function log(string) {
if (DEBUG)
console.log('[BD:MissingShowFinder] ' + string);
}