@@ -324,9 +324,24 @@ func (p *Plex) GetOnDeck() (SearchResultsEpisode, error) {
324324}
325325
326326// Download media associated with metadata
327- func (p * Plex ) Download (meta Metadata , path string ) error {
327+ func (p * Plex ) Download (meta Metadata , path string , createFolders bool , skipIfExists bool ) error {
328+
329+ if len (meta .Media ) == 0 {
330+ return fmt .Errorf ("no media associated with metadata, skipping" )
331+ }
328332
329333 path = filepath .Join (path )
334+ if createFolders {
335+
336+ if meta .ParentTitle != "" && meta .GrandparentTitle != "" { // for tv shows and music
337+ path = filepath .Join (path , meta .GrandparentTitle , meta .ParentTitle )
338+ } else { // for movies
339+ path = filepath .Join (path , meta .Title )
340+ }
341+ if err := os .MkdirAll (path , 0700 ); err != nil {
342+ return err
343+ }
344+ }
330345
331346 for _ , media := range meta .Media {
332347
@@ -335,9 +350,14 @@ func (p *Plex) Download(meta Metadata, path string) error {
335350 // get original filename from original path
336351 split := strings .Split (part .File , "/" )
337352 file := split [len (split )- 1 ]
338- // compute filepath
339- fp := fmt .Sprintf ("%s/%s" , path , file )
340- fp = filepath .Join (fp )
353+
354+ fp := filepath .Join (path , file )
355+
356+ _ , exists := os .Stat (fp )
357+
358+ if exists == nil && skipIfExists {
359+ return nil
360+ }
341361
342362 query := fmt .Sprintf ("%s%s?download=1" , p .URL , part .Key )
343363
0 commit comments