2022-09-11 02:01:06 +01:00
|
|
|
import 'dart:convert';
|
2022-08-18 15:33:33 +01:00
|
|
|
import 'dart:io';
|
2022-09-11 02:01:06 +01:00
|
|
|
import 'package:collection/collection.dart';
|
|
|
|
import 'package:dio/dio.dart';
|
2023-04-18 13:27:26 +05:30
|
|
|
import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';
|
2023-03-05 14:42:46 +05:30
|
|
|
import 'package:flutter/foundation.dart';
|
2022-08-18 15:33:33 +01:00
|
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
2022-09-12 01:41:53 +01:00
|
|
|
import 'package:injectable/injectable.dart';
|
2023-08-11 06:56:19 +05:45
|
|
|
import 'package:revanced_manager/app/app.locator.dart';
|
2022-09-11 02:01:06 +01:00
|
|
|
import 'package:revanced_manager/models/patch.dart';
|
2023-07-01 05:26:03 +05:45
|
|
|
import 'package:revanced_manager/services/manager_api.dart';
|
|
|
|
|
2022-09-12 01:41:53 +01:00
|
|
|
@lazySingleton
|
2022-08-01 01:16:27 +05:30
|
|
|
class GithubAPI {
|
2022-10-11 20:19:50 +05:30
|
|
|
late Dio _dio = Dio();
|
2023-08-11 06:56:19 +05:45
|
|
|
late final ManagerAPI _managerAPI = locator<ManagerAPI>();
|
2023-05-06 05:39:46 +05:30
|
|
|
|
2023-04-18 13:27:26 +05:30
|
|
|
final _cacheOptions = CacheOptions(
|
|
|
|
store: MemCacheStore(),
|
2022-09-28 22:26:54 +05:30
|
|
|
maxStale: const Duration(days: 1),
|
2023-04-18 13:27:26 +05:30
|
|
|
priority: CachePriority.high,
|
2022-09-11 02:01:06 +01:00
|
|
|
);
|
2023-04-18 13:27:26 +05:30
|
|
|
|
2022-08-30 02:07:28 +01:00
|
|
|
final Map<String, String> repoAppPath = {
|
|
|
|
'com.google.android.youtube': 'youtube',
|
|
|
|
'com.google.android.apps.youtube.music': 'music',
|
|
|
|
'com.twitter.android': 'twitter',
|
|
|
|
'com.reddit.frontpage': 'reddit',
|
|
|
|
'com.zhiliaoapp.musically': 'tiktok',
|
|
|
|
'de.dwd.warnapp': 'warnwetter',
|
|
|
|
'com.garzotto.pflotsh.ecmwf_a': 'ecmwf',
|
2022-10-10 18:57:32 +05:30
|
|
|
'com.spotify.music': 'spotify',
|
2022-08-30 02:07:28 +01:00
|
|
|
};
|
|
|
|
|
2023-01-30 18:05:06 +05:30
|
|
|
Future<void> initialize(String repoUrl) async {
|
2022-10-14 23:35:33 +05:30
|
|
|
try {
|
2023-01-30 18:05:06 +05:30
|
|
|
_dio = Dio(
|
|
|
|
BaseOptions(
|
|
|
|
baseUrl: repoUrl,
|
|
|
|
),
|
|
|
|
);
|
2022-10-11 20:19:50 +05:30
|
|
|
|
2023-04-18 13:27:26 +05:30
|
|
|
_dio.interceptors.add(DioCacheInterceptor(options: _cacheOptions));
|
2023-03-05 14:42:46 +05:30
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-10-11 20:19:50 +05:30
|
|
|
}
|
2022-09-11 02:01:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> clearAllCache() async {
|
2022-10-14 23:35:33 +05:30
|
|
|
try {
|
2023-04-18 13:27:26 +05:30
|
|
|
await _cacheOptions.store!.clean();
|
2023-09-29 18:39:07 +02:00
|
|
|
await DefaultCacheManager().emptyCache();
|
2023-03-05 14:42:46 +05:30
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-10-14 23:35:33 +05:30
|
|
|
}
|
2022-08-18 17:32:58 +01:00
|
|
|
}
|
|
|
|
|
2023-04-18 19:45:29 +05:30
|
|
|
Future<Map<String, dynamic>?> getLatestRelease(
|
|
|
|
String repoName,
|
|
|
|
) async {
|
2023-07-08 22:26:31 +05:45
|
|
|
try {
|
|
|
|
final response = await _dio.get(
|
|
|
|
'/repos/$repoName/releases',
|
|
|
|
);
|
|
|
|
return response.data[0];
|
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-04 02:08:56 +05:45
|
|
|
Future<Map<String, dynamic>?> getPatchesRelease(
|
|
|
|
String repoName,
|
|
|
|
String version,
|
|
|
|
) async {
|
|
|
|
try {
|
|
|
|
final response = await _dio.get(
|
|
|
|
'/repos/$repoName/releases/tags/$version',
|
|
|
|
);
|
|
|
|
return response.data;
|
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<Map<String, dynamic>?> getLatestPatchesRelease(
|
|
|
|
String repoName,
|
|
|
|
) async {
|
|
|
|
try {
|
|
|
|
final response = await _dio.get(
|
|
|
|
'/repos/$repoName/releases/latest',
|
|
|
|
);
|
|
|
|
return response.data;
|
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-08 22:26:31 +05:45
|
|
|
Future<Map<String, dynamic>?> getLatestManagerRelease(
|
2023-07-10 19:36:50 +07:00
|
|
|
String repoName,
|
|
|
|
) async {
|
2022-08-09 01:16:33 +01:00
|
|
|
try {
|
2023-01-30 18:05:06 +05:30
|
|
|
final response = await _dio.get(
|
2022-12-10 01:10:43 +13:00
|
|
|
'/repos/$repoName/releases',
|
2022-08-09 01:16:33 +01:00
|
|
|
);
|
2023-07-01 05:26:03 +05:45
|
|
|
final Map<String, dynamic> releases = response.data[0];
|
|
|
|
int updates = 0;
|
2023-07-10 19:36:50 +07:00
|
|
|
final String currentVersion =
|
|
|
|
await ManagerAPI().getCurrentManagerVersion();
|
2023-07-01 05:26:03 +05:45
|
|
|
while (response.data[updates]['tag_name'] != 'v$currentVersion') {
|
|
|
|
updates++;
|
|
|
|
}
|
2023-07-10 19:36:50 +07:00
|
|
|
for (int i = 1; i < updates; i++) {
|
|
|
|
releases.update(
|
2023-07-10 19:45:50 +07:00
|
|
|
'body',
|
|
|
|
(value) =>
|
|
|
|
value +
|
|
|
|
'\n' +
|
|
|
|
'# ' +
|
|
|
|
response.data[i]['tag_name'] +
|
|
|
|
'\n' +
|
|
|
|
response.data[i]['body'],
|
|
|
|
);
|
2023-07-01 05:26:03 +05:45
|
|
|
}
|
|
|
|
return releases;
|
2023-03-05 14:42:46 +05:30
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-12 09:18:03 +01:00
|
|
|
return null;
|
2022-08-09 01:16:33 +01:00
|
|
|
}
|
2022-08-01 01:16:27 +05:30
|
|
|
}
|
2022-08-01 23:36:27 +05:30
|
|
|
|
2023-04-18 19:45:29 +05:30
|
|
|
Future<File?> getLatestReleaseFile(
|
|
|
|
String extension,
|
|
|
|
String repoName,
|
|
|
|
) async {
|
2022-09-11 02:01:06 +01:00
|
|
|
try {
|
2023-05-06 05:39:46 +05:30
|
|
|
final Map<String, dynamic>? release = await getLatestRelease(repoName);
|
2022-09-11 02:01:06 +01:00
|
|
|
if (release != null) {
|
2023-01-30 18:05:06 +05:30
|
|
|
final Map<String, dynamic>? asset =
|
2022-09-11 02:01:06 +01:00
|
|
|
(release['assets'] as List<dynamic>).firstWhereOrNull(
|
|
|
|
(asset) => (asset['name'] as String).endsWith(extension),
|
|
|
|
);
|
|
|
|
if (asset != null) {
|
|
|
|
return await DefaultCacheManager().getSingleFile(
|
|
|
|
asset['browser_download_url'],
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2023-03-05 14:42:46 +05:30
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-11 02:01:06 +01:00
|
|
|
}
|
|
|
|
return null;
|
2022-08-26 02:01:53 +01:00
|
|
|
}
|
|
|
|
|
2023-08-04 02:08:56 +05:45
|
|
|
Future<File?> getPatchesReleaseFile(
|
|
|
|
String extension,
|
|
|
|
String repoName,
|
|
|
|
String version,
|
2023-08-11 06:56:19 +05:45
|
|
|
String url,
|
2023-08-04 02:08:56 +05:45
|
|
|
) async {
|
2022-09-11 02:01:06 +01:00
|
|
|
try {
|
2023-08-11 06:56:19 +05:45
|
|
|
if (url.isNotEmpty) {
|
|
|
|
return await DefaultCacheManager().getSingleFile(
|
|
|
|
url,
|
|
|
|
);
|
|
|
|
}
|
2023-08-04 02:08:56 +05:45
|
|
|
final Map<String, dynamic>? release =
|
|
|
|
await getPatchesRelease(repoName, version);
|
|
|
|
if (release != null) {
|
|
|
|
final Map<String, dynamic>? asset =
|
|
|
|
(release['assets'] as List<dynamic>).firstWhereOrNull(
|
|
|
|
(asset) => (asset['name'] as String).endsWith(extension),
|
|
|
|
);
|
|
|
|
if (asset != null) {
|
2023-08-11 06:56:19 +05:45
|
|
|
final String downloadUrl = asset['browser_download_url'];
|
|
|
|
if (extension == '.apk') {
|
|
|
|
_managerAPI.setIntegrationsDownloadURL(downloadUrl);
|
|
|
|
} else {
|
2023-09-20 22:35:32 +00:00
|
|
|
_managerAPI.setPatchesDownloadURL(downloadUrl);
|
2023-08-11 06:56:19 +05:45
|
|
|
}
|
2023-08-04 02:08:56 +05:45
|
|
|
return await DefaultCacheManager().getSingleFile(
|
2023-08-11 06:56:19 +05:45
|
|
|
downloadUrl,
|
2023-08-04 02:08:56 +05:45
|
|
|
);
|
|
|
|
}
|
2022-09-11 02:01:06 +01:00
|
|
|
}
|
2023-03-05 14:42:46 +05:30
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-11 02:01:06 +01:00
|
|
|
}
|
2023-08-04 02:08:56 +05:45
|
|
|
return null;
|
2022-08-12 23:37:16 +05:30
|
|
|
}
|
2022-08-01 01:16:27 +05:30
|
|
|
}
|