revanced-manager/lib/services/github_api.dart

123 lines
3.5 KiB
Dart
Raw Normal View History

import 'dart:convert';
2022-08-18 16:33:33 +02:00
import 'dart:io';
import 'package:collection/collection.dart';
import 'package:dio/dio.dart';
2022-10-09 19:31:10 +02:00
import 'package:dio_brotli_transformer/dio_brotli_transformer.dart';
import 'package:dio_http_cache_lts/dio_http_cache_lts.dart';
2022-08-18 16:33:33 +02:00
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:injectable/injectable.dart';
2022-10-07 14:53:43 +02:00
import 'package:native_dio_client/native_dio_client.dart';
import 'package:revanced_manager/models/patch.dart';
@lazySingleton
class GithubAPI {
final Dio _dio = Dio(
2022-10-09 19:31:10 +02:00
BaseOptions(
baseUrl: 'https://api.github.com',
headers: {
'accept-encoding': 'br',
},
),
2022-10-07 14:53:43 +02:00
)..httpClientAdapter = NativeAdapter();
final DioCacheManager _dioCacheManager = DioCacheManager(CacheConfig());
final Options _cacheOptions = buildCacheOptions(
const Duration(hours: 6),
maxStale: const Duration(days: 1),
);
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',
'com.spotify.music': 'spotify',
};
void initialize() {
_dio.interceptors.add(_dioCacheManager.interceptor);
2022-10-09 19:31:10 +02:00
_dio.transformer = DioBrotliTransformer(transformer: DefaultTransformer());
}
Future<void> clearAllCache() async {
await _dioCacheManager.clearAll();
2022-08-18 18:32:58 +02:00
}
Future<Map<String, dynamic>?> _getLatestRelease(String repoName) async {
try {
var response = await _dio.get(
'/repos/$repoName/releases/latest',
options: _cacheOptions,
);
return response.data;
} on Exception {
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(
'/repos/$repoName/commits',
queryParameters: {
'path': path,
'since': since.toIso8601String(),
},
options: _cacheOptions,
);
List<dynamic> commits = response.data;
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,
)
.toList();
2022-10-08 18:36:45 +02:00
} catch (e) {
return List.empty();
}
}
2022-08-12 20:07:16 +02:00
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;
}
return null;
}
Future<List<Patch>> getPatches(String repoName) async {
List<Patch> patches = [];
try {
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 List.empty();
}
return patches;
2022-08-12 20:07:16 +02:00
}
}