// LastFMMatchTrackChart Search lfm, match to spotify, return results
LastFMMatchTrackChart Search lfm, match to spotify, return results
// LastFMMatchTrackChart Search lfm match to spotify return results
func LastFMMatchTrackChart(token *oauth2.Token, user string, from string, to string, limit int) (ft *models.FoundTracks, e error) {
	c := spotifyclient.NewClient(token)
	c.AutoRetry = true
	pc := &c
	lfm := lastfmclient.Auth()
	lt, _ := lastfmclient.GetTrackChart(lfm, user, from, to)
	var m []models.Track
	var u []models.Track
	wg := sync.WaitGroup{}
	for i, t := range lt {
		if i == limit {
			log.Debug("Limit reached")
			break 
		}
		wg.Add(1)
		go func(t models.Track) {
			st, err := spotifyclient.SearchTrack(pc, t.Artists[0].Name, t.Album.Name, t.Name)
			if err != nil {
				e = err
				wg.Done()
				return
			}
			if len(st) > 0 {
				mt := st[0]
				mt.Rank = t.Rank
				mt.PlayCount = t.PlayCount
				mt.SearchedFor = t.Artists[0].Name + " - " + t.Name
				m = append(m, mt)
			} else {
				u = append(u, t)
			}
			log.Debug("Searched for ", t.Artists[0].Name, " - ", t.Name)
			wg.Done()
		}(t)
	}
	wg.Wait()
	sortByRank(m)
	sortByRank(u)
	return &models.FoundTracks{
		Unmatched:      u,
		Matched:        m,
		Total:          len(u) + len(m),
		TotalMatched:   len(m),
		TotalUnmatched: len(u),
	}, e
}
func LastFMMatchTrackChart ( token * oauth2 . Token , user string , from string , to string , limit int ) ( ft * models . FoundTracks , e error ) { c := spotifyclient . NewClient ( token ) c . AutoRetry = true pc := & c lfm := lastfmclient . Auth ( ) lt , _ := lastfmclient . GetTrackChart ( lfm , user , from , to ) var m [ ] models . Track var u [ ] models . Track wg := sync . WaitGroup { } for i , t := range lt { if i == limit { log . Debug ( " " ) break } wg . Add ( 1 ) go func ( t models . Track ) { st , err := spotifyclient . SearchTrack ( pc , t . Artists [ 0 ] . Name , t . Album . Name , t . Name ) if err != nil { e = err wg . Done ( ) return } if len ( st ) > 0 { mt := st [ 0 ] mt . Rank = t . Rank mt . PlayCount = t . PlayCount mt . SearchedFor = t . Artists [ 0 ] . Name + " " + t . Name m = append ( m , mt ) } else { u = append ( u , t ) } log . Debug ( " " , t . Artists [ 0 ] . Name , " " , t . Name ) wg . Done ( ) } ( t ) } wg . Wait ( ) sortByRank ( m ) sortByRank ( u ) return & models . FoundTracks { Unmatched : u , Matched : m , Total : len ( u ) + len ( m ) , TotalMatched : len ( m ) , TotalUnmatched : len ( u ) , } , e }
LastFMMatchTrackChart Search lfm, match to spotify, return results
LastFMMatchTrackChart Search lfm match to spotify return results
// clients // search tracks on lastfm // match tracks on spotify // do this concurrently // guard := make(chan struct{}, 100) // break here // guard <- struct{}{} // would block if guard channel is already filled // did we find something? // <-guard // return matched and unmatched