Skip to content

Commit 94d1011

Browse files
authored
Merge pull request #40 from fwicht/download-enhancements
Download enhancements
2 parents f917a46 + a3a691e commit 94d1011

File tree

3 files changed

+39
-5
lines changed

3 files changed

+39
-5
lines changed

cmd/commands.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,10 @@ func downloadMedia(c *cli.Context) error {
895895
downloadPath = "."
896896
}
897897

898+
createFolders := c.Bool("folders")
899+
900+
skipIfExists := c.Bool("skip")
901+
898902
// search for media
899903
results, err := plexConn.Search(c.Args().First())
900904

@@ -927,7 +931,7 @@ func downloadMedia(c *cli.Context) error {
927931
selectedMedia := results.MediaContainer.Metadata[selection]
928932

929933
// download media
930-
if err := plexConn.Download(selectedMedia, downloadPath); err != nil {
934+
if err := plexConn.Download(selectedMedia, downloadPath, createFolders, skipIfExists); err != nil {
931935
return cli.NewExitError(err, 1)
932936
}
933937

cmd/main.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,16 @@ func main() {
153153
Name: "download",
154154
Usage: "download media from your plex server",
155155
Action: downloadMedia,
156+
Flags: []cli.Flag{
157+
cli.BoolFlag{
158+
Name: "folders",
159+
Usage: "create folder hierarchy",
160+
},
161+
cli.BoolFlag{
162+
Name: "skip",
163+
Usage: "skip download if file already exists",
164+
},
165+
},
156166
},
157167
}
158168

plex.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)