Skip to content

Commit

Permalink
improved error handling, added check to help mitigate full album down…
Browse files Browse the repository at this point in the history
…loads
  • Loading branch information
LumePart committed Apr 13, 2024
1 parent dae56bf commit 0bfabf5
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 20 deletions.
14 changes: 7 additions & 7 deletions listenbrainz.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func getReccs(cfg Listenbrainz) []string {

err := json.Unmarshal(body, &reccs)
if err != nil {
log.Fatalf("Failed to unmarshal body: %v", err)
log.Fatalf("Failed to unmarshal body: %s", err.Error())
}

for _, rec := range reccs.Payload.Mbids {
Expand All @@ -120,7 +120,7 @@ func getTracks(mbids []string) Track {

err := json.Unmarshal(body, &recordings)
if err != nil {
log.Fatalf("Failed to unmarshal body: %v", err)
log.Fatalf("Failed to unmarshal body: %s", err.Error())
}

for _, recording := range recordings {
Expand All @@ -147,7 +147,7 @@ func getWeeklyExploration(cfg Listenbrainz) (string, error) {

err := json.Unmarshal(body, &playlists)
if err != nil {
log.Fatalf("Failed to unmarshal body: %v", err)
log.Fatalf("Failed to unmarshal body: %s", err.Error())
}

for _, playlist := range playlists.Playlist {
Expand All @@ -174,7 +174,7 @@ func parseWeeklyExploration(identifier string) Track {

err := json.Unmarshal(body, &exploration)
if err != nil {
log.Fatalf("Failed to unmarshal body: %v", err)
log.Fatalf("Failed to unmarshal body: %s", err.Error())
}

for _, track := range exploration.Playlist.Tracks {
Expand All @@ -199,17 +199,17 @@ func lbRequest(path string) []byte { // Handle ListenBrainz API requests
reqString := fmt.Sprintf("https://api.listenbrainz.org/1/%s", path)
req, err := http.NewRequest(http.MethodGet, reqString, nil)
if err != nil {
log.Fatalf("Failed to initialize request: %v", err)
log.Fatalf("Failed to initialize request: %s", err.Error())
}

resp, err := client.Do(req)
if err != nil {
log.Fatalf("Failed to make request: %v", err)
log.Fatalf("Failed to make request: %s", err.Error())
}

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Failed to read response body: %v", err)
log.Fatalf("Failed to read response body: %s", err.Error())
}
return body
}
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func main() {
log.Fatal(err.Error())
}
tracks = parseWeeklyExploration(id)
} else { // use reccommendations from API
} else { // use recommendations from API
mbids := getReccs(cfg.Listenbrainz)
tracks = getTracks(mbids)
}
Expand Down
2 changes: 1 addition & 1 deletion subsonic.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func genToken(cfg Subsonic) Subsonic {

_, err := rand.Read(salt[:])
if err != nil {
log.Fatalf("failed to read salt: %v", err)
log.Fatalf("failed to read salt: %s", err.Error())
}

saltStr := base64.StdEncoding.EncodeToString(salt)
Expand Down
27 changes: 16 additions & 11 deletions youtube.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,30 +29,35 @@ type Videos struct {



func queryYT(song string, artist string, cfg Youtube) Videos { // Queries youtube for the song
func queryYT(song, artist, album string, cfg Youtube) Videos { // Queries youtube for the song

var escQuery string
client := http.Client{}
if song == artist || song == album { // append "song" to search query, if album or artist has an self-titled track
escQuery = url.PathEscape(fmt.Sprintf("%s - %s song", song, artist))
} else {
escQuery = url.PathEscape(fmt.Sprintf("%s - %s", song, artist))
}

escQuery := url.PathEscape(fmt.Sprintf("%s - %s", song, artist))
query := fmt.Sprintf("https://youtube.googleapis.com/youtube/v3/search?part=snippet&q=%s&type=video&videoCategoryId=10&key=%s", escQuery, cfg.APIKey)
req, err := http.NewRequest(http.MethodGet, query, nil)
if err != nil {
log.Fatalf("Failed to initialize request: %v", err)
log.Fatalf("Failed to initialize request: %s", err.Error())
}

var videos Videos
resp, err := client.Do(req)
if err != nil {
log.Fatalf("Failed to make request: %v", err)
log.Fatalf("Failed to make request: %s", err.Error())
}

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Fatalf("Failed to read response body: %v", err)
log.Fatalf("Failed to read response body: %s", err.Error())
}
err = json.Unmarshal(body, &videos)
if err != nil {
log.Fatalf("Failed to unmarshal body: %v", err)
log.Fatalf("Failed to unmarshal body: %s", err.Error())
}

return videos
Expand All @@ -63,7 +68,7 @@ func downloadAndFormat(song string, artist string, name string, cfg Youtube) (st

var format youtube.Format

videos := queryYT(song, artist, cfg)
videos := queryYT(song, artist, name, cfg)


yt_client := youtube.Client{}
Expand Down Expand Up @@ -103,21 +108,21 @@ func downloadAndFormat(song string, artist string, name string, cfg Youtube) (st

stream, _, err := yt_client.GetStream(video, &format)
if err != nil {
log.Printf("Failed to get video stream: %v", err)
log.Printf("Failed to get video stream: %s", err.Error())
break
}
defer stream.Close()

input := fmt.Sprintf("%s%s - %s.webm", cfg.DownloadDir,s, a)
file, err := os.Create(input)
if err != nil {
log.Fatalf("Failed to create song file: %v", err)
log.Fatalf("Failed to create song file: %s", err.Error())
}
defer file.Close()

_, err = io.Copy(file, stream)
if err != nil {
log.Printf("Failed to copy stream to file: %v", err)
log.Printf("Failed to copy stream to file: %s", err.Error())
break // If the download fails (downloads a few bytes) then it will get triggered here: "tls: bad record MAC"
}

Expand All @@ -129,7 +134,7 @@ func downloadAndFormat(song string, artist string, name string, cfg Youtube) (st
}).OverWriteOutput().ErrorToStdOut().Run()

if err != nil {
log.Fatalf("Failed to convert file via ffmpeg: %v", err)
log.Fatalf("Failed to convert file via ffmpeg: %s", err.Error())
}

return fmt.Sprintf("%s %s %s", song, artist, name), fmt.Sprintf("%s - %s", s, a)
Expand Down

0 comments on commit 0bfabf5

Please sign in to comment.