Exemplo n.º 1
0
Arquivo: main.go Projeto: brandur/wgt2
func main() {
	// create an output directory (the assets subdirectory here because its
	// parent will be created as a matter of course)
	err := os.MkdirAll(TargetAssetsDir, 0755)
	if err != nil {
		log.Fatal(err.Error())
	}

	template, err := ace.Load("index", "", &ace.Options{
		DynamicReload: true,
		FuncMap:       templateFuncMap(),
	})
	if err != nil {
		log.Fatal(err.Error())
	}

	db, err := wgt2.LoadDatabase(DBFilename)
	if err != nil {
		log.Fatal(err.Error())
	}

	var artists []*wgt2.Artist
	for _, artist := range db.Artists.Data {
		artists = append(artists, artist)
	}

	// Go doesn't exactly make sorting easy ...
	sort.Sort(artistSlice(artists))

	var playlists []*wgt2.Playlist
	for _, playlist := range db.Playlists.Data {
		playlists = append(playlists, playlist)
	}

	file, err := os.Create(TargetDir + "index")
	if err != nil {
		log.Fatal(err.Error())
	}
	defer file.Close()

	writer := bufio.NewWriter(file)
	defer writer.Flush()

	data := map[string]interface{}{
		"artists":   artists,
		"playlists": playlists,
	}
	if err := template.Execute(writer, data); err != nil {
		log.Fatal(err.Error())
	}
}
Exemplo n.º 2
0
Arquivo: main.go Projeto: brandur/wgt2
func main() {
	body, err := fetchBody()
	if err != nil {
		log.Fatal(err.Error())
	}

	var dbArtists []*wgt2.RawArtist
	artists := extractArtists(body)
	for _, artist := range artists {
		log.Printf("Got raw artist: %v", artist)
		dbArtists = append(dbArtists, &wgt2.RawArtist{WGTName: artist})
	}

	db, err := wgt2.LoadDatabase(DBFilename)
	if err != nil {
		log.Fatal(err.Error())
	}

	db.RawArtists.SetData(dbArtists)
	db.Save()
}
Exemplo n.º 3
0
Arquivo: main.go Projeto: brandur/wgt2
func main() {
	var conf Conf
	err := envdecode.Decode(&conf)
	if err != nil {
		log.Fatal(err.Error())
	}

	client := wgt2.GetSpotifyClient(conf.ClientID, conf.ClientSecret, conf.RefreshToken)

	db, err := wgt2.LoadDatabase(DBFilename)
	if err != nil {
		log.Fatal(err.Error())
	}

	user, err := client.CurrentUser()
	if err != nil {
		log.Fatal(err.Error())
	}

	var trackIDs []spotify.ID

	//
	// Main playlist
	//

	trackIDs = nil
	for _, artist := range db.Artists.Data {
		bound := TracksPerArtist
		if bound > len(artist.TopTracks) {
			bound = len(artist.TopTracks)
		}
		for _, track := range artist.TopTracks[0:bound] {
			trackIDs = append(trackIDs, spotify.ID(track.ID))
		}
	}

	dbPlaylist, err := updatePlaylist(client, user, PlaylistName, trackIDs)
	if err != nil {
		log.Fatal(err.Error())
	}
	db.Playlists.AddPlaylist(dbPlaylist)

	//
	// Popular playlist
	//

	trackIDs = nil
	for _, artist := range db.Artists.Data {
		// an arbitrary threshold
		if artist.Popularity < 20 {
			continue
		}

		bound := TracksPerArtist
		if bound > len(artist.TopTracks) {
			bound = len(artist.TopTracks)
		}
		for _, track := range artist.TopTracks[0:bound] {
			trackIDs = append(trackIDs, spotify.ID(track.ID))
		}
	}

	dbPlaylist, err = updatePlaylist(client, user, PlaylistNamePopular, trackIDs)
	if err != nil {
		log.Fatal(err.Error())
	}
	db.Playlists.AddPlaylist(dbPlaylist)

	//
	// Obscure playlist
	//

	trackIDs = nil
	for _, artist := range db.Artists.Data {
		// an arbitrary threshold
		if artist.Popularity >= 20 {
			continue
		}

		bound := TracksPerArtist
		if bound > len(artist.TopTracks) {
			bound = len(artist.TopTracks)
		}
		for _, track := range artist.TopTracks[0:bound] {
			trackIDs = append(trackIDs, spotify.ID(track.ID))
		}
	}

	dbPlaylist, err = updatePlaylist(client, user, PlaylistNameObscure, trackIDs)
	if err != nil {
		log.Fatal(err.Error())
	}
	db.Playlists.AddPlaylist(dbPlaylist)

	err = db.Save()
	if err != nil {
		log.Fatal(err.Error())
	}
}
Exemplo n.º 4
0
Arquivo: main.go Projeto: brandur/wgt2
func main() {
	var conf Conf
	err := envdecode.Decode(&conf)
	if err != nil {
		log.Fatal(err.Error())
	}

	client := wgt2.GetSpotifyClient(conf.ClientID, conf.ClientSecret, conf.RefreshToken)

	db, err := wgt2.LoadDatabase(DBFilename)
	if err != nil {
		log.Fatal(err.Error())
	}

	for _, rawArtist := range db.RawArtists.Data {
		dbArtist := db.Artists.GetArtistByWGTName(rawArtist.WGTName)
		if dbArtist != nil {
			log.Printf("Have artist already: %v", dbArtist.Name)
			continue
		}

		searchName := rawArtist.WGTName
		if val, ok := ManualOverrides[searchName]; ok {
			if val == "" {
				log.Printf("Skipping '%v' due to override", searchName)
				continue
			}

			log.Printf("Using manual override '%v' for '%v'", val, searchName)
			searchName = val
		}

		res, err := client.Search(searchName, spotify.SearchTypeArtist)
		if err != nil {
			log.Fatal(err.Error())
		}

		if len(res.Artists.Artists) < 1 {
			log.Printf("Artist not found: %v", rawArtist.WGTName)
			continue
		}

		artist := res.Artists.Artists[0]

		dbArtist = &wgt2.Artist{
			Genres:     artist.Genres,
			ID:         string(artist.ID),
			Name:       artist.Name,
			Popularity: artist.Popularity,
			URI:        string(artist.URI),
			WGTName:    rawArtist.WGTName,
		}

		log.Printf("Found artist: %v (from: %v; popularity: %v/100)",
			artist.Name, rawArtist.WGTName, artist.Popularity)

		tracks, err := client.GetArtistsTopTracks(artist.ID, "US")
		if err != nil {
			log.Fatal(err.Error())
		}

		for _, track := range tracks {
			dbTrack := wgt2.Track{
				ID:         string(track.ID),
				Name:       track.Name,
				Popularity: track.Popularity,
				URI:        string(track.URI),
			}
			dbArtist.TopTracks = append(dbArtist.TopTracks, dbTrack)
		}

		db.Artists.AddArtist(dbArtist)
	}

	err = db.Save()
	if err != nil {
		log.Fatal(err.Error())
	}
}