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

adding last.fm integration #65

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@

markscribe
dist/
.env
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,46 @@ This function requires GoodReads API key!

This function requires a `LITERAL_EMAIL` and `LITERAL_PASSWORD`.

### Favourite albums of all time 🎢

```
{{range lastFmFavouriteAlbums 5}}
- {{.Artist.Name}} - {{.Name}}
{{- end}}
```

This function requires last.fm User, API Key, and API Secret!

### Favourite artists of all time πŸ‘¨β€πŸŽ€

```
{{range lastFmFavouriteArtists 5}}
- {{.Name}} ({{.PlayCount}})
{{- end}}
```

This function requires last.fm User, API Key, and API Secret!

### Favourite tracks of all time πŸ’Ώ

```
{{range lastFmFavouriteTracks 5}}
- {{.Artist.Name}} - {{.Name}} ({{.PlayCount}})
{{- end}}
```

This function requires last.fm User, API Key, and API Secret!

### Most recent tracks 🎺

```
{{range lastfmRecentTracks 10}}
- {{.Artist.Name}} - {{.Name}}
{{- end}}
```

This function requires last.fm User, API Key, and API Secret!

## Template Engine

markscribe uses Go's powerful template engine. You can find its documentation
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/caarlos0/env/v6 v6.10.1
github.com/dustin/go-humanize v1.0.1
github.com/mmcdole/gofeed v1.2.1
github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0
github.com/shurcooL/githubv4 v0.0.0-20191127044304-8f68eb5628d0
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f
golang.org/x/oauth2 v0.7.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjY
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0 h1:cgqwZtnR+IQfUYDLJ3Kiy4aE+O/wExTzEIg8xwC4Qfs=
github.com/shkh/lastfm-go v0.0.0-20191215035245-89a801c244e0/go.mod h1:n3nudMl178cEvD44PaopxH9jhJaQzthSxUzLO5iKMy4=
github.com/shurcooL/githubv4 v0.0.0-20191127044304-8f68eb5628d0 h1:T9uus1QvcPgeLShS30YOnnzk3r9Vvygp45muhlrufgY=
github.com/shurcooL/githubv4 v0.0.0-20191127044304-8f68eb5628d0/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo=
github.com/shurcooL/graphql v0.0.0-20181231061246-d48a9a75455f h1:tygelZueB1EtXkPI6mQ4o9DQ0+FKW41hTbunoXZCTqk=
Expand Down
53 changes: 53 additions & 0 deletions lastfm.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package main

import (
"github.com/shkh/lastfm-go/lastfm"
)

func lastfmFavouriteAlbums(count int) interface{} {
params := lastfm.P{
"user": lastfmUser,
"limit": count,
}
albums, err := lastfmClient.User.GetTopAlbums(params)
if err != nil {
panic(err)
}
return albums.Albums
}

func lastfmFavouriteTracks(count int) interface{} {
params := lastfm.P{
"user": lastfmUser,
"limit": count,
}
tracks, err := lastfmClient.User.GetTopTracks(params)
if err != nil {
panic(err)
}
return tracks.Tracks
}

func lastfmFavouriteArtists(count int) interface{} {
params := lastfm.P{
"user": lastfmUser,
"limit": count,
}
artists, err := lastfmClient.User.GetTopArtists(params)
if err != nil {
panic(err)
}
return artists.Artists
}

func lastfmRecentTracks(count int) interface{} {
params := lastfm.P{
"user": lastfmUser,
"limit": count,
}
tracks, err := lastfmClient.User.GetRecentTracks(params)
if err != nil {
panic(err)
}
return tracks.Tracks
}
22 changes: 19 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,21 @@ import (
"time"

"github.com/KyleBanks/goodreads"
"github.com/shkh/lastfm-go/lastfm"
"github.com/shurcooL/githubv4"
"golang.org/x/oauth2"
)

var (
gitHubClient *githubv4.Client
goodReadsClient *goodreads.Client
goodReadsID string
username string
lastfmClient *lastfm.Api

goodReadsID string
username string
lastfmUser string
lastfmAPIKey string
lastfmSecret string

write = flag.String("write", "", "write output to")
)
Expand Down Expand Up @@ -58,6 +64,11 @@ func main() {
"goodReadsCurrentlyReading": goodReadsCurrentlyReading,
/* Literal.club */
"literalClubCurrentlyReading": literalClubCurrentlyReading,
/* last.fm */
"lastfmFavouriteAlbums": lastfmFavouriteAlbums,
"lastfmFavouriteTracks": lastfmFavouriteTracks,
"lastfmFavouriteArtists": lastfmFavouriteArtists,
"lastfmRecentTracks": lastfmRecentTracks,
/* Utils */
"humanize": humanized,
"reverse": reverse,
Expand All @@ -70,10 +81,14 @@ func main() {
os.Exit(1)
}

var httpClient *http.Client
gitHubToken := os.Getenv("GITHUB_TOKEN")
goodReadsToken := os.Getenv("GOODREADS_TOKEN")
goodReadsID = os.Getenv("GOODREADS_USER_ID")
lastfmUser = os.Getenv("LASTFM_USER")
lastfmAPIKey = os.Getenv("LASTFM_API_KEY")
lastfmSecret = os.Getenv("LASTFM_API_SECRET")

var httpClient *http.Client
if len(gitHubToken) > 0 {
httpClient = oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: gitHubToken},
Expand All @@ -82,6 +97,7 @@ func main() {

gitHubClient = githubv4.NewClient(httpClient)
goodReadsClient = goodreads.NewClient(goodReadsToken)
lastfmClient = lastfm.New(lastfmAPIKey, lastfmSecret)

if len(gitHubToken) > 0 {
username, err = getUsername()
Expand Down
28 changes: 28 additions & 0 deletions templates/lastfm.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
### Hi there πŸ‘‹

## Favourite albums of all time 🎢

{{range lastfmFavouriteAlbums 5}}
- {{.Artist.Name}} - {{.Name}}
{{- end}}


## Favourite artists of all time πŸ‘¨β€πŸŽ€

{{range lastfmFavouriteArtists 5}}
- {{.Name}} ({{.PlayCount}})
{{- end}}


## Favourite tracks of all time πŸ’Ώ

{{range lastfmFavouriteTracks 5}}
- {{.Artist.Name}} - {{.Name}} ({{.PlayCount}})
{{- end}}


## Most recent tracks 🎺

{{range lastfmRecentTracks 10}}
- {{.Artist.Name}} - {{.Name}}
{{- end}}