Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple QoL updates #12

Merged
merged 23 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/audit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Audit

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:

audit:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v2

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.22

- name: Verify dependencies
run: go mod verify

- name: Build
run: go build -v ./...

- name: Run go vet
run: go vet ./...

- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest

- name: Run staticcheck
run: staticcheck ./...
12 changes: 12 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
default: lint

.PHONY: init
init:
go mod download

.PHONY: lint
lint:
go mod tidy
go fmt ./...
go vet ./...
staticcheck ./...
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
# SpotifyFM
This application is a `Golang` CLI that is in a bit of a transient state. Since I am currently in school, it has been put on the back-burner.
This application is a `Golang` CLI that is in a bit of a transient state. Since I am currently in school, it has been put on the back-burner and is currently something I contribute to when the time feels right.

Sometime during the summer of 2023 I will further flesh this out and leave it in a final state, at least from the `Golang` perspective.
## Project Requirements
- Golang v1.22
- A [Spotify API token](https://developer.spotify.com/documentation/web-api) and the following environment variables:
- `SPOTIFY_ID`: The application ID created from the above web-API link.
- `SPOTIFY_SECRET`: The application secret created from the above web-API link.
- `SPOTIFY_USER_NAME`: Your username on Spotify.
- A [Last.FM token](https://www.last.fm/api/authentication) and the following environment variables:
- `LAST_FM_API_KEY`: The application ID created from the above web-API link.
- `LAST_FM_SHARED_SECRET`: The application secret created from the above web-API link.

If you have any interest in, general questions about, or features you'd like to see in the the application, please don't hesitate to [reach out to me](mailto:[email protected]).
## Commands
- `go run main.go lastfm`: Holds commands that use the Spotify endpoints.
- `go run main.go spotify`: Holds commands that use the Last.FM endpoints.
- `go run main.go both`: Holds commands that use both the Spotify and Last.FM endpoints.
145 changes: 4 additions & 141 deletions cmd/both.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"strings"
"time"

"github.com/go-echarts/go-echarts/v2/charts"
"github.com/go-echarts/go-echarts/v2/opts"
"github.com/spf13/cobra"
"github.com/zmb3/spotify"

Expand All @@ -35,21 +33,20 @@ func NewRecentTrackInformationCmd(lastFMHandler endpoints.LastFMHandler, spotify
return nil
},
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Starting the recent track fetching process...")
tracksForDuration, audioFeatures := getRecentTrackInformation(constants.StartOf2022, lastFMHandler, spotifyHandler)
tracksForDuration, audioFeatures := getRecentTrackInformation(constants.StartOf2024, lastFMHandler, spotifyHandler)
printoutResultsToTxt(tracksForDuration, audioFeatures)
},
}

return cmd
}

func getRecentTrackInformation(fromDate int64, lastFMHandler endpoints.LastFMHandler, spotifyHandler endpoints.SpotifyHandler) ([]models.Track, map[spotify.ID]*spotify.AudioFeatures) {
tracksForDuration := lastFMHandler.GetAllRecentTracks(fromDate, lastFMHandler.GetUserInfo().Name)
func getRecentTrackInformation(fromDate time.Time, lastFMHandler endpoints.LastFMHandler, spotifyHandler endpoints.SpotifyHandler) ([]models.Track, map[spotify.ID]*spotify.AudioFeatures) {
fmt.Printf("Starting the recent track fetching process with %s as the start date...\n", fromDate.Format("01-02-2006"))
tracksForDuration := lastFMHandler.GetAllRecentTracks(fromDate.Unix(), lastFMHandler.GetUserInfo().Name)
models.AddLastFMTrackList(tracksForDuration)
fmt.Println("-----------------------------")
fmt.Printf("There are this many tracks: %d\n", len(tracksForDuration))
nonCachedTrackIDs := make([]spotify.ID, 0)
couldNotFindInSearch := 0
couldNotMatchInSearch := 0
spotifyAPICallForTrack := 0
Expand Down Expand Up @@ -88,7 +85,6 @@ func getRecentTrackInformation(fromDate int64, lastFMHandler endpoints.LastFMHan
if searchResult != nil {
comparisonResult := compareMultipleReturnedTracks(t, searchResult)
if comparisonResult != constants.EmptyString {
nonCachedTrackIDs = append(nonCachedTrackIDs, comparisonResult)
t.SpotifyID = comparisonResult
tracksForDuration[i] = t
trackToIDHash[searchKey] = comparisonResult
Expand Down Expand Up @@ -212,139 +208,6 @@ func printoutResultsToTxt(tracks []models.Track, features map[spotify.ID]*spotif
_ = dataWriter.Flush()
}

func printoutResultsWFHValenceComparison(tracks []models.Track, features map[spotify.ID]*spotify.AudioFeatures) {

// Calculation vars -- pre pandemic
prePandemicMondayVarSum := 0
prePandemicMondayTracks := 0
prePandemicTuesdayVarSum := 0
prePandemicTuesdayTracks := 0
prePandemicWednesdayVarSum := 0
prePandemicWednesdayTracks := 0
prePandemicThursdayVarSum := 0
prePandemicThursdayTracks := 0
prePandemicFridayVarSum := 0
prePandemicFridayTracks := 0

// Calculation vars -- pandemic
pandemicMondayVarSum := 0
pandemicMondayTracks := 0
pandemicTuesdayVarSum := 0
pandemicTuesdayTracks := 0
pandemicWednesdayVarSum := 0
pandemicWednesdayTracks := 0
pandemicThursdayVarSum := 0
pandemicThursdayTracks := 0
pandemicFridayVarSum := 0
pandemicFridayTracks := 0

for _, t := range tracks {
// I only want dem weekday tracks
if t.ListenDate.Weekday() == 0 || t.ListenDate.Weekday() == 6 {
continue
}

// I only want dem work hour tracks 10 AM - 6 PM
if t.ListenDate.Hour() < 10 || t.ListenDate.Hour() > 18 {
continue
}

// An audio feature id is required
if af, ok := features[t.SpotifyID]; !ok || af == nil {
continue
}

wantedValue := int(features[t.SpotifyID].Valence * 100.00)

if t.ListenDate.Before(constants.WFHStartDay) {
if t.ListenDate.Weekday() == 1 {
prePandemicMondayVarSum += wantedValue
prePandemicMondayTracks++
} else if t.ListenDate.Weekday() == 2 {
prePandemicTuesdayVarSum += wantedValue
prePandemicTuesdayTracks++
} else if t.ListenDate.Weekday() == 3 {
prePandemicWednesdayVarSum += wantedValue
prePandemicWednesdayTracks++
} else if t.ListenDate.Weekday() == 4 {
prePandemicThursdayVarSum += wantedValue
prePandemicThursdayTracks++
} else if t.ListenDate.Weekday() == 5 {
prePandemicFridayVarSum += wantedValue
prePandemicFridayTracks++
}
} else {
if t.ListenDate.Weekday() == 1 {
pandemicMondayVarSum += wantedValue
pandemicMondayTracks++
} else if t.ListenDate.Weekday() == 2 {
pandemicTuesdayVarSum += wantedValue
pandemicTuesdayTracks++
} else if t.ListenDate.Weekday() == 3 {
pandemicWednesdayVarSum += wantedValue
pandemicWednesdayTracks++
} else if t.ListenDate.Weekday() == 4 {
pandemicThursdayVarSum += wantedValue
pandemicThursdayTracks++
} else if t.ListenDate.Weekday() == 5 {
pandemicFridayVarSum += wantedValue
pandemicFridayTracks++
}
}
}

bar := charts.NewBar()
bar.SetGlobalOptions(
charts.WithColorsOpts(opts.Colors{"#bbbbbb", "#88c442"}),
charts.WithTitleOpts(
opts.Title{Title: "Valence of Tracks During the Workday: Before and During the Pandemic"},
),
)

bar.SetXAxis([]string{"Monday", "Tuesday", "Wednesday", "Thursday", "Friday"}).
AddSeries(
"Pre-pandemic",
[]opts.BarData{
{
Value: prePandemicMondayVarSum / prePandemicMondayTracks,
},
{
Value: prePandemicTuesdayVarSum / prePandemicTuesdayTracks,
},
{
Value: prePandemicWednesdayVarSum / prePandemicWednesdayTracks,
},
{
Value: prePandemicThursdayVarSum / prePandemicThursdayTracks,
},
{
Value: prePandemicFridayVarSum / prePandemicFridayTracks,
},
}).
AddSeries(
"Pandemic",
[]opts.BarData{
{
Value: pandemicMondayVarSum / pandemicMondayTracks,
},
{
Value: pandemicTuesdayVarSum / pandemicTuesdayTracks,
},
{
Value: pandemicWednesdayVarSum / pandemicWednesdayTracks,
},
{
Value: pandemicThursdayVarSum / pandemicThursdayTracks,
},
{
Value: pandemicFridayVarSum / pandemicFridayTracks,
},
})

f, _ := os.Create("bar.html")
bar.Render(f)
}

func compareMultipleReturnedTracks(localTrack models.Track, searchTracks []spotify.FullTrack) spotify.ID {
for _, searchTrack := range searchTracks {
if models.RemoveNonWordCharacters(searchTrack.Name) == models.RemoveNonWordCharacters(localTrack.Name) &&
Expand Down
2 changes: 1 addition & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func NewRootCmd(lastFMHandler endpoints.LastFMHandler, spotifyHandler endpoints.
NewLastFMCmd(): {
NewTrackVsTimeCmd(lastFMHandler),
},
NewSpotifymd(): {
NewSpotifyCmd(): {
NewAudioFeatureProcessing(spotifyHandler),
},
} {
Expand Down
2 changes: 1 addition & 1 deletion cmd/spoitfy.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
"github.com/michplunkett/spotifyfm/models"
)

func NewSpotifymd() *cobra.Command {
func NewSpotifyCmd() *cobra.Command {
return &cobra.Command{
Use: "spotify",
Short: "Runs Spotify subcommands.",
Expand Down
7 changes: 4 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module github.com/michplunkett/spotifyfm

go 1.19
go 1.22

require (
github.com/go-echarts/go-echarts/v2 v2.2.4
Expand All @@ -13,9 +13,10 @@ require (
)

require (
github.com/golang/protobuf v1.2.0 // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
golang.org/x/net v0.23.0 // indirect
google.golang.org/appengine v1.4.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
)
13 changes: 10 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/go-echarts/go-echarts/v2 v2.2.4 h1:SKJpdyNIyD65XjbUZjzg6SwccTNXEgmh+PlaO23g2H0=
github.com/go-echarts/go-echarts/v2 v2.2.4/go.mod h1:6TOomEztzGDVDkOSCFBq3ed7xOYfbOqhaBzD0YV771A=
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand All @@ -36,11 +40,14 @@ golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d h1:TzXSXBo42m9gQenoE3b9BGiEpg5IG2JkU5FkPIawgtw=
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4 h1:YUO/7uOKsKeq9UokNS62b8FYywz3ker1l1vDZRCRefw=
golang.org/x/sync v0.0.0-20181221193216-37e7f081c4d4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/appengine v1.4.0 h1:/wp5JvzpHIxhs/dumFmF7BXTf3Z+dd4uXta4kVyO508=
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
Expand Down
6 changes: 3 additions & 3 deletions models/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package models

import (
"encoding/json"
"io/ioutil"
"os"
"time"
)

Expand All @@ -26,7 +26,7 @@ type SpotifyConfigs struct {
}

func GetConfigValues() *FileConfigs {
configFile, _ := ioutil.ReadFile(configFileName)
configFile, _ := os.ReadFile(configFileName)

configInfo := FileConfigs{}
_ = json.Unmarshal(configFile, &configInfo)
Expand All @@ -40,5 +40,5 @@ func (config *FileConfigs) SetConfigValues() {

func setConfigFileValues(configValues *FileConfigs) {
file, _ := json.MarshalIndent(configValues, "", " ")
_ = ioutil.WriteFile(configFileName, file, 0644)
_ = os.WriteFile(configFileName, file, 0644)
}
12 changes: 6 additions & 6 deletions models/track.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package models

import (
"encoding/json"
"io/ioutil"
"os"
"regexp"
"strconv"
"strings"
Expand Down Expand Up @@ -100,7 +100,7 @@ func RemoveNonWordCharacters(name string) string {
func GetSpotifySearchToSongIDs() map[string]spotify.ID {
searchToIDHash := make(map[string]spotify.ID, 0)

songIDFile, err := ioutil.ReadFile(songIDsFileName)
songIDFile, err := os.ReadFile(songIDsFileName)
if err != nil {
return searchToIDHash
}
Expand All @@ -119,13 +119,13 @@ func AddSpotifySearchToSongIDs(searchToID map[string]spotify.ID) {
}

file, _ := json.MarshalIndent(validSearches, "", " ")
_ = ioutil.WriteFile(songIDsFileName, file, fileMode)
_ = os.WriteFile(songIDsFileName, file, fileMode)
}

func GetSpotifyIDToAudioFeatures() map[spotify.ID]*spotify.AudioFeatures {
audioFeatures := make(map[spotify.ID]*spotify.AudioFeatures, 0)

songIDFile, err := ioutil.ReadFile(songAudioFeaturesFileName)
songIDFile, err := os.ReadFile(songAudioFeaturesFileName)
if err != nil {
return audioFeatures
}
Expand All @@ -137,10 +137,10 @@ func GetSpotifyIDToAudioFeatures() map[spotify.ID]*spotify.AudioFeatures {

func AddSpotifyIDToAudioFeatures(idToAudioFeatures map[spotify.ID]*spotify.AudioFeatures) {
file, _ := json.MarshalIndent(idToAudioFeatures, "", " ")
_ = ioutil.WriteFile(songAudioFeaturesFileName, file, fileMode)
_ = os.WriteFile(songAudioFeaturesFileName, file, fileMode)
}

func AddLastFMTrackList(tracks []Track) {
file, _ := json.MarshalIndent(tracks, "", " ")
_ = ioutil.WriteFile(songListFileName, file, fileMode)
_ = os.WriteFile(songListFileName, file, fileMode)
}
Loading
Loading