feat: Implement single tracks downloading

This commit is contained in:
NaiJi ✨ 2023-02-04 10:24:05 +04:00
parent 578e3709f8
commit 963fd4bc1a
5 changed files with 59 additions and 58 deletions

View File

@ -19,6 +19,10 @@ Future<bool> download({
success = await _downloadArtist(object, path); success = await _downloadArtist(object, path);
break; break;
case FunkEntity.track:
success = await _downloadTrack(object, path);
break;
default: default:
console.info(' nothing...'); console.info(' nothing...');
break; break;
@ -100,11 +104,11 @@ Future<bool> _downloadAlbum(
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'];
final result = _downloadSongObject( final result = _downloadTrackObject(
FunkObject( FunkObject(
domain: object.domain, domain: object.domain,
id: songResponse['listen_url'], id: songResponse['listen_url'],
kind: FunkEntity.song, kind: FunkEntity.track,
), ),
'$path/$pathAppend/$songTitle.$ext', '$path/$pathAppend/$songTitle.$ext',
); );
@ -116,7 +120,27 @@ Future<bool> _downloadAlbum(
return successes.every((final success) => success); 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 FunkObject object,
final String path, final String path,
) async { ) async {

View File

@ -1,7 +1,5 @@
enum FunkEntity { enum FunkEntity {
error, track,
song,
album, album,
artist, artist,
profile, profile,

View File

@ -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;
}

View File

@ -70,6 +70,17 @@ ParseResult extract(final List<String> args) {
result = stageResult.result; result = stageResult.result;
break; break;
case ParsingStage.track:
final stageResult = _onEntityStage(
arg,
result,
currentStage,
FunkEntity.track,
);
currentStage = stageResult.stage;
result = stageResult.result;
break;
case ParsingStage.domain: case ParsingStage.domain:
final stageResult = _onDomainStage(arg, result, currentStage); final stageResult = _onDomainStage(arg, result, currentStage);
currentStage = stageResult.stage; currentStage = stageResult.stage;
@ -102,6 +113,8 @@ usage: funkblubber [[-OPTIONS|URL]...]
[-A|--artist ID] Provide with artist ID to download their albums. [-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. [-p|--path PATH] Provide to explicitly define download directory.
Assumed '.'. Assumed '.'.
@ -219,6 +232,10 @@ StageResult _onNothingStage(
case '--album': case '--album':
currentStage = ParsingStage.album; currentStage = ParsingStage.album;
break; break;
case '-t':
case '--track':
currentStage = ParsingStage.track;
break;
case '-u': case '-u':
case '--upload': case '--upload':
currentStage = ParsingStage.upload; currentStage = ParsingStage.upload;
@ -256,6 +273,19 @@ StageResult _onNothingStage(
++i; ++i;
break; 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': case 'albums':
result = ParseResult( result = ParseResult(
action: Action.download, action: Action.download,

View File

@ -2,7 +2,7 @@ enum ParsingStage {
nothing, nothing,
album, album,
artist, artist,
song, track,
path, path,
upload, upload,
domain, domain,