podkasto

Unnamed repository; edit this file 'description' to name the repository.
git clone git://git.swab.dev/podkasto.git
Log | Files | Refs | README | LICENSE

commit dff083eafd6a5b15633227a548ef36d528480887
parent 8136eab5f95fd2b75d1bdbebc80e6113542f15bf
Author: Jaron Swab <jaron@swab.dev>
Date:   Sun, 25 Apr 2021 13:52:46 -0400

Added ability to search by term

Diffstat:
Aconfig/congif.go | 55+++++++++++++++++++++++++++++++++++++++++++++++++++++++
Aexample.conf | 6++++++
Ago.mod | 3+++
Amain.go | 21+++++++++++++++++++++
Apodkasto | 0
Arequest/request.go | 43+++++++++++++++++++++++++++++++++++++++++++
Asearch/terms.go | 73+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 201 insertions(+), 0 deletions(-)

diff --git a/config/congif.go b/config/congif.go @@ -0,0 +1,55 @@ +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/example.conf b/example.conf @@ -0,0 +1,6 @@ +# Podkasto Configuration File +# Each line is a key value pair separated by a single space. +# Lines starting with "#" and empty lines are ignored. + +apiKey ApiKeyHere +apiSecret ApiSecrectHere diff --git a/go.mod b/go.mod @@ -0,0 +1,3 @@ +module github.com/jrswab/podkasto + +go 1.16 diff --git a/main.go b/main.go @@ -0,0 +1,21 @@ +package main + +import ( + "flag" + + "github.com/jrswab/podkasto/config" + "github.com/jrswab/podkasto/search" +) + +func main() { + var term = flag.String("term", "", "A term to search for.") + flag.Parse() + + conf := new(config.Config) + conf.Load() + + if *term != "" { + q := &search.Query{ApiKey: conf.ApiKey, ApiSecret: conf.ApiSecret} + q.ByTerm(*term) + } +} diff --git a/podkasto b/podkasto Binary files differ. 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/search/%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/terms.go b/search/terms.go @@ -0,0 +1,73 @@ +package search + +import ( + "encoding/json" + "fmt" + "log" + + "github.com/jrswab/podkasto/request" +) + +type Podcasts struct { + Status string `json:"status"` + Feeds []Feeds `json:"feeds"` + Count int `json:"count"` + Query string `json:"query"` + Description string `json:"description"` +} + +type Categories struct { + Num102 string `json:"102"` +} + +type Feeds 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"` + Crawlerrors int `json:"crawlErrors"` + Parseerrors int `json:"parseErrors"` + Categories Categories `json:"categories"` + Locked int `json:"locked"` + Imageurlhash int `json:"imageUrlHash"` +} + +type Query struct { + ApiKey string + ApiSecret string +} + +func (q *Query) ByTerm(term string) { + //searchQuery := fmt.Sprintf("%sbyterm?q=%s&pretty=%s", searchBase, query, isPretty) + searchQuery := fmt.Sprintf("byterm?q=%s", term) + + body, err := request.Send(searchQuery, q.ApiKey, q.ApiSecret) + if err != nil { + log.Fatal(err) + } + + search := new(Podcasts) + err = json.Unmarshal(body, &search) + if err != nil { + log.Fatal(err) + } + + fmt.Println("%+v", search) +}