Skip to content

Commit

Permalink
add ignore resolver option
Browse files Browse the repository at this point in the history
  • Loading branch information
mnsrulz committed Aug 31, 2024
1 parent 50a8a9a commit af60448
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 5 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ In some of the sharing sites, it's required to send some header information back
| timeout | number | timeout in seconds
| googleDrive.apiKey | string | optional api key to avoid anonymous access. Defaults to empty to allow anonymous access.
| customResolvers | array | custom resolvers you can inject for resolving urls. Refer to [app.tests](src/tests/app.tests.ts) for implementation samples
| ignoreResolvers | array | regex array you can inject to ignore executing specific resolvers. Refer to [app.tests](src/tests/app.tests.ts) for implementation samples


```js
Expand Down
4 changes: 3 additions & 1 deletion src/UrlResolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ export class UrlResolver {
timeout: 30
}, options);
const instance = this;

const _allResolvers = _options.customResolvers ? [...this.allResolvers, ..._options.customResolvers] : this.allResolvers;
const _filteredResolvers = _allResolvers.filter(j=> !_options.ignoreResolvers?.some(r=>r.test(j.name)));
let result: ResolvedMediaItem[] = [];
const timeoutPromise = new Promise(resolve => setTimeout(resolve, _options.timeout * 1000));
const actualPromise = _();
Expand All @@ -36,7 +38,7 @@ export class UrlResolver {
const urlsToResolve = typeof urlToResolve === "string" ? [urlToResolve] : urlToResolve;
const promises: Promise<ResolvedMediaItem[]>[] = [];
for (const iteratorurl of urlsToResolve) {
for (const resolver of _allResolvers) {
for (const resolver of _filteredResolvers) {
const element = new resolver();
const promise = element.resolve(iteratorurl, _options, instance).then(resolvedItems => result = [...result, ...resolvedItems]);
promises.push(promise);
Expand Down
15 changes: 14 additions & 1 deletion src/UrlResolverOptions.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,23 @@
import { BaseUrlResolver } from "./BaseResolver.js";

export type UrlResolverOptions = {
/**
* timeout in seconds
*/
timeout: number;
/**
* instruct the resolver to extract contenttype, lastmodifieddate and size of the file.
*/
extractMetaInformation: boolean;
/**
* allows you to provide a custom list of resolvers.
*/
customResolvers: { new(): BaseUrlResolver; }[];
googleDrive: {
apiKey: string
}
};
/**
* It's useful when you don't want to execute a list of resolvers. You can pass regular expression array.
*/
ignoreResolvers: RegExp[]
};
4 changes: 2 additions & 2 deletions src/libs/hdMoviesFlix.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { URL } from 'url';
export class hdMoviesFlixResolver extends BaseUrlResolver {
constructor() {
super({
domains: [/https?:\/\/(hdmoviesflix|moviesflix)/]
domains: [/https?:\/\/(hdmoviesflix|moviesflix|themoviesflix)/]
});
}

Expand All @@ -17,7 +17,7 @@ export class hdMoviesFlixResolver extends BaseUrlResolver {
return [{ title: decodeUrl.host, link: decodeUrl.href } as ResolvedMediaItem];
} else {
const response = await this.gotInstance(_urlToResolve);
const links = this.scrapeAllLinks(response.body, '.mb-container');
const links = this.scrapeAllLinks(response.body, '.page-body');
return links;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libs/linkrit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { BaseUrlResolver, ResolvedMediaItem } from "../BaseResolver.js";
export class LinkritResolver extends BaseUrlResolver {
constructor() {
super({
domains: [/https?:\/\/linkrit\.com/, /https?:\/\/extralinks/, /https?:\/\/dlink\.mobilejsr\.com/, /https?:\/\/dlinks\.dailypotrika/],
domains: [/https?:\/\/linkrit\.com/, /https?:\/\/extralinks/, /https?:\/\/(dlink\.)?mobilejsr\.com/, /https?:\/\/dlinks\.dailypotrika/],
useCookies: true
});
}
Expand Down
27 changes: 27 additions & 0 deletions src/tests/app.tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,30 @@ test('custom resolver registration', async t => {

t.is(result[0].link, resolvedFile);
});


test('resolver deregistration', async t => {
const resolvedFile = 'http://mylink/resolvedfile.mkv';
class customResolver extends BaseUrlResolver {
constructor() {
super({
domains: [/https?:\/\/(testdomain)/],
speedRank: 70
});
}

async resolveInner(_urlToResolve: string): Promise<ResolvedMediaItem | ResolvedMediaItem[]> {
return {
link: resolvedFile
} as ResolvedMediaItem;
}
}

const result = await nurl.resolve('http://testdomain.com/file.mkv', {
timeout: 100,
customResolvers: [customResolver],
ignoreResolvers: [/customResolver/]
});

t.is(result.length, 0);
});

0 comments on commit af60448

Please sign in to comment.