✨ Fetch data from MusicBrainz
This commit is contained in:
		
					parent
					
						
							
								c9498340d0
							
						
					
				
			
			
				commit
				
					
						1e78a95e8a
					
				
			
		
					 3 changed files with 89 additions and 5 deletions
				
			
		
							
								
								
									
										12
									
								
								app/run.go
									
										
									
									
									
								
							
							
						
						
									
										12
									
								
								app/run.go
									
										
									
									
									
								
							|  | @ -38,8 +38,18 @@ func testFp() { | ||||||
| 	quit() | 	quit() | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
|  | func testMb() { | ||||||
|  | 	id := "497f2f22-809b-4c9e-a692-72f7d8dcaaa2" | ||||||
|  | 	mb, err := media.GetMbRecording(id) | ||||||
|  | 	if err != nil { | ||||||
|  | 		panic(err) | ||||||
|  | 	} | ||||||
|  | 	fmt.Printf("%+v\n", mb) | ||||||
|  | 	quit() | ||||||
|  | } | ||||||
|  | 
 | ||||||
| func Run(step AppStep) { | func Run(step AppStep) { | ||||||
| 	testFp() | 	testMb() | ||||||
| 	for step < Quit { | 	for step < Quit { | ||||||
| 		switch step { | 		switch step { | ||||||
| 		case Pick: | 		case Pick: | ||||||
|  |  | ||||||
							
								
								
									
										78
									
								
								media/brainz.go
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										78
									
								
								media/brainz.go
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,78 @@ | ||||||
|  | package media | ||||||
|  | 
 | ||||||
|  | import ( | ||||||
|  | 	"encoding/json" | ||||||
|  | 	"fmt" | ||||||
|  | 	"io" | ||||||
|  | 	"net/http" | ||||||
|  | 	//"codeberg.org/danjones000/strip-beats/config" | ||||||
|  | ) | ||||||
|  | 
 | ||||||
|  | type MbRecording struct { | ||||||
|  | 	Id               string | ||||||
|  | 	FirstReleaseDate string `json:"first-release-date"` | ||||||
|  | 	Length           int | ||||||
|  | 	Title            string | ||||||
|  | 	Video            bool | ||||||
|  | 	Releases         []MbRelease | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type MbRelease struct { | ||||||
|  | 	Id       string | ||||||
|  | 	Country  string | ||||||
|  | 	Date     string | ||||||
|  | 	Media    []MbMedia | ||||||
|  | 	Status   string | ||||||
|  | 	StatusId string `json:"status-id"` | ||||||
|  | 	Title    string | ||||||
|  | 	// ReleaseEvents []MbReleaseEvent `json:"release-events"` | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type MbMedia struct { | ||||||
|  | 	FormatId    string `json:"format-id"` | ||||||
|  | 	Position    int | ||||||
|  | 	TrackOffset int `json:"track-offset"` | ||||||
|  | 	Format      string | ||||||
|  | 	TrackCount  int `json:"track-count"` | ||||||
|  | 	Tracks      []MbTrack | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | type MbTrack struct { | ||||||
|  | 	Id       string | ||||||
|  | 	Number   string | ||||||
|  | 	Title    string | ||||||
|  | 	Position int | ||||||
|  | 	Length   int | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func GetMbRecording(id string) (MbRecording, error) { | ||||||
|  | 	rec := MbRecording{Id: id} | ||||||
|  | 	err := FillMbRecording(&rec) | ||||||
|  | 	return rec, err | ||||||
|  | } | ||||||
|  | 
 | ||||||
|  | func FillMbRecording(rec *MbRecording) error { | ||||||
|  | 	url := fmt.Sprintf("https://musicbrainz.org/ws/2/recording/%s", rec.Id) | ||||||
|  | 	req, err := http.NewRequest(http.MethodGet, url, nil) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	q := req.URL.Query() | ||||||
|  | 	q.Add("fmt", "json") | ||||||
|  | 	q.Add("inc", "releases+media") | ||||||
|  | 	req.URL.RawQuery = q.Encode() | ||||||
|  | 	req.Header.Set("User-Agent", "strip-beats/0.1.0 (https://codeberg.org/danjones000/strip-beats/)") | ||||||
|  | 
 | ||||||
|  | 	resp, err := http.DefaultClient.Do(req) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	defer resp.Body.Close() | ||||||
|  | 	body, err := io.ReadAll(resp.Body) | ||||||
|  | 	if err != nil { | ||||||
|  | 		return err | ||||||
|  | 	} | ||||||
|  | 	err = json.Unmarshal(body, rec) | ||||||
|  | 
 | ||||||
|  | 	return err | ||||||
|  | } | ||||||
|  | @ -54,10 +54,6 @@ type IdError struct { | ||||||
| 	Message string | 	Message string | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type MbRecording struct { |  | ||||||
| 	Id string |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| func LookupFingerprint(print FPrint) (IdResults, error) { | func LookupFingerprint(print FPrint) (IdResults, error) { | ||||||
| 	res := IdResults{} | 	res := IdResults{} | ||||||
| 	key := config.GetConfig().AcousticIdKey | 	key := config.GetConfig().AcousticIdKey | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue