Added F-Droid search (#526) + search UI improvements

This commit is contained in:
Imran Remtulla
2023-05-03 18:40:34 -04:00
parent 9ec345761e
commit 639fc20fcb
7 changed files with 76 additions and 28 deletions

View File

@@ -70,7 +70,7 @@ class Codeberg extends AppSource {
}
@override
Future<Map<String, String>> search(String query) async {
Future<Map<String, List<String>>> search(String query) async {
return gh.searchCommon(
query,
'https://$host/api/v1/repos/search?q=${Uri.encodeQueryComponent(query)}&limit=100',

View File

@@ -1,6 +1,7 @@
import 'dart:convert';
import 'package:easy_localization/easy_localization.dart';
import 'package:html/parser.dart';
import 'package:http/http.dart';
import 'package:obtainium/custom_errors.dart';
import 'package:obtainium/providers/source_provider.dart';
@@ -9,6 +10,7 @@ class FDroid extends AppSource {
FDroid() {
host = 'f-droid.org';
name = tr('fdroid');
canSearch = true;
}
@override
@@ -68,4 +70,32 @@ class FDroid extends AppSource {
'https://$host/repo/$appId',
standardUrl);
}
@override
Future<Map<String, List<String>>> search(String query) async {
Response res = await get(Uri.parse('https://search.$host/?q=$query'));
if (res.statusCode == 200) {
Map<String, List<String>> urlsWithDescriptions = {};
parse(res.body).querySelectorAll('.package-header').forEach((e) {
String? url = e.attributes['href'];
if (url != null) {
try {
standardizeUrl(url);
} catch (e) {
url = null;
}
}
if (url != null) {
urlsWithDescriptions[url] = [
e.querySelector('.package-name')?.text.trim() ?? '',
e.querySelector('.package-summary')?.text.trim() ??
tr('noDescription')
];
}
});
return urlsWithDescriptions;
} else {
throw getObtainiumHttpError(res);
}
}
}

View File

@@ -213,19 +213,21 @@ class GitHub extends AppSource {
return AppNames(names[0], names[1]);
}
Future<Map<String, String>> searchCommon(
Future<Map<String, List<String>>> searchCommon(
String query, String requestUrl, String rootProp,
{Function(Response)? onHttpErrorCode}) async {
Response res = await get(Uri.parse(requestUrl));
if (res.statusCode == 200) {
Map<String, String> urlsWithDescriptions = {};
Map<String, List<String>> urlsWithDescriptions = {};
for (var e in (jsonDecode(res.body)[rootProp] as List<dynamic>)) {
urlsWithDescriptions.addAll({
e['html_url'] as String:
((e['archived'] == true ? '[ARCHIVED] ' : '') +
(e['description'] != null
? e['description'] as String
: tr('noDescription')))
e['html_url'] as String: [
e['full_name'] as String,
((e['archived'] == true ? '[ARCHIVED] ' : '') +
(e['description'] != null
? e['description'] as String
: tr('noDescription')))
]
});
}
return urlsWithDescriptions;
@@ -238,7 +240,7 @@ class GitHub extends AppSource {
}
@override
Future<Map<String, String>> search(String query) async {
Future<Map<String, List<String>>> search(String query) async {
return searchCommon(
query,
'https://${await getCredentialPrefixIfAny()}api.$host/search/repositories?q=${Uri.encodeQueryComponent(query)}&per_page=100',