podkasto

Podkasto is a CLI podcast search tool built on podcastindex.org
git clone git://git.swab.dev/podkasto.git
Log | Files | Refs | README | LICENSE

commit 27c0069aa5d1095901acc392d778521d6b1b2f7f
parent 20f4be790de8b0574d20a6243bdd8866cfcfdad7
Author: Jaron Swab <jaron@swab.dev>
Date:   Mon, 26 Apr 2021 21:21:20 -0400

removed "api" package

Diffstat:
Acommon.go | 53+++++++++++++++++++++++++++++++++++++++++++++++++++++
Aconfig.go | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Dconfig/congif.go | 55-------------------------------------------------------
Dgo.mod | 3---
Mmain.go | 4++--
Drequest/request.go | 43-------------------------------------------
Dsearch/common.go | 59-----------------------------------------------------------
Dsearch/podcasts.go | 26--------------------------
Dsearch/search.go | 48------------------------------------------------
9 files changed, 110 insertions(+), 236 deletions(-)

diff --git a/common.go b/common.go @@ -0,0 +1,53 @@ +package main + +// Response contains the unmarshaled data from a successful query to PodcastIndex.org +type Search struct { + Status string `json:"status"` + Query string `json:"query"` + Feeds []Feed `json:"feeds"` + Description string `json:"description"` + Count int `json:"count"` +} + +type Podcast struct { + Status string `json:"status"` + Query Query `json:"query"` + Feed Feed `json:"feed"` + Description string `json:"description"` + Count int `json:"count"` +} + +type Query struct { + ID string `json:"id"` +} + +// Feed contains the unmarshaled data about a returned feed from PodcastingIndex.org +type Feed struct { + ID int `json:"id"` + Title string `json:"title"` + URL string `json:"url"` + Originalurl string `json:"originalUrl"` + Link string `json:"link"` + Description string `json:"description"` + Author string `json:"author"` + Ownername string `json:"ownerName"` + Image string `json:"image"` + Artwork string `json:"artwork"` + Lastupdatetime int `json:"lastUpdateTime"` + Lastcrawltime int `json:"lastCrawlTime"` + Lastparsetime int `json:"lastParseTime"` + Lastgoodhttpstatustime int `json:"lastGoodHttpStatusTime"` + Lasthttpstatus int `json:"lastHttpStatus"` + Contenttype string `json:"contentType"` + Itunesid int `json:"itunesId"` + Generator string `json:"generator"` + Language string `json:"language"` + Type int `json:"type"` + Dead int `json:"dead"` + Chash string `json:"chash"` + EpisodeCount int `json:"episodeCount"` + Crawlerrors int `json:"crawlErrors"` + Parseerrors int `json:"parseErrors"` + Locked int `json:"locked"` + Imageurlhash int `json:"imageUrlHash"` +} diff --git a/config.go b/config.go @@ -0,0 +1,55 @@ +package main + +import ( + "fmt" + "log" + "os" + "strings" +) + +type Config struct { + ApiKey string + ApiSecret string +} + +func (c *Config) Load() { + var ( + err error + configBytes []byte + ) + + configDir := os.Getenv("XDG_CONFIG_HOME") + if configDir != "" { + configBytes, err = os.ReadFile(fmt.Sprintf("%s/podkasto.conf", configDir)) + if err != nil { + log.Fatalf("could not load $XDG_CONFIG_HOME/podkasto.conf: %s", err) + } + } + + if configDir == "" { + configDir = os.Getenv("HOME") + + configBytes, err = os.ReadFile(fmt.Sprintf("%s/.podkasto.conf", configDir)) + if err != nil { + log.Fatalf("could not load $HOME/.podkasto.conf: %s", err) + } + } + + configSettings := strings.Split(string(configBytes), "\n") + + for _, line := range configSettings { + lineData := strings.Split(line, " ") + + if len(lineData) <= 1 { + continue + } + + switch lineData[0] { + case "apiKey": + c.ApiKey = lineData[1] + case "apiSecret": + c.ApiSecret = lineData[1] + + } + } +} diff --git a/config/congif.go b/config/congif.go @@ -1,55 +0,0 @@ -package config - -import ( - "fmt" - "log" - "os" - "strings" -) - -type Config struct { - ApiKey string - ApiSecret string -} - -func (c *Config) Load() { - var ( - err error - configBytes []byte - ) - - configDir := os.Getenv("XDG_CONFIG_HOME") - if configDir != "" { - configBytes, err = os.ReadFile(fmt.Sprintf("%s/podkasto.conf", configDir)) - if err != nil { - log.Fatalf("could not load $XDG_CONFIG_HOME/podkasto.conf: %s", err) - } - } - - if configDir == "" { - configDir = os.Getenv("HOME") - - configBytes, err = os.ReadFile(fmt.Sprintf("%s/.podkasto.conf", configDir)) - if err != nil { - log.Fatalf("could not load $HOME/.podkasto.conf: %s", err) - } - } - - configSettings := strings.Split(string(configBytes), "\n") - - for _, line := range configSettings { - lineData := strings.Split(line, " ") - - if len(lineData) <= 1 { - continue - } - - switch lineData[0] { - case "apiKey": - c.ApiKey = lineData[1] - case "apiSecret": - c.ApiSecret = lineData[1] - - } - } -} diff --git a/go.mod b/go.mod @@ -1,3 +0,0 @@ -module github.com/jrswab/podkasto - -go 1.16 diff --git a/main.go b/main.go @@ -3,8 +3,8 @@ package main import ( "flag" - "github.com/jrswab/podkasto/config" - "github.com/jrswab/podkasto/search" + "github.com/jrswab/podkasto-api/search" + "github.com/jrswab/podkasto-cli/config" ) func main() { diff --git a/request/request.go b/request/request.go @@ -1,43 +0,0 @@ -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/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 @@ -1,59 +0,0 @@ -package search - -// API contains the API Key and Secret for sending requests to PodcastIndex.org -type API struct { - ApiKey string - ApiSecret string -} - -// Response contains the unmarshaled data from a successful query to PodcastIndex.org -type Search struct { - Status string `json:"status"` - Query string `json:"query"` - Feeds []Feed `json:"feeds"` - Description string `json:"description"` - Count int `json:"count"` -} - -type Podcasts struct { - Status string `json:"status"` - Query Query `json:"query"` - Feed Feed `json:"feed"` - Description string `json:"description"` - Count int `json:"count"` -} - -type Query struct { - ID string `json:"id"` -} - -// Feed contains the unmarshaled data about a returned feed from PodcastingIndex.org -type Feed struct { - ID int `json:"id"` - Title string `json:"title"` - URL string `json:"url"` - Originalurl string `json:"originalUrl"` - Link string `json:"link"` - Description string `json:"description"` - Author string `json:"author"` - Ownername string `json:"ownerName"` - Image string `json:"image"` - Artwork string `json:"artwork"` - Lastupdatetime int `json:"lastUpdateTime"` - Lastcrawltime int `json:"lastCrawlTime"` - Lastparsetime int `json:"lastParseTime"` - Lastgoodhttpstatustime int `json:"lastGoodHttpStatusTime"` - Lasthttpstatus int `json:"lastHttpStatus"` - Contenttype string `json:"contentType"` - Itunesid int `json:"itunesId"` - Generator string `json:"generator"` - Language string `json:"language"` - Type int `json:"type"` - Dead int `json:"dead"` - Chash string `json:"chash"` - EpisodeCount int `json:"episodeCount"` - Crawlerrors int `json:"crawlErrors"` - Parseerrors int `json:"parseErrors"` - Locked int `json:"locked"` - Imageurlhash int `json:"imageUrlHash"` -} diff --git a/search/podcasts.go b/search/podcasts.go @@ -1,26 +0,0 @@ -package search - -import ( - "encoding/json" - "fmt" - "log" - - "github.com/jrswab/podkasto/request" -) - -func (q *API) ByFeedID(id string) { - searchQuery := fmt.Sprintf("podcasts/byfeedid?id=%s&pretty", id) - - body, err := request.Send(searchQuery, q.ApiKey, q.ApiSecret) - if err != nil { - log.Fatalf("query \"ByFeedID\" failed: %s", err) - } - - resp := new(Podcasts) - err = json.Unmarshal(body, &resp) - if err != nil { - log.Fatalf("query \"ByFeedID\" failed: %s", err) - } - - fmt.Printf("%+v\n", resp.Feed) -} diff --git a/search/search.go b/search/search.go @@ -1,48 +0,0 @@ -package search - -import ( - "encoding/json" - "fmt" - "log" - - "github.com/jrswab/podkasto/request" -) - -func (q *API) ByTerm(term string) { - //searchQuery := fmt.Sprintf("%sbyterm?q=%s&pretty=%s", searchBase, query, isPretty) - searchQuery := fmt.Sprintf("search/byterm?q=%s", term) - - body, err := request.Send(searchQuery, q.ApiKey, q.ApiSecret) - if err != nil { - log.Fatal(err) - } - - response := new(Search) - err = json.Unmarshal(body, &response) - if err != nil { - log.Fatalf("query \"ByTerm\" failed: %s", err) - } - - for _, feed := range response.Feeds { - fmt.Printf("%+v\n\n", feed) - } -} - -func (q *API) ByPerson(name string) { - searchQuery := fmt.Sprintf("search/byperson?q=%s", name) - - body, err := request.Send(searchQuery, q.ApiKey, q.ApiSecret) - if err != nil { - log.Fatal(err) - } - - response := new(Search) - err = json.Unmarshal(body, &response) - if err != nil { - log.Fatalf("query \"ByPerson\" failed: %s", err) - } - - for _, feed := range response.Feeds { - fmt.Printf("%+v\n\n", feed) - } -}