import 'dart:convert'; import 'package:http/http.dart' as http; import '../models/xtream_models.dart'; class XtreamApiService { String? _server; String? _username; String? _password; String? _baseUrl; void setCredentials(String server, String username, String password) { _server = server; _username = username; _password = password; _baseUrl = server.startsWith('http') ? server : 'http://$server'; } String? get server => _server; String? get username => _username; Future> authenticate() async { final url = '$_baseUrl/player_api.php'; final response = await http.get( Uri.parse('$url?username=$_username&password=$_password'), ); if (response.statusCode == 200) { return json.decode(response.body); } throw Exception('Authentication failed: ${response.statusCode}'); } Future getUserInfo() async { final data = await authenticate(); return XtreamUserInfo.fromJson(data); } Future> getLiveCategories() async { final url = '$_baseUrl/player_api.php'; final response = await http.get( Uri.parse('$url?username=$_username&password=$_password&action=get_live_categories'), ); if (response.statusCode == 200) { final List data = json.decode(response.body); return data.map((e) => XtreamCategory.fromJson(e)).toList(); } throw Exception('Failed to load categories'); } Future> getVodCategories() async { final url = '$_baseUrl/player_api.php'; final response = await http.get( Uri.parse('$url?username=$_username&password=$_password&action=get_vod_categories'), ); if (response.statusCode == 200) { final List data = json.decode(response.body); return data.map((e) => XtreamCategory.fromJson(e)).toList(); } throw Exception('Failed to load VOD categories'); } Future> getSeriesCategories() async { final url = '$_baseUrl/player_api.php'; final response = await http.get( Uri.parse('$url?username=$_username&password=$_password&action=get_series_categories'), ); if (response.statusCode == 200) { final List data = json.decode(response.body); return data.map((e) => XtreamCategory.fromJson(e)).toList(); } throw Exception('Failed to load series categories'); } Future> getLiveStreams(String categoryId) async { final url = '$_baseUrl/player_api.php'; String apiUrl = '$url?username=$_username&password=$_password&action=get_live_streams'; if (categoryId.isNotEmpty) { apiUrl += '&category_id=$categoryId'; } final response = await http.get(Uri.parse(apiUrl)); if (response.statusCode == 200) { final List data = json.decode(response.body); return data.map((e) { final stream = XtreamStream.fromJson(e); stream.url = '$_baseUrl/live/$_username/$_password/${stream.streamId}.ts'; return stream; }).toList(); } throw Exception('Failed to load live streams'); } Future> getVodStreams(String categoryId) async { final url = '$_baseUrl/player_api.php'; String apiUrl = '$url?username=$_username&password=$_password&action=get_vod_streams'; if (categoryId.isNotEmpty) { apiUrl += '&category_id=$categoryId'; } final response = await http.get(Uri.parse(apiUrl)); if (response.statusCode == 200) { final List data = json.decode(response.body); return data.map((e) { final stream = XtreamStream.fromJson(e); final ext = stream.containerExtension ?? 'm3u8'; stream.url = '$_baseUrl/vod/$_username/$_password/${stream.streamId}.$ext'; return stream; }).toList(); } throw Exception('Failed to load VOD streams'); } Future> getSeries() async { final url = '$_baseUrl/player_api.php'; final response = await http.get( Uri.parse('$url?username=$_username&password=$_password&action=get_series'), ); if (response.statusCode == 200) { final List data = json.decode(response.body); return data.map((e) => XtreamSeries.fromJson(e)).toList(); } throw Exception('Failed to load series'); } Future> getSeriesEpisodes(int seriesId) async { final url = '$_baseUrl/player_api.php'; final response = await http.get( Uri.parse('$url?username=$_username&password=$_password&action=get_series_info&series_id=$seriesId'), ); if (response.statusCode == 200) { final data = json.decode(response.body); final List episodesData = data['episodes'] ?? []; final List allEpisodes = []; for (final seasonData in episodesData) { final season = seasonData['season_number'] ?? 0; final List episodes = seasonData['episodes'] ?? []; for (final ep in episodes) { final episode = XtreamEpisode.fromJson(ep); final ext = episode.containerExtension ?? 'm3u8'; episode.url = '$_baseUrl/series/$_username/$_password/${episode.episodeId}.$ext'; allEpisodes.add(episode); } } return allEpisodes; } throw Exception('Failed to load series episodes'); } String getStreamUrl(int streamId, {String type = 'live'}) { final ext = type == 'live' ? 'ts' : 'm3u8'; return '$_baseUrl/$type/$_username/$_password/$streamId.$ext'; } }