mangayomi-mirror/lib/services/anime_extractors/streamlare_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

83 lines
2.9 KiB
Dart

import 'package:http_interceptor/http_interceptor.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';
class StreamlareExtractor {
final InterceptedClient client =
MClient.init(reqcopyWith: {'useDartHttpClient': true});
Future<List<Video>> videosFromUrl(String url,
{String prefix = "", String suffix = ""}) async {
try {
final id = url.split('/').last;
final playlistResponse = await client.post(
Uri.parse('https://slwatch.co/api/video/stream/get'),
headers: {'Content-Type': 'application/json'},
body: '{"id":"$id"}',
);
final playlist = playlistResponse.body;
final type = playlist.substringAfter('"type":"').substringBefore('"');
if (type == 'hls') {
final masterPlaylistUrl = playlist
.substringAfter('"file":"')
.substringBefore('"')
.replaceAll(r'\/', '/');
final masterPlaylistResponse =
await client.get(Uri.parse(masterPlaylistUrl));
final masterPlaylist = masterPlaylistResponse.body;
const separator = '#EXT-X-STREAM-INF';
return masterPlaylist
.substringAfter(separator)
.split(separator)
.map((value) {
final quality =
'${value.substringAfter('RESOLUTION=').substringAfter('x').substringBefore(',')}p';
final videoUrl =
value.substringAfter('\n').substringBefore('\n').let((urlPart) {
return !urlPart.startsWith('http')
? masterPlaylistUrl.substringBefore('master.m3u8') + urlPart
: urlPart;
});
return Video(
videoUrl, _buildQuality(quality, prefix, suffix), videoUrl);
}).toList();
} else {
const separator = '"label":"';
List<Video> videoList = [];
List<String> values =
playlist.substringAfter(separator).split(separator);
for (var value in values) {
final quality = value.substringAfter(separator).substringBefore('",');
final apiUrl = value
.substringAfter('"file":"')
.substringBefore('",')
.replaceAll('\\', '');
final response = await client.post(Uri.parse(apiUrl));
final videoUrl = response.request!.url.toString();
videoList.add(Video(
videoUrl, _buildQuality(quality, prefix, suffix), videoUrl));
}
return videoList;
}
} catch (_) {
return [];
}
}
String _buildQuality(String resolution,
[String prefix = '', String suffix = '']) {
final buffer = StringBuffer();
if (prefix.isNotEmpty) buffer.write('$prefix ');
buffer.write('Streamlare:$resolution');
if (suffix.isNotEmpty) buffer.write(' $suffix');
return buffer.toString();
}
}