2023-01-26 16:35:58 -05:00
|
|
|
import 'dart:core';
|
2023-01-29 17:18:41 -05:00
|
|
|
import 'dart:io';
|
2023-01-26 16:35:58 -05:00
|
|
|
import 'package:dio/dio.dart';
|
|
|
|
import 'package:funkblubber/funkentity.dart';
|
|
|
|
import 'package:funkblubber/console.dart' as console;
|
|
|
|
|
2023-01-29 17:18:41 -05:00
|
|
|
Future<void> download({
|
|
|
|
required final FunkObject object,
|
|
|
|
final String path = '.',
|
|
|
|
}) async {
|
2023-01-31 02:37:32 -05:00
|
|
|
console.info('Downloading');
|
2023-01-26 16:35:58 -05:00
|
|
|
switch (object.kind) {
|
|
|
|
case FunkEntity.album:
|
2023-01-29 17:18:41 -05:00
|
|
|
await _downloadAlbum(object, path);
|
2023-01-26 16:35:58 -05:00
|
|
|
return;
|
|
|
|
|
2023-01-29 17:46:11 -05:00
|
|
|
case FunkEntity.artist:
|
|
|
|
await _downloadArtist(object, path);
|
|
|
|
return;
|
|
|
|
|
2023-01-26 16:35:58 -05:00
|
|
|
default:
|
2023-01-31 02:37:32 -05:00
|
|
|
console.info(' nothing...');
|
2023-01-26 16:35:58 -05:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 17:46:11 -05:00
|
|
|
Future<void> _downloadArtist(
|
|
|
|
final FunkObject object,
|
|
|
|
final String path,
|
|
|
|
) async {
|
2023-01-29 17:55:35 -05:00
|
|
|
Response response = await Dio().get(
|
|
|
|
'https://${object.domain}/api/v1/artists/${object.id}/?'
|
|
|
|
'refresh=false',
|
|
|
|
);
|
|
|
|
|
|
|
|
final String pathAppend = response.data['name'];
|
|
|
|
await Directory('$path/$pathAppend').create();
|
2023-01-31 02:37:32 -05:00
|
|
|
console.info('Artist: $pathAppend');
|
2023-01-29 17:55:35 -05:00
|
|
|
|
|
|
|
response = await Dio().get(
|
2023-01-29 17:46:11 -05:00
|
|
|
'https://${object.domain}/api/v1/albums/?'
|
|
|
|
'artist=${object.id}&ordering=creation_date&'
|
|
|
|
'page=1&page_size=16&scope=all',
|
|
|
|
);
|
|
|
|
|
|
|
|
for (final albumResponse in response.data['results']) {
|
|
|
|
_downloadAlbum(
|
|
|
|
FunkObject(
|
|
|
|
domain: object.domain,
|
|
|
|
id: albumResponse['id'].toString(),
|
|
|
|
kind: FunkEntity.album,
|
|
|
|
),
|
2023-01-29 17:55:35 -05:00
|
|
|
'$path/$pathAppend',
|
2023-01-29 17:46:11 -05:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-29 17:18:41 -05:00
|
|
|
Future<void> _downloadAlbum(
|
|
|
|
final FunkObject object,
|
|
|
|
final String path,
|
|
|
|
) async {
|
2023-01-26 16:35:58 -05:00
|
|
|
final response = await Dio().get(
|
|
|
|
'https://${object.domain}/api/v1/tracks/?'
|
|
|
|
'album=${object.id}&ordering=creation_date&'
|
|
|
|
'page=1&page_size=16&scope=all',
|
|
|
|
);
|
|
|
|
|
2023-01-29 17:18:41 -05:00
|
|
|
final String pathAppend = response.data['results'][0]['album']['title'];
|
2023-01-29 17:55:35 -05:00
|
|
|
await Directory('$path/$pathAppend').create();
|
2023-01-31 02:37:32 -05:00
|
|
|
console.info('Album: $pathAppend');
|
2023-01-29 17:18:41 -05:00
|
|
|
|
|
|
|
for (final songResponse in response.data['results']) {
|
|
|
|
final String songTitle = songResponse['title'];
|
|
|
|
final String ext = songResponse['uploads'][0]['extension'];
|
|
|
|
_downloadSongObject(
|
|
|
|
FunkObject(
|
|
|
|
domain: object.domain,
|
|
|
|
id: songResponse['listen_url'],
|
|
|
|
kind: FunkEntity.song,
|
|
|
|
),
|
|
|
|
'$path/$pathAppend/$songTitle.$ext',
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Future<void> _downloadSongObject(
|
|
|
|
final FunkObject object,
|
|
|
|
final String path,
|
|
|
|
) async {
|
|
|
|
final Response response = await Dio().get(
|
|
|
|
'https://${object.domain}${object.id}',
|
|
|
|
options: Options(
|
|
|
|
responseType: ResponseType.bytes,
|
|
|
|
followRedirects: false,
|
|
|
|
),
|
|
|
|
);
|
2023-01-31 02:37:32 -05:00
|
|
|
console.info('- $path');
|
2023-01-29 17:18:41 -05:00
|
|
|
final File file = File(path);
|
|
|
|
final accessFile = file.openSync(mode: FileMode.write);
|
|
|
|
accessFile.writeFromSync(response.data);
|
|
|
|
await accessFile.close();
|
2023-01-26 16:35:58 -05:00
|
|
|
}
|