revanced-manager/lib/services/github_api.dart

151 lines
4.3 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';
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';
import 'package:revanced_manager/models/patch.dart';
2022-10-14 20:05:33 +02:00
import 'package:sentry_dio/sentry_dio.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
@lazySingleton
class GithubAPI {
2022-10-11 16:49:50 +02:00
late Dio _dio = Dio();
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',
};
Future<void> initialize(String repoUrl) async {
2022-10-14 20:05:33 +02:00
try {
_dio = Dio(
BaseOptions(
baseUrl: repoUrl,
),
);
2022-10-11 16:49:50 +02:00
2022-10-14 20:05:33 +02:00
_dio.interceptors.add(_dioCacheManager.interceptor);
_dio.addSentry(
captureFailedRequests: true,
);
} on Exception catch (e, s) {
Sentry.captureException(e, stackTrace: s).ignore();
2022-10-11 16:49:50 +02:00
}
}
Future<void> clearAllCache() async {
2022-10-14 20:05:33 +02:00
try {
await _dioCacheManager.clearAll();
} on Exception catch (e, s) {
Sentry.captureException(e, stackTrace: s).ignore();
2022-10-14 20:05:33 +02:00
}
2022-08-18 18:32:58 +02:00
}
Future<Map<String, dynamic>?> getLatestRelease(String repoName) async {
try {
final response = await _dio.get(
2022-12-09 13:10:43 +01:00
'/repos/$repoName/releases',
options: _cacheOptions,
);
2022-12-09 13:10:43 +01:00
return response.data[0];
2022-10-14 20:05:33 +02:00
} on Exception catch (e, s) {
Sentry.captureException(e, stackTrace: s).ignore();
return null;
}
}
Future<List<String>> getCommits(
String packageName,
String repoName,
DateTime since,
) async {
final String path =
'src/main/kotlin/app/revanced/patches/${repoAppPath[packageName]}';
try {
final response = await _dio.get(
'/repos/$repoName/commits',
queryParameters: {
'path': path,
'since': since.toIso8601String(),
},
options: _cacheOptions,
);
final 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-14 20:05:33 +02:00
} on Exception catch (e, s) {
Sentry.captureException(e, stackTrace: s).ignore();
return List.empty();
}
}
2022-08-12 20:07:16 +02:00
Future<File?> getLatestReleaseFile(String extension, String repoName) async {
try {
final Map<String, dynamic>? release = await getLatestRelease(repoName);
if (release != null) {
final 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'],
);
}
}
2022-10-14 20:05:33 +02:00
} on Exception catch (e, s) {
Sentry.captureException(e, stackTrace: s).ignore();
return null;
}
return null;
}
Future<List<Patch>> getPatches(String repoName) async {
List<Patch> patches = [];
try {
final File? f = await getLatestReleaseFile('.json', repoName);
if (f != null) {
final List<dynamic> list = jsonDecode(f.readAsStringSync());
patches = list.map((patch) => Patch.fromJson(patch)).toList();
}
2022-10-14 20:05:33 +02:00
} on Exception catch (e, s) {
Sentry.captureException(e, stackTrace: s).ignore();
return List.empty();
}
return patches;
2022-08-12 20:07:16 +02:00
}
Future<String> getLastestReleaseVersion(String repoName) async {
try {
final Map<String, dynamic>? release = await getLatestRelease(repoName);
if (release != null) {
return release['tag_name'];
} else {
return 'Unknown';
}
} on Exception catch (e, s) {
Sentry.captureException(e, stackTrace: s).ignore();
return '';
}
}
}