podkasto-api

A Go package to interact with PodcastIndex.org
git clone git://git.swab.dev/podkasto-api.git
Log | Files | Refs | README | LICENSE

commit 839713891118956ea251b13350bbd0e8f3096d7f
parent 5fac1f15907c70adfc280aba992937dc3da9d9a5
Author: Jaron Swab <jaron@swab.dev>
Date:   Mon, 26 Apr 2021 21:01:51 -0400

initial commit

Diffstat:
Arequest/request.go | 43+++++++++++++++++++++++++++++++++++++++++++
Asearch/common.go | 9+++++++++
Asearch/podcasts.go | 46++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 98 insertions(+), 0 deletions(-)

diff --git a/request/request.go b/request/request.go @@ -0,0 +1,43 @@ +package request + +import ( + "crypto/sha1" + "fmt" + "io/ioutil" + "net/http" + "strconv" + "time" +) + +func Send(searchQuery, apiKey, apiSecret string) ([]byte, error) { + url := fmt.Sprintf("https://api.podcastindex.org/api/1.0/%s", searchQuery) + req, err := http.NewRequest(http.MethodGet, url, nil) + if err != nil { + return nil, err + } + + req.Header.Set("X-Auth-Key", apiKey) + req.Header.Set("User-Agent", "Podkasto-API/0.0.1") + + headerTime := strconv.FormatInt(time.Now().Unix(), 10) + req.Header.Set("X-Auth-Date", headerTime) + + h := sha1.New() + h.Write([]byte(fmt.Sprintf("%s%s%s", apiKey, apiSecret, headerTime))) + req.Header.Set("Authorization", fmt.Sprintf("%x", h.Sum(nil))) + + client := http.Client{Timeout: time.Second * 33} + + res, err := client.Do(req) + if err != nil { + return nil, err + } + + body, err := ioutil.ReadAll(res.Body) + defer res.Body.Close() + if err != nil { + return nil, err + } + + return body, nil +} diff --git a/search/common.go b/search/common.go @@ -0,0 +1,9 @@ +package search + +func getData(query string) string { + body, err := request.Send(searchQuery, q.ApiKey, q.ApiSecret) + if err != nil { + return body, fmt.Errorf("getData() failed: %s", err) + } + +} diff --git a/search/podcasts.go b/search/podcasts.go @@ -0,0 +1,46 @@ +package search + +import ( + "fmt" + "net/url" +) + +// ByTerm returns a slice of podcasts containing the term passed in. +func (q *API) ByTerm(term string) (string, error) { + return getData(url.QueryEscape(fmt.Sprintf("search/byterm?q=%s", term))) +} + +// ByPerson returs a slice of podcasts by a person's name. +func (q *API) ByPerson(name string) (string, error) { + return getData(url.QueryEscape(fmt.Sprintf("search/byperson?q=%s", name))) +} + +// ByFeedID returns details about a podcast by Podcast Index ID. +func (q *API) ByFeedID(id string) (string, error) { + return getData(url.QueryEscape(fmt.Sprintf("podcasts/byfeedid?id=%s", id))) +} + +// ByItunesID returns details about a podcast by Apple iTunes ID. +func (q *API) ByItunesID(URL string) (string, error) { + return getData(url.QueryEscape(fmt.Sprintf("podcasts/byitunesid?id=%s", id))) +} + +// ByFeedURL returns details about a podcast by URL. +func (q *API) ByFeedURL(URL string) (string, error) { + return getData(url.QueryEscape(fmt.Sprintf("podcasts/byfeedurl?url=%s", URL))) +} + +// ByTag returns all feeds that support the specified podcast namespace tag. +func (q *API) ByTag() (string, err) { + return getData(url.QueryEscape(fmt.Sprintf("podcasts/bytag?podcast-value"))) +} + +// Trending returns the podcasts/feeds that in the index that are trending. +func (q *API) Trending() (string, err) { + return getData(url.QueryEscape(fmt.Sprintf("podcasts/trending"))) +} + +// Dead returns all feeds that have been marked dead. +func (q *API) Dead() (string, err) { + return getData(url.QueryEscape(fmt.Sprintf("podcasts/dead"))) +}