17
17
18
18
namespace Nitefox . App . Media ;
19
19
20
- public class MediaDownloader (
21
- IFileService fileService ,
22
- FfmpegService ffmpegService ,
23
- NitefoxConfig nitefoxConfig ,
24
- MetadataService metadataService )
20
+ public class MediaDownloader
25
21
{
26
-
27
- private readonly DataStore _mediaDataStore = new ( $ "{ nitefoxConfig . DownloadLocation } downloads.json",
28
- reloadBeforeGetCollection : true ) ;
22
+ private readonly IFileService _fileService ;
23
+ private readonly DataStore _mediaDataStore ;
24
+ private readonly FfmpegService _ffmpegService ;
25
+ private readonly NitefoxConfig _nitefoxConfig ;
26
+ private readonly MetadataService _metadataService ;
29
27
28
+ public MediaDownloader (
29
+ IFileService fileService ,
30
+ FfmpegService ffmpegService ,
31
+ NitefoxConfig nitefoxConfig ,
32
+ MetadataService metadataService )
33
+ {
34
+ _fileService = fileService ;
35
+ _ffmpegService = ffmpegService ;
36
+ _nitefoxConfig = nitefoxConfig ;
37
+ _metadataService = metadataService ;
38
+ _mediaDataStore = new DataStore ( $ "{ _nitefoxConfig . DownloadLocation } downloads.json",
39
+ reloadBeforeGetCollection : true ) ;
40
+ }
41
+
30
42
public async Task < TrackDownload > Download ( string title , string url , string author , string ? collection = null )
31
43
{
32
44
var trackDownload = new TrackDownload
@@ -39,12 +51,12 @@ public async Task<TrackDownload> Download(string title, string url, string autho
39
51
40
52
try
41
53
{
42
- await ffmpegService . DownloadFfmpeg ( ) ;
54
+ await _ffmpegService . DownloadFfmpeg ( ) ;
43
55
44
- ffmpegService . ConfigureFfmpeg ( ) ;
45
- fileService . SetupDirectories ( ) ;
56
+ _ffmpegService . ConfigureFfmpeg ( ) ;
57
+ _fileService . SetupDirectories ( ) ;
46
58
47
- var streamUrl = await metadataService . GetYoutubeSongStream ( url ) ;
59
+ var streamUrl = await _metadataService . GetYoutubeSongStream ( url ) ;
48
60
49
61
if ( string . IsNullOrWhiteSpace ( streamUrl ) )
50
62
{
@@ -53,12 +65,12 @@ public async Task<TrackDownload> Download(string title, string url, string autho
53
65
54
66
if ( collection is not null )
55
67
{
56
- var songAlbumPath = $ "{ nitefoxConfig . DownloadLocation } { collection } \\ ";
68
+ var songAlbumPath = $ "{ _nitefoxConfig . DownloadLocation } { collection } \\ ";
57
69
58
70
trackDownload . Collection = collection ;
59
71
trackDownload . SavePath = $ "{ songAlbumPath } { songSaveName } ";
60
72
trackDownload . IsDownloaded =
61
- await ffmpegService . StreamConvert ( streamUrl , $ "{ songAlbumPath } { songSaveName } ") ;
73
+ await _ffmpegService . StreamConvert ( streamUrl , $ "{ songAlbumPath } { songSaveName } ") ;
62
74
63
75
if ( trackDownload . IsDownloaded )
64
76
{
@@ -69,9 +81,9 @@ await _mediaDataStore.GetCollection<TrackDownload>()
69
81
return trackDownload ;
70
82
}
71
83
72
- trackDownload . SavePath = $ "{ nitefoxConfig . DownloadLocation } { songSaveName } ";
84
+ trackDownload . SavePath = $ "{ _nitefoxConfig . DownloadLocation } { songSaveName } ";
73
85
trackDownload . IsDownloaded =
74
- await ffmpegService . StreamConvert ( streamUrl , $ "{ nitefoxConfig . DownloadLocation } { songSaveName } ") ;
86
+ await _ffmpegService . StreamConvert ( streamUrl , $ "{ _nitefoxConfig . DownloadLocation } { songSaveName } ") ;
75
87
76
88
if ( trackDownload . IsDownloaded )
77
89
{
@@ -90,9 +102,9 @@ await _mediaDataStore.GetCollection<TrackDownload>()
90
102
91
103
public async IAsyncEnumerable < TrackDownload > DownloadAlbum ( string title , string id )
92
104
{
93
- fileService . CreateMediaDirectory ( nitefoxConfig . DownloadLocation , title . ToPathSafeString ( ) ) ;
105
+ _fileService . CreateMediaDirectory ( _nitefoxConfig . DownloadLocation , title . ToPathSafeString ( ) ) ;
94
106
95
- foreach ( var track in await metadataService . GetAlbumTracksMetadata ( id , title ) )
107
+ foreach ( var track in await _metadataService . GetAlbumTracksMetadata ( id , title ) )
96
108
{
97
109
var authors = track . Artists . Select ( artist => artist . Name ) . Take ( 3 ) . ToDelimitedString ( ) ;
98
110
yield return await Download ( track . Title , track . Url , authors , title . ToPathSafeString ( ) ) ;
@@ -101,9 +113,9 @@ public async IAsyncEnumerable<TrackDownload> DownloadAlbum(string title, string
101
113
102
114
public async IAsyncEnumerable < TrackDownload > DownloadPlaylist ( string title , string id )
103
115
{
104
- fileService . CreateMediaDirectory ( nitefoxConfig . DownloadLocation , title . ToPathSafeString ( ) ) ;
116
+ _fileService . CreateMediaDirectory ( _nitefoxConfig . DownloadLocation , title . ToPathSafeString ( ) ) ;
105
117
106
- foreach ( var track in await metadataService . GetPlaylistTracksMetadata ( id , title ) )
118
+ foreach ( var track in await _metadataService . GetPlaylistTracksMetadata ( id , title ) )
107
119
{
108
120
var authors = track . Artists . Select ( artist => artist . Name ) . Take ( 3 ) . ToDelimitedString ( ) ;
109
121
yield return await Download ( track . Title , track . Url , authors , title . ToPathSafeString ( ) ) ;
0 commit comments