mangayomi/lib/services/anime_extractors/streamwish_extractor.dart
kodjomoustapha b10c3f3a22 misc changes
- remove cronet_http & cupertino_http
- use rhttp package as default http client
- fix #198 #200 crashes on multiple downloads
- fix #162 #102 unable to download with forbidden characters in the name (as it is fixed this can cause reading problems concerning chapters downloaded before this version)
- now supports all features on all platforms such as VPNs and HTTP proxies thanks to rhttp package
2024-08-21 13:30:13 +01:00

71 lines
2.4 KiB
Dart

import 'package:http_interceptor/http_interceptor.dart';
import 'package:js_packer/js_packer.dart';
import 'package:mangayomi/models/video.dart';
import 'package:mangayomi/services/http/m_client.dart';
import 'package:mangayomi/utils/extensions/others.dart';
import 'package:mangayomi/utils/extensions/string_extensions.dart';
import 'package:mangayomi/utils/xpath_selector.dart';
class StreamWishExtractor {
final InterceptedClient client =
MClient.init(reqcopyWith: {'useDartHttpClient': true});
final Map<String, String> headers = {};
Future<List<Video>> videosFromUrl(String url, String prefix) async {
final videoList = <Video>[];
try {
final response = await client.get(Uri.parse(url), headers: headers);
final jsEval = xpathSelector(response.body)
.queryXPath('//script[contains(text(), "m3u8")]/text()')
.attrs;
if (jsEval.isEmpty) {
return [];
}
String? masterUrl = jsEval.first!
.let(
(script) {
if (script.contains("function(p,a,c")) {
return JSPacker(script).unpack() ?? "";
}
return script;
},
)
.substringAfter('source')
.substringAfter('file:"')
.substringBefore('"');
if (masterUrl.isEmpty) return [];
final playlistHeaders = Map<String, String>.from(headers)
..addAll({
'Accept': '*/*',
'Host': Uri.parse(masterUrl).host,
'Origin': 'https://${Uri.parse(url).host}',
'Referer': 'https://${Uri.parse(url).host}/',
});
final masterBase =
'${'https://${Uri.parse(masterUrl).host}${Uri.parse(masterUrl).path}'.substringBeforeLast('/')}/';
final masterPlaylistResponse =
await client.get(Uri.parse(masterUrl), headers: playlistHeaders);
final masterPlaylist = masterPlaylistResponse.body;
const separator = '#EXT-X-STREAM-INF:';
masterPlaylist.substringAfter(separator).split(separator).forEach((it) {
final quality =
'$prefix - ${it.substringAfter('RESOLUTION=').substringAfter('x').substringBefore(',')}p ';
final videoUrl =
masterBase + it.substringAfter('\n').substringBefore('\n');
videoList
.add(Video(videoUrl, quality, videoUrl, headers: playlistHeaders));
});
return videoList;
} catch (_) {
return [];
}
}
}