mirror of
https://github.com/ImranR98/Obtainium.git
synced 2025-07-17 07:06:43 +02:00
Lint all files
This commit is contained in:
@ -100,28 +100,37 @@ bool _isNumeric(String s) {
|
||||
return s.codeUnitAt(0) >= 48 && s.codeUnitAt(0) <= 57;
|
||||
}
|
||||
|
||||
List<MapEntry<String, String>> getLinksInLines(String lines) => RegExp(
|
||||
r'(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?')
|
||||
.allMatches(lines)
|
||||
.map((match) =>
|
||||
MapEntry(match.group(0)!, match.group(0)?.split('/').last ?? ''))
|
||||
.toList();
|
||||
List<MapEntry<String, String>> getLinksInLines(String lines) =>
|
||||
RegExp(
|
||||
r'(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?',
|
||||
)
|
||||
.allMatches(lines)
|
||||
.map(
|
||||
(match) =>
|
||||
MapEntry(match.group(0)!, match.group(0)?.split('/').last ?? ''),
|
||||
)
|
||||
.toList();
|
||||
|
||||
// Given an HTTP response, grab some links according to the common additional settings
|
||||
// (those that apply to intermediate and final steps)
|
||||
Future<List<MapEntry<String, String>>> grabLinksCommon(
|
||||
Response res, Map<String, dynamic> additionalSettings) async {
|
||||
Response res,
|
||||
Map<String, dynamic> additionalSettings,
|
||||
) async {
|
||||
if (res.statusCode != 200) {
|
||||
throw getObtainiumHttpError(res);
|
||||
}
|
||||
var html = parse(res.body);
|
||||
List<MapEntry<String, String>> allLinks = html
|
||||
.querySelectorAll('a')
|
||||
.map((element) => MapEntry(
|
||||
.map(
|
||||
(element) => MapEntry(
|
||||
element.attributes['href'] ?? '',
|
||||
element.text.isNotEmpty
|
||||
? element.text
|
||||
: (element.attributes['href'] ?? '').split('/').last))
|
||||
: (element.attributes['href'] ?? '').split('/').last,
|
||||
),
|
||||
)
|
||||
.where((element) => element.key.isNotEmpty)
|
||||
.map((e) => MapEntry(ensureAbsoluteUrl(e.key, res.request!.url), e.value))
|
||||
.toList();
|
||||
@ -134,9 +143,13 @@ Future<List<MapEntry<String, String>>> grabLinksCommon(
|
||||
var jsonStrings = collectAllStringsFromJSONObject(jsonDecode(res.body));
|
||||
allLinks = getLinksInLines(jsonStrings.join('\n'));
|
||||
if (allLinks.isEmpty) {
|
||||
allLinks = getLinksInLines(jsonStrings.map((l) {
|
||||
return ensureAbsoluteUrl(l, res.request!.url);
|
||||
}).join('\n'));
|
||||
allLinks = getLinksInLines(
|
||||
jsonStrings
|
||||
.map((l) {
|
||||
return ensureAbsoluteUrl(l, res.request!.url);
|
||||
})
|
||||
.join('\n'),
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
//
|
||||
@ -165,17 +178,20 @@ Future<List<MapEntry<String, String>>> grabLinksCommon(
|
||||
} catch (e) {
|
||||
// Some links may not have valid encoding
|
||||
}
|
||||
return Uri.parse(filterLinkByText ? element.value : link)
|
||||
.path
|
||||
.toLowerCase()
|
||||
.endsWith('.apk');
|
||||
return Uri.parse(
|
||||
filterLinkByText ? element.value : link,
|
||||
).path.toLowerCase().endsWith('.apk');
|
||||
}).toList();
|
||||
}
|
||||
if (!skipSort) {
|
||||
links.sort((a, b) => additionalSettings['sortByLastLinkSegment'] == true
|
||||
? compareAlphaNumeric(a.key.split('/').where((e) => e.isNotEmpty).last,
|
||||
b.key.split('/').where((e) => e.isNotEmpty).last)
|
||||
: compareAlphaNumeric(a.key, b.key));
|
||||
links.sort(
|
||||
(a, b) => additionalSettings['sortByLastLinkSegment'] == true
|
||||
? compareAlphaNumeric(
|
||||
a.key.split('/').where((e) => e.isNotEmpty).last,
|
||||
b.key.split('/').where((e) => e.isNotEmpty).last,
|
||||
)
|
||||
: compareAlphaNumeric(a.key, b.key),
|
||||
);
|
||||
}
|
||||
if (additionalSettings['reverseSort'] == true) {
|
||||
links = links.reversed.toList();
|
||||
@ -201,102 +217,119 @@ class HTML extends AppSource {
|
||||
|
||||
var finalStepFormitems = [
|
||||
[
|
||||
GeneratedFormTextField('customLinkFilterRegex',
|
||||
label: tr('customLinkFilterRegex'),
|
||||
hint: 'download/(.*/)?(android|apk|mobile)',
|
||||
required: false,
|
||||
additionalValidators: [
|
||||
(value) {
|
||||
return regExValidator(value);
|
||||
}
|
||||
])
|
||||
GeneratedFormTextField(
|
||||
'customLinkFilterRegex',
|
||||
label: tr('customLinkFilterRegex'),
|
||||
hint: 'download/(.*/)?(android|apk|mobile)',
|
||||
required: false,
|
||||
additionalValidators: [
|
||||
(value) {
|
||||
return regExValidator(value);
|
||||
},
|
||||
],
|
||||
),
|
||||
],
|
||||
[
|
||||
GeneratedFormSwitch('versionExtractWholePage',
|
||||
label: tr('versionExtractWholePage'))
|
||||
]
|
||||
GeneratedFormSwitch(
|
||||
'versionExtractWholePage',
|
||||
label: tr('versionExtractWholePage'),
|
||||
),
|
||||
],
|
||||
];
|
||||
var commonFormItems = [
|
||||
[GeneratedFormSwitch('filterByLinkText', label: tr('filterByLinkText'))],
|
||||
[GeneratedFormSwitch('skipSort', label: tr('skipSort'))],
|
||||
[GeneratedFormSwitch('reverseSort', label: tr('takeFirstLink'))],
|
||||
[
|
||||
GeneratedFormSwitch('sortByLastLinkSegment',
|
||||
label: tr('sortByLastLinkSegment'))
|
||||
GeneratedFormSwitch(
|
||||
'sortByLastLinkSegment',
|
||||
label: tr('sortByLastLinkSegment'),
|
||||
),
|
||||
],
|
||||
];
|
||||
var intermediateFormItems = [
|
||||
[
|
||||
GeneratedFormTextField('customLinkFilterRegex',
|
||||
label: tr('intermediateLinkRegex'),
|
||||
hint: '([0-9]+.)*[0-9]+/\$',
|
||||
required: true,
|
||||
additionalValidators: [(value) => regExValidator(value)])
|
||||
GeneratedFormTextField(
|
||||
'customLinkFilterRegex',
|
||||
label: tr('intermediateLinkRegex'),
|
||||
hint: '([0-9]+.)*[0-9]+/\$',
|
||||
required: true,
|
||||
additionalValidators: [(value) => regExValidator(value)],
|
||||
),
|
||||
],
|
||||
[
|
||||
GeneratedFormSwitch('autoLinkFilterByArch',
|
||||
label: tr('autoLinkFilterByArch'), defaultValue: false)
|
||||
GeneratedFormSwitch(
|
||||
'autoLinkFilterByArch',
|
||||
label: tr('autoLinkFilterByArch'),
|
||||
defaultValue: false,
|
||||
),
|
||||
],
|
||||
];
|
||||
HTML() {
|
||||
additionalSourceAppSpecificSettingFormItems = [
|
||||
[
|
||||
GeneratedFormSubForm(
|
||||
'intermediateLink', [...intermediateFormItems, ...commonFormItems],
|
||||
label: tr('intermediateLink'))
|
||||
GeneratedFormSubForm('intermediateLink', [
|
||||
...intermediateFormItems,
|
||||
...commonFormItems,
|
||||
], label: tr('intermediateLink')),
|
||||
],
|
||||
finalStepFormitems[0],
|
||||
...commonFormItems,
|
||||
...finalStepFormitems.sublist(1),
|
||||
[
|
||||
GeneratedFormSubForm(
|
||||
'requestHeader',
|
||||
'requestHeader',
|
||||
[
|
||||
[
|
||||
[
|
||||
GeneratedFormTextField('requestHeader',
|
||||
label: tr('requestHeader'),
|
||||
required: false,
|
||||
additionalValidators: [
|
||||
(value) {
|
||||
if ((value ?? 'empty:valid')
|
||||
.split(':')
|
||||
.map((e) => e.trim())
|
||||
.where((e) => e.isNotEmpty)
|
||||
.length <
|
||||
2) {
|
||||
return tr('invalidInput');
|
||||
}
|
||||
return null;
|
||||
}
|
||||
])
|
||||
]
|
||||
GeneratedFormTextField(
|
||||
'requestHeader',
|
||||
label: tr('requestHeader'),
|
||||
required: false,
|
||||
additionalValidators: [
|
||||
(value) {
|
||||
if ((value ?? 'empty:valid')
|
||||
.split(':')
|
||||
.map((e) => e.trim())
|
||||
.where((e) => e.isNotEmpty)
|
||||
.length <
|
||||
2) {
|
||||
return tr('invalidInput');
|
||||
}
|
||||
return null;
|
||||
},
|
||||
],
|
||||
),
|
||||
],
|
||||
label: tr('requestHeader'),
|
||||
defaultValue: [
|
||||
{
|
||||
'requestHeader':
|
||||
'User-Agent: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36'
|
||||
}
|
||||
])
|
||||
],
|
||||
label: tr('requestHeader'),
|
||||
defaultValue: [
|
||||
{
|
||||
'requestHeader':
|
||||
'User-Agent: Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Mobile Safari/537.36',
|
||||
},
|
||||
],
|
||||
),
|
||||
],
|
||||
[
|
||||
GeneratedFormDropdown(
|
||||
'defaultPseudoVersioningMethod',
|
||||
[
|
||||
MapEntry('partialAPKHash', tr('partialAPKHash')),
|
||||
MapEntry('APKLinkHash', tr('APKLinkHash')),
|
||||
MapEntry('ETag', 'ETag')
|
||||
],
|
||||
label: tr('defaultPseudoVersioningMethod'),
|
||||
defaultValue: 'partialAPKHash')
|
||||
]
|
||||
'defaultPseudoVersioningMethod',
|
||||
[
|
||||
MapEntry('partialAPKHash', tr('partialAPKHash')),
|
||||
MapEntry('APKLinkHash', tr('APKLinkHash')),
|
||||
MapEntry('ETag', 'ETag'),
|
||||
],
|
||||
label: tr('defaultPseudoVersioningMethod'),
|
||||
defaultValue: 'partialAPKHash',
|
||||
),
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
@override
|
||||
Future<Map<String, String>?> getRequestHeaders(
|
||||
Map<String, dynamic> additionalSettings,
|
||||
{bool forAPKDownload = false}) async {
|
||||
Map<String, dynamic> additionalSettings, {
|
||||
bool forAPKDownload = false,
|
||||
}) async {
|
||||
if (additionalSettings.isNotEmpty) {
|
||||
if (additionalSettings['requestHeader']?.isNotEmpty != true) {
|
||||
additionalSettings['requestHeader'] = [];
|
||||
@ -336,8 +369,9 @@ class HTML extends AppSource {
|
||||
.toList();
|
||||
for (int i = 0; i < (additionalSettings['intermediateLink'].length); i++) {
|
||||
var intLinks = await grabLinksCommon(
|
||||
await sourceRequest(currentUrl, additionalSettings),
|
||||
additionalSettings['intermediateLink'][i]);
|
||||
await sourceRequest(currentUrl, additionalSettings),
|
||||
additionalSettings['intermediateLink'][i],
|
||||
);
|
||||
if (intLinks.isEmpty) {
|
||||
throw NoReleasesError(note: currentUrl);
|
||||
} else {
|
||||
@ -353,11 +387,17 @@ class HTML extends AppSource {
|
||||
String versionExtractionWholePageString = currentUrl;
|
||||
if (additionalSettings['directAPKLink'] != true) {
|
||||
Response res = await sourceRequest(currentUrl, additionalSettings);
|
||||
versionExtractionWholePageString =
|
||||
res.body.split('\r\n').join('\n').split('\n').join('\\n');
|
||||
versionExtractionWholePageString = res.body
|
||||
.split('\r\n')
|
||||
.join('\n')
|
||||
.split('\n')
|
||||
.join('\\n');
|
||||
links = await grabLinksCommon(res, additionalSettings);
|
||||
links = filterApks(links, additionalSettings['apkFilterRegEx'],
|
||||
additionalSettings['invertAPKFilter']);
|
||||
links = filterApks(
|
||||
links,
|
||||
additionalSettings['apkFilterRegEx'],
|
||||
additionalSettings['invertAPKFilter'],
|
||||
);
|
||||
if (links.isEmpty) {
|
||||
throw NoReleasesError(note: currentUrl);
|
||||
}
|
||||
@ -373,37 +413,45 @@ class HTML extends AppSource {
|
||||
}
|
||||
String? version;
|
||||
version = extractVersion(
|
||||
additionalSettings['versionExtractionRegEx'] as String?,
|
||||
additionalSettings['matchGroupToUse'] as String?,
|
||||
additionalSettings['versionExtractWholePage'] == true
|
||||
? versionExtractionWholePageString
|
||||
: relDecoded);
|
||||
var apkReqHeaders =
|
||||
await getRequestHeaders(additionalSettings, forAPKDownload: true);
|
||||
additionalSettings['versionExtractionRegEx'] as String?,
|
||||
additionalSettings['matchGroupToUse'] as String?,
|
||||
additionalSettings['versionExtractWholePage'] == true
|
||||
? versionExtractionWholePageString
|
||||
: relDecoded,
|
||||
);
|
||||
var apkReqHeaders = await getRequestHeaders(
|
||||
additionalSettings,
|
||||
forAPKDownload: true,
|
||||
);
|
||||
if (version == null &&
|
||||
additionalSettings['defaultPseudoVersioningMethod'] == 'ETag') {
|
||||
version = await checkETagHeader(rel,
|
||||
headers: apkReqHeaders,
|
||||
allowInsecure: additionalSettings['allowInsecure'] == true);
|
||||
version = await checkETagHeader(
|
||||
rel,
|
||||
headers: apkReqHeaders,
|
||||
allowInsecure: additionalSettings['allowInsecure'] == true,
|
||||
);
|
||||
if (version == null) {
|
||||
throw NoVersionError();
|
||||
}
|
||||
}
|
||||
version ??=
|
||||
additionalSettings['defaultPseudoVersioningMethod'] == 'APKLinkHash'
|
||||
? rel.hashCode.toString()
|
||||
: (await checkPartialDownloadHashDynamic(rel,
|
||||
headers: apkReqHeaders,
|
||||
allowInsecure: additionalSettings['allowInsecure'] == true))
|
||||
.toString();
|
||||
? rel.hashCode.toString()
|
||||
: (await checkPartialDownloadHashDynamic(
|
||||
rel,
|
||||
headers: apkReqHeaders,
|
||||
allowInsecure: additionalSettings['allowInsecure'] == true,
|
||||
)).toString();
|
||||
return APKDetails(
|
||||
version,
|
||||
[rel].map((e) {
|
||||
var uri = Uri.parse(e);
|
||||
var fileName =
|
||||
uri.pathSegments.isNotEmpty ? uri.pathSegments.last : uri.origin;
|
||||
return MapEntry('${e.hashCode}-$fileName', e);
|
||||
}).toList(),
|
||||
AppNames(uri.host, tr('app')));
|
||||
version,
|
||||
[rel].map((e) {
|
||||
var uri = Uri.parse(e);
|
||||
var fileName = uri.pathSegments.isNotEmpty
|
||||
? uri.pathSegments.last
|
||||
: uri.origin;
|
||||
return MapEntry('${e.hashCode}-$fileName', e);
|
||||
}).toList(),
|
||||
AppNames(uri.host, tr('app')),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user