aboutsummaryrefslogtreecommitdiffstats
path: root/lfmclient.go
diff options
context:
space:
mode:
Diffstat (limited to 'lfmclient.go')
-rw-r--r--lfmclient.go120
1 files changed, 102 insertions, 18 deletions
diff --git a/lfmclient.go b/lfmclient.go
index 41ecd37..1b39ce9 100644
--- a/lfmclient.go
+++ b/lfmclient.go
@@ -2,10 +2,11 @@ package main
import (
"encoding/json"
- "fmt"
"io/ioutil"
"log"
"net/http"
+ "strconv"
+ "time"
)
type Image struct {
@@ -19,6 +20,7 @@ type AlbumInfo struct {
Name string `json:"title"`
Url string
Position `json:"@attr"`
+ Artist string
}
type Date struct {
@@ -44,11 +46,29 @@ type Position struct {
}
type TrackInfo struct {
- Name string
- Mbid string
- Duration string
- Artist ArtistInfo
- Album AlbumInfo
+ Name string
+ Mbid string
+ Duration string
+ Artist ArtistInfo
+ Album AlbumInfo
+ PlayCount string
+ Listeners string
+}
+
+type RecentTrack struct {
+ Name string
+ Mbid string
+ Url string
+ Artist struct {
+ Name string `json:"#text"`
+ Mbid string
+ }
+ Album struct {
+ Name string `json:"#text"`
+ Mbid string
+ }
+ Images []Image `json:"image"`
+ Date Date
}
func (app *App) LfmQuery(payload map[string]string) []byte {
@@ -66,8 +86,7 @@ func (app *App) LfmQuery(payload map[string]string) []byte {
return body
}
-func (a AlbumInfo) GetImage(size string) string {
- images := a.Images
+func GetImage(images []Image, size string) string {
for _, image := range images {
if image.Size == size {
return image.Href
@@ -99,13 +118,75 @@ func (app *App) TrackInfo(artist, name string) TrackInfo {
return dst["track"]
}
-func (app *App) RecentTracks(user string) {
- r := app.LfmQuery(map[string]string{
+func (app *App) ImportRecentTracks(user *User) {
+ s := &Session{
+ UserId: user.Id,
+ Client: "import",
+ ClientVersion: "",
+ Key: randomToken(16),
+ Protocol: "1.2.1",
+ }
+ app.PutSession(s)
+ i := app.NewImport(user.LfmName)
+ payload := map[string]string{
"method": "user.getRecentTracks",
- "limit": "10",
- "user": user,
- })
- fmt.Println(string(r))
+ "limit": "200",
+ "user": user.LfmName,
+ "to": strconv.Itoa(int(i.To.Unix())),
+ }
+ if i.From.IsZero() {
+ payload["from"] = "0"
+ } else {
+ payload["from"] = strconv.Itoa(int(i.From.Unix()))
+ }
+ var dst map[string]struct {
+ Attrs PageAttrs `json:"@attr"`
+ Tracks []RecentTrack `json:"track"`
+ }
+ scrobbles := make([]Scrobble, 0, 200)
+ var st time.Time
+ for {
+ scrobbles = scrobbles[:0]
+ if !i.LastFetch.IsZero() {
+ payload["to"] = strconv.Itoa(int(i.LastFetch.Unix()))
+ }
+ r := app.LfmQuery(payload)
+ json.Unmarshal(r, &dst)
+ tracks := dst["recenttracks"].Tracks
+ if len(tracks) == 0 {
+ i.Done = true
+ break
+ }
+ for _, t := range tracks {
+ ts, _ := strconv.Atoi(t.Date.Uts)
+ if ts == 0 {
+ continue
+ }
+ st = time.Unix(int64(ts), 0)
+ scrobble := Scrobble{
+ Artist: NewCorrectable(t.Artist.Name),
+ Album: NewCorrectable(t.Album.Name),
+ TrackName: NewCorrectable(t.Name),
+ Mbid: t.Mbid,
+ Time: st,
+ UserId: user.Id,
+ SessionKey: s.Key,
+ Image: GetImage(t.Images, "medium"),
+ }
+ app.GetScrobbleSong(&scrobble)
+ scrobbles = append(scrobbles, scrobble)
+ }
+ if err := app.PutScrobbles(scrobbles); err != nil {
+ log.Println(err)
+ }
+ i.LastFetch = st
+ if err := app.SaveImport(i); err != nil {
+ log.Println(err)
+ }
+ }
+ if err := app.SaveImport(i); err != nil {
+ log.Println(err)
+ }
}
func (app *App) LovedTracks(user string) []TrackInfo {
@@ -125,10 +206,11 @@ func (app *App) LovedTracks(user string) []TrackInfo {
}
func (app *App) GetSong(s *Song) {
- if s.Album != "" {
+ if s.Album != "" || s.Mbid != "" {
if app.GetSongId(s) != nil {
if s.Image == "" {
- s.Image = app.AlbumInfo(s.Artist, s.Album).GetImage("medium")
+ s.Image = GetImage(app.AlbumInfo(s.Artist, s.Album).Images,
+ "medium")
}
if err := app.InsertSong(s); err != nil {
log.Println(err)
@@ -138,7 +220,9 @@ func (app *App) GetSong(s *Song) {
t := app.TrackInfo(s.Artist, s.Name)
s.Album = t.Album.Name
s.Mbid = t.Mbid
- s.Image = t.Album.GetImage("medium")
- app.InsertSong(s)
+ s.Image = GetImage(t.Album.Images, "medium")
+ if app.GetSongId(s) != nil {
+ app.InsertSong(s)
+ }
}
}