funkblubber/lib/funkblubber.dart

64 lines
1.6 KiB
Dart
Raw Normal View History

2023-01-26 22:35:58 +01:00
import 'dart:core';
import 'dart:io';
2023-01-26 22:35:58 +01:00
import 'package:dio/dio.dart';
import 'package:funkblubber/funkentity.dart';
import 'package:funkblubber/console.dart' as console;
Future<void> download({
required final FunkObject object,
final String path = '.',
}) async {
2023-01-26 22:35:58 +01:00
switch (object.kind) {
case FunkEntity.album:
await _downloadAlbum(object, path);
2023-01-26 22:35:58 +01:00
return;
default:
return;
}
}
Future<void> _downloadAlbum(
final FunkObject object,
final String path,
) async {
2023-01-26 22:35:58 +01: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',
);
final String pathAppend = response.data['results'][0]['album']['title'];
await Directory(pathAppend).create();
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,
),
);
final File file = File(path);
final accessFile = file.openSync(mode: FileMode.write);
accessFile.writeFromSync(response.data);
await accessFile.close();
2023-01-26 22:35:58 +01:00
}