101 lines
3.3 KiB
Dart
101 lines
3.3 KiB
Dart
|
import 'package:dio/dio.dart';
|
||
|
import 'package:pmu_labs/data/dtos/bosses_dto.dart';
|
||
|
import 'package:pmu_labs/data/mappers/bosses_mapper.dart';
|
||
|
import 'package:pmu_labs/domain/models/card.dart';
|
||
|
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;
|
||
|
|
||
|
return {
|
||
|
'title': title,
|
||
|
'imageUrl': imageUrl,
|
||
|
'description': description,
|
||
|
};
|
||
|
}).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;
|
||
|
});
|
||
|
}
|
||
|
|
||
|
void getBySearch(Map<String, dynamic> map, String? title){
|
||
|
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
|
||
|
Future<List<CardData>?> loadData({String? q}) async {
|
||
|
try {
|
||
|
String url = '';
|
||
|
if(q != null && q != ""){
|
||
|
url ='$_baseUrl?action=query&titles=$q&prop=pageimages&piprop=original&format=json&origin=*';
|
||
|
}else{
|
||
|
url =
|
||
|
'$_baseUrl?action=query&generator=categorymembers&gcmtitle=Category:Боссы&gcmnamespace=0&gcmlimit=50&prop=pageimages&piprop=original&format=json&origin=*';
|
||
|
}
|
||
|
|
||
|
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
|
||
|
url
|
||
|
);
|
||
|
final pages = response.data['query']['pages'] as Map<String, dynamic>;
|
||
|
|
||
|
removeByTitle(pages, 'Боссы');
|
||
|
|
||
|
|
||
|
|
||
|
List<(String, String)> descs = [];
|
||
|
|
||
|
for (var boss in pages.values) {
|
||
|
final Response<dynamic> respDesc = await _dio.get<
|
||
|
Map<dynamic, dynamic>>(
|
||
|
'https://sekiro.fandom.com/ru/api.php?action=parse&page=${boss['title']}&prop=text§ion=1&format=json');
|
||
|
final htmlContent = respDesc.data['parse']['text']['*'];
|
||
|
var doc = html.parse(htmlContent);
|
||
|
final String text = doc.body?.text as String;
|
||
|
descs.add((boss['title'], text));
|
||
|
}
|
||
|
|
||
|
final transData = transformJsonToBossesDtoFormat(pages, descs);
|
||
|
|
||
|
if(q != null && q != ""){
|
||
|
getBySearch(pages, q);
|
||
|
final transData = transformJsonToBossesDtoFormat(pages, descs);
|
||
|
final BossesDto dto = BossesDto.fromJson(transData);
|
||
|
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
|
||
|
return data;
|
||
|
}
|
||
|
|
||
|
final BossesDto dto = BossesDto.fromJson(transData);
|
||
|
final List<CardData>? data = dto.data?.map((e) => e.toDomain()).toList();
|
||
|
return data;
|
||
|
} on DioException catch (e) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
}
|