Initial commit: Xtream IPTV Player for Android TV
This commit is contained in:
161
lib/services/xtream_api.dart
Normal file
161
lib/services/xtream_api.dart
Normal file
@@ -0,0 +1,161 @@
|
||||
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<Map<String, dynamic>> 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<XtreamUserInfo> getUserInfo() async {
|
||||
final data = await authenticate();
|
||||
return XtreamUserInfo.fromJson(data);
|
||||
}
|
||||
|
||||
Future<List<XtreamCategory>> 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<dynamic> data = json.decode(response.body);
|
||||
return data.map((e) => XtreamCategory.fromJson(e)).toList();
|
||||
}
|
||||
throw Exception('Failed to load categories');
|
||||
}
|
||||
|
||||
Future<List<XtreamCategory>> 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<dynamic> data = json.decode(response.body);
|
||||
return data.map((e) => XtreamCategory.fromJson(e)).toList();
|
||||
}
|
||||
throw Exception('Failed to load VOD categories');
|
||||
}
|
||||
|
||||
Future<List<XtreamCategory>> 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<dynamic> data = json.decode(response.body);
|
||||
return data.map((e) => XtreamCategory.fromJson(e)).toList();
|
||||
}
|
||||
throw Exception('Failed to load series categories');
|
||||
}
|
||||
|
||||
Future<List<XtreamStream>> 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<dynamic> 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<List<XtreamStream>> 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<dynamic> 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<List<XtreamSeries>> 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<dynamic> data = json.decode(response.body);
|
||||
return data.map((e) => XtreamSeries.fromJson(e)).toList();
|
||||
}
|
||||
throw Exception('Failed to load series');
|
||||
}
|
||||
|
||||
Future<List<XtreamEpisode>> 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<dynamic> episodesData = data['episodes'] ?? [];
|
||||
|
||||
final List<XtreamEpisode> allEpisodes = [];
|
||||
for (final seasonData in episodesData) {
|
||||
final season = seasonData['season_number'] ?? 0;
|
||||
final List<dynamic> 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';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user