Skip to content

Instantly share code, notes, and snippets.

@itssoap
Created March 7, 2023 07:49
Show Gist options
  • Save itssoap/ecbf328481f0be371710da67697ead3e to your computer and use it in GitHub Desktop.
Save itssoap/ecbf328481f0be371710da67697ead3e to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Soap's PTP - Export collection to CSV
// @namespace http://tampermonkey.net/
// @version 1.6
// @description Add a button to export a whole collection to a .csv (imdb id, title, year, director, tags)
// @author Sapphire_e, Chameleon
// @include /^https?:\/\/passthepopcorn.me\/collages.php.*(\?|&)id=(\d*).*/
// @grant none
// ==/UserScript==
let csv = "sep=;\nTitle;Year;ReleaseName\n" // csv informations
const downloadCsv = () => {
const element = document.createElement('a');
const collection_id = new URLSearchParams(window.location.search).get("id")
const collection_title = document.querySelector(".page__title").textContent.trim()
element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(csv.slice(0, -1)));
element.setAttribute('download', `[PTP-Collection-${collection_id}] ${collection_title}.csv`);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
};
const nextPage = () => {
if (document.querySelector(".pagination__current-page")) {
if (parseInt(document.querySelector("#filter_torrents_count").textContent.replace(/[( Results),]/, "")) === parseInt(document.querySelector(".pagination__current-page").textContent.split("-")[1])) {
console.log("[Script: PTP - Export collection to CSV] Last page scraped.")
console.log("[Script: PTP - Export collection to CSV] Export finished. Downloading...")
downloadCsv()
stop()
console.log("[Script: PTP - Export collection to CSV] Done")
} else {
document.querySelector(".pagination__current-page").nextSibling.nextSibling.click()
}
} else {
console.log("[Script: PTP - Export collection to CSV] Export finished. Downloading...")
downloadCsv()
stop()
console.log("[Script: PTP - Export collection to CSV] Done")
}
};
const getPage = () => {
document.querySelectorAll(".group.basic-movie-list__details-row.js-basic-movie-list__details-row").forEach((movie) => {
const imdb_id = movie.querySelector('a[href*="imdb.com"]') ? /title\/(tt\d*)($|\/.*)/.exec(movie.querySelector('a[href*="imdb.com"]'))[1] : "no_imdb_id"
const title = movie.querySelector(".basic-movie-list__movie__title").textContent
const year = movie.querySelector(".basic-movie-list__movie__year").textContent.replace(/[\[\]]/g, "")
const director = movie.querySelector(".basic-movie-list__movie__director-list") ? movie.querySelector(".basic-movie-list__movie__director-list").textContent.replace(" by ", "").trim() : "no_directors_available"
const tags = movie.querySelector(".basic-movie-list__movie__tags") ? movie.querySelector(".basic-movie-list__movie__tags").textContent.trim() : "no_tags_available"
csv += `${imdb_id};"${title}";${year};"${director}";"${tags}"\n`
})
console.log("[Script: PTP - Export collection to CSV] Page scraped... Navigating to the next page.")
nextPage()
};
const observer = new MutationObserver(function() {
getPage()
});
const main = (t = true) => {
if (document.querySelector(".pagination__link.pagination__link--first")) {
document.querySelector(".pagination__link.pagination__link--first").click()
console.log("[Script: PTP - Export collection to CSV] Navigating to the first page.")
} else {
if (t) getPage()
}
const pageList = document.querySelector(".pagination.pagination--top.js-pagination")
observer.observe(pageList, {attributes: true, childList: true, subtree: true})
};
const stop = () => {
observer.disconnect()
};
// Chameleon's approach
// This is done this way, rather than a loop of some kind, mostly for the 1 second timeout between PTP page loads
async function getPage2(page)
{
// We have an API for getting collage pages, so you don't have to mess around with parsing one of 5 different pages
let urler = '/collages.php?action=get_page&page='+page+'&'+String(window.location.href.split('?')[1])
let result=await fetch(urler).then(response => response.json());
let flag=0;
for(var i=0; i<result.CoverView.Movies.length; i++)
{
//title;year;releases
let m=result.CoverView.Movies[i];
console.log(String(m));
console.log(String(m).length);
console.log(m);
let Title=String(m.Title)
csv+=valueTidyup(Title.replace(",", ""))
csv+=';'+valueTidyup(m.Year);
var release_name="Unknown";
for(var j=0; j<m.GroupingQualities[0].Torrents.length; j++)
{
try {release_name=m.GroupingQualities?m.GroupingQualities[0].Torrents[j].Title:m.Torrents[j].ReleaseName; } catch(e){}
const htmlregex=/<a.*?title='(.*?)'.*?>/;
const match = String(release_name.replace(/'.*\n/g, "")).match(htmlregex)
release_name=';'+decodeEntities(unfucker(match[1]));
if(j>0) release_name=';'+release_name;
csv+=release_name;
csv+='\n';
}
}
if(result.pages.indexOf('pagination__link--next')==-1)
{
downloadCsv();
return;
}
window.setTimeout(getPage2.bind(undefined, page+1), 1000);
}
let htmlTextarea=document.createElement('textarea');
function valueTidyup(value)
{
//value=value.replace(/;/g, '\;');
htmlTextarea.innerHTML=value;
value=htmlTextarea.value.trim();
if(value.indexOf(',')!=-1)
value='"'+value+'"';
value=value.split(' AKA ');
if(value.length==1)
return value[0];
return value[1];
}
function unfucker(str)
{
return str.replace(/(&#(\d+);)/g, function(match, capture, charCode) {
return String.fromCharCode(charCode);
});
}
var decodeEntities = (function() {
// this prevents any overhead from creating the object each time
var element = document.createElement('div');
function decodeHTMLEntities (str) {
if(str && typeof str === 'string') {
// strip script/html tags
str = str.replace(/<script[^>]*>([\S\s]*?)<\/script>/gmi, '');
str = str.replace(/<\/?\w(?:[^"'>]|"[^"]*"|'[^']*')*>/gmi, '');
element.innerHTML = str;
str = element.textContent;
element.textContent = '';
}
return str;
}
return decodeHTMLEntities;
})();
(function() {
'use strict';
document.querySelector(".linkbox").insertAdjacentHTML("beforeend", '[<a class="linkbox__link" href="#" id="collection_export">Export collection as .csv2</a>]')
document.querySelector("#collection_export").addEventListener("click", function (event) {
event.preventDefault()
console.log("[Script: PTP - Export collection to CSV] Starting the export...")
//main()
getPage2(1);
})
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment