revanced-manager/lib/services/github_api.dart

90 lines
2.5 KiB
Dart
Raw Normal View History

2022-08-18 16:33:33 +02:00
import 'dart:io';
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
import 'package:github/github.dart';
import 'package:timeago/timeago.dart';
class GithubAPI {
2022-08-18 16:33:33 +02:00
final GitHub _github = GitHub();
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',
};
Future<String?> latestReleaseVersion(String org, String repoName) async {
2022-08-18 18:32:58 +02:00
try {
var latestRelease = await _github.repositories.getLatestRelease(
RepositorySlug(org, repoName),
);
return latestRelease.tagName;
} on Exception {
return null;
}
}
2022-08-19 20:13:43 +02:00
Future<File?> latestReleaseFile(
String extension,
String org,
String repoName,
2022-08-19 20:13:43 +02:00
) async {
try {
2022-08-18 16:33:33 +02:00
var latestRelease = await _github.repositories.getLatestRelease(
RepositorySlug(org, repoName),
);
2022-08-18 16:33:33 +02:00
String? url = latestRelease.assets
?.firstWhere((asset) =>
asset.name != null &&
2022-08-19 20:13:43 +02:00
asset.name!.endsWith(extension) &&
!asset.name!.contains('-sources') &&
!asset.name!.contains('-javadoc'))
.browserDownloadUrl;
2022-08-18 16:33:33 +02:00
if (url != null) {
return await DefaultCacheManager().getSingleFile(url);
}
} on Exception {
2022-08-18 16:33:33 +02:00
return null;
}
2022-08-18 16:33:33 +02:00
return null;
}
Future<String> latestCommitTime(String org, String repoName) async {
try {
2022-08-18 16:33:33 +02:00
var repo = await _github.repositories.getRepository(
RepositorySlug(org, repoName),
);
2022-08-17 18:07:00 +02:00
return repo.pushedAt != null
? format(repo.pushedAt!, locale: 'en_short')
: '';
} on Exception {
2022-08-17 18:07:00 +02:00
return '';
}
}
2022-08-12 20:07:16 +02:00
Future<List<Contributor>> getContributors(String org, String repoName) async {
return await (_github.repositories.listContributors(
RepositorySlug(org, repoName),
)).toList();
}
Future<List<RepositoryCommit>> getCommits(
String packageName,
String org,
String repoName,
) async {
String path =
'src/main/kotlin/app/revanced/patches/${repoAppPath[packageName]}';
return await (PaginationHelper(_github)
.objects<Map<String, dynamic>, RepositoryCommit>(
'GET',
'/repos/$org/$repoName/commits',
(i) => RepositoryCommit.fromJson(i),
params: <String, dynamic>{'path': path},
)).toList();
2022-08-12 20:07:16 +02:00
}
}