-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimdb.actors.ratings.user.js
260 lines (235 loc) · 10.1 KB
/
imdb.actors.ratings.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
// ==UserScript==
// @name IMDB Person's Title Ratings
// @version 1.0.3
// @license GPL
// @description Adds ratings & vote counts with sorting ability to a person's movie/TV show lists
// @namespace http://userscripts.org/users/518906
// @icon http://www.imdb.com/favicon.ico
// @author Nonya Beesnes, Wardenclyffe Tower
// @match https://www.imdb.com/name/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
// @require https://raw.githubusercontent.com/datejs/Datejs/master/build/date.js
// @require https://raw.githubusercontent.com/evanplaice/jquery-csv/master/src/jquery.csv.min.js
// @require https://raw.github.com/odyniec/MonkeyConfig/master/monkeyconfig.js
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @grant GM_addStyle
// ==/UserScript==
// add css
var css = '<style type="text/css"> \
.your_rating_column, .votes_column, .rating_column, .year_column{padding-left: 0px;} \
.your_rating_column, .votes_column, .rating_column{float: right;width:65px} \
.header .your_rating_column a, .header .votes_column a, .header .rating_column a, .header .year_column a{cursor:pointer} \
.your_rating_column a:hover, .votes_column a:hover, .rating_column a:hover, .year_column a:hover{text-decoration:none !important;color:inherit} \
.year_column{width:40px;text-align:inherit} \
.header .sorted{background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAYUlEQVQoz2NgQAKSaZs2APF/KN7AgAtQRyFQoB+qAISfIyl8jiTeC1JoCsQ/kRSgY5CcMczUHDwKc9CdsAaLovVAzIiukB+I7yIpug/Egrh8bQx10y8gNmPAB6DuLUAXBwDx5XOePVdilwAAAABJRU5ErkJggg==) left no-repeat} \
.header .sortedDesc{background:url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAoAAAAKCAYAAACNMs+9AAAAXElEQVQoz2NgQAOSaZtygLiAAR8AKjAG4p9A/AuITXAp4gfiu0D8H4pBbH5sCtcgKYLhNdjc9R8HzkF3Fy6FIDljkMJeIN4Axc+RFDxHEu9Fd8IGJIUb8AUP5QoBYK5yX24/ZOIAAAAASUVORK5CIIA=) left no-repeat} \
</style>';
$(css).appendTo('head');
// add headers (Rating, Votes, Year)
$('.filmo-category-section').prepend('<div class="filmo-row header"><div class="year_column sorted"><a title="Sort">Year</a></div><div class="votes_column mellow"><a title="Sort">Votes</a></div><div class="your_rating_column"><a title="Sort">Your</a></div><div class="rating_column"><a title="Sort">Rating</a></div><br></div>');
$('#filmoform').prepend('<select id="filmoform-type-select" name="sort" class="fixed"><option value="">Show all</option><option value="?">Feature Film</option><option value="typeVideoGame">Video Games</option><option value="typeTvSeries">TV Series</option><option value="typeShort">Short</option></select>');
$('#filmoform-type-select').on('change', function() {
var selectedType = $("#filmoform-type-select").val();
if (selectedType == '') {
$('.filmo-row:not(.header)').show();
} else if (selectedType == '?') {
$('.filmo-row:not(.header)').show();
$('.filmo-row.typeVideoGame, .filmo-row.typeTvSeries, .filmo-row.typeShort').hide();
} else {
$('.filmo-row:not(.header)').hide();
$('.filmo-row.'+selectedType).show();
}
});
// Configuration
cfg = new MonkeyConfig({
title: 'IMDB Configuration',
menuCommand: true,
params: {
omdb_username: {
type: 'text',
'default': 'foo'
}
}
});
// add the rating & votes to the movie/TV show
function addData(yearSpan, rating, myRating, votes, released) {
'use strict';
$(yearSpan).attr('title', released);
$(yearSpan).after('<span class="votes_column mellow"><small>' + votes + '</small></span><span class="your_rating_column">' + myRating + '</span><span class="rating_column">' + rating + '</span>');
}
var userDataLoaded = false;
function loadUserRatingsAndStoreToGmStorage() {
console.log('GM_getValue(imdbLastModified) = ' + GM_getValue('imdbLastModified'));
console.log('Delta: ' + (Date.now() - GM_getValue('imdbLastModified')));
if (GM_getValue('imdbLastModified') && (Date.now() - GM_getValue('imdbLastModified') < 3600000 * 1)) {
return;
}
var userId = "";
// Get user ID
var imdbUserEditPageUrl = 'https://www.imdb.com/activity/editprofile';
$.ajax({
dataType: "html",
url: imdbUserEditPageUrl,
success: function(html_data) {
var scrapedUserId = $(html_data).find('.auth-imput-row div').text();
console.log('Parsed UserId: ' + scrapedUserId);
userId = scrapedUserId;
}
});
var imdbUserRatingsCsvUrl = 'https://www.imdb.com/list/export?list_id=ratings&author_id=' + userId;
$.ajax({
dataType: "text",
url: imdbUserRatingsCsvUrl,
success: function( csv_text ) {
console.log('Parsing CSV.');
var arr = $.csv.toArrays(csv_text);
console.log('Parsed. Entries: ' + arr.length);
for (var i = 1; i < arr.length; i++) {
key = arr[i][0];
rating = arr[i][1];
if (GM_getValue(key) != rating) { // GM_getValue is much faster than setValue
GM_setValue(key, rating);
}
}
GM_setValue('imdbLastModified', Date.now());
}
});
}
function scrapUserId() {
var userLink = $('#navUserMenu .navCategory a').attr('href');
var re = /\/user\/([a-z0-9]+)\//;
var match = re.exec(userLink);
if (!match)
window.alert("Can't find username. " + userLink);
else
return match[1];
}
function getUserRating(imdbId) {
if (!userDataLoaded) {
userDataLoaded = true;
loadUserRatingsAndStoreToGmStorage();
}
return GM_getValue(imdbId, '-');
}
// iterate through the movies/TV shows in the passed section and lookup the rating & vote count via omdbApi.com
// if the section doesn't already have ratings added and is visible.
function addRatingsToSection(filmoCategorySection) {
'use strict';
if (!$(filmoCategorySection).hasClass('hasRatings') && $(filmoCategorySection).is(':visible')) {
$(filmoCategorySection).addClass('hasRatings');
$(filmoCategorySection).find('.filmo-row:not(.header)').each(function() {
var imdbId = $(this).attr('id').split('-')[1];
var omdbUser = cfg.get('omdb_username');
var omdbUrl = 'https://www.omdbapi.com/?i=' + imdbId + '&apikey=' + omdbUser;
var yearSpan = $(this).find('span.year_column');
var myRating = getUserRating(imdbId);
var itemText = $(this).text();
if (itemText.indexOf("(TV Series)") > 0 || itemText.indexOf("(TV Mini-Series)") > 0) $(this).addClass("typeTvSeries");
if (itemText.indexOf("(Video Game)") > 0) $(this).addClass("typeVideoGame");
if (itemText.indexOf("(Short)") > 0 || itemText.indexOf("(Video short)") > 0) $(this).addClass("typeShort");
$.getJSON(omdbUrl, function( data ) {
if (data.Response === 'True') {
addData(yearSpan, data.imdbRating, myRating, data.imdbVotes, data.Released);
} else {
addData(yearSpan, 'n/a', myRating, 'n/a', yearSpan.text().trim().slice(0,4));
}
}).fail(function( jqxhr, textStatus, error ) {
var err = textStatus + ", " + error;
console.log("Request Failed: " + err);
addData(yearSpan, '<a href="#" onclick="cfg.open();">unauth</a>', myRating, '...', yearSpan.text().trim().slice(0,4));
});
});
}
}
// from https://github.com/Teun/thenBy.js
firstBy=(function(){function e(f){f.thenBy=t;return f}function t(y,x){x=this;return e(function(a,b){return x(a,b)||y(a,b)})}return e})();
// calculates sort order
function calcSort(a, b, sortProperty, direction) {
'use strict';
var opA, opB, selector;
switch (sortProperty) {
case 'Rating':
selector = '.rating_column';
break;
case 'Votes':
selector = '.votes_column';
break;
case 'Year':
var aval = $(a).find('.year_column').attr('title');
if (aval === 'n/a') {aval = $(a).find('.year_column').text().trim().slice(0,4);}
var bval = $(b).find('.year_column').attr('title');
if (bval === 'n/a') {bval = $(b).find('.year_column').text().trim().slice(0,4);}
return Date.parse(aval) < Date.parse(bval) ? (1 * direction) : (-1 * direction);
//selector = '.year_column';
//break;
default:
break;
}
opA = Number($(a).find(selector).text().replace(',','').replace('n/a', '0'));
opB = Number($(b).find(selector).text().replace(',','').replace('n/a', '0'));
return opA < opB ? (1 * direction) : (opA > opB ? -1 * direction : 0);
}
// sorts the list of movies/TV Shows
function sortCategory(sectionDiv, sortBy, direction) {
'use strict';
var rows = $(sectionDiv).children('div').not('.header').remove();
var secondSortProperty, thirdSortProperty;
switch (sortBy) {
case 'Rating':
secondSortProperty = 'Votes';
thirdSortProperty = 'Year';
break;
case 'Votes':
secondSortProperty = 'Rating';
thirdSortProperty = 'Year';
break;
case 'Year':
secondSortProperty = 'Rating';
thirdSortProperty = 'Votes';
break;
}
var s = firstBy(function (a, b) {return calcSort(a, b, sortBy, direction);})
.thenBy(function (a, b) {return calcSort(a, b, secondSortProperty, direction);})
.thenBy(function (a, b) {return calcSort(a, b, thirdSortProperty, direction);});
rows.sort(s);
// add class odd or even
rows.each(function(index) {
$(this).removeClass('odd even');
if (index % 2 === 0) {$(this).addClass('odd');}
else {$(this).addClass('even');}
});
$(sectionDiv).append(rows);
}
//add click event handlers to the headers (Rating, Votes, Year)
$('.filmo-row.header div a').click(function () {
'use strict';
var linkDiv = $(this).parent('div');
var direction = 1;
if (linkDiv.hasClass('sorted')) {
direction = -1;
linkDiv.addClass('sortedDesc');
linkDiv.removeClass('sorted');
}
else {
linkDiv.addClass('sorted');
linkDiv.removeClass('sortedDesc');
}
sortCategory($(this).parents('.filmo-category-section'), $(this).text(), direction);
linkDiv.siblings('div').removeClass('sorted sortedDesc');
});
// watch for changes to the category sections' style attribute, add ratings when made visible (cut down on hits to omdbApi.com)
var observer = new MutationObserver(function(mutations) {
'use strict';
addRatingsToSection(mutations[0].target);
});
$('.filmo-category-section').each(function () {
'use strict';
observer.observe(this, {
attributes: true,
attributeFilter: ["style"]
});
});
// initially add ratings only to the sections that aren't collapsed (cut down on hits to omdbApi.com)
addRatingsToSection($('.filmo-category-section:visible'));