Skip to content

Commit

Permalink
Handle non HDR and non 4K titles
Browse files Browse the repository at this point in the history
  • Loading branch information
Morea committed Dec 9, 2023
1 parent dad06ac commit 124f1a1
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 39 deletions.
53 changes: 47 additions & 6 deletions Find Unique Titles/dist/find.unique.titles.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@
return "PTP";
}
async search(request) {
if (!isSupportedCategory(request.category)) return _tracker__WEBPACK_IMPORTED_MODULE_0__.SearchResult.NOT_ALLOWED;
if (!this.isAllowed(request)) return _tracker__WEBPACK_IMPORTED_MODULE_0__.SearchResult.NOT_ALLOWED;
let torrents = [];
let result;
if (!request.imdbId) {
Expand Down Expand Up @@ -1569,6 +1569,10 @@
for (let torrent of request.torrents) if (searchTorrent(torrent, torrents)) searchResult = _tracker__WEBPACK_IMPORTED_MODULE_0__.SearchResult.EXIST_BUT_MISSING_SLOT; else torrent.dom.style.display = "none";
return searchResult;
}
isAllowed(request) {
if (!isSupportedCategory(request.category)) return false;
return true;
}
insertTrackersSelect(select) {
let element = document.querySelector(".search-form__footer__buttons");
if (!element) return;
Expand Down Expand Up @@ -1611,6 +1615,10 @@
if ("SD" === second.resolution) return isSD(first.resolution);
}
const searchTorrent = (torrent, availableTorrents) => {
if ("x265" == torrent.container && "2160p" != torrent.resolution && !torrent.tags?.includes("HDR")) {
common_logger__WEBPACK_IMPORTED_MODULE_3__.logger.debug("[PTP] Torrent not allowed: non HDR X265 and not 2160p");
return false;
}
const similarTorrents = availableTorrents.filter((e => sameResolution(torrent, e) && (void 0 === torrent.container || sameContainer(e.container, torrent.container)) && (!torrent.tags.includes("Remux") || e.tags.includes("Remux"))));
if (0 == similarTorrents.length && torrent.resolution && torrent.container) return true;
if (1 == similarTorrents.length) if (torrent.size > 1.5 * similarTorrents[0].size || similarTorrents[0].size > 1.5 * torrent.size) return true;
Expand Down Expand Up @@ -1876,11 +1884,13 @@
}
async* getSearchRequest() {
common_logger__WEBPACK_IMPORTED_MODULE_1__.logger.debug("[{0}] Parsing titles to check", this.name());
const elements = document.querySelectorAll(".torrent");
const elements = Array.from(document.querySelectorAll(".torrent"));
yield {
total: elements.length
};
for (let element of elements) {
const torrentTitle = element.querySelector(".name a").childNodes[0].textContent;
common_logger__WEBPACK_IMPORTED_MODULE_1__.logger.debug("[TL] Checking torrent: {0}", torrentTitle);
const imdbId = (0, _utils_utils__WEBPACK_IMPORTED_MODULE_2__.parseImdbIdFromLink)(element);
const size = (0, _utils_utils__WEBPACK_IMPORTED_MODULE_2__.parseSize)(element.querySelector(".td-size")?.textContent);
const category = parseCategory(element);
Expand All @@ -1890,8 +1900,10 @@
const request = {
torrents: [ {
size,
tags: [],
dom: element
tags: (0, _utils_utils__WEBPACK_IMPORTED_MODULE_2__.parseTags)(torrentTitle),
dom: element,
resolution: (0, _utils_utils__WEBPACK_IMPORTED_MODULE_2__.parseResolution)(torrentTitle),
container: (0, _utils_utils__WEBPACK_IMPORTED_MODULE_2__.parseCodec)(torrentTitle)
} ],
dom: [ element ],
imdbId,
Expand Down Expand Up @@ -2353,10 +2365,12 @@
},
"./src/utils/utils.ts": (__unused_webpack_module, __webpack_exports__, __webpack_require__) => {
__webpack_require__.d(__webpack_exports__, {
parseCodec: () => parseCodec,
parseImdbId: () => parseImdbId,
parseImdbIdFromLink: () => parseImdbIdFromLink,
parseResolution: () => parseResolution,
parseSize: () => parseSize,
parseTags: () => parseTags,
parseYearAndTitleFromReleaseName: () => parseYearAndTitleFromReleaseName
});
const parseSize = text => {
Expand All @@ -2377,9 +2391,18 @@
return results[0];
};
const parseResolution = text => {
const resolutions = [ "720p", "1080p", "2160p" ];
const resolutionsAndAliases = {
"720p": [ "720p", "hd" ],
"1080p": [ "1080p", "fhd", "full_hd" ],
"2160p": [ "2160p", "uhd", "4k" ],
SD: [ "sd", "pal", "ntsc" ]
};
if (!text) return null;
for (let resolution of resolutions) if (text.includes(resolution)) return resolution;
for (let resolution in resolutionsAndAliases) {
let aliases = resolutionsAndAliases[resolution];
for (let alias of aliases) if (text.includes(alias)) return resolution;
if (text.includes(resolution)) return resolution;
}
const regex = /\b(\d{3})x(\d{3})\b/;
const match = text.match(regex);
if (match) return match[0];
Expand All @@ -2401,6 +2424,24 @@
title: void 0
};
};
const parseCodec = title => {
title = title.toLowerCase();
const codecAndAlias = {
x264: [ "x264", "h264", "h.264", "h 264" ],
x265: [ "x265", "h265", "h.265", "h 265", "hevc" ]
};
for (let codec in codecAndAlias) {
let aliases = codecAndAlias[codec];
for (let alias of aliases) if (title.includes(alias)) return codec;
}
return null;
};
const parseTags = title => {
const tags = [];
if (title.toLowerCase().includes("remux")) tags.push("Remux");
if (title.includes("HDR")) tags.push("HDR");
return tags;
};
},
"../common/dist/dom/index.mjs": (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {
__webpack_require__.d(__webpack_exports__, {
Expand Down
41 changes: 20 additions & 21 deletions Find Unique Titles/src/trackers/PTP.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
import { addToMemoryCache, getFromMemoryCache } from "../utils/cache";
import {
parseImdbIdFromLink,
parseResolution,
parseSize,
} from "../utils/utils";
import {
Category,
MetaData,
Request,
SearchResult,
toGenerator,
Torrent,
tracker,
} from "./tracker";
import { parseImdbIdFromLink, parseResolution, parseSize } from "../utils/utils";
import { Category, MetaData, Request, SearchResult, toGenerator, Torrent, tracker } from "./tracker";
import { findFirst, insertBefore } from "common/dom";
import { fetchAndParseHtml } from "common/http";
import { logger } from "common/logger";


function isSupportedCategory(category: Category | undefined) {
return (
category === undefined ||
Expand All @@ -27,7 +16,7 @@ function isSupportedCategory(category: Category | undefined) {
}

const parseTorrents = (element: HTMLElement) => {
const torrents = [];
const torrents: Array<Torrent> = [];
if (element.classList.contains("cover-movie-list__movie")) {
return [];
}
Expand All @@ -38,7 +27,7 @@ const parseTorrents = (element: HTMLElement) => {
return;
}
const size = parseSize(element.children[2].textContent);
let title = element.querySelector(".torrent-info-link").textContent;
let title = element.querySelector(".torrent-info-link")!!.textContent!!;
const resolution = parseResolution(title);
const tags = [];
if (title.includes("Remux")) {
Expand Down Expand Up @@ -129,7 +118,7 @@ export default class PTP implements tracker {
}

async search(request: Request): Promise<SearchResult> {
if (!isSupportedCategory(request.category)) return SearchResult.NOT_ALLOWED;
if (!this.isAllowed(request)) return SearchResult.NOT_ALLOWED;
let torrents = [];
let result;
if (!request.imdbId) {
Expand All @@ -146,7 +135,7 @@ export default class PTP implements tracker {
result = await fetchAndParseHtml(query_url);
torrents = parseAvailableTorrents(result);
} else {
return SearchResult.NOT_CHECKED
return SearchResult.NOT_CHECKED;
}
} else {
torrents = getFromMemoryCache(request.imdbId);
Expand All @@ -168,12 +157,12 @@ export default class PTP implements tracker {
}
}
if (request.imdbId) {
return SearchResult.NOT_EXIST
return SearchResult.NOT_EXIST;
} else {
return SearchResult.MAYBE_NOT_EXIST
return SearchResult.MAYBE_NOT_EXIST;
}
}
let searchResult: SearchResult = SearchResult.EXIST
let searchResult: SearchResult = SearchResult.EXIST;
for (let torrent of request.torrents) {
if (searchTorrent(torrent, torrents)) {
searchResult = SearchResult.EXIST_BUT_MISSING_SLOT;
Expand All @@ -184,6 +173,12 @@ export default class PTP implements tracker {
return searchResult;
}

private isAllowed(request: Request) : boolean {
if (!isSupportedCategory(request.category)) return false;

return true
}

insertTrackersSelect(select: HTMLSelectElement): void {
let element = document.querySelector(".search-form__footer__buttons");
if (!element) return;
Expand Down Expand Up @@ -247,6 +242,10 @@ function sameResolution(first: Torrent, second: Torrent) {
}

const searchTorrent = (torrent: Torrent, availableTorrents: Array<Torrent>) => {
if (torrent.container == "x265" && torrent.resolution != "2160p" && !torrent.tags?.includes("HDR")) {
logger.debug("[PTP] Torrent not allowed: non HDR X265 and not 2160p")
return false;
}
const similarTorrents = availableTorrents.filter((e) => {
return (
sameResolution(torrent, e) &&
Expand Down
24 changes: 14 additions & 10 deletions Find Unique Titles/src/trackers/TL.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { parseImdbIdFromLink, parseSize } from "../utils/utils";
import {
Category,
MetaData,
Request,
SearchResult,
tracker,
} from "./tracker";
parseCodec,
parseImdbIdFromLink,
parseResolution,
parseSize,
parseTags,
} from "../utils/utils";
import { Category, MetaData, Request, SearchResult, tracker } from "./tracker";
import { addChild } from "common/dom";
import { logger } from "common/logger";

Expand Down Expand Up @@ -49,11 +49,13 @@ export default class TL implements tracker {

async *getSearchRequest(): AsyncGenerator<MetaData | Request, void, void> {
logger.debug(`[{0}] Parsing titles to check`, this.name());
const elements = document.querySelectorAll(".torrent");
const elements = Array.from(document.querySelectorAll(".torrent"));
yield {
total: elements.length,
};
for (let element of elements) {
const torrentTitle = element.querySelector(".name a")!!.childNodes[0].textContent!!;
logger.debug("[TL] Checking torrent: {0}", torrentTitle)
const imdbId = parseImdbIdFromLink(element as HTMLElement);
const size = parseSize(
element.querySelector(".td-size")?.textContent as string
Expand All @@ -63,14 +65,16 @@ export default class TL implements tracker {
let year = undefined;
if (category == Category.MOVIE) {
({ title, year } = parseYearAndTitle(element));
}

}
const request: Request = {
torrents: [
{
size,
tags: [],
tags: parseTags(torrentTitle),
dom: element as HTMLElement,
resolution: parseResolution(torrentTitle),
container: parseCodec(torrentTitle),
},
],
dom: [element as HTMLElement],
Expand Down
41 changes: 39 additions & 2 deletions Find Unique Titles/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,20 @@ export const parseImdbId = (text: string) => {
};

export const parseResolution = (text: string) => {
const resolutions = ["720p", "1080p", "2160p"];
const resolutionsAndAliases: Record<string, string[]> = {
"720p": ["720p", "hd"],
"1080p": ["1080p", "fhd", "full_hd"],
"2160p": ["2160p", "uhd", "4k"],
SD: ["sd", "pal", "ntsc"],
};
if (!text) return null;
for (let resolution of resolutions) {
for (let resolution in resolutionsAndAliases) {
let aliases = resolutionsAndAliases[resolution];
for (let alias of aliases) {
if (text.includes(alias)) {
return resolution;
}
}
if (text.includes(resolution)) return resolution;
}
const regex = /\b(\d{3})x(\d{3})\b/;
Expand All @@ -59,3 +70,29 @@ export const parseYearAndTitleFromReleaseName = (releaseName: string) => {
}
return { year: undefined, title: undefined };
};

export const parseCodec = (title: string) => {
title = title.toLowerCase();
const codecAndAlias: Record<string, string[]> = {
x264: ["x264", "h264", "h.264", "h 264"],
x265: ["x265", "h265", "h.265", "h 265", "hevc"],
};
for (let codec in codecAndAlias) {
let aliases = codecAndAlias[codec];
for (let alias of aliases) {
if (title.includes(alias)) {
return codec;
}
}
}

return null;
};

export const parseTags = (title: string) => {
const tags: string[] = [];
if (title.toLowerCase().includes("remux")) tags.push("Remux");
if (title.includes("HDR")) tags.push("HDR");

return tags;
};

0 comments on commit 124f1a1

Please sign in to comment.