// initializeItunesPlaylist initializes itunesPlaylist value. If there is no // itunesPlaylist set up, then itunesPlaylist set up to blank string. Blank // string is the sign, what tracks should not to be added to iTunes. // // initializeItunesPlaylist sets blank string to config, if OS is darwin func initializeItunesPlaylist(cmd *cobra.Command) { var playlist string if runtime.GOOS == "darwin" { if flagChanged(cmd.Flags(), "itunesPlaylist") { playlist = itunesPlaylist } else { playlist = config.Get("itunesPlaylist") } if playlist == "" { ui.Warning("you didn't set an iTunes playlist. Tracks won't be added to iTunes.") } else { playlistsList, err := applescript.ListOfPlaylists() if err != nil { ui.Term("couldn't get list of playlists", err) } if !strings.Contains(playlistsList, playlist) { ui.Term("playlist "+playlist+" doesn't exist. Please enter correct name.", nil) } } } config.Set("itunesPlaylist", playlist) }
func handleError(err error) { switch { case strings.Contains(err.Error(), "403"): ui.Term("you're not allowed to see these tracks", nil) case strings.Contains(err.Error(), "404"): ui.Term("there are no tracks", nil) } }
func handleStatusCode(statusCode int) error { switch { case statusCode == 403: return ErrForbidden case statusCode == 404: return ErrNotFound case statusCode >= 300 && statusCode < 500: ui.Term("invalid response from SoundCloud: "+strconv.Itoa(statusCode), nil) case statusCode >= 500: ui.Term("there is a problem by SoundCloud. Please wait a while", nil) } return nil }
func Favorites(count, offset uint, uid string) ([]track.Track, error) { requestsCount := float64(count) / float64(tracksLimit) requestsCount = math.Ceil(requestsCount) var limit uint var tracks []track.Track params := url.Values{} for i := uint(0); i < uint(requestsCount); i++ { if count < tracksLimit { limit = count } else { limit = tracksLimit } count -= limit params.Set("limit", strconv.Itoa(int(limit))) params.Set("offset", strconv.Itoa(int((i*tracksLimit)+offset))) bFavs, err := getFavorites(uid, params) if err == ErrNotFound { break } if err != nil { return nil, err } var favs []track.Track if err := json.Unmarshal(bFavs, &favs); err != nil { ui.Term("could't unmarshal JSON with likes", err) } tracks = append(tracks, favs...) } return tracks, nil }
func (tp TracksProcessor) ProcessAll(tracks []track.Track) { if len(tracks) == 0 { ui.Term("there are no tracks to download", nil) } var errors []string // Start with last track for i := len(tracks) - 1; i >= 0; i-- { track := tracks[i] if err := tp.Process(track); err != nil { errors = append(errors, track.Fullname()+": "+err.Error()) ui.Error("there was an error while downloading "+track.Fullname(), err) } ui.Println("") } if len(errors) > 0 { ui.Println(ui.RedString("There were errors while downloading tracks:")) for _, errText := range errors { ui.Println(ui.RedString(" " + errText)) } ui.Println("") } ui.Success("Done!") ui.Quit() }
func getTracks(cmd *cobra.Command, args []string) { initializeConfig(cmd) tp := tracksprocessor.NewConfiguredTracksProcessor() var arg string if len(args) == 0 { arg = "1" } else { arg = args[0] } var downloadTracks []track.Track if isSoundCloudURL(arg) { downloadTracks = getTrackFromURL(arg) } else if num, err := strconv.Atoi(arg); err == nil { downloadTracks, err = getLastTracks(uint(num)) if err != nil { handleError(err) } } else { ui.Term("you've entered invalid argument. Run 'nehm get --help' for usage.", nil) } tp.ProcessAll(downloadTracks) }
func UID(permalink string) string { params := url.Values{} params.Set("url", soundCloudLink+permalink) ui.Println("Getting ID of user") bUser, err := resolve(params) if err != nil { ui.Term("there was a problem by resolving an id of user", err) } var jUser JSONUser if err := json.Unmarshal(bUser, &jUser); err != nil { ui.Term("couldn't unmarshall JSON with user object", err) } return strconv.Itoa(jUser.ID) }
// initializeConfig initializes a config with flags. func initializeConfig(cmd *cobra.Command) { err := config.ReadInConfig() if err == config.ErrNotExist { ui.Warning("there is no config file. Read README to configure nehm") } else if err != nil { ui.Term("", err) } initializeDlFolder(cmd) initializePermalink(cmd) initializeItunesPlaylist(cmd) }
func TrackFromURI(uri string) []track.Track { params := url.Values{} params.Set("url", uri) bTrack, err := resolve(params) if err == ErrForbidden { ui.Term("you haven't got any access to this track", err) } if err == ErrNotFound { ui.Term("you've entered invalid url", err) } if err != nil { ui.Term("couldn't get track from url", err) } var t track.Track if err := json.Unmarshal(bTrack, &t); err != nil { ui.Term("couldn't unmarshal JSON with track from URL", err) } return []track.Track{t} }
// initializePermalink initializes permalink value. If there is no permalink // set up, then program is terminating. func initializePermalink(cmd *cobra.Command) { var p string if flagChanged(cmd.Flags(), "permalink") { p = permalink } else { p = config.Get("permalink") } if p == "" { ui.Term("you didn't set a permalink. Use flag '-p' or set permalink in config file.\nTo know, what is permalink, read FAQ.", nil) } else { config.Set("permalink", p) } }
func Search(query string, limit, offset uint) ([]track.Track, error) { params := url.Values{} params.Set("q", query) params.Set("limit", strconv.Itoa(int(limit))) params.Set("offset", strconv.Itoa(int(offset))) bFound, err := search(params) if err != nil { return nil, err } var found []track.Track if err := json.Unmarshal(bFound, &found); err != nil { ui.Term("couldn't unmarshal JSON with search results", err) } return found, nil }