| 
									
										
										
										
											2017-10-18 16:12:02 -05:00
										 |  |  | // -*- tab-width: 4; js-indent-level: 4; -*-
 | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  | // ==UserScript==
 | 
					
						
							|  |  |  | // @name         Trakt Scraper
 | 
					
						
							|  |  |  | // @namespace    danielrayjones
 | 
					
						
							| 
									
										
										
										
											2018-06-22 14:29:30 -05:00
										 |  |  | // @version      0.0.2
 | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  | // @description  Scrape lists of shows/movies from Trakt and download a JSON file
 | 
					
						
							|  |  |  | // @author       Dan Jones
 | 
					
						
							|  |  |  | // @match        https://trakt.tv/*
 | 
					
						
							|  |  |  | // @grant        none
 | 
					
						
							| 
									
										
										
										
											2017-10-19 11:55:21 -05:00
										 |  |  | // @require      https://raw.githubusercontent.com/tommcfarlin/konami-code/master/src/jquery.konami.min.js
 | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  | // ==/UserScript==
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 09:40:04 -05:00
										 |  |  | /* global $, compressedCache, localStorage */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  | (function() { | 
					
						
							|  |  |  |     'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 11:55:21 -05:00
										 |  |  |     $(window).konami({ | 
					
						
							|  |  |  |         code: [71,69,84], | 
					
						
							|  |  |  |         eventName: 'konami.get' | 
					
						
							|  |  |  |     }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     $(window).on('konami.get', getStuff); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 11:22:12 -05:00
										 |  |  |     String.prototype.lpad = function(padString, length) { | 
					
						
							|  |  |  |         let str = this; | 
					
						
							|  |  |  |         while (str.length < length) | 
					
						
							|  |  |  |             str = padString + str; | 
					
						
							|  |  |  |         return str; | 
					
						
							|  |  |  |     }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 11:55:21 -05:00
										 |  |  |     let watched_shows; | 
					
						
							|  |  |  |     let watched_movies; | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |     function processEpisode(that) { | 
					
						
							|  |  |  |         let $ep = $(that); | 
					
						
							|  |  |  |         let $series = $ep.find('[itemtype="http://schema.org/TVSeries"]'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 11:22:12 -05:00
										 |  |  |         if ($series.length < 1) { | 
					
						
							|  |  |  |             $series = $('[itemtype="http://schema.org/TVSeries"]'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  |         let series = $ep.data('show-id'); | 
					
						
							|  |  |  |         let ep = $ep.data('episode-id'); | 
					
						
							|  |  |  |         //let url = $ep.data('url');
 | 
					
						
							|  |  |  |         let url = $ep.children('[itemprop="url"]').attr('content'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let series_title = $series.children('[itemprop="name"]').attr('content'); | 
					
						
							| 
									
										
										
										
											2017-10-19 11:22:12 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         let $ep_title = $ep.children('[itemprop="name"]'); | 
					
						
							|  |  |  |         if ($ep_title.length < 1) { | 
					
						
							|  |  |  |             $ep_title = $ep.find('[itemprop="name"]'); | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-15 09:40:04 -05:00
										 |  |  |         let released = $ep.data('released') || null; | 
					
						
							|  |  |  |         if (released) { | 
					
						
							|  |  |  |             released = (new Date(released)).getTime()/1000; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 11:22:12 -05:00
										 |  |  |         let ep_title = $ep_title.attr('content'); | 
					
						
							|  |  |  |         let ep_number = $ep.find('[itemprop="episodeNumber"]').attr('content'); | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  |         let season_number = $ep.data('season-number'); | 
					
						
							|  |  |  |         //let title = $ep.data('title');
 | 
					
						
							| 
									
										
										
										
											2017-10-19 11:22:12 -05:00
										 |  |  |         let title = series_title + " " + season_number + "x" + String(ep_number).lpad("0", 2) + ' "' + ep_title + '"'; | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 16:12:02 -05:00
										 |  |  |         let watched = watched_shows[series] ? watched_shows[series].e[ep] : null; | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 11:55:21 -05:00
										 |  |  |         let this_ep = { | 
					
						
							|  |  |  |             series_id: series, | 
					
						
							|  |  |  |             episode_id: ep, | 
					
						
							|  |  |  |             title, | 
					
						
							|  |  |  |             url, | 
					
						
							| 
									
										
										
										
											2019-10-15 09:40:04 -05:00
										 |  |  |             released, | 
					
						
							| 
									
										
										
										
											2017-10-19 11:55:21 -05:00
										 |  |  |             watches: watched ? watched[1] : 0, | 
					
						
							|  |  |  |             last_watched: watched ? watched[0] : null, | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return this_ep; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     function processMovie(that) { | 
					
						
							|  |  |  |         let $mov = $(that); | 
					
						
							|  |  |  |         let id = $mov.data('movie-id'); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         let title = $mov.children('[itemprop="name"]').attr('content'); | 
					
						
							|  |  |  |         let url = $mov.children('[itemprop="url"]').attr('content'); | 
					
						
							|  |  |  |         let watched = watched_movies[id]; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 11:55:21 -05:00
										 |  |  |         let this_mov = { | 
					
						
							|  |  |  |             id, | 
					
						
							|  |  |  |             title, | 
					
						
							|  |  |  |             url, | 
					
						
							|  |  |  |             watches: watched ? watched[1] : 0, | 
					
						
							|  |  |  |             last_watched: watched ? watched[0] : null, | 
					
						
							|  |  |  |         }; | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return this_mov; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 11:55:21 -05:00
										 |  |  |     function getStuff() { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-22 14:27:42 -05:00
										 |  |  |         if ('compressedCache' in window) { | 
					
						
							|  |  |  |             watched_shows = compressedCache.get('watched_shows'); | 
					
						
							|  |  |  |             watched_movies = compressedCache.get('watched_movies'); | 
					
						
							|  |  |  |         } else { | 
					
						
							|  |  |  |             watched_shows = JSON.parse(localStorage.watched_shows); | 
					
						
							|  |  |  |             watched_movies = JSON.parse(localStorage.watched_movies); | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2017-10-19 11:55:21 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 12:15:57 -05:00
										 |  |  |         let $el = $('<a>'); | 
					
						
							|  |  |  |         $(document.body).append($el); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 11:55:21 -05:00
										 |  |  |         let items = $('[itemtype="http://schema.org/TVEpisode"], [itemtype="http://schema.org/Movie"]').map(function() { | 
					
						
							|  |  |  |             let type = $(this).attr('itemtype').replace(/^https?:\/\/schema.org\//, ''); | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 11:55:21 -05:00
										 |  |  |             let data = {}; | 
					
						
							|  |  |  |             switch(type) { | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  |             case "TVEpisode": | 
					
						
							|  |  |  |                 data = processEpisode(this); | 
					
						
							|  |  |  |                 break; | 
					
						
							|  |  |  |             case "Movie": | 
					
						
							|  |  |  |                 data = processMovie(this); | 
					
						
							|  |  |  |                 break; | 
					
						
							| 
									
										
										
										
											2017-10-19 11:55:21 -05:00
										 |  |  |             } | 
					
						
							|  |  |  |             data.type = type; | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 11:55:21 -05:00
										 |  |  |             return data; | 
					
						
							|  |  |  |         }); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         console.log(items); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-19 12:15:57 -05:00
										 |  |  |         let data_string = 'data:text/json;charset=utf-8,' + encodeURIComponent(JSON.stringify(items.toArray())); | 
					
						
							|  |  |  |         $el.attr('href', data_string); | 
					
						
							|  |  |  |         $el.attr('download', 'watched.json'); | 
					
						
							|  |  |  |         $el.get(0).click(); | 
					
						
							| 
									
										
										
										
											2017-10-19 11:55:21 -05:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-18 16:02:05 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | })(); |