From 052140fb3ccd4b386d8d98ba7355d676c1e0693d Mon Sep 17 00:00:00 2001 From: Andinus Date: Wed, 18 Mar 2020 23:04:21 +0530 Subject: Initial commit for Cetus v0.5.0 --- pkg/apod/json.go | 48 +++++++++++++++++++++++++++++++++++++++++++++++ pkg/apod/print.go | 19 +++++++++++++++++++ pkg/apod/rand.go | 25 +++++++++++++++++++++++++ pkg/bing/bpod.go | 23 ----------------------- pkg/bpod/json.go | 55 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ pkg/bpod/print.go | 14 ++++++++++++++ pkg/cetus/cetus.go | 1 - pkg/cetus/req.go | 5 +++-- pkg/nasa/apod.go | 44 ------------------------------------------- 9 files changed, 164 insertions(+), 70 deletions(-) create mode 100644 pkg/apod/json.go create mode 100644 pkg/apod/print.go create mode 100644 pkg/apod/rand.go delete mode 100644 pkg/bing/bpod.go create mode 100644 pkg/bpod/json.go create mode 100644 pkg/bpod/print.go delete mode 100644 pkg/nasa/apod.go (limited to 'pkg') diff --git a/pkg/apod/json.go b/pkg/apod/json.go new file mode 100644 index 0000000..4589925 --- /dev/null +++ b/pkg/apod/json.go @@ -0,0 +1,48 @@ +package apod + +import ( + "encoding/json" + "fmt" + "regexp" + + "framagit.org/andinus/cetus/pkg/cetus" +) + +// Res holds the response from the api. +type Res struct { + Copyright string `json:"copyright"` + Date string `json:"date"` + Explanation string `json:"explanation"` + HDURL string `json:"hdurl"` + MediaType string `json:"media_type"` + ServiceVersion string `json:"service_version"` + Title string `json:"title"` + URL string `json:"url"` + + Code int `json:"code"` + Msg string `json:"msg"` +} + +// UnmarshalJson will take body as input & unmarshal it to res +func UnmarshalJson(res *Res, body string) error { + err := json.Unmarshal([]byte(body), res) + if err != nil { + return fmt.Errorf("UnmarshalJson failed\n%s", err.Error()) + } + return nil +} + +// GetJson returns json response received from the api +func GetJson(reqInfo map[string]string) (string, error) { + re := regexp.MustCompile("((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])") + if !re.MatchString(reqInfo["date"]) { + return "", fmt.Errorf("%s does not match format 'YYYY-MM-DD'", reqInfo["date"]) + } + + params := make(map[string]string) + params["api_key"] = reqInfo["apiKey"] + params["date"] = reqInfo["date"] + + body, err := cetus.GetRes(reqInfo["api"], params) + return string(body), err +} diff --git a/pkg/apod/print.go b/pkg/apod/print.go new file mode 100644 index 0000000..a087665 --- /dev/null +++ b/pkg/apod/print.go @@ -0,0 +1,19 @@ +package apod + +import ( + "fmt" +) + +// Print will print the json output +func Print(res Res) { + fmt.Printf("Title: %s\n\n", res.Title) + fmt.Printf("Copyright: %s\n", res.Copyright) + fmt.Printf("Date: %s\n\n", res.Date) + fmt.Printf("Media Type: %s\n", res.MediaType) + if res.MediaType == "image" { + fmt.Printf("URL: %s\n\n", res.HDURL) + } else { + fmt.Printf("URL: %s\n\n", res.URL) + } + fmt.Printf("Explanation: %s\n", res.Explanation) +} diff --git a/pkg/apod/rand.go b/pkg/apod/rand.go new file mode 100644 index 0000000..cf92784 --- /dev/null +++ b/pkg/apod/rand.go @@ -0,0 +1,25 @@ +package apod + +import ( + "math/rand" + "time" +) + +// RandDate returns a random date between 1995-06-16 & today +func RandDate() string { + var ( + min int64 + max int64 + sec int64 + delta int64 + date string + ) + min = time.Date(1995, 6, 16, 0, 0, 0, 0, time.UTC).Unix() + max = time.Now().UTC().Unix() + delta = max - min + + sec = rand.Int63n(delta) + min + date = time.Unix(sec, 0).Format("2006-01-02") + + return date +} diff --git a/pkg/bing/bpod.go b/pkg/bing/bpod.go deleted file mode 100644 index c7ee79d..0000000 --- a/pkg/bing/bpod.go +++ /dev/null @@ -1,23 +0,0 @@ -package bing - -import ( - "time" - - "framagit.org/andinus/cetus/pkg/cetus" -) - -// GetBpodJson returns json response received from the api -func GetBpodJson(reqInfo map[string]string, t time.Duration) (string, error) { - params := make(map[string]string) - params["format"] = "js" - params["n"] = "1" - - // if random is true then fetch 7 photos - if reqInfo["random"] == "true" { - params["n"] = "7" - - } - - body, err := cetus.GetRes(reqInfo["api"], params, t) - return string(body), err -} diff --git a/pkg/bpod/json.go b/pkg/bpod/json.go new file mode 100644 index 0000000..4d74668 --- /dev/null +++ b/pkg/bpod/json.go @@ -0,0 +1,55 @@ +package bpod + +import ( + "encoding/json" + "fmt" + "math/rand" + + "framagit.org/andinus/cetus/pkg/cetus" +) + +type Res struct { + StartDate string `json:"startdate"` + FullStartDate string `json:"fullstartdate"` + EndDate string `json:"enddate"` + Url string `json:"url"` + UrlBase string `json:"urlbase"` + Copyright string `json:"copyright"` + CopyrightLink string `json:"copyrightlink"` + Title string `json:"title"` + Hsh string `json:"hsh"` +} + +type List struct { + Photos []Res `json:"images"` +} + +// UnmarshalJson will take body as input & unmarshal it to res +func UnmarshalJson(body string) (Res, error) { + list := List{} + res := Res{} + + err := json.Unmarshal([]byte(body), &list) + if err != nil { + return res, fmt.Errorf("UnmarshalJson failed\n%s", err.Error()) + } + + res = list.Photos[rand.Intn(len(list.Photos))] + return res, nil +} + +// GetJson returns json response received from the api +func GetJson(reqInfo map[string]string) (string, error) { + params := make(map[string]string) + params["format"] = "js" + params["n"] = "1" + + // if random is true then fetch 7 photos + if reqInfo["random"] == "true" { + params["n"] = "7" + + } + + body, err := cetus.GetRes(reqInfo["api"], params) + return string(body), err +} diff --git a/pkg/bpod/print.go b/pkg/bpod/print.go new file mode 100644 index 0000000..75bf948 --- /dev/null +++ b/pkg/bpod/print.go @@ -0,0 +1,14 @@ +package bpod + +import ( + "fmt" +) + +// Print will print the json output +func Print(res Res) { + fmt.Printf("Title: %s\n\n", res.Title) + fmt.Printf("Copyright: %s\n", res.Copyright) + fmt.Printf("Copyright Link: %s\n", res.CopyrightLink) + fmt.Printf("Date: %s\n\n", res.StartDate) + fmt.Printf("URL: %s\n", res.Url) +} diff --git a/pkg/cetus/cetus.go b/pkg/cetus/cetus.go index 17fd906..0e9b7c2 100644 --- a/pkg/cetus/cetus.go +++ b/pkg/cetus/cetus.go @@ -2,7 +2,6 @@ package cetus import ( "fmt" - "log" ) var version string = "v0.5.0" diff --git a/pkg/cetus/req.go b/pkg/cetus/req.go index 84e52a5..ed81374 100644 --- a/pkg/cetus/req.go +++ b/pkg/cetus/req.go @@ -8,9 +8,10 @@ import ( ) // GetRes returns api response -func GetRes(api string, params map[string]string, t time.Duration) (string, error) { +func GetRes(api string, params map[string]string) (string, error) { c := http.Client{ - Timeout: time.Second * t, + // TODO: timeout should be configurable by the user + Timeout: time.Second * 64, } req, err := http.NewRequest(http.MethodGet, api, nil) diff --git a/pkg/nasa/apod.go b/pkg/nasa/apod.go deleted file mode 100644 index 606bbdf..0000000 --- a/pkg/nasa/apod.go +++ /dev/null @@ -1,44 +0,0 @@ -package nasa - -import ( - "fmt" - "math/rand" - "regexp" - "time" - - "framagit.org/andinus/cetus/pkg/cetus" -) - -// RandDate returns a random date between 1995-06-16 & today -func RandDate() string { - var ( - min int64 - max int64 - sec int64 - delta int64 - date string - ) - min = time.Date(1995, 6, 16, 0, 0, 0, 0, time.UTC).Unix() - max = time.Now().UTC().Unix() - delta = max - min - - sec = rand.Int63n(delta) + min - date = time.Unix(sec, 0).Format("2006-01-02") - - return date -} - -// GetApodJson returns json response received from the api -func GetApodJson(reqInfo map[string]string, t time.Duration) (string, error) { - re := regexp.MustCompile("((19|20)\\d\\d)-(0?[1-9]|1[012])-(0?[1-9]|[12][0-9]|3[01])") - if !re.MatchString(reqInfo["date"]) { - return "", fmt.Errorf("%s does not match format 'YYYY-MM-DD'", reqInfo["date"]) - } - - params := make(map[string]string) - params["api_key"] = reqInfo["apiKey"] - params["date"] = reqInfo["date"] - - body, err := cetus.GetRes(reqInfo["api"], params, t) - return string(body), err -} -- cgit 1.4.1-2-gfad0 t/066stream.mu?h=hlt&id=8fb0e672c2f5431141d58128f623d1cc02d67bb8'>8fb0e672 ^
760f683f ^
77d5b5d6 ^
104854ca ^
85b2f61b ^
748b6865 ^
8fb0e672 ^

760f683f ^
9acf5c0f ^

85b2f61b ^
4f3510d0 ^
7a84094a ^
a0331a9b ^
7a84094a ^
51b0936f ^
4f3510d0 ^

760f683f ^
4f3510d0 ^

9acf5c0f ^




760f683f ^
050a93ac ^

85b2f61b ^
51b0936f ^
7a84094a ^
a0331a9b ^
7a84094a ^
51b0936f ^
4f3510d0 ^

760f683f ^
4f3510d0 ^

050a93ac ^


760f683f ^
77d5b5d6 ^
104854ca ^
85b2f61b ^
7a84094a ^
ea19d0dc ^
7a84094a ^
748b6865 ^



8fb0e672 ^

760f683f ^
77d5b5d6 ^
104854ca ^
85b2f61b ^
7a84094a ^
a0331a9b ^
7a84094a ^
104854ca ^
8fb0e672 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80