Skip to content

Commit

Permalink
made chanegs in how we used to load modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Manish Rawat committed Aug 15, 2020
1 parent 841bfd0 commit 40ba425
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 67 deletions.
138 changes: 71 additions & 67 deletions src/UrlResolver.ts
Original file line number Diff line number Diff line change
@@ -1,88 +1,92 @@
import { readdirSync } from "fs";
import { ResolvedMediaItem } from "./BaseResolver";
export class UrlResolver {
private allResolvers: any[];
constructor() {
var resolvers: any[] = [];
readdirSync(__dirname + '/libs/').forEach(function (file) {
if (file.match(/\.js$/) !== null && file !== 'index.js') {
var name = file.replace('.js', '');
var inst = require('./libs/' + file);
var allInstances = Object.keys(inst);
allInstances.forEach(element => {
resolvers.push(inst[element]);
});
}
});
import { ResolvedMediaItem, BaseUrlResolver } from "./BaseResolver";
import * as allResolverImports from "./allResolvers";

this.allResolvers = resolvers;
}
export class UrlResolver {
private allResolvers: any[];
constructor() {
//do filtering of the resolvers here
this.allResolvers = Object.values(allResolverImports).filter((x) =>
x.prototype instanceof BaseUrlResolver
);
}

/**
/**
*
* @param {string} urlToResolve
* @returns {string}
*/
async resolve(urlToResolve: string, options: Partial<UrlResolverOptions> = {}): Promise<ResolvedMediaItem[]> {
const _options = Object.assign({
timeout: 30
}, options);
const _allResolvers = this.allResolvers;
const timeoutPromise = new Promise<ResolvedMediaItem[]>((resolve) => {
setTimeout(resolve, _options.timeout * 1000);
return [];
});
const actualPromise = _();
return await Promise.race([timeoutPromise, actualPromise]);
async function _() {
for (let index = 0; index < _allResolvers.length; index++) {
const element = new _allResolvers[index]();
var fs = await element.resolve(urlToResolve);
if (fs && fs.length > 0)
return fs;
}
async resolve(
urlToResolve: string,
options: Partial<UrlResolverOptions> = {},
): Promise<ResolvedMediaItem[]> {
const _options = Object.assign({
timeout: 30,
}, options);
const _allResolvers = this.allResolvers;
const timeoutPromise = new Promise<ResolvedMediaItem[]>((resolve) => {
setTimeout(resolve, _options.timeout * 1000);
return [];
});
const actualPromise = _();
return await Promise.race([timeoutPromise, actualPromise]);
async function _() {
for (let index = 0; index < _allResolvers.length; index++) {
const element = new _allResolvers[index]();
var fs = await element.resolve(urlToResolve);
if (fs && fs.length > 0) {
return fs;
}
}
}
}

/**
/**
* Resolve recursively all the urls until all not fetched. It's a heavy call and
* can take minutes to resolve if the sources are slow to respond.
* @param {string} urlToResolve
* @returns {collection of resolved links}
*/
async resolveRecursive(urlToResolve: string, options: Partial<UrlResolverOptions> = {}): Promise<ResolvedMediaItem[]> {
const _options = Object.assign({
timeout: 30
}, options);
async resolveRecursive(
urlToResolve: string,
options: Partial<UrlResolverOptions> = {},
): Promise<ResolvedMediaItem[]> {
const _options = Object.assign({
timeout: 30,
}, options);

var instanceOfUrlResolver = this;
var myPlayableResources: any[] = [];
var visitedUrls: any[] = [];

var instanceOfUrlResolver = this;
var myPlayableResources: any[] = [];
var visitedUrls: any[] = [];
const timeoutPromise = new Promise(function (resolve, reject) {
setTimeout(resolve, _options.timeout * 1000);
});
const actualPromise = explode(urlToResolve);
await Promise.race([timeoutPromise, actualPromise]);

const timeoutPromise = new Promise(function (resolve, reject) {
setTimeout(resolve, _options.timeout * 1000);
return myPlayableResources;
async function explode(urlToVisit: string) {
if (visitedUrls.some((x) => x === urlToVisit)) return;
visitedUrls.push(urlToVisit);
console.log(urlToVisit);
var resolvedUrls = await instanceOfUrlResolver.resolve(
urlToVisit,
options,
) as ResolvedMediaItem[];
if (resolvedUrls) {
var p: any[] = [];
resolvedUrls.filter((x) => x.isPlayable).forEach((x) =>
myPlayableResources.push(x)
);
resolvedUrls.filter((x) => !x.isPlayable).forEach((x) => {
p.push(explode(x.link));
});
const actualPromise = explode(urlToResolve);
await Promise.race([timeoutPromise, actualPromise]);

return myPlayableResources;
async function explode(urlToVisit: string) {
if (visitedUrls.some(x => x === urlToVisit)) return;
visitedUrls.push(urlToVisit);
console.log(urlToVisit);
var resolvedUrls = await instanceOfUrlResolver.resolve(urlToVisit, options) as ResolvedMediaItem[];
if (resolvedUrls) {
var p: any[] = []
resolvedUrls.filter(x => x.isPlayable).forEach(x => myPlayableResources.push(x));
resolvedUrls.filter(x => !x.isPlayable).forEach(x => {
p.push(explode(x.link));
});
await Promise.all(p);
}
}
await Promise.all(p);
}
}
}
}

type UrlResolverOptions = {
timeout: number
}
timeout: number;
};
20 changes: 20 additions & 0 deletions src/allResolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export * from "./libs/cloudmailru";
export * from "./libs/cric8cc";
export * from "./libs/crnews";
export * from "./libs/daddyLive";
export * from "./libs/dlfiles";
export * from "./libs/dood";
export * from "./libs/extralinks";
export * from "./libs/extraMoviesResolver";
export * from "./libs/freespinwins";
export * from "./libs/gdrive";
export * from "./libs/hblinks";
export * from "./libs/indishare";
export * from "./libs/letsupload";
export * from "./libs/linkrit";
export * from "./libs/linkstaker";
export * from "./libs/megaup";
export * from "./libs/streamwire";
export * from "./libs/wicket";
export * from "./libs/yourupload";
export * from "./libs/zupload";

0 comments on commit 40ba425

Please sign in to comment.