Last active
February 15, 2016 13:05
-
-
Save AviDuda/3ace9db989975fc6f4ba to your computer and use it in GitHub Desktop.
Promoter userscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Promoter press requests | |
// @namespace https://timmy.im/ | |
// @version 1.0 | |
// @description Makes the Promoter press request page better. | |
// @author Tomáš Duda | |
// @match https://*.promoterapp.com/press_requests/open | |
// @grant GM_getValue | |
// @grant GM_setValue | |
// @grant GM_addStyle | |
// ==/UserScript== | |
/* jshint -W097 */ | |
'use strict'; | |
// CSS fixes | |
GM_addStyle('.timeline_box { clear: both; }'); | |
$('.box.actions').prepend($('<a id="wipe-youtubers" class="action" href="#">Wipe small YouTubers</a> <a id="get-twitch-data" class="action" href="#">Get Twitch data</a>')); | |
$('#wipe-youtubers').click(function(e) { | |
e.preventDefault(); | |
var ytCount = parseInt(prompt("Minimum number of subscribers to keep", 2000)); | |
$('.entry_row').each(function(i, entry) { | |
var followerCount = $(entry).find('.follower_count').text(); | |
if (! isNaN(parseInt(followerCount))) { | |
var over1k = followerCount.match(/(.*) K/); | |
if (over1k) { | |
followerCount = over1k[1] * 1000; | |
} | |
if (parseInt(followerCount) == followerCount && followerCount < ytCount) { | |
$(entry).find('.icon_remove').removeAttr('data-confirm').click(); | |
} | |
} | |
}); | |
console.log('Done!'); | |
}); | |
window.twitchChannelEntries = []; | |
window.twitchChannels = {}; | |
$('#get-twitch-data').click(function(e) { | |
e.preventDefault(); | |
GM_setValue('twitchChannels', '{}'); | |
$('.entry_row').each(function(i, entry) { | |
var link = $(entry).find('a:nth-child(3)').attr('href'); | |
var twitchAccount = link.match(/twitch\.tv\/(\w+)/); | |
if (twitchAccount !== null) { | |
twitchAccount = twitchAccount[1]; | |
twitchChannelEntries.push(twitchAccount); | |
} | |
}); | |
var requests = []; | |
for (var i = 0; i < twitchChannelEntries.length; i++) { | |
var request = $.ajax({ | |
url: 'https://api.twitch.tv/kraken/channels/' + twitchChannelEntries[i], | |
method: 'GET', | |
dataType: 'json', | |
headers: { 'Client-ID': 'PromoterSCS' } | |
}); | |
request.done(function(data) { | |
var username = data.name; | |
twitchChannels[username] = { | |
followers: data.followers, | |
views: data.views | |
}; | |
}); | |
requests.push(request); | |
} | |
var updateGMChannels = function() { | |
GM_setValue('twitchChannels', JSON.stringify(twitchChannels)); | |
GM_setValue('twitchChannelsUpdatedAt', Date.now()); | |
console.log('Twitch data updated.'); | |
} | |
$.when.apply($, requests).then(updateGMChannels, updateGMChannels); | |
}); | |
var twitchChannelsGM = GM_getValue('twitchChannels', '{}'); | |
twitchChannels = JSON.parse(twitchChannelsGM); | |
$('.entry_row').each(function(i, entry) { | |
// add a data attribute for all YouTubers | |
var youtubePlay = $(entry).find('.fa-youtube-play'); | |
if (youtubePlay.length > 0) { | |
var youtubeChannel = $(entry).find('') | |
$(entry).attr('data-youtube', $(youtubePlay).parent().attr('href').replace(/.*channel\//, '')); | |
} | |
// add Twitch counter and data attribute | |
var link = $(entry).find('a:nth-child(3)').attr('href'); | |
var twitchAccount = link.match(/twitch\.tv\/(\w+)/); | |
if (twitchAccount !== null) { | |
twitchAccount = twitchAccount[1]; | |
if (twitchChannels[twitchAccount]) { | |
$(entry).find('.reserved_code').prepend('<a class="twitter" href="https://twitch.tv/' + twitchAccount + '" target="_blank">Twitch: ' + twitchChannels[twitchAccount].followers + ' followers, ' + twitchChannels[twitchAccount].views + ' views</a>'); | |
$(entry).attr('data-twitch', twitchAccount); | |
} | |
} | |
}); | |
// filters | |
$('.box.actions').append('<br><br><select id="press-requests-filter"><option value="all" selected="selected">show all</option><option value="youtube">show YouTubers</option><option value="twitch">show Twitch streamers</option></select>'); | |
$('#press-requests-filter').change(function() { | |
var filter = $(this).val(); | |
if (filter == 'all') { | |
$('.entry_row').show(); | |
} | |
else { | |
$('.entry_row[data-' + filter + ']').show(); | |
$('.entry_row:not([data-' + filter + '])').hide(); | |
} | |
$('.timeline_box').show(); | |
$('.timeline_box').not(function() { | |
return $(this).find('.entry_row:visible').length > 0; | |
}).hide(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment