A way to send current playing track in cider to teal collection

Now using recordings and ISRC if possible

Changed files
+128 -83
types
+22 -17
main.go
··· 18 18 "github.com/teal-fm/piper/api/teal" 19 19 ) 20 20 21 - const AGENT string = "teal-cider/0.0.1" 21 + const AGENT string = "teal-cider/0.0.2" 22 22 23 23 var ENV_DATA envData = envData{} 24 24 ··· 52 52 return r, nil 53 53 } 54 54 55 - func getInfos(song types.NowPlaying) (types.MBRecord, error) { 56 - r := types.MBRecord{} 57 - 58 - req, err := http.NewRequest("GET", "https://musicbrainz.org/ws/2/release", nil) 55 + func getMbRecord(query string) (types.MBRecord, error) { 56 + record := types.MBRecord{} 57 + req, err := http.NewRequest("GET", "https://musicbrainz.org/ws/2/recording", nil) 59 58 if err != nil { 60 - return r, err 59 + return record, err 61 60 } 62 61 63 - query := fmt.Sprintf("recording:\"%s\" AND artist:\"%s\" AND date:\"%s\"", song.Info.Name, song.Info.ArtistName, song.Info.ReleaseDate) 64 - 65 62 req.Header.Add("User-Agent", AGENT) 66 63 req.Header.Add("content-type", "false") 67 64 68 65 queryParam := req.URL.Query() 66 + queryParam.Add("fmt", "json") 69 67 queryParam.Add("query", query) 70 - queryParam.Add("fmt", "json") 71 68 req.URL.RawQuery = queryParam.Encode() 72 69 73 70 res, err := http.DefaultClient.Do(req) 74 71 if err != nil { 75 - return r, err 72 + return record, err 76 73 } 77 74 78 - record := types.MBRecord{} 79 75 err = json.NewDecoder(res.Body).Decode(&record) 80 - if err != nil { 81 - return r, err 76 + return record, err 77 + } 78 + 79 + func getInfos(song types.NowPlaying) (types.MBRecord, error) { 80 + isrc := song.Info.Isrc[len(song.Info.Isrc)-12:] 81 + query := fmt.Sprintf("isrc:\"%s\"", isrc) 82 + r, err := getMbRecord(query) 83 + if err == nil && r.Count > 0 { 84 + return r, nil 82 85 } 83 86 84 - return record, nil 87 + query = fmt.Sprintf("recording:\"%s\" AND artist:\"%s\" AND date:\"%s\"", song.Info.Name, song.Info.ArtistName, song.Info.ReleaseDate) 88 + r, err = getMbRecord(query) 89 + return r, err 85 90 } 86 91 87 92 func recordToTeal(records types.MBRecord, s types.NowPlaying) teal.AlphaFeedPlay { ··· 103 108 SubmissionClientAgent: &dupAgent, 104 109 } 105 110 } else { 106 - r := records.Releases[0] 111 + r := records.Recordings[0] 107 112 108 113 artists := make([]teal.AlphaFeedDefs_Artist, 0) 109 114 for _, a := range r.ArtistCredit { ··· 122 127 Artists: artistsRef, 123 128 TrackName: r.Title, 124 129 TrackMbId: &r.ID, 125 - ReleaseName: &r.ReleaseGroup.Title, 126 - ReleaseMbId: &r.ReleaseGroup.ID, 130 + ReleaseName: &r.Releases[0].Title, 131 + ReleaseMbId: &r.Releases[0].ID, 127 132 Duration: &duration, 128 133 PlayedTime: &now, 129 134 SubmissionClientAgent: &dupAgent,
+106 -66
types/musicbrainz.go
··· 1 1 package types 2 2 3 - import "time" 3 + import ( 4 + "encoding/json" 5 + "time" 6 + ) 7 + 8 + func UnmarshalWelcome(data []byte) (MBRecord, error) { 9 + var r MBRecord 10 + err := json.Unmarshal(data, &r) 11 + return r, err 12 + } 13 + 14 + func (r *MBRecord) Marshal() ([]byte, error) { 15 + return json.Marshal(r) 16 + } 4 17 5 18 type MBRecord struct { 6 - Created time.Time `json:"created"` 7 - Count int `json:"count"` 8 - Offset int `json:"offset"` 9 - Releases []struct { 10 - ID string `json:"id"` 11 - Score int `json:"score"` 12 - StatusID string `json:"status-id"` 13 - PackagingID string `json:"packaging-id,omitempty"` 14 - ArtistCreditID string `json:"artist-credit-id"` 15 - Count int `json:"count"` 16 - Title string `json:"title"` 17 - Status string `json:"status"` 18 - Packaging string `json:"packaging,omitempty"` 19 - TextRepresentation struct { 20 - Language string `json:"language"` 21 - Script string `json:"script"` 22 - } `json:"text-representation,omitempty"` 23 - ArtistCredit []struct { 24 - Name string `json:"name"` 25 - Artist struct { 26 - ID string `json:"id"` 27 - Name string `json:"name"` 28 - SortName string `json:"sort-name"` 29 - Disambiguation string `json:"disambiguation"` 30 - } `json:"artist"` 31 - } `json:"artist-credit"` 32 - ReleaseGroup struct { 33 - ID string `json:"id"` 34 - TypeID string `json:"type-id"` 35 - PrimaryTypeID string `json:"primary-type-id"` 36 - Title string `json:"title"` 37 - PrimaryType string `json:"primary-type"` 38 - } `json:"release-group"` 39 - Date string `json:"date"` 40 - Country string `json:"country"` 41 - ReleaseEvents []struct { 42 - Date string `json:"date"` 43 - Area struct { 44 - ID string `json:"id"` 45 - Name string `json:"name"` 46 - SortName string `json:"sort-name"` 47 - Iso31661Codes []string `json:"iso-3166-1-codes"` 48 - } `json:"area"` 49 - } `json:"release-events"` 50 - LabelInfo []struct { 51 - CatalogNumber string `json:"catalog-number"` 52 - Label struct { 53 - ID string `json:"id"` 54 - Name string `json:"name"` 55 - } `json:"label"` 56 - } `json:"label-info"` 57 - TrackCount int `json:"track-count"` 58 - Media []struct { 59 - ID string `json:"id"` 60 - Format string `json:"format"` 61 - DiscCount int `json:"disc-count"` 62 - TrackCount int `json:"track-count"` 63 - } `json:"media"` 64 - Tags []struct { 65 - Count int `json:"count"` 66 - Name string `json:"name"` 67 - } `json:"tags,omitempty"` 68 - Asin string `json:"asin,omitempty"` 69 - Barcode string `json:"barcode,omitempty"` 70 - } `json:"releases"` 19 + Created time.Time `json:"created"` 20 + Count int64 `json:"count"` 21 + Offset int64 `json:"offset"` 22 + Recordings []Recording `json:"recordings"` 23 + } 24 + 25 + type Recording struct { 26 + ID string `json:"id"` 27 + Score int64 `json:"score"` 28 + ArtistCreditID string `json:"artist-credit-id"` 29 + Title string `json:"title"` 30 + Length int64 `json:"length"` 31 + Disambiguation *string `json:"disambiguation,omitempty"` 32 + Video interface{} `json:"video"` 33 + ArtistCredit []ArtistCredit `json:"artist-credit"` 34 + FirstReleaseDate string `json:"first-release-date"` 35 + Releases []Release `json:"releases"` 36 + Isrcs []string `json:"isrcs,omitempty"` 37 + } 38 + 39 + type ArtistCredit struct { 40 + Joinphrase *string `json:"joinphrase,omitempty"` 41 + Name string `json:"name"` 42 + Artist Artist `json:"artist"` 43 + } 44 + 45 + type Artist struct { 46 + ID string `json:"id"` 47 + Name string `json:"name"` 48 + SortName string `json:"sort-name"` 49 + Disambiguation *string `json:"disambiguation,omitempty"` 50 + Aliases []Alias `json:"aliases,omitempty"` 51 + ISO31661Codes []string `json:"iso-3166-1-codes,omitempty"` 52 + } 53 + 54 + type Alias struct { 55 + SortName string `json:"sort-name"` 56 + TypeID *string `json:"type-id,omitempty"` 57 + Name string `json:"name"` 58 + Locale *string `json:"locale"` 59 + Type *string `json:"type"` 60 + Primary *bool `json:"primary"` 61 + BeginDate interface{} `json:"begin-date"` 62 + EndDate interface{} `json:"end-date"` 63 + } 64 + 65 + type Release struct { 66 + ID string `json:"id"` 67 + StatusID *string `json:"status-id,omitempty"` 68 + ArtistCreditID string `json:"artist-credit-id"` 69 + Count int64 `json:"count"` 70 + Title string `json:"title"` 71 + Status *string `json:"status,omitempty"` 72 + Disambiguation *string `json:"disambiguation,omitempty"` 73 + ArtistCredit []ArtistCredit `json:"artist-credit"` 74 + ReleaseGroup ReleaseGroup `json:"release-group"` 75 + Date *string `json:"date,omitempty"` 76 + Country *string `json:"country,omitempty"` 77 + ReleaseEvents []ReleaseEvent `json:"release-events,omitempty"` 78 + TrackCount int64 `json:"track-count"` 79 + Media []Media `json:"media"` 80 + } 81 + 82 + type Media struct { 83 + ID string `json:"id"` 84 + Position int64 `json:"position"` 85 + Format string `json:"format"` 86 + Track []Track `json:"track"` 87 + TrackCount int64 `json:"track-count"` 88 + TrackOffset int64 `json:"track-offset"` 89 + } 90 + 91 + type Track struct { 92 + ID string `json:"id"` 93 + Number string `json:"number"` 94 + Title string `json:"title"` 95 + Length *int64 `json:"length,omitempty"` 96 + } 97 + 98 + type ReleaseEvent struct { 99 + Date string `json:"date"` 100 + Area Artist `json:"area"` 101 + } 102 + 103 + type ReleaseGroup struct { 104 + ID string `json:"id"` 105 + TypeID string `json:"type-id"` 106 + PrimaryTypeID string `json:"primary-type-id"` 107 + Title string `json:"title"` 108 + PrimaryType string `json:"primary-type"` 109 + SecondaryTypes []string `json:"secondary-types,omitempty"` 110 + SecondaryTypeIDS []string `json:"secondary-type-ids,omitempty"` 71 111 }