feat: Implement single tracks downloading
This commit is contained in:
parent
578e3709f8
commit
963fd4bc1a
|
@ -19,6 +19,10 @@ Future<bool> download({
|
|||
success = await _downloadArtist(object, path);
|
||||
break;
|
||||
|
||||
case FunkEntity.track:
|
||||
success = await _downloadTrack(object, path);
|
||||
break;
|
||||
|
||||
default:
|
||||
console.info(' nothing...');
|
||||
break;
|
||||
|
@ -100,11 +104,11 @@ Future<bool> _downloadAlbum(
|
|||
for (final songResponse in response.data['results']) {
|
||||
final String songTitle = songResponse['title'];
|
||||
final String ext = songResponse['uploads'][0]['extension'];
|
||||
final result = _downloadSongObject(
|
||||
final result = _downloadTrackObject(
|
||||
FunkObject(
|
||||
domain: object.domain,
|
||||
id: songResponse['listen_url'],
|
||||
kind: FunkEntity.song,
|
||||
kind: FunkEntity.track,
|
||||
),
|
||||
'$path/$pathAppend/$songTitle.$ext',
|
||||
);
|
||||
|
@ -116,7 +120,27 @@ Future<bool> _downloadAlbum(
|
|||
return successes.every((final success) => success);
|
||||
}
|
||||
|
||||
Future<bool> _downloadSongObject(
|
||||
Future<bool> _downloadTrack(
|
||||
final FunkObject object,
|
||||
final String path,
|
||||
) async {
|
||||
final response = await Dio().get(
|
||||
'https://${object.domain}/api/v1/tracks/${object.id}/?refresh=false',
|
||||
);
|
||||
|
||||
final String songTitle = response.data['title'];
|
||||
final String ext = response.data['uploads'][0]['extension'];
|
||||
return _downloadTrackObject(
|
||||
FunkObject(
|
||||
domain: object.domain,
|
||||
id: response.data['uploads'][0]['listen_url'],
|
||||
kind: FunkEntity.track,
|
||||
),
|
||||
'$path/$songTitle.$ext',
|
||||
);
|
||||
}
|
||||
|
||||
Future<bool> _downloadTrackObject(
|
||||
final FunkObject object,
|
||||
final String path,
|
||||
) async {
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
enum FunkEntity {
|
||||
error,
|
||||
|
||||
song,
|
||||
track,
|
||||
album,
|
||||
artist,
|
||||
profile,
|
||||
|
|
|
@ -1,51 +0,0 @@
|
|||
import 'package:funkblubber/funkentity.dart';
|
||||
import 'package:funkblubber/console.dart' as console;
|
||||
|
||||
FunkObject extract(final List<String> args) {
|
||||
FunkObject result = FunkObject(
|
||||
kind: FunkEntity.error,
|
||||
id: '',
|
||||
domain: '',
|
||||
);
|
||||
|
||||
if (args.isEmpty) {
|
||||
console.error('no arguments provided');
|
||||
return result;
|
||||
}
|
||||
|
||||
try {
|
||||
final Uri uri = Uri.parse(args[0]);
|
||||
|
||||
final segments = uri.pathSegments;
|
||||
for (int i = 0; i < segments.length; ++i) {
|
||||
switch (segments[i]) {
|
||||
case 'artists':
|
||||
result = FunkObject(
|
||||
kind: FunkEntity.artist,
|
||||
id: segments[i + 1],
|
||||
domain: uri.host,
|
||||
);
|
||||
++i;
|
||||
break;
|
||||
|
||||
case 'albums':
|
||||
result = FunkObject(
|
||||
kind: FunkEntity.album,
|
||||
id: segments[i + 1],
|
||||
domain: uri.host,
|
||||
);
|
||||
++i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.error(e.toString());
|
||||
result = FunkObject(
|
||||
kind: FunkEntity.error,
|
||||
id: '',
|
||||
domain: '',
|
||||
);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
|
@ -70,6 +70,17 @@ ParseResult extract(final List<String> args) {
|
|||
result = stageResult.result;
|
||||
break;
|
||||
|
||||
case ParsingStage.track:
|
||||
final stageResult = _onEntityStage(
|
||||
arg,
|
||||
result,
|
||||
currentStage,
|
||||
FunkEntity.track,
|
||||
);
|
||||
currentStage = stageResult.stage;
|
||||
result = stageResult.result;
|
||||
break;
|
||||
|
||||
case ParsingStage.domain:
|
||||
final stageResult = _onDomainStage(arg, result, currentStage);
|
||||
currentStage = stageResult.stage;
|
||||
|
@ -102,6 +113,8 @@ usage: funkblubber [[-OPTIONS|URL]...]
|
|||
|
||||
[-A|--artist ID] Provide with artist ID to download their albums.
|
||||
|
||||
[-t|--track ID] Provide with track ID to download a single track entity.
|
||||
|
||||
[-p|--path PATH] Provide to explicitly define download directory.
|
||||
Assumed '.'.
|
||||
|
||||
|
@ -219,6 +232,10 @@ StageResult _onNothingStage(
|
|||
case '--album':
|
||||
currentStage = ParsingStage.album;
|
||||
break;
|
||||
case '-t':
|
||||
case '--track':
|
||||
currentStage = ParsingStage.track;
|
||||
break;
|
||||
case '-u':
|
||||
case '--upload':
|
||||
currentStage = ParsingStage.upload;
|
||||
|
@ -256,6 +273,19 @@ StageResult _onNothingStage(
|
|||
++i;
|
||||
break;
|
||||
|
||||
case 'tracks':
|
||||
result = ParseResult(
|
||||
action: Action.download,
|
||||
success: true,
|
||||
object: FunkObject(
|
||||
kind: FunkEntity.track,
|
||||
id: segments[i + 1],
|
||||
domain: uri.host,
|
||||
),
|
||||
);
|
||||
++i;
|
||||
break;
|
||||
|
||||
case 'albums':
|
||||
result = ParseResult(
|
||||
action: Action.download,
|
||||
|
|
|
@ -2,7 +2,7 @@ enum ParsingStage {
|
|||
nothing,
|
||||
album,
|
||||
artist,
|
||||
song,
|
||||
track,
|
||||
path,
|
||||
upload,
|
||||
domain,
|
||||
|
|
Loading…
Reference in New Issue