Skip to content

Commit

Permalink
TS: Parse title and year
Browse files Browse the repository at this point in the history
  • Loading branch information
Morea committed Dec 9, 2023
1 parent fc6145f commit dad06ac
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 14 deletions.
54 changes: 45 additions & 9 deletions Find Unique Titles/dist/find.unique.titles.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -1920,10 +1920,10 @@
__webpack_require__.d(__webpack_exports__, {
default: () => TSeeds
});
var _utils_utils__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__("./src/utils/utils.ts");
var _utils_utils__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("./src/utils/utils.ts");
var _tracker__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__("./src/trackers/tracker.ts");
var common_dom__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__("../common/dist/dom/index.mjs");
var common_http__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__("../common/dist/http/index.mjs");
var common_http__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__("../common/dist/http/index.mjs");
var common_searcher__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__("../common/dist/searcher/index.mjs");
var common_trackers__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__("../common/dist/trackers/index.mjs");
const parseCategory = element => {
Expand All @@ -1937,6 +1937,20 @@
if (icon.classList.contains("fa-gamepad")) return _tracker__WEBPACK_IMPORTED_MODULE_0__.Category.GAME;
return;
};
const parseYearAndTitle = element => {
const spans = element.querySelectorAll("h1.movie-heading span");
if (1 == spans.length) {
const releaseName = element.querySelector('ol li.active span[itemprop="title"]').textContent.trim();
return (0, _utils_utils__WEBPACK_IMPORTED_MODULE_1__.parseYearAndTitleFromReleaseName)(releaseName);
}
const title = spans[0].textContent?.trim();
let yearText = spans[1].textContent.trim();
const year = parseInt(yearText.substring(1, yearText.length - 1), 10);
return {
title,
year
};
};
class TSeeds {
canBeUsedAsSource() {
return true;
Expand All @@ -1950,30 +1964,35 @@
async* getSearchRequest() {
let torrentsSelector = "#torrent-list-table tbody tr";
if (isCategoryPage()) torrentsSelector = ".cat-torrents table tbody tr";
let nodes = document.querySelectorAll(torrentsSelector);
let nodes = Array.from(document.querySelectorAll(torrentsSelector));
yield {
total: nodes.length
};
for (const element of nodes) {
const category = parseCategory(element);
let imdbId = null;
let title;
let year;
if (category == _tracker__WEBPACK_IMPORTED_MODULE_0__.Category.MOVIE) {
const link = element.querySelector('a.view-torrent[href*="/torrents/"]');
let response = await (0, common_http__WEBPACK_IMPORTED_MODULE_1__.fetchAndParseHtml)(link.href);
imdbId = (0, _utils_utils__WEBPACK_IMPORTED_MODULE_2__.parseImdbIdFromLink)(response);
let response = await (0, common_http__WEBPACK_IMPORTED_MODULE_2__.fetchAndParseHtml)(link.href);
imdbId = (0, _utils_utils__WEBPACK_IMPORTED_MODULE_1__.parseImdbIdFromLink)(response);
({title, year} = parseYearAndTitle(response));
}
let sizeText = element.querySelector(".torrent-listings-size span")?.textContent;
if (isCategoryPage()) sizeText = element.children[7]?.textContent?.trim();
const size = (0, _utils_utils__WEBPACK_IMPORTED_MODULE_2__.parseSize)(sizeText);
const size = (0, _utils_utils__WEBPACK_IMPORTED_MODULE_1__.parseSize)(sizeText);
const request = {
torrents: [ {
size,
tags: [],
dom: element
dom: element,
resolution: (0, _utils_utils__WEBPACK_IMPORTED_MODULE_1__.parseResolution)(element.querySelector("a.view-torrent").textContent)
} ],
dom: [ element ],
imdbId,
title: "",
title,
year,
category
};
yield request;
Expand Down Expand Up @@ -2337,7 +2356,8 @@
parseImdbId: () => parseImdbId,
parseImdbIdFromLink: () => parseImdbIdFromLink,
parseResolution: () => parseResolution,
parseSize: () => parseSize
parseSize: () => parseSize,
parseYearAndTitleFromReleaseName: () => parseYearAndTitleFromReleaseName
});
const parseSize = text => {
let size = null;
Expand Down Expand Up @@ -2365,6 +2385,22 @@
if (match) return match[0];
return null;
};
const parseYearAndTitleFromReleaseName = releaseName => {
const regex = /^(.+?)\.(\d{4})\./;
const match = releaseName.match(regex);
if (match) {
const title = match[1].replace(/\./g, " ").trim();
const year = parseInt(match[2], 10);
return {
year,
title
};
}
return {
year: void 0,
title: void 0
};
};
},
"../common/dist/dom/index.mjs": (__unused_webpack___webpack_module__, __webpack_exports__, __webpack_require__) => {
__webpack_require__.d(__webpack_exports__, {
Expand Down
3 changes: 2 additions & 1 deletion Find Unique Titles/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,6 @@
"last 1 chrome version",
"last 1 firefox version"
],
"sideEffects": false
"sideEffects": false,
"type": "module"
}
28 changes: 25 additions & 3 deletions Find Unique Titles/src/trackers/TSeeds.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { parseImdbIdFromLink, parseSize } from "../utils/utils";
import {
parseImdbIdFromLink, parseResolution,
parseSize,
parseYearAndTitleFromReleaseName
} from "../utils/utils";
import { Category, MetaData, Request, SearchResult, tracker } from "./tracker";
import { addChild } from "common/dom";
import { fetchAndParseHtml } from "common/http";
Expand All @@ -17,6 +21,19 @@ const parseCategory = (element: Element): Category | undefined => {
return undefined;
};

const parseYearAndTitle = (element: HTMLElement) => {
const spans = element.querySelectorAll("h1.movie-heading span");
if (spans.length == 1) {
const releaseName = element
.querySelector('ol li.active span[itemprop="title"]')!!
.textContent!!.trim();
return parseYearAndTitleFromReleaseName(releaseName);
}
const title = spans[0].textContent?.trim();
let yearText = spans[1].textContent!!.trim();
const year = parseInt(yearText.substring(1, yearText.length - 1), 10);
return { title, year };
};
export default class TSeeds implements tracker {
canBeUsedAsSource(): boolean {
return true;
Expand All @@ -35,13 +52,15 @@ export default class TSeeds implements tracker {
if (isCategoryPage()) {
torrentsSelector = ".cat-torrents table tbody tr";
}
let nodes = document.querySelectorAll(torrentsSelector);
let nodes = Array.from(document.querySelectorAll(torrentsSelector));
yield {
total: nodes.length,
};
for (const element of nodes) {
const category = parseCategory(element);
let imdbId = null;
let title = undefined;
let year = undefined;
if (category == Category.MOVIE) {
const link: HTMLAnchorElement | null = element.querySelector(
'a.view-torrent[href*="/torrents/"]'
Expand All @@ -50,6 +69,7 @@ export default class TSeeds implements tracker {
(link as HTMLAnchorElement).href
);
imdbId = parseImdbIdFromLink(response as HTMLElement);
({ title, year } = parseYearAndTitle(response));
}
let sizeText = element.querySelector(".torrent-listings-size span")
?.textContent as string;
Expand All @@ -64,11 +84,13 @@ export default class TSeeds implements tracker {
size,
tags: [],
dom: element as HTMLElement,
resolution: parseResolution(element.querySelector('a.view-torrent')!!.textContent!!)
},
],
dom: [element as HTMLElement],
imdbId,
title: "",
title,
year,
category,
};
yield request;
Expand Down
12 changes: 12 additions & 0 deletions Find Unique Titles/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,15 @@ export const parseResolution = (text: string) => {
}
return null;
};

export const parseYearAndTitleFromReleaseName = (releaseName: string) => {
const regex = /^(.+?)\.(\d{4})\./;
const match = releaseName.match(regex);

if (match) {
const title = match[1].replace(/\./g, " ").trim();
const year = parseInt(match[2], 10);
return { year, title };
}
return { year: undefined, title: undefined };
};
3 changes: 2 additions & 1 deletion Find Unique Titles/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"resolveJsonModule": true,
"moduleResolution": "NodeNext"
"moduleResolution": "NodeNext",
"lib": ["es2021", "dom"]
},
"exclude": ["./dist"]
}

0 comments on commit dad06ac

Please sign in to comment.