KatTrak

A Trakt system for integrating with Kickass Torrents.

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

  1. // ==UserScript==
  2. // @name KatTrak
  3. // @namespace PXgamer
  4. // @version 0.6
  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. };
  27.  
  28. // Config Params
  29. // ---------------------------
  30. //GM_setValue('katTrakAuth', ''); location.reload(); // Uncomment to reset the auth_code.
  31.  
  32.  
  33. // DO NOT EDIT BELOW THIS LINE
  34. // ---------------------------
  35.  
  36. function getQV(variable)
  37. {
  38. var query = window.location.search.substring(1);
  39. var vars = query.split("&");
  40. for (var i=0;i<vars.length;i++) {
  41. var pair = vars[i].split("=");
  42. if(pair[0] == variable){return pair[1];}
  43. }
  44. return(false);
  45. }
  46.  
  47. var getURL = location.href.toLowerCase();
  48. var sendData;
  49.  
  50. if (getURL.indexOf('pxstat.us/trakt/?kattrakauth') > -1) {
  51. GM_setValue('katTrakAuth', getQV('katTrakAuth'));
  52.  
  53. if (getQV('ret') == 'ktInstall') {
  54. location.href = 'https://pxgamer.github.io/PX-Scripts/KatTrak/#checkup';
  55. }
  56. else {
  57. location.href = 'https://kat.cr/';
  58. }
  59. }
  60. if (getURL.indexOf('pxgamer.github.io') > -1) {
  61. var logged_in_valid = false;
  62. $('.unauthKt').parent().replaceWith($('.unauthKt').parent().html());
  63. $.ajax({
  64. beforeSend: function (request)
  65. {
  66. request.setRequestHeader("Content-type", "application/json");
  67. request.setRequestHeader("trakt-api-key", "9efcadc5be0011a406fa0819192bd3aef0b3b2d9fa6ba90f3ffd3907138195d3");
  68. request.setRequestHeader("trakt-api-version", 2);
  69. request.setRequestHeader("Authorization", "Bearer "+auth_code+"");
  70. },
  71. type: "GET",
  72. async: false,
  73. url: "https://api-v2launch.trakt.tv/sync/last_activities",
  74. success: function (data) {
  75. if (data.all !== '') {
  76. logged_in_valid = true;
  77. }
  78. },
  79. returnData: "json"
  80. });
  81. if (auth_code !== '' && logged_in_valid) {
  82. $('.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>');
  83. $('.unauthKt').replaceWith('<button class="btn btn-lg btn-danger unauthKt" type="button">Unauthorise KatTrak</button>');
  84. $('.unauthKt').on('click', function() {
  85. GM_setValue('katTrakAuth', '');
  86. location.reload();
  87. });
  88. }
  89. else if (auth_code !== '') {
  90. $('.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>');
  91. $('.unauthKt').replaceWith('<button class="btn btn-lg btn-primary unauthKt" type="button">Re-Authorise KatTrak</button>');
  92. $('.unauthKt').on('click', function() {
  93. GM_setValue('katTrakAuth', '');
  94. location.href = 'https://trakt.tv/oauth/authorize?client_id=9efcadc5be0011a406fa0819192bd3aef0b3b2d9fa6ba90f3ffd3907138195d3&redirect_uri=https%3A%2F%2Fpxstat.us%2Ftrakt%2F&response_type=code';
  95. });
  96. }
  97. else {
  98. $('.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>');
  99. $('.unauthKt').replaceWith('<button class="btn btn-lg btn-primary unauthKt" type="button">Authorise KatTrak</button>');
  100. $('.unauthKt').on('click', function() {
  101. GM_setValue('katTrakAuth', '');
  102. location.href = 'https://trakt.tv/oauth/authorize?client_id=9efcadc5be0011a406fa0819192bd3aef0b3b2d9fa6ba90f3ffd3907138195d3&redirect_uri=https%3A%2F%2Fpxstat.us%2Ftrakt%2F&response_type=code';
  103. });
  104. }
  105. $.ajax({
  106. type: "GET",
  107. async: false,
  108. url: 'https://pxstat.us/misc/ktcheck/',
  109. success: function (data) {
  110. var ss = /\/\/ @version ([0-9.]+)\n\/\//g;
  111. info.latestV = parseFloat(ss.exec(data)[1]);
  112. },
  113. dataType: "html"
  114. });
  115. if (info.latestV > info.currentV) { $('.installBtn').replaceWith('<button class="btn btn-lg btn-warning installBtn" type="button">Update Available</button>'); }
  116. else if (info.latestV == info.currentV) { $('.installBtn').replaceWith('<button class="btn btn-lg btn-success installBtn" type="button">Up to Date</button>'); }
  117. else { $('.installBtn').replaceWith('<button class="btn btn-lg btn-danger installBtn" type="button">Unable to Check Version</button>'); }
  118. }
  119. if (getURL.indexOf('kat.cr') > -1 && getURL.indexOf('.html') > -1) {
  120. var category = $('span[id^="cat_"] strong a[href]:first').text();
  121. $('a.kaGiantButton[href^="/torrents/"][data-download]').attr('target', '_blank');
  122. var imdbId = "tt" + $('a.plain[href^="http://www.imdb.com/title/tt"]').text();
  123. if (category == 'Movies') {
  124. sendData = {
  125. "movies": [
  126. {
  127. "ids": {
  128. "imdb": imdbId
  129. }
  130. }
  131. ]
  132. };
  133. }
  134. else if (category == 'TV') {
  135. sendData = {
  136. "shows": [
  137. {
  138. "ids": {
  139. "imdb": imdbId
  140. }
  141. }
  142. ]
  143. };
  144. }
  145.  
  146. $('a.kaGiantButton[href^="/torrents/"][data-download]').on('click', function() {
  147. $.ajax({
  148. beforeSend: function (request)
  149. {
  150. request.setRequestHeader("Content-type", "application/json");
  151. request.setRequestHeader("trakt-api-key", "9efcadc5be0011a406fa0819192bd3aef0b3b2d9fa6ba90f3ffd3907138195d3");
  152. request.setRequestHeader("trakt-api-version", 2);
  153. request.setRequestHeader("Authorization", "Bearer "+auth_code+"");
  154. },
  155. type: "POST",
  156. url: "https://api-v2launch.trakt.tv/sync/collection",
  157. data: JSON.stringify(sendData),
  158. success: function (data) {
  159. console.log(data);
  160. },
  161. returnData: "json"
  162. });
  163. });
  164. $('a.kaGiantButton[href^="magnet:?xt"][data-nop]').on('click', function() {
  165. $.ajax({
  166. beforeSend: function (request)
  167. {
  168. request.setRequestHeader("Content-type", "application/json");
  169. request.setRequestHeader("trakt-api-key", "9efcadc5be0011a406fa0819192bd3aef0b3b2d9fa6ba90f3ffd3907138195d3");
  170. request.setRequestHeader("trakt-api-version", 2);
  171. request.setRequestHeader("Authorization", "Bearer "+auth_code+"");
  172. },
  173. type: "POST",
  174. url: "https://api-v2launch.trakt.tv/sync/collection",
  175. data: JSON.stringify(sendData),
  176. success: function (data) {
  177. console.log(data);
  178. },
  179. returnData: "json"
  180. });
  181. });
  182. }
  183. })();