KatTrak

A Trakt system for integrating with Kickass Torrents.

当前为 2016-06-20 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name KatTrak
  3. // @namespace PXgamer
  4. // @version 0.8
  5. // @description A Trakt system for integrating with Kickass Torrents.
  6. // @author PXgamer
  7. // @include *kat.cr/*
  8. // @include *pxstat.us/trakt*
  9. // @include *pxgamer.github.io/PX-Scripts/KatTrak/*
  10. // @grant GM_getValue
  11. // @grant GM_setValue
  12. // @grant GM_info
  13. // ==/UserScript==
  14. /*jshint multistr: true */
  15.  
  16. (function() {
  17. 'use strict';
  18.  
  19. // NOTE: To set this up, run through the auth process here: https://pxgamer.github.io/PX-Scripts/KatTrak/
  20. // No, I'm not going to steal your data or anything. This is just a project to add what you download to your Trakt.tv Collection.
  21.  
  22. var auth_code = GM_getValue('katTrakAuth', '');
  23. var info = {
  24. currentV: parseFloat(GM_info.script.version),
  25. latestV: 0.0,
  26. browser: getBrowser(),
  27. scriptManager: getScriptManager()
  28. };
  29. var settings = {
  30. debug: false,
  31. logEvents: false,
  32. logHTTP: false,
  33. logAuth: false,
  34. logInfo: false
  35. };
  36.  
  37. // Config Params
  38. // ---------------------------
  39. //GM_setValue('katTrakAuth', ''); location.reload(); // Uncomment to reset the auth_code.
  40.  
  41.  
  42. // DO NOT EDIT BELOW THIS LINE
  43. // ---------------------------
  44.  
  45. function getQV(variable)
  46. {
  47. if (settings.debug && settings.logEvents){console.info('EVENT Getting Query Variable');}
  48. var query = window.location.search.substring(1);
  49. var vars = query.split("&");
  50. for (var i=0;i<vars.length;i++) {
  51. var pair = vars[i].split("=");
  52. if(pair[0] == variable){return pair[1];}
  53. }
  54. return(false);
  55. }
  56.  
  57. var getURL = location.href.toLowerCase();
  58. var sendData;
  59.  
  60. if (getURL.indexOf('pxstat.us/trakt/?kattrakauth') > -1) {
  61. if (settings.debug && settings.logEvents){console.info('EVENT Redirecting KatTrak');}
  62. GM_setValue('katTrakAuth', getQV('katTrakAuth'));
  63.  
  64. if (getQV('ret') == 'ktInstall') {
  65. location.href = 'https://pxgamer.github.io/PX-Scripts/KatTrak/#checkup';
  66. }
  67. else {
  68. location.href = 'https://kat.cr/';
  69. }
  70. }
  71. if (getURL.indexOf('pxgamer.github.io') > -1) {
  72. if (settings.debug && settings.logEvents){console.info('EVENT Running on Home Page');}
  73. var logged_in_valid = false;
  74. $('.unauthKt').parent().replaceWith($('.unauthKt').parent().html());
  75. if (settings.debug && settings.logHTTP){console.info('GET https://api-v2launch.trakt.tv/sync/last_activities');}
  76. $.ajax({
  77. beforeSend: function (request)
  78. {
  79. request.setRequestHeader("Content-type", "application/json");
  80. request.setRequestHeader("trakt-api-key", "9efcadc5be0011a406fa0819192bd3aef0b3b2d9fa6ba90f3ffd3907138195d3");
  81. request.setRequestHeader("trakt-api-version", 2);
  82. request.setRequestHeader("Authorization", "Bearer "+auth_code+"");
  83. },
  84. type: "GET",
  85. async: false,
  86. url: "https://api-v2launch.trakt.tv/sync/last_activities",
  87. success: function (data) {
  88. if (data.all !== '') {
  89. logged_in_valid = true;
  90. }
  91. },
  92. returnData: "json"
  93. });
  94. if (auth_code !== '' && logged_in_valid) {
  95. $('.checkup-box').html('<table style="margin-left: 20%;"><tr style="text-align: left;"><td>Status:</td><td style="padding: 15px"></td><td>Success</td></tr><tr style="text-align: left;"><td>Auth Code:</td><td style="padding: 15px"></td><td>' + auth_code + '</td></tr></table>');
  96. $('.unauthKt').replaceWith('<button class="btn btn-lg btn-danger unauthKt" type="button">Unauthorise KatTrak</button>');
  97. $('.unauthKt').on('click', function() {
  98. GM_setValue('katTrakAuth', '');
  99. location.reload();
  100. });
  101. }
  102. else if (auth_code !== '') {
  103. $('.checkup-box').html('<table style="margin-left: 20%;"><tr style="text-align: left;"><td>Status:</td><td style="padding: 15px"></td><td>Failed: Invalid Auth Code</td></tr><tr style="text-align: left;"><td>Auth Code:</td><td style="padding: 15px"></td><td>' + auth_code + '</td></tr></table>');
  104. $('.unauthKt').replaceWith('<button class="btn btn-lg btn-primary unauthKt" type="button">Re-Authorise KatTrak</button>');
  105. $('.unauthKt').on('click', function() {
  106. GM_setValue('katTrakAuth', '');
  107. location.href = 'https://trakt.tv/oauth/authorize?client_id=9efcadc5be0011a406fa0819192bd3aef0b3b2d9fa6ba90f3ffd3907138195d3&redirect_uri=https%3A%2F%2Fpxstat.us%2Ftrakt%2F&response_type=code';
  108. });
  109. }
  110. else {
  111. $('.checkup-box').html('<table style="margin-left: 20%;"><tr style="text-align: left;"><td>Status:</td><td style="padding: 15px"></td><td>Failed: No Code Provided</td></tr><tr style="text-align: left;"><td>Auth Code:</td><td style="padding: 15px"></td><td>' + auth_code + '</td></tr></table>');
  112. $('.unauthKt').replaceWith('<button class="btn btn-lg btn-primary unauthKt" type="button">Authorise KatTrak</button>');
  113. $('.unauthKt').on('click', function() {
  114. GM_setValue('katTrakAuth', '');
  115. location.href = 'https://trakt.tv/oauth/authorize?client_id=9efcadc5be0011a406fa0819192bd3aef0b3b2d9fa6ba90f3ffd3907138195d3&redirect_uri=https%3A%2F%2Fpxstat.us%2Ftrakt%2F&response_type=code';
  116. });
  117. }
  118. if (settings.debug && settings.logHTTP){console.info('GET https://pxstat.us/misc/ktcheck/');}
  119. $.ajax({
  120. type: "GET",
  121. async: false,
  122. url: 'https://pxstat.us/misc/ktcheck/',
  123. success: function (data) {
  124. var ss = /\/\/ @version ([0-9.]+)\n\/\//g;
  125. info.latestV = parseFloat(ss.exec(data)[1]);
  126. },
  127. dataType: "html"
  128. });
  129. if (info.latestV > info.currentV) { $('.installBtn').replaceWith('<button class="btn btn-lg btn-warning installBtn" type="button">Update Available</button>'); }
  130. else if (info.latestV == info.currentV) { $('.installBtn').replaceWith('<button class="btn btn-lg btn-success installBtn" type="button">Up to Date</button>'); }
  131. else { $('.installBtn').replaceWith('<button class="btn btn-lg btn-danger installBtn" type="button">Unable to Check Version</button>'); }
  132.  
  133. $('#settings-config').html('Version: ' + info.currentV + ' <br>Success: '+logged_in_valid+' <br>Browser: ' + info.browser + ' <br>ScriptManager: ' + info.scriptManager);
  134. if (settings.debug && settings.logEvents){console.info('EVENT Finished');}
  135. }
  136. if (getURL.indexOf('kat.cr') > -1 && getURL.indexOf('.html') > -1) {
  137. if (settings.debug && settings.logEvents){console.info('EVENT Running on Kickass Torrents');}
  138. var category = $('span[id^="cat_"] strong a[href]:first').text();
  139. $('a.kaGiantButton[href^="/torrents/"][data-download]').attr('target', '_blank');
  140. var imdbId = "tt" + $('a.plain[href^="http://www.imdb.com/title/tt"]').text();
  141. if (category == 'Movies') {
  142. sendData = {
  143. "movies": [
  144. {
  145. "ids": {
  146. "imdb": imdbId
  147. }
  148. }
  149. ]
  150. };
  151. }
  152. else if (category == 'TV') {
  153. sendData = {
  154. "shows": [
  155. {
  156. "ids": {
  157. "imdb": imdbId
  158. }
  159. }
  160. ]
  161. };
  162. }
  163.  
  164. $('a.kaGiantButton[href^="/torrents/"][data-download]').on('click', function() {
  165. if (settings.debug && settings.logHTTP){console.info('POST https://api-v2launch.trakt.tv/sync/collection');}
  166. $.ajax({
  167. beforeSend: function (request)
  168. {
  169. request.setRequestHeader("Content-type", "application/json");
  170. request.setRequestHeader("trakt-api-key", "9efcadc5be0011a406fa0819192bd3aef0b3b2d9fa6ba90f3ffd3907138195d3");
  171. request.setRequestHeader("trakt-api-version", 2);
  172. request.setRequestHeader("Authorization", "Bearer "+auth_code+"");
  173. },
  174. type: "POST",
  175. url: "https://api-v2launch.trakt.tv/sync/collection",
  176. data: JSON.stringify(sendData),
  177. success: function (data) {
  178. console.log(data);
  179. },
  180. returnData: "json"
  181. });
  182. });
  183. $('a.kaGiantButton[href^="magnet:?xt"][data-nop]').on('click', function() {
  184. if (settings.debug && settings.logHTTP){console.info('POST https://api-v2launch.trakt.tv/sync/collection');}
  185. $.ajax({
  186. beforeSend: function (request)
  187. {
  188. request.setRequestHeader("Content-type", "application/json");
  189. request.setRequestHeader("trakt-api-key", "9efcadc5be0011a406fa0819192bd3aef0b3b2d9fa6ba90f3ffd3907138195d3");
  190. request.setRequestHeader("trakt-api-version", 2);
  191. request.setRequestHeader("Authorization", "Bearer "+auth_code+"");
  192. },
  193. type: "POST",
  194. url: "https://api-v2launch.trakt.tv/sync/collection",
  195. data: JSON.stringify(sendData),
  196. success: function (data) {
  197. console.log(data);
  198. },
  199. returnData: "json"
  200. });
  201. });
  202. if (settings.debug && settings.logEvents){console.info('EVENT Finished');}
  203. }
  204. if (settings.debug && settings.logAuth) {console.info('Auth Code: ' + auth_code);}
  205. if (settings.debug && settings.logInfo) {console.info(info);}
  206. })();
  207.  
  208. function contains(string, search) {
  209. return string.indexOf(search) != -1;
  210. }
  211. function getBrowser() {
  212. var ua = navigator.userAgent;
  213. if (contains(ua, 'Firefox')) {
  214. return "Firefox";
  215. } else if (contains(ua, 'Sleipnir')) {
  216. return "Sleipnir"; // Mobile
  217. } else if (contains(ua, 'UCBrowser')) {
  218. return "UCBrowser"; // Mobile
  219. } else if (contains(ua, 'Dolfin')) {
  220. return "Dolphin"; // Mobile
  221. } else if (contains(ua, 'MSIE')) {
  222. return "InternetExplorer";
  223. } else if (contains(ua, 'Midori')) {
  224. return "Midori";
  225. } else if (contains(ua, 'Opera') || contains(ua, 'OPR')) {
  226. return "Opera";
  227. } else if (contains(ua, 'Chrome')) {
  228. return "Chrome";
  229. } else if (contains(ua, 'Safari')) {
  230. return "Safari";
  231. } else if (contains(ua, 'Konqueror')) {
  232. return "Konqueror";
  233. } else if (contains(ua, 'PaleMoon')) {
  234. return "PaleMoon"; // fork firefox
  235. } else if (contains(ua, 'Cyberfox')) {
  236. return "Cyberfox"; // fork firefox
  237. } else if (contains(ua, 'SeaMonkey')) {
  238. return "SeaMonkey"; // fork firefox
  239. } else if (contains(ua, 'Iceweasel')) {
  240. return "Iceweasel"; // fork firefox
  241. } else {
  242. return ua;
  243. }
  244. }
  245. function getScriptManager() {
  246. if (typeof GM_info == 'object') {
  247. // Greasemonkey (Firefox)
  248. if (typeof GM_info.uuid != 'undefined') {
  249. return 'Greasemonkey';
  250. } // Tampermonkey (Chrome/Opera)
  251. else if (typeof GM_info.scriptHandler != 'undefined') {
  252. return 'Tampermonkey';
  253. }
  254. } else {
  255. // Scriptish (Firefox)
  256. if (typeof GM_getMetadata == 'function') {
  257. return 'Scriptish';
  258. } // NinjaKit (Safari/Chrome)
  259. else if (typeof GM_setValue != 'undefined' &&
  260. typeof GM_getResourceText == 'undefined' &&
  261. typeof GM_getResourceURL == 'undefined' &&
  262. typeof GM_openInTab == 'undefined' &&
  263. typeof GM_setClipboard == 'undefined') {
  264. return 'NinjaKit';
  265. } else { // Native
  266. return 'Native';
  267. }
  268. }
  269. }