feat: Implement basic error handling

This commit is contained in:
NaiJi ✨ 2023-02-02 02:18:06 +04:00
parent ebd0e6836d
commit 578e3709f8
2 changed files with 71 additions and 26 deletions

View File

@ -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.");
}
} }

View File

@ -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;
} }
return success;
} }
Future<void> _downloadArtist( 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'];
await Directory('$path/$pathAppend').create(); bool folderCreated = true;
try {
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);
} }
final List<bool> successes = await Future.wait(results);
return successes.every((final success) => success);
} }
Future<void> _downloadAlbum( 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'];
await Directory('$path/$pathAppend').create(); bool folderCreated = true;
try {
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,23 +108,36 @@ Future<void> _downloadAlbum(
), ),
'$path/$pathAppend/$songTitle.$ext', '$path/$pathAppend/$songTitle.$ext',
); );
results.add(result);
} }
final List<bool> successes = await Future.wait(results);
return successes.every((final success) => success);
} }
Future<void> _downloadSongObject( Future<bool> _downloadSongObject(
final FunkObject object, final FunkObject object,
final String path, final String path,
) async { ) async {
final Response response = await Dio().get( bool success = true;
'https://${object.domain}${object.id}', try {
options: Options( final Response response = await Dio().get(
responseType: ResponseType.bytes, 'https://${object.domain}${object.id}',
followRedirects: false, options: Options(
), responseType: ResponseType.bytes,
); followRedirects: false,
console.info('- $path'); ),
final File file = File(path); );
final accessFile = file.openSync(mode: FileMode.write); console.info('- $path');
accessFile.writeFromSync(response.data); final File file = File(path);
await accessFile.close(); final accessFile = file.openSync(mode: FileMode.write);
accessFile.writeFromSync(response.data);
await accessFile.close();
} catch (e) {
console.error(e.toString());
success = false;
}
return success;
} }