-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try explore url if direct download failed
- Loading branch information
1 parent
161d903
commit 4801c3e
Showing
3 changed files
with
74 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { Source, SourceType } from '../../normalize-source'; | ||
import { proxies } from '../../settings'; | ||
|
||
export const exploreUrl = async function(source: Source): Promise<Source> { | ||
if (source.type !== SourceType.direct || source.src instanceof Blob) { | ||
return source; | ||
} | ||
|
||
const attepmts: ((src: Source) => Generator<Source>)[] = [ | ||
tryAsIs, | ||
tryFromQueryParams, | ||
tryProxy, | ||
]; | ||
|
||
for (const attempt of attepmts) { | ||
for (const result of attempt(source)) { | ||
try { | ||
const a = await fetch(result.src as string, { mode: 'cors', method: 'HEAD' }); | ||
return result; | ||
} catch { | ||
} | ||
} | ||
} | ||
|
||
return source; | ||
}; | ||
|
||
const tryAsIs = function* (source: Source): Generator<Source> { | ||
yield source; | ||
}; | ||
|
||
const tryFromQueryParams = function* (source: Source): Generator<Source> { | ||
if (typeof source.src !== 'string') return source; | ||
let url: URL; | ||
try { | ||
url = new URL(source.src); | ||
} catch { | ||
return source | ||
} | ||
for (const [_, value] of url.searchParams) { | ||
try { | ||
new URL(value); | ||
} catch { | ||
continue; | ||
} | ||
const result = new Source({ type: SourceType.direct, src: value }); | ||
yield result; | ||
yield* tryFromQueryParams(result); | ||
} | ||
}; | ||
|
||
const tryProxy = function* (source: Source): Generator<Source> { | ||
if (typeof source.src !== 'string') return source; | ||
|
||
const paths = new URL(source.src).pathname.split('.'); | ||
const extenssion = paths[paths.length - 1]; | ||
const isHls = extenssion === 'm3u8'; | ||
if ((isHls && !proxies.hlsUrl) || (!isHls && !proxies.regularUrl)) { | ||
yield source; | ||
} | ||
let src: string; | ||
if (isHls) { | ||
src = `${proxies.hlsUrl}/${btoa(source.src)}.m3u8`; | ||
} else { | ||
src = `${proxies.regularUrl}?url=${source.src}`; | ||
} | ||
yield new Source({ ...source, src }); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters