Skip to content

Commit

Permalink
Cleanup (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
ImranR98 authored Nov 6, 2022
1 parent ed4a26d commit 97ab723
Show file tree
Hide file tree
Showing 19 changed files with 505 additions and 550 deletions.
13 changes: 7 additions & 6 deletions lib/app_sources/apkmirror.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:html/parser.dart';
import 'package:http/http.dart';
import 'package:obtainium/components/generated_form.dart';
import 'package:obtainium/custom_errors.dart';
import 'package:obtainium/providers/source_provider.dart';

class APKMirror implements AppSource {
Expand All @@ -12,7 +13,7 @@ class APKMirror implements AppSource {
RegExp standardUrlRegEx = RegExp('^https?://$host/apk/[^/]+/[^/]+');
RegExpMatch? match = standardUrlRegEx.firstMatch(url.toLowerCase());
if (match == null) {
throw notValidURL(runtimeType.toString());
throw InvalidURLError(runtimeType.toString());
}
return url.substring(0, match.end);
}
Expand Down Expand Up @@ -57,22 +58,22 @@ class APKMirror implements AppSource {
String standardUrl, List<String> additionalData) async {
Response res = await get(Uri.parse('$standardUrl/feed'));
if (res.statusCode != 200) {
throw couldNotFindReleases;
throw NoReleasesError();
}
var nextUrl = parse(res.body)
.querySelector('item')
?.querySelector('link')
?.nextElementSibling
?.innerHtml;
if (nextUrl == null) {
throw couldNotFindReleases;
throw NoReleasesError();
}
Response res2 = await get(Uri.parse(nextUrl), headers: {
'User-Agent':
'Mozilla/5.0 (X11; Linux x86_64; rv:105.0) Gecko/20100101 Firefox/105.0'
});
if (res2.statusCode != 200) {
throw couldNotFindReleases;
throw NoReleasesError();
}
var html2 = parse(res2.body);
var origin = Uri.parse(standardUrl).origin;
Expand All @@ -85,11 +86,11 @@ class APKMirror implements AppSource {
.map((e) => '$origin$e')
.toList();
if (apkUrls.isEmpty) {
throw noAPKFound;
throw NoAPKError();
}
var version = html2.querySelector('span.active.accent_color')?.innerHtml;
if (version == null) {
throw couldNotFindLatestVersion;
throw NoVersionError();
}
return APKDetails(version, apkUrls);
}
Expand Down
11 changes: 6 additions & 5 deletions lib/app_sources/fdroid.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:html/parser.dart';
import 'package:http/http.dart';
import 'package:obtainium/components/generated_form.dart';
import 'package:obtainium/custom_errors.dart';
import 'package:obtainium/providers/source_provider.dart';

class FDroid implements AppSource {
Expand All @@ -18,7 +19,7 @@ class FDroid implements AppSource {
RegExp standardUrlRegExA = RegExp('^https?://$host/+packages/+[^/]+');
match = standardUrlRegExA.firstMatch(url.toLowerCase());
if (match == null) {
throw notValidURL(runtimeType.toString());
throw InvalidURLError(runtimeType.toString());
}
return url.substring(0, match.end);
}
Expand All @@ -36,7 +37,7 @@ class FDroid implements AppSource {
if (res.statusCode == 200) {
var releases = parse(res.body).querySelectorAll('.package-version');
if (releases.isEmpty) {
throw couldNotFindReleases;
throw NoReleasesError();
}
String? latestVersion = releases[0]
.querySelector('.package-version-header b')
Expand All @@ -45,7 +46,7 @@ class FDroid implements AppSource {
.sublist(1)
.join(' ');
if (latestVersion == null) {
throw couldNotFindLatestVersion;
throw NoVersionError();
}
List<String> apkUrls = releases
.where((element) =>
Expand All @@ -64,11 +65,11 @@ class FDroid implements AppSource {
.where((element) => element.isNotEmpty)
.toList();
if (apkUrls.isEmpty) {
throw noAPKFound;
throw NoAPKError();
}
return APKDetails(latestVersion, apkUrls);
} else {
throw couldNotFindReleases;
throw NoReleasesError();
}
}

Expand Down
10 changes: 5 additions & 5 deletions lib/app_sources/github.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class GitHub implements AppSource {
RegExp standardUrlRegEx = RegExp('^https?://$host/[^/]+/[^/]+');
RegExpMatch? match = standardUrlRegEx.firstMatch(url.toLowerCase());
if (match == null) {
throw notValidURL(runtimeType.toString());
throw InvalidURLError(runtimeType.toString());
}
return url.substring(0, match.end);
}
Expand Down Expand Up @@ -84,14 +84,14 @@ class GitHub implements AppSource {
break;
}
if (targetRelease == null) {
throw couldNotFindReleases;
throw NoReleasesError();
}
if ((targetRelease['apkUrls'] as List<String>).isEmpty) {
throw noAPKFound;
throw NoAPKError();
}
String? version = targetRelease['tag_name'];
if (version == null) {
throw couldNotFindLatestVersion;
throw NoVersionError();
}
return APKDetails(version, targetRelease['apkUrls']);
} else {
Expand All @@ -102,7 +102,7 @@ class GitHub implements AppSource {
.round());
}

throw couldNotFindReleases;
throw NoReleasesError();
}
}

Expand Down
13 changes: 8 additions & 5 deletions lib/app_sources/gitlab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:html/parser.dart';
import 'package:http/http.dart';
import 'package:obtainium/app_sources/github.dart';
import 'package:obtainium/components/generated_form.dart';
import 'package:obtainium/custom_errors.dart';
import 'package:obtainium/providers/source_provider.dart';

class GitLab implements AppSource {
Expand All @@ -13,7 +14,7 @@ class GitLab implements AppSource {
RegExp standardUrlRegEx = RegExp('^https?://$host/[^/]+/[^/]+');
RegExpMatch? match = standardUrlRegEx.firstMatch(url.toLowerCase());
if (match == null) {
throw notValidURL(runtimeType.toString());
throw InvalidURLError(runtimeType.toString());
}
return url.substring(0, match.end);
}
Expand All @@ -39,7 +40,9 @@ class GitLab implements AppSource {
...getLinksFromParsedHTML(
entryContent,
RegExp(
'^${escapeRegEx(standardUri.path)}/uploads/[^/]+/[^/]+\\.apk\$',
'^${standardUri.path.replaceAllMapped(RegExp(r'[.*+?^${}()|[\]\\]'), (x) {
return '\\${x[0]}';
})}/uploads/[^/]+/[^/]+\\.apk\$',
caseSensitive: false),
standardUri.origin),
// GitLab releases may contain links to externally hosted APKs
Expand All @@ -49,18 +52,18 @@ class GitLab implements AppSource {
.toList()
];
if (apkUrlList.isEmpty) {
throw noAPKFound;
throw NoAPKError();
}

var entryId = entry?.querySelector('id')?.innerHtml;
var version =
entryId == null ? null : Uri.parse(entryId).pathSegments.last;
if (version == null) {
throw couldNotFindLatestVersion;
throw NoVersionError();
}
return APKDetails(version, apkUrlList);
} else {
throw couldNotFindReleases;
throw NoReleasesError();
}
}

Expand Down
9 changes: 5 additions & 4 deletions lib/app_sources/izzyondroid.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:html/parser.dart';
import 'package:http/http.dart';
import 'package:obtainium/components/generated_form.dart';
import 'package:obtainium/custom_errors.dart';
import 'package:obtainium/providers/source_provider.dart';

class IzzyOnDroid implements AppSource {
Expand All @@ -12,7 +13,7 @@ class IzzyOnDroid implements AppSource {
RegExp standardUrlRegEx = RegExp('^https?://$host/repo/apk/[^/]+');
RegExpMatch? match = standardUrlRegEx.firstMatch(url.toLowerCase());
if (match == null) {
throw notValidURL(runtimeType.toString());
throw InvalidURLError(runtimeType.toString());
}
return url.substring(0, match.end);
}
Expand All @@ -37,7 +38,7 @@ class IzzyOnDroid implements AppSource {
.map((e) => 'https://$host${e.attributes['href'] ?? ''}')
.toList();
if (multipleVersionApkUrls.isEmpty) {
throw noAPKFound;
throw NoAPKError();
}
var version = parsedHtml
.querySelector('#keydata')
Expand All @@ -50,11 +51,11 @@ class IzzyOnDroid implements AppSource {
?.children[1]
.innerHtml;
if (version == null) {
throw couldNotFindLatestVersion;
throw NoVersionError();
}
return APKDetails(version, [multipleVersionApkUrls[0]]);
} else {
throw couldNotFindReleases;
throw NoReleasesError();
}
}

Expand Down
7 changes: 4 additions & 3 deletions lib/app_sources/mullvad.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:html/parser.dart';
import 'package:http/http.dart';
import 'package:obtainium/components/generated_form.dart';
import 'package:obtainium/custom_errors.dart';
import 'package:obtainium/providers/source_provider.dart';

class Mullvad implements AppSource {
Expand All @@ -12,7 +13,7 @@ class Mullvad implements AppSource {
RegExp standardUrlRegEx = RegExp('^https?://$host');
RegExpMatch? match = standardUrlRegEx.firstMatch(url.toLowerCase());
if (match == null) {
throw notValidURL(runtimeType.toString());
throw InvalidURLError(runtimeType.toString());
}
return url.substring(0, match.end);
}
Expand All @@ -36,12 +37,12 @@ class Mullvad implements AppSource {
?.split('/')
.last;
if (version == null) {
throw couldNotFindLatestVersion;
throw NoVersionError();
}
return APKDetails(
version, ['https://mullvad.net/download/app/apk/latest']);
} else {
throw couldNotFindReleases;
throw NoReleasesError();
}
}

Expand Down
7 changes: 4 additions & 3 deletions lib/app_sources/signal.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:convert';
import 'package:http/http.dart';
import 'package:obtainium/components/generated_form.dart';
import 'package:obtainium/custom_errors.dart';
import 'package:obtainium/providers/source_provider.dart';

class Signal implements AppSource {
Expand All @@ -27,15 +28,15 @@ class Signal implements AppSource {
var json = jsonDecode(res.body);
String? apkUrl = json['url'];
if (apkUrl == null) {
throw noAPKFound;
throw NoAPKError();
}
String? version = json['versionName'];
if (version == null) {
throw couldNotFindLatestVersion;
throw NoVersionError();
}
return APKDetails(version, [apkUrl]);
} else {
throw couldNotFindReleases;
throw NoReleasesError();
}
}

Expand Down
9 changes: 5 additions & 4 deletions lib/app_sources/sourceforge.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'package:html/parser.dart';
import 'package:http/http.dart';
import 'package:obtainium/components/generated_form.dart';
import 'package:obtainium/custom_errors.dart';
import 'package:obtainium/providers/source_provider.dart';

class SourceForge implements AppSource {
Expand All @@ -12,7 +13,7 @@ class SourceForge implements AppSource {
RegExp standardUrlRegEx = RegExp('^https?://$host/projects/[^/]+');
RegExpMatch? match = standardUrlRegEx.firstMatch(url.toLowerCase());
if (match == null) {
throw notValidURL(runtimeType.toString());
throw InvalidURLError(runtimeType.toString());
}
return url.substring(0, match.end);
}
Expand Down Expand Up @@ -42,7 +43,7 @@ class SourceForge implements AppSource {

String? version = getVersion(allDownloadLinks[0]);
if (version == null) {
throw couldNotFindLatestVersion;
throw NoVersionError();
}
var apkUrlListAllReleases = allDownloadLinks
.where((element) => element.toLowerCase().endsWith('.apk/download'))
Expand All @@ -52,11 +53,11 @@ class SourceForge implements AppSource {
.where((element) => getVersion(element) == version)
.toList();
if (apkUrlList.isEmpty) {
throw noAPKFound;
throw NoAPKError();
}
return APKDetails(version, apkUrlList);
} else {
throw couldNotFindReleases;
throw NoReleasesError();
}
}

Expand Down
Loading

0 comments on commit 97ab723

Please sign in to comment.