This repository has been archived by the owner on Feb 13, 2020. It is now read-only.
/
get.go
64 lines (54 loc) · 1.58 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright 2016 Albert Nigmatzianov. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package commands
import (
"strconv"
"strings"
"github.com/bogem/nehm/client"
"github.com/bogem/nehm/config"
"github.com/bogem/nehm/track"
"github.com/bogem/nehm/tracksprocessor"
"github.com/bogem/nehm/ui"
"github.com/spf13/cobra"
)
var (
getCommand = &cobra.Command{
Use: "get ([number] or [url])",
Short: "download either inputed count of likes or track from entered url, set tags (and add to your iTunes library)",
Aliases: []string{"g"},
Run: getTracks,
}
)
func init() {
addCommonFlags(getCommand)
addOffsetFlag(getCommand)
addPermalinkFlag(getCommand)
}
func getTracks(cmd *cobra.Command, args []string) {
initializeConfig(cmd)
var downloadTracks []track.Track
if len(args) == 0 {
downloadTracks = getLastTracks(1)
} else {
arg := args[0]
if isSoundCloudURL(arg) {
downloadTracks = getTrackFromURL(arg)
} else if num, err := strconv.Atoi(arg); err == nil {
downloadTracks = getLastTracks(uint(num))
} else {
ui.Term("You've entered invalid argument. Run 'nehm get --help' for usage.", nil)
}
}
tracksprocessor.NewConfiguredTracksProcessor().ProcessAll(downloadTracks)
}
func getLastTracks(count uint) []track.Track {
uid := client.UID(config.GetPermalink())
return client.Favorites(count, offset, uid)
}
func isSoundCloudURL(url string) bool {
return strings.Contains(url, "soundcloud.com")
}
func getTrackFromURL(url string) []track.Track {
return client.TrackFromURI(url)
}