Files
fastwatcher/lib/features/sender/data/services/link_extraction_service.dart
T

44 lines
1.2 KiB
Dart

import 'package:flutter_inappwebview/flutter_inappwebview.dart';
abstract class ILinkExtractionService {
Future<String?> extractVideoUrl(String pageUrl);
}
class LinkExtractionService implements ILinkExtractionService {
HeadlessInAppWebView? _headlessWebView;
@override
Future<String?> extractVideoUrl(String pageUrl) async {
// 1. Check if it's already a video link
if (_isVideoUrl(pageUrl)) return pageUrl;
// 2. Use Headless WebView to find the video
String? foundUrl;
_headlessWebView = HeadlessInAppWebView(
initialUrlRequest: URLRequest(url: WebUri(pageUrl)),
onLoadResource: (controller, resource) {
final url = resource.url.toString();
if (_isVideoUrl(url)) {
foundUrl = url;
_headlessWebView?.dispose();
}
},
);
await _headlessWebView?.run();
// Wait for some time to catch resources
await Future.delayed(const Duration(seconds: 5));
return foundUrl;
}
bool _isVideoUrl(String url) {
final lower = url.toLowerCase();
return lower.contains('.m3u8') ||
lower.contains('.mp4') ||
lower.contains('.mkv');
}
}