From 666dbb5cf21ad85d1690bd4f1fe411019d0babef Mon Sep 17 00:00:00 2001 From: fedy95 Date: Wed, 17 Feb 2021 20:41:28 +0300 Subject: [PATCH] add radio-t integration --- app/main.go | 71 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 64 insertions(+), 7 deletions(-) diff --git a/app/main.go b/app/main.go index 22feca4..888af99 100644 --- a/app/main.go +++ b/app/main.go @@ -1,15 +1,72 @@ package main import ( - "fmt" - "log" - "net/http" + "encoding/json" + "fmt" + "io/ioutil" + "log" + "net/http" + "time" ) + +type Podcasts struct { + URL string `json:"url"` + Title string `json:"title"` + Date time.Time `json:"date"` + Categories []string `json:"categories"` + Image string `json:"image,omitempty"` + FileName string `json:"file_name,omitempty"` + Body string `json:"body"` + ShowNotes string `json:"show_notes,omitempty"` + AudioUrl string `json:"audio_url"` + TimeLabels []TimeLabels + ShowNum int `json:"show_num,omitempty"` +} + +type TimeLabels struct { + Topic string `json:"topic,omitempty"` + Time time.Time `json:"time,omitempty"` + Duration int `json:"duration,omitempty"` +} + + func main() { - http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { - fmt.Fprintf(w, "Go app is worked!") - }) + var lines []string + fmt.Println(getRadioTThemes(1)) + for _, podcast := range getRadioTThemes(5) { + lines = append(lines, fmt.Sprintf("[%s](%s) %s\n %s\n\n", + podcast.Title, + podcast.URL, + podcast.Date.Format("2006-01-02"), + podcast.Body, + )) + } + + http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintf(w, "%s", lines) + }) + + log.Fatal(http.ListenAndServe(":7070", nil)) +} + +func getRadioTThemes(numPodcasts int) []Podcasts { + var url string + url = fmt.Sprintf("https://radio-t.com/site-api/last/%d?categories=podcast", numPodcasts) + response, err := http.Get(url) + + if err != nil { + panic(err.Error()) + } + + body, err := ioutil.ReadAll(response.Body) + + if err != nil { + panic(err.Error()) + } + + var podcasts []Podcasts + json.Unmarshal(body, &podcasts) - log.Fatal(http.ListenAndServe(":7070", nil)) + return podcasts }