2024-10-29 01:49:32 +04:00
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:pmu_labs/data/dtos/bosses_dto.dart';
|
|
|
|
import 'package:pmu_labs/data/mappers/bosses_mapper.dart';
|
2024-12-21 14:35:19 +04:00
|
|
|
import 'package:pmu_labs/domain/models/card_data.dart';
|
|
|
|
import 'package:pmu_labs/domain/models/home_data.dart';
|
2024-10-29 01:49:32 +04:00
|
|
|
import 'package:pretty_dio_logger/pretty_dio_logger.dart';
|
|
|
|
import 'package:html/parser.dart' as html;
|
|
|
|
|
|
|
|
import 'api_interface.dart';
|
|
|
|
|
|
|
|
Map<String, dynamic> transformJsonToBossesDtoFormat(
|
|
|
|
Map<String, dynamic> pages, List<(String, String)> descs) {
|
|
|
|
final transformedData = pages.values.map((boss) {
|
|
|
|
final title = boss['title'] as String;
|
|
|
|
final imageUrl = boss['original']?['source'] as String? ?? '';
|
|
|
|
final description = descs.firstWhere((desc) => desc.$1 == title).$2;
|
2024-11-25 23:02:19 +04:00
|
|
|
final id = boss['pageid'].toString();
|
2024-10-29 01:49:32 +04:00
|
|
|
|
|
|
|
return {
|
|
|
|
'title': title,
|
|
|
|
'imageUrl': imageUrl,
|
|
|
|
'description': description,
|
2024-11-25 23:02:19 +04:00
|
|
|
'id': id,
|
2024-10-29 01:49:32 +04:00
|
|
|
};
|
|
|
|
}).toList();
|
|
|
|
|
|
|
|
// Формируем структуру, которая будет соответствовать BossesDto
|
|
|
|
return {
|
|
|
|
'data': transformedData,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void removeByTitle(Map<String, dynamic> map, String title) {
|
|
|
|
map.removeWhere((key, value) {
|
|
|
|
return value is Map<String, dynamic> && value['title'] == title;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2024-11-25 23:02:19 +04:00
|
|
|
void getBySearch(Map<String, dynamic> map, String? title) {
|
2024-10-29 01:49:32 +04:00
|
|
|
map.removeWhere((key, value) {
|
|
|
|
return value is Map<String, dynamic> && value['title'] != title;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class BossesRepository extends ApiInterface {
|
|
|
|
static final Dio _dio = Dio()
|
|
|
|
..interceptors.add(PrettyDioLogger(
|
|
|
|
requestHeader: true,
|
|
|
|
requestBody: true,
|
|
|
|
));
|
|
|
|
|
|
|
|
static const String _baseUrl = 'https://sekiro.fandom.com/ru/api.php';
|
|
|
|
|
|
|
|
@override
|
2024-12-21 14:35:19 +04:00
|
|
|
Future<HomeData?> loadData({
|
2024-11-25 23:02:19 +04:00
|
|
|
OnErrorCallback? onError,
|
|
|
|
String? q,
|
|
|
|
}) async {
|
2024-10-29 01:49:32 +04:00
|
|
|
try {
|
|
|
|
String url = '';
|
2024-11-25 23:02:19 +04:00
|
|
|
if (q != null && q != "") {
|
|
|
|
url =
|
|
|
|
'$_baseUrl?action=query&titles=$q&prop=pageimages&piprop=original&format=json&origin=*';
|
|
|
|
} else {
|
2024-10-29 01:49:32 +04:00
|
|
|
url =
|
|
|
|
'$_baseUrl?action=query&generator=categorymembers&gcmtitle=Category:Боссы&gcmnamespace=0&gcmlimit=50&prop=pageimages&piprop=original&format=json&origin=*';
|
|
|
|
}
|
2024-12-21 14:35:19 +04:00
|
|
|
final HomeData? data;
|
2024-11-25 23:02:19 +04:00
|
|
|
BossesDto dto;
|
2024-10-29 01:49:32 +04:00
|
|
|
|
2024-11-25 23:02:19 +04:00
|
|
|
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(url);
|
2024-10-29 01:49:32 +04:00
|
|
|
final pages = response.data['query']['pages'] as Map<String, dynamic>;
|
|
|
|
|
|
|
|
removeByTitle(pages, 'Боссы');
|
|
|
|
|
|
|
|
List<(String, String)> descs = [];
|
|
|
|
|
2024-12-21 14:35:19 +04:00
|
|
|
final Response<dynamic> respDesc = await _dio.get<Map<dynamic, dynamic>>(
|
|
|
|
'https://sekiro.fandom.com/ru/api.php?action=query&generator=categorymembers&gcmtitle=Category:Боссы&gcmnamespace=0&gcmlimit=50&prop=revisions&rvprop=content&format=json&origin=*');
|
|
|
|
|
2024-10-29 01:49:32 +04:00
|
|
|
for (var boss in pages.values) {
|
2024-12-21 14:35:19 +04:00
|
|
|
final htmlContent = respDesc.data['query']['pages']['${boss['pageid']}']['revisions'][0]['*'];
|
|
|
|
var document = html.parse(htmlContent);
|
|
|
|
String text = document.body?.text ?? '';
|
|
|
|
text = text.split("==Галерея==")[0];
|
|
|
|
text = text.split("== Галерея ==")[0];
|
2024-12-21 20:15:27 +04:00
|
|
|
text = text.split("== Видео ==")[0];
|
2024-12-21 14:35:19 +04:00
|
|
|
text = text.replaceAll(RegExp(r'\{[^\}]*\}'), '');
|
2024-12-21 20:15:27 +04:00
|
|
|
text = text.replaceAll(RegExp(r'[{}]'), '');
|
2024-12-21 14:35:19 +04:00
|
|
|
descs.add((boss['title'], text.trim()));
|
2024-10-29 01:49:32 +04:00
|
|
|
}
|
|
|
|
|
2024-12-21 14:35:19 +04:00
|
|
|
|
|
|
|
|
2024-10-29 01:49:32 +04:00
|
|
|
final transData = transformJsonToBossesDtoFormat(pages, descs);
|
2024-11-25 23:02:19 +04:00
|
|
|
dto = BossesDto.fromJson(transData);
|
|
|
|
if (q != null && q != "") {
|
2024-10-29 01:49:32 +04:00
|
|
|
getBySearch(pages, q);
|
|
|
|
final transData = transformJsonToBossesDtoFormat(pages, descs);
|
2024-11-25 23:02:19 +04:00
|
|
|
dto = BossesDto.fromJson(transData);
|
2024-10-29 01:49:32 +04:00
|
|
|
}
|
|
|
|
|
2024-11-25 23:02:19 +04:00
|
|
|
|
2024-12-21 14:35:19 +04:00
|
|
|
data = dto.toDomain();
|
2024-10-29 01:49:32 +04:00
|
|
|
return data;
|
|
|
|
} on DioException catch (e) {
|
2024-11-11 18:13:49 +04:00
|
|
|
onError?.call(e.error?.toString());
|
2024-10-29 01:49:32 +04:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|