Added descriptions to GitHub starred imports

This commit is contained in:
Imran Remtulla
2022-11-12 10:40:54 -05:00
parent 18951d6461
commit 3100a3a08c
3 changed files with 22 additions and 16 deletions

View File

@@ -12,14 +12,20 @@ class GitHubStars implements MassAppUrlSource {
@override @override
late List<String> requiredArgs = ['Username']; late List<String> requiredArgs = ['Username'];
Future<List<String>> getOnePageOfUserStarredUrls( Future<Map<String, String>> getOnePageOfUserStarredUrlsWithDescriptions(
String username, int page) async { String username, int page) async {
Response res = await get(Uri.parse( Response res = await get(Uri.parse(
'https://${await GitHub().getCredentialPrefixIfAny()}api.github.com/users/$username/starred?per_page=100&page=$page')); 'https://${await GitHub().getCredentialPrefixIfAny()}api.github.com/users/$username/starred?per_page=100&page=$page'));
if (res.statusCode == 200) { if (res.statusCode == 200) {
return (jsonDecode(res.body) as List<dynamic>) Map<String, String> urlsWithDescriptions = {};
.map((e) => e['html_url'] as String) for (var e in (jsonDecode(res.body) as List<dynamic>)) {
.toList(); urlsWithDescriptions.addAll({
e['html_url'] as String: e['description'] != null
? e['description'] as String
: 'No description'
});
}
return urlsWithDescriptions;
} else { } else {
if (res.headers['x-ratelimit-remaining'] == '0') { if (res.headers['x-ratelimit-remaining'] == '0') {
throw RateLimitError( throw RateLimitError(
@@ -33,19 +39,20 @@ class GitHubStars implements MassAppUrlSource {
} }
@override @override
Future<List<String>> getUrls(List<String> args) async { Future<Map<String, String>> getUrlsWithDescriptions(List<String> args) async {
if (args.length != requiredArgs.length) { if (args.length != requiredArgs.length) {
throw ObtainiumError('Wrong number of arguments provided'); throw ObtainiumError('Wrong number of arguments provided');
} }
List<String> urls = []; Map<String, String> urlsWithDescriptions = {};
var page = 1; var page = 1;
while (true) { while (true) {
var pageUrls = await getOnePageOfUserStarredUrls(args[0], page++); var pageUrls =
urls.addAll(pageUrls); await getOnePageOfUserStarredUrlsWithDescriptions(args[0], page++);
urlsWithDescriptions.addAll(pageUrls);
if (pageUrls.length < 100) { if (pageUrls.length < 100) {
break; break;
} }
} }
return urls; return urlsWithDescriptions;
} }
} }

View File

@@ -357,8 +357,10 @@ class _ImportExportPageState extends State<ImportExportPage> {
setState(() { setState(() {
importInProgress = true; importInProgress = true;
}); });
var urls = await source var urlsWithDescriptions =
.getUrls(values); await source
.getUrlsWithDescriptions(
values);
var selectedUrls = var selectedUrls =
await showDialog< await showDialog<
List<String>?>( List<String>?>(
@@ -368,10 +370,7 @@ class _ImportExportPageState extends State<ImportExportPage> {
ctx) { ctx) {
return UrlSelectionModal( return UrlSelectionModal(
urlsWithDescriptions: urlsWithDescriptions:
Map.fromIterable( urlsWithDescriptions);
urls,
value: (item) =>
''));
}); });
if (selectedUrls != null) { if (selectedUrls != null) {
var errors = var errors =

View File

@@ -167,7 +167,7 @@ class AppSource {
abstract class MassAppUrlSource { abstract class MassAppUrlSource {
late String name; late String name;
late List<String> requiredArgs; late List<String> requiredArgs;
Future<List<String>> getUrls(List<String> args); Future<Map<String, String>> getUrlsWithDescriptions(List<String> args);
} }
class SourceProvider { class SourceProvider {