Skip to content

Commit

Permalink
change search query to use all artist names and fetch song according …
Browse files Browse the repository at this point in the history
…to duration
  • Loading branch information
BharatKalluri committed Aug 1, 2021
1 parent 32e1cbd commit eb49cae
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
9 changes: 7 additions & 2 deletions src/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"log"
"os"
"strings"

"github.com/zmb3/spotify"
)
Expand Down Expand Up @@ -87,8 +88,12 @@ func DownloadTrackList(cli UserData) {
fmt.Println("Found", len(cli.TrackList), "tracks")
fmt.Println("Searching and downloading tracks")
for _, val := range cli.TrackList {
searchTerm := val.Name + " " + val.Artists[0].Name
youtubeID, err := GetYoutubeId(searchTerm)
var artistNames []string
for _, artistInfo := range val.Artists {
artistNames = append(artistNames, artistInfo.Name)
}
searchTerm := strings.Join(artistNames, " ") + " " + val.Name
youtubeID, err := GetYoutubeId(searchTerm, val.Duration/1000)
if err != nil {
log.Printf("Error occured for %s error: %s", val.Name, err)
continue
Expand Down
35 changes: 33 additions & 2 deletions src/youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
"io/ioutil"
"net/http"
"net/url"
"strconv"
"strings"
)

var httpClient = &http.Client{}
var durationMatchThreshold = 5

type SearchResult struct {
Title, Uploader, URL, Duration, ID string
Expand All @@ -20,16 +22,45 @@ type SearchResult struct {
Extra []string
}

func convertStringDurationToSeconds(durationStr string) int {
splitEntities := strings.Split(durationStr, ":")
if len(splitEntities) == 1 {
seconds, _ := strconv.Atoi(splitEntities[0])
return seconds
} else if len(splitEntities) == 2 {
seconds, _ := strconv.Atoi(splitEntities[1])
minutes, _ := strconv.Atoi(splitEntities[0])
return (minutes * 60) + seconds
} else if len(splitEntities) == 3 {
seconds, _ := strconv.Atoi(splitEntities[2])
minutes, _ := strconv.Atoi(splitEntities[1])
hours, _ := strconv.Atoi(splitEntities[0])
return ((hours * 60) * 60) + (minutes * 60) + seconds
} else {
return 0
}
}

// GetYoutubeId takes the query as string and returns the search results video ID's
func GetYoutubeId(searchQuery string) (string, error) {
searchResults, err := ytSearch(searchQuery, 1)
func GetYoutubeId(searchQuery string, songDurationInSeconds int) (string, error) {
searchResults, err := ytSearch(searchQuery, 10)
if err != nil {
return "", err
}
if len(searchResults) == 0 {
errorMessage := fmt.Sprintf("no songs found for %s", searchQuery)
return "", errors.New(errorMessage)
}
// Try for the closest match timestamp wise
for _, result := range searchResults {
allowedDurationRangeStart := songDurationInSeconds - durationMatchThreshold
allowedDurationRangeEnd := songDurationInSeconds + durationMatchThreshold
resultSongDuration := convertStringDurationToSeconds(result.Duration)
if resultSongDuration >= allowedDurationRangeStart && resultSongDuration <= allowedDurationRangeEnd {
return result.ID, nil
}
}
// Else return the first result if nothing is found
return searchResults[0].ID, nil
}

Expand Down

0 comments on commit eb49cae

Please sign in to comment.