<commit_before>package spotify

import (
	sp "github.com/fabiofalci/go-libspotify/spotify"
	"github.com/fabiofalci/sconsify/infrastructure"
	"github.com/fabiofalci/sconsify/sconsify"
	webspotify "github.com/zmb3/spotify"
	"strings"
	"time"
)

func (spotify *Spotify) shutdownSpotify() {
	spotify.session.Logout()
	spotify.session.Close()
	spotify.initCache()
	spotify.publisher.ShutdownEngine()
}

func (spotify *Spotify) play(trackUri *sconsify.Track) {

	player := spotify.session.Player()
	if !spotify.paused || spotify.currentTrack != trackUri {
		link, err := spotify.session.ParseLink(trackUri.URI)
		if err != nil {
			return
		}

		track, err := link.Track()
		if err != nil {
			return
		}

		if trackUri.IsPartial() {
			trackUri = sconsify.ToSconsifyTrack(track)
		}

		if !spotify.isTrackAvailable(track) {
			if trackUri.IsFromWebApi() {
				retry := trackUri.RetryLoading()
				if retry < 4 {
					go func() {
						time.Sleep(100 * time.Millisecond)
						spotify.publisher.Play(trackUri)
					}()
					return
				}
			}
			spotify.publisher.TrackNotAvailable(trackUri)
			return
		}
		if err := player.Load(track); err != nil {
			return
		}
		spotify.publisher.NewTrackLoaded(track.Duration())
	}
	player.Play()

	spotify.publisher.TrackPlaying(trackUri)
	spotify.currentTrack = trackUri
	spotify.paused = false
	return
}

func (spotify *Spotify) isTrackAvailable(track *sp.Track) bool {
	return track.Availability() == sp.TrackAvailabilityAvailable
}

func (spotify *Spotify) search(query string) {
	playlists := sconsify.InitPlaylists()

	query = checkAlias(query)
	name := " " + query

	playlist := sconsify.InitSearchPlaylist(name, name, func(playlist *sconsify.Playlist) {
		options := createWebSpotifyOptions(50, playlist.Tracks())
		// TODO use spotify.client while still token valid, then switch to Default one
		if searchResult, err := webspotify.DefaultClient.SearchOpt(query, webspotify.SearchTypeTrack, options); err == nil {
			numberOfTracks := len(searchResult.Tracks.Tracks)
			infrastructure.Debugf("Search '%v' returned %v track(s)", query, numberOfTracks)
			for _, track := range searchResult.Tracks.Tracks {
				webArtist := track.Artists[0]
				artist := sconsify.InitArtist(string(webArtist.URI), webArtist.Name)
				playlist.AddTrack(sconsify.InitWebApiTrack(string(track.URI), artist, track.Name, track.TimeDuration().String()))
				infrastructure.Debugf("\tTrack '%v' (%v)", track.URI, track.Name)
			}
		} else {
			infrastructure.Debugf("Spotify search returning error: %v", err)
		}
	})
	playlist.ExecuteLoad()
	playlists.AddPlaylist(playlist)

	spotify.publisher.NewPlaylist(playlists)
}

func checkAlias(query string) string {<commit_msg>Use auth client when searching<commit_after>package spotify

import (
	sp "github.com/fabiofalci/go-libspotify/spotify"
	"github.com/fabiofalci/sconsify/infrastructure"
	"github.com/fabiofalci/sconsify/sconsify"
	webspotify "github.com/zmb3/spotify"
	"strings"
	"time"
)

func (spotify *Spotify) shutdownSpotify() {
	spotify.session.Logout()
	spotify.session.Close()
	spotify.initCache()
	spotify.publisher.ShutdownEngine()
}

func (spotify *Spotify) play(trackUri *sconsify.Track) {

	player := spotify.session.Player()
	if !spotify.paused || spotify.currentTrack != trackUri {
		link, err := spotify.session.ParseLink(trackUri.URI)
		if err != nil {
			return
		}

		track, err := link.Track()
		if err != nil {
			return
		}

		if trackUri.IsPartial() {
			trackUri = sconsify.ToSconsifyTrack(track)
		}

		if !spotify.isTrackAvailable(track) {
			if trackUri.IsFromWebApi() {
				retry := trackUri.RetryLoading()
				if retry < 4 {
					go func() {
						time.Sleep(100 * time.Millisecond)
						spotify.publisher.Play(trackUri)
					}()
					return
				}
			}
			spotify.publisher.TrackNotAvailable(trackUri)
			return
		}
		if err := player.Load(track); err != nil {
			return
		}
		spotify.publisher.NewTrackLoaded(track.Duration())
	}
	player.Play()

	spotify.publisher.TrackPlaying(trackUri)
	spotify.currentTrack = trackUri
	spotify.paused = false
	return
}

func (spotify *Spotify) isTrackAvailable(track *sp.Track) bool {
	return track.Availability() == sp.TrackAvailabilityAvailable
}

func (spotify *Spotify) search(query string) {
	playlists := sconsify.InitPlaylists()

	query = checkAlias(query)
	name := " " + query

	playlist := sconsify.InitSearchPlaylist(name, name, func(playlist *sconsify.Playlist) {
		options := createWebSpotifyOptions(50, playlist.Tracks())
		// search is an auth endpoint now, when the token expires, it won't work
		if searchResult, err := spotify.client.SearchOpt(query, webspotify.SearchTypeTrack, options); err == nil {
			numberOfTracks := len(searchResult.Tracks.Tracks)
			infrastructure.Debugf("Search '%v' returned %v track(s)", query, numberOfTracks)
			for _, track := range searchResult.Tracks.Tracks {
				webArtist := track.Artists[0]
				artist := sconsify.InitArtist(string(webArtist.URI), webArtist.Name)
				playlist.AddTrack(sconsify.InitWebApiTrack(string(track.URI), artist, track.Name, track.TimeDuration().String()))
				infrastructure.Debugf("\tTrack '%v' (%v)", track.URI, track.Name)
			}
		} else {
			infrastructure.Debugf("Spotify search returning error: %v", err)
		}
	})
	playlist.ExecuteLoad()
	playlists.AddPlaylist(playlist)

	spotify.publisher.NewPlaylist(playlists)
}

func checkAlias(query string) string {
{'fraction_non_alphanumeric': 0.13046715056765307, 'fraction_numerical': 0.00297785222408338, 'mean_word_length': 4.3901705115346035, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2197994', 'n_tokens_mistral': 2071, 'n_tokens_neox': 1840, 'n_words': 341}