mirror of
https://github.com/ReVanced/revanced-manager.git
synced 2025-05-20 07:56:47 +08:00
feat: Add ReVanced API and implement cache on it and on Github API
This commit is contained in:
@ -1,11 +1,21 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
// ignore: depend_on_referenced_packages
|
||||
import 'package:collection/collection.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:dio_http_cache_lts/dio_http_cache_lts.dart';
|
||||
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
||||
import 'package:github/github.dart';
|
||||
import 'package:timeago/timeago.dart';
|
||||
import 'package:revanced_manager/models/patch.dart';
|
||||
|
||||
class GithubAPI {
|
||||
final GitHub _github = GitHub();
|
||||
|
||||
final String apiUrl = 'https://api.github.com';
|
||||
final Dio _dio = Dio();
|
||||
final DioCacheManager _dioCacheManager = DioCacheManager(
|
||||
CacheConfig(
|
||||
defaultMaxAge: const Duration(hours: 1),
|
||||
defaultMaxStale: const Duration(days: 7),
|
||||
),
|
||||
);
|
||||
final Map<String, String> repoAppPath = {
|
||||
'com.google.android.youtube': 'youtube',
|
||||
'com.google.android.apps.youtube.music': 'music',
|
||||
@ -16,31 +26,78 @@ class GithubAPI {
|
||||
'com.garzotto.pflotsh.ecmwf_a': 'ecmwf',
|
||||
};
|
||||
|
||||
Future<String?> latestReleaseVersion(String repoName) async {
|
||||
try {
|
||||
var latestRelease = await _github.repositories.getLatestRelease(
|
||||
RepositorySlug.full(repoName),
|
||||
);
|
||||
return latestRelease.tagName;
|
||||
} on Exception {
|
||||
return null;
|
||||
}
|
||||
void initialize() {
|
||||
_dio.interceptors.add(_dioCacheManager.interceptor);
|
||||
}
|
||||
|
||||
Future<File?> latestReleaseFile(String extension, String repoName) async {
|
||||
Future<void> clearAllCache() async {
|
||||
await _dioCacheManager.clearAll();
|
||||
}
|
||||
|
||||
Future<Map<String, dynamic>?> _getLatestRelease(String repoName) async {
|
||||
try {
|
||||
var latestRelease = await _github.repositories.getLatestRelease(
|
||||
RepositorySlug.full(repoName),
|
||||
var response = await _dio.get(
|
||||
'$apiUrl/repos/$repoName/releases/latest',
|
||||
options: buildCacheOptions(const Duration(hours: 1)),
|
||||
);
|
||||
String? url = latestRelease.assets
|
||||
?.firstWhere((asset) =>
|
||||
asset.name != null &&
|
||||
asset.name!.endsWith(extension) &&
|
||||
!asset.name!.contains('-sources') &&
|
||||
!asset.name!.contains('-javadoc'))
|
||||
.browserDownloadUrl;
|
||||
if (url != null) {
|
||||
return await DefaultCacheManager().getSingleFile(url);
|
||||
if (response.headers.value(DIO_CACHE_HEADER_KEY_DATA_SOURCE) != null) {
|
||||
print('1 - From cache');
|
||||
} else {
|
||||
print('1 - From net');
|
||||
}
|
||||
return response.data;
|
||||
} on Exception {
|
||||
// ignore
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<List<String>> getCommits(
|
||||
String packageName,
|
||||
String repoName,
|
||||
DateTime since,
|
||||
) async {
|
||||
String path =
|
||||
'src/main/kotlin/app/revanced/patches/${repoAppPath[packageName]}';
|
||||
try {
|
||||
var response = await _dio.get(
|
||||
'$apiUrl/repos/$repoName/commits',
|
||||
queryParameters: {
|
||||
'path': path,
|
||||
'per_page': 3,
|
||||
'since': since.toIso8601String(),
|
||||
},
|
||||
options: buildCacheOptions(const Duration(hours: 1)),
|
||||
);
|
||||
if (response.headers.value(DIO_CACHE_HEADER_KEY_DATA_SOURCE) != null) {
|
||||
print('2 - From cache');
|
||||
} else {
|
||||
print('2 - From net');
|
||||
}
|
||||
List<dynamic> commits = response.data;
|
||||
return commits
|
||||
.map((commit) =>
|
||||
(commit['commit']['message'] as String).split('\n')[0])
|
||||
.toList();
|
||||
} on Exception {
|
||||
// ignore
|
||||
}
|
||||
return List.empty();
|
||||
}
|
||||
|
||||
Future<File?> getLatestReleaseFile(String extension, String repoName) async {
|
||||
try {
|
||||
Map<String, dynamic>? release = await _getLatestRelease(repoName);
|
||||
if (release != null) {
|
||||
Map<String, dynamic>? asset =
|
||||
(release['assets'] as List<dynamic>).firstWhereOrNull(
|
||||
(asset) => (asset['name'] as String).endsWith(extension),
|
||||
);
|
||||
if (asset != null) {
|
||||
return await DefaultCacheManager().getSingleFile(
|
||||
asset['browser_download_url'],
|
||||
);
|
||||
}
|
||||
}
|
||||
} on Exception {
|
||||
return null;
|
||||
@ -48,37 +105,17 @@ class GithubAPI {
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<String> latestCommitTime(String repoName) async {
|
||||
Future<List<Patch>> getPatches(String repoName) async {
|
||||
List<Patch> patches = [];
|
||||
try {
|
||||
var repo = await _github.repositories.getRepository(
|
||||
RepositorySlug.full(repoName),
|
||||
);
|
||||
return repo.pushedAt != null
|
||||
? format(repo.pushedAt!, locale: 'en_short')
|
||||
: '';
|
||||
File? f = await getLatestReleaseFile('.json', repoName);
|
||||
if (f != null) {
|
||||
List<dynamic> list = jsonDecode(f.readAsStringSync());
|
||||
patches = list.map((patch) => Patch.fromJson(patch)).toList();
|
||||
}
|
||||
} on Exception {
|
||||
return '';
|
||||
// ignore
|
||||
}
|
||||
}
|
||||
|
||||
Future<List<Contributor>> getContributors(String repoName) async {
|
||||
return await (_github.repositories.listContributors(
|
||||
RepositorySlug.full(repoName),
|
||||
)).toList();
|
||||
}
|
||||
|
||||
Future<List<RepositoryCommit>> getCommits(
|
||||
String packageName,
|
||||
String repoName,
|
||||
) async {
|
||||
String path =
|
||||
'src/main/kotlin/app/revanced/patches/${repoAppPath[packageName]}';
|
||||
return await (PaginationHelper(_github)
|
||||
.objects<Map<String, dynamic>, RepositoryCommit>(
|
||||
'GET',
|
||||
'/repos/$repoName/commits',
|
||||
(i) => RepositoryCommit.fromJson(i),
|
||||
params: <String, dynamic>{'path': path},
|
||||
)).toList();
|
||||
return patches;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user