[WIP] music platform user data scraper
teal-fm atproto
at main 2.7 kB view raw
1package lastfm 2 3import ( 4 "encoding/json" 5 "strconv" 6 "time" 7) 8 9// Structs to represent the Last.fm API response for user.getrecenttracks 10type RecentTracksResponse struct { 11 RecentTracks RecentTracks `json:"recenttracks"` 12} 13 14type RecentTracks struct { 15 Tracks []Track `json:"track"` 16 Attr TrackXMLAttr `json:"@attr"` 17} 18 19type Track struct { 20 Artist Artist `json:"artist"` 21 Streamable string `json:"streamable"` // Typically "0" or "1" 22 Image []Image `json:"image"` 23 MBID string `json:"mbid"` // MusicBrainz ID for the track 24 Album Album `json:"album"` 25 Name string `json:"name"` 26 URL string `json:"url"` 27 Date *TrackDate `json:"date,omitempty"` // Use pointer for optional fields 28 Attr *struct { // Custom handling for @attr.nowplaying 29 NowPlaying string `json:"nowplaying"` // Field name corrected to match struct tag 30 } `json:"@attr,omitempty"` // This captures the @attr object within the track 31} 32 33type Artist struct { 34 MBID string `json:"mbid"` // MusicBrainz ID for the artist 35 Text string `json:"#text"` 36} 37 38type Image struct { 39 Size string `json:"size"` // "small", "medium", "large", "extralarge" 40 Text string `json:"#text"` // URL of the image 41} 42 43type Album struct { 44 MBID string `json:"mbid"` // MusicBrainz ID for the album 45 Text string `json:"#text"` // Album name 46} 47 48// ApiTrackDate This is the real structure returned from lastFM. 49// Represents a date associated with a track, including both a Unix timestamp and a human-readable string. 50// UTS is a Unix timestamp stored as a string. 51// Text contains the human-readable date string. 52type ApiTrackDate struct { 53 UTS string `json:"uts"` // Unix timestamp string 54 Text string `json:"#text"` // Human-readable date string 55} 56 57// TrackDate This is the struct we use to represent a date associated with a track. 58// It is a wrapper around time.Time that implements json.Unmarshaler. 59type TrackDate struct { 60 time.Time 61} 62 63// UnmarshalJSON Implements json.Unmarshaler. 64// Parses the UTS field from the API response and converts it to a time.Time. 65// The time.Time is stored in the Time field. 66// The Text field is ignored since it can be parsed from the Time field if needed. 67func (t *TrackDate) UnmarshalJSON(b []byte) (err error) { 68 var apiTrackDate ApiTrackDate 69 if err := json.Unmarshal(b, &apiTrackDate); err != nil { 70 return err 71 } 72 uts, err := strconv.ParseInt(apiTrackDate.UTS, 10, 64) 73 if err != nil { 74 return err 75 } 76 date := time.Unix(uts, 0).UTC() 77 t.Time = date 78 return 79} 80 81type TrackXMLAttr struct { 82 User string `json:"user"` 83 TotalPages string `json:"totalPages"` 84 Page string `json:"page"` 85 PerPage string `json:"perPage"` 86 Total string `json:"total"` 87}