2022-09-11 03:01:06 +02:00
|
|
|
import 'dart:convert';
|
2022-08-18 16:33:33 +02:00
|
|
|
import 'dart:io';
|
2023-01-30 13:35:06 +01:00
|
|
|
|
2022-09-11 03:01:06 +02:00
|
|
|
import 'package:collection/collection.dart';
|
|
|
|
import 'package:dio/dio.dart';
|
2023-04-18 09:57:26 +02:00
|
|
|
import 'package:dio_cache_interceptor/dio_cache_interceptor.dart';
|
2023-03-05 10:12:46 +01:00
|
|
|
import 'package:flutter/foundation.dart';
|
2022-08-18 16:33:33 +02:00
|
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
2022-09-12 02:41:53 +02:00
|
|
|
import 'package:injectable/injectable.dart';
|
2023-04-18 09:57:26 +02:00
|
|
|
import 'package:native_dio_adapter/native_dio_adapter.dart';
|
2022-09-11 03:01:06 +02:00
|
|
|
import 'package:revanced_manager/models/patch.dart';
|
2022-07-31 21:46:27 +02:00
|
|
|
|
2022-09-12 02:41:53 +02:00
|
|
|
@lazySingleton
|
2022-07-31 21:46:27 +02:00
|
|
|
class GithubAPI {
|
2022-10-11 16:49:50 +02:00
|
|
|
late Dio _dio = Dio();
|
2023-04-18 16:15:29 +02:00
|
|
|
|
2023-04-18 09:57:26 +02:00
|
|
|
final _cacheOptions = CacheOptions(
|
|
|
|
store: MemCacheStore(),
|
2022-09-28 18:56:54 +02:00
|
|
|
maxStale: const Duration(days: 1),
|
2023-04-18 09:57:26 +02:00
|
|
|
priority: CachePriority.high,
|
2022-09-11 03:01:06 +02:00
|
|
|
);
|
2023-04-18 09:57:26 +02:00
|
|
|
|
2022-08-30 03:07:28 +02: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 15:27:32 +02:00
|
|
|
'com.spotify.music': 'spotify',
|
2022-08-30 03:07:28 +02:00
|
|
|
};
|
|
|
|
|
2023-01-30 13:35:06 +01:00
|
|
|
Future<void> initialize(String repoUrl) async {
|
2022-10-14 20:05:33 +02:00
|
|
|
try {
|
2023-04-18 09:57:26 +02:00
|
|
|
if (Platform.isIOS || Platform.isMacOS || Platform.isAndroid) {
|
|
|
|
final CronetEngine androidCronetEngine = await CronetEngine.build(
|
|
|
|
userAgent: 'ReVanced Manager',
|
|
|
|
enableBrotli: true,
|
|
|
|
enableQuic: true,
|
|
|
|
);
|
|
|
|
_dio.httpClientAdapter =
|
|
|
|
NativeAdapter(androidCronetEngine: androidCronetEngine);
|
|
|
|
|
|
|
|
_dio = Dio(
|
|
|
|
BaseOptions(
|
|
|
|
baseUrl: repoUrl,
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-01-30 13:35:06 +01:00
|
|
|
_dio = Dio(
|
|
|
|
BaseOptions(
|
|
|
|
baseUrl: repoUrl,
|
|
|
|
),
|
|
|
|
);
|
2022-10-11 16:49:50 +02:00
|
|
|
|
2023-04-18 09:57:26 +02:00
|
|
|
_dio.interceptors.add(DioCacheInterceptor(options: _cacheOptions));
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-10-11 16:49:50 +02:00
|
|
|
}
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> clearAllCache() async {
|
2022-10-14 20:05:33 +02:00
|
|
|
try {
|
2023-04-18 09:57:26 +02:00
|
|
|
await _cacheOptions.store!.clean();
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-10-14 20:05:33 +02:00
|
|
|
}
|
2022-08-18 18:32:58 +02:00
|
|
|
}
|
|
|
|
|
2023-04-18 16:15:29 +02:00
|
|
|
Future<Map<String, dynamic>?> getLatestRelease(
|
|
|
|
String repoName,
|
|
|
|
) async {
|
2022-08-09 02:16:33 +02:00
|
|
|
try {
|
2023-01-30 13:35:06 +01:00
|
|
|
final response = await _dio.get(
|
2022-12-09 13:10:43 +01:00
|
|
|
'/repos/$repoName/releases',
|
2022-08-09 02:16:33 +02:00
|
|
|
);
|
2022-12-09 13:10:43 +01:00
|
|
|
return response.data[0];
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-12 10:18:03 +02:00
|
|
|
return null;
|
2022-08-09 02:16:33 +02:00
|
|
|
}
|
2022-07-31 21:46:27 +02:00
|
|
|
}
|
2022-08-01 20:06:27 +02:00
|
|
|
|
2022-09-11 03:01:06 +02:00
|
|
|
Future<List<String>> getCommits(
|
|
|
|
String packageName,
|
|
|
|
String repoName,
|
|
|
|
DateTime since,
|
|
|
|
) async {
|
2023-01-30 13:35:06 +01:00
|
|
|
final String path =
|
2022-09-11 03:01:06 +02:00
|
|
|
'src/main/kotlin/app/revanced/patches/${repoAppPath[packageName]}';
|
2022-08-09 02:16:33 +02:00
|
|
|
try {
|
2023-01-30 13:35:06 +01:00
|
|
|
final response = await _dio.get(
|
2022-09-19 01:28:26 +02:00
|
|
|
'/repos/$repoName/commits',
|
2022-09-11 03:01:06 +02:00
|
|
|
queryParameters: {
|
|
|
|
'path': path,
|
|
|
|
'since': since.toIso8601String(),
|
|
|
|
},
|
2022-08-09 02:16:33 +02:00
|
|
|
);
|
2023-01-30 13:35:06 +01:00
|
|
|
final List<dynamic> commits = response.data;
|
2022-09-11 03:01:06 +02:00
|
|
|
return commits
|
2022-10-08 18:36:45 +02:00
|
|
|
.map(
|
|
|
|
(commit) => (commit['commit']['message']).split('\n')[0] +
|
|
|
|
' - ' +
|
|
|
|
commit['commit']['author']['name'] +
|
|
|
|
'\n' as String,
|
|
|
|
)
|
2022-09-11 03:01:06 +02:00
|
|
|
.toList();
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-08-09 02:16:33 +02:00
|
|
|
}
|
2023-03-05 10:12:46 +01:00
|
|
|
return [];
|
2022-08-01 20:12:38 +02:00
|
|
|
}
|
2022-08-12 20:07:16 +02:00
|
|
|
|
2023-04-18 16:15:29 +02:00
|
|
|
Future<File?> getLatestReleaseFile(
|
|
|
|
String extension,
|
|
|
|
String repoName,
|
|
|
|
) async {
|
2022-09-11 03:01:06 +02:00
|
|
|
try {
|
2023-04-18 16:15:29 +02:00
|
|
|
final Map<String, dynamic>? release =
|
|
|
|
await getLatestRelease(repoName);
|
2022-09-11 03:01:06 +02:00
|
|
|
if (release != null) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final Map<String, dynamic>? asset =
|
2022-09-11 03:01:06 +02: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 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
|
|
|
return null;
|
2022-08-26 03:01:53 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 03:01:06 +02:00
|
|
|
Future<List<Patch>> getPatches(String repoName) async {
|
|
|
|
List<Patch> patches = [];
|
|
|
|
try {
|
2023-01-30 13:35:06 +01:00
|
|
|
final File? f = await getLatestReleaseFile('.json', repoName);
|
2022-09-11 03:01:06 +02:00
|
|
|
if (f != null) {
|
2023-01-30 13:35:06 +01:00
|
|
|
final List<dynamic> list = jsonDecode(f.readAsStringSync());
|
2022-09-11 03:01:06 +02:00
|
|
|
patches = list.map((patch) => Patch.fromJson(patch)).toList();
|
|
|
|
}
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
2022-09-11 03:01:06 +02:00
|
|
|
}
|
2023-03-05 10:12:46 +01:00
|
|
|
|
2022-09-11 03:01:06 +02:00
|
|
|
return patches;
|
2022-08-12 20:07:16 +02:00
|
|
|
}
|
2022-11-23 05:33:56 +01:00
|
|
|
|
|
|
|
Future<String> getLastestReleaseVersion(String repoName) async {
|
|
|
|
try {
|
2023-04-18 16:15:29 +02:00
|
|
|
final Map<String, dynamic>? release =
|
|
|
|
await getLatestRelease(repoName);
|
2022-11-23 05:33:56 +01:00
|
|
|
if (release != null) {
|
|
|
|
return release['tag_name'];
|
|
|
|
} else {
|
|
|
|
return 'Unknown';
|
|
|
|
}
|
2023-03-05 10:12:46 +01:00
|
|
|
} on Exception catch (e) {
|
|
|
|
if (kDebugMode) {
|
|
|
|
print(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 'Unknown';
|
2022-11-23 05:33:56 +01:00
|
|
|
}
|
|
|
|
}
|
2022-07-31 21:46:27 +02:00
|
|
|
}
|