feat: Implement basic error handling
This commit is contained in:
parent
ebd0e6836d
commit
578e3709f8
|
@ -11,8 +11,12 @@ void main(final List<String> arguments) async {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
await funkblubber.download(
|
final bool success = await funkblubber.download(
|
||||||
object: result.object!,
|
object: result.object!,
|
||||||
path: result.localPath ?? '.',
|
path: result.localPath ?? '.',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!success) {
|
||||||
|
console.error("couldn't execute request successfully.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,27 +4,30 @@ import 'package:dio/dio.dart';
|
||||||
import 'package:funkblubber/funkentity.dart';
|
import 'package:funkblubber/funkentity.dart';
|
||||||
import 'package:funkblubber/console.dart' as console;
|
import 'package:funkblubber/console.dart' as console;
|
||||||
|
|
||||||
Future<void> download({
|
Future<bool> download({
|
||||||
required final FunkObject object,
|
required final FunkObject object,
|
||||||
required final String path,
|
required final String path,
|
||||||
}) async {
|
}) async {
|
||||||
|
bool success = false;
|
||||||
console.info('Downloading');
|
console.info('Downloading');
|
||||||
switch (object.kind) {
|
switch (object.kind) {
|
||||||
case FunkEntity.album:
|
case FunkEntity.album:
|
||||||
await _downloadAlbum(object, path);
|
success = await _downloadAlbum(object, path);
|
||||||
return;
|
break;
|
||||||
|
|
||||||
case FunkEntity.artist:
|
case FunkEntity.artist:
|
||||||
await _downloadArtist(object, path);
|
success = await _downloadArtist(object, path);
|
||||||
return;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
console.info(' nothing...');
|
console.info(' nothing...');
|
||||||
return;
|
break;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _downloadArtist(
|
return success;
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> _downloadArtist(
|
||||||
final FunkObject object,
|
final FunkObject object,
|
||||||
final String path,
|
final String path,
|
||||||
) async {
|
) async {
|
||||||
|
@ -34,7 +37,16 @@ Future<void> _downloadArtist(
|
||||||
);
|
);
|
||||||
|
|
||||||
final String pathAppend = response.data['name'];
|
final String pathAppend = response.data['name'];
|
||||||
|
bool folderCreated = true;
|
||||||
|
try {
|
||||||
await Directory('$path/$pathAppend').create();
|
await Directory('$path/$pathAppend').create();
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e.toString());
|
||||||
|
folderCreated = false;
|
||||||
|
}
|
||||||
|
if (!folderCreated) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
console.info('Artist: $pathAppend');
|
console.info('Artist: $pathAppend');
|
||||||
|
|
||||||
response = await Dio().get(
|
response = await Dio().get(
|
||||||
|
@ -43,8 +55,9 @@ Future<void> _downloadArtist(
|
||||||
'page=1&page_size=16&scope=all',
|
'page=1&page_size=16&scope=all',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
final List<Future<bool>> results = [];
|
||||||
for (final albumResponse in response.data['results']) {
|
for (final albumResponse in response.data['results']) {
|
||||||
_downloadAlbum(
|
final result = _downloadAlbum(
|
||||||
FunkObject(
|
FunkObject(
|
||||||
domain: object.domain,
|
domain: object.domain,
|
||||||
id: albumResponse['id'].toString(),
|
id: albumResponse['id'].toString(),
|
||||||
|
@ -52,10 +65,15 @@ Future<void> _downloadArtist(
|
||||||
),
|
),
|
||||||
'$path/$pathAppend',
|
'$path/$pathAppend',
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
results.add(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _downloadAlbum(
|
final List<bool> successes = await Future.wait(results);
|
||||||
|
return successes.every((final success) => success);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> _downloadAlbum(
|
||||||
final FunkObject object,
|
final FunkObject object,
|
||||||
final String path,
|
final String path,
|
||||||
) async {
|
) async {
|
||||||
|
@ -66,13 +84,23 @@ Future<void> _downloadAlbum(
|
||||||
);
|
);
|
||||||
|
|
||||||
final String pathAppend = response.data['results'][0]['album']['title'];
|
final String pathAppend = response.data['results'][0]['album']['title'];
|
||||||
|
bool folderCreated = true;
|
||||||
|
try {
|
||||||
await Directory('$path/$pathAppend').create();
|
await Directory('$path/$pathAppend').create();
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e.toString());
|
||||||
|
folderCreated = false;
|
||||||
|
}
|
||||||
|
if (!folderCreated) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
console.info('Album: $pathAppend');
|
console.info('Album: $pathAppend');
|
||||||
|
|
||||||
|
final List<Future<bool>> results = [];
|
||||||
for (final songResponse in response.data['results']) {
|
for (final songResponse in response.data['results']) {
|
||||||
final String songTitle = songResponse['title'];
|
final String songTitle = songResponse['title'];
|
||||||
final String ext = songResponse['uploads'][0]['extension'];
|
final String ext = songResponse['uploads'][0]['extension'];
|
||||||
_downloadSongObject(
|
final result = _downloadSongObject(
|
||||||
FunkObject(
|
FunkObject(
|
||||||
domain: object.domain,
|
domain: object.domain,
|
||||||
id: songResponse['listen_url'],
|
id: songResponse['listen_url'],
|
||||||
|
@ -80,13 +108,20 @@ Future<void> _downloadAlbum(
|
||||||
),
|
),
|
||||||
'$path/$pathAppend/$songTitle.$ext',
|
'$path/$pathAppend/$songTitle.$ext',
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
results.add(result);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> _downloadSongObject(
|
final List<bool> successes = await Future.wait(results);
|
||||||
|
return successes.every((final success) => success);
|
||||||
|
}
|
||||||
|
|
||||||
|
Future<bool> _downloadSongObject(
|
||||||
final FunkObject object,
|
final FunkObject object,
|
||||||
final String path,
|
final String path,
|
||||||
) async {
|
) async {
|
||||||
|
bool success = true;
|
||||||
|
try {
|
||||||
final Response response = await Dio().get(
|
final Response response = await Dio().get(
|
||||||
'https://${object.domain}${object.id}',
|
'https://${object.domain}${object.id}',
|
||||||
options: Options(
|
options: Options(
|
||||||
|
@ -99,4 +134,10 @@ Future<void> _downloadSongObject(
|
||||||
final accessFile = file.openSync(mode: FileMode.write);
|
final accessFile = file.openSync(mode: FileMode.write);
|
||||||
accessFile.writeFromSync(response.data);
|
accessFile.writeFromSync(response.data);
|
||||||
await accessFile.close();
|
await accessFile.close();
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e.toString());
|
||||||
|
success = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return success;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue