From e6b0afd6b97b2402e218583e7c22551b383712df Mon Sep 17 00:00:00 2001 From: Andinus Date: Tue, 24 Mar 2020 19:52:43 +0530 Subject: Add apod package --- apod/json.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ apod/randomdate.go | 31 ++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 apod/json.go create mode 100644 apod/randomdate.go (limited to 'apod') diff --git a/apod/json.go b/apod/json.go new file mode 100644 index 0000000..c6da815 --- /dev/null +++ b/apod/json.go @@ -0,0 +1,64 @@ +package apod + +import ( + "encoding/json" + "fmt" + "regexp" + + "framagit.org/andinus/cetus/pkg/request" +) + +// APOD holds the response from the api. Not every field is returned +// in every request. Code & Msg should be filled only if the api +// returns an error, this behaviour was observed and shouldn't be +// trusted. +type APOD 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 { + err = fmt.Errorf("json.go: unmarshalling json failed\n%s", + err.Error()) + } + return err +} + +// GetJson takes reqInfo as input and returns the body and an error. +func GetJson(reqInfo map[string]string) (string, error) { + var body string + var err error + + // This regexp is not perfect and does not guarantee that the + // request will not fail because of wrong date, this will + // eliminate many wrong dates though. + 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"]) { + err = fmt.Errorf("json.go: %s does not match format 'YYYY-MM-DD'", + reqInfo["date"]) + return body, err + } + + // reqInfo is map[string]string and params is built from it, currently + // it takes apiKey and the date from reqInfo to build param. If any + // new key/value is added to reqInfo then it must be addded here too, + // it won't be sent as param directly. + params := make(map[string]string) + params["api_key"] = reqInfo["apiKey"] + params["date"] = reqInfo["date"] + + body, err = request.GetRes(reqInfo["api"], params) + return body, err +} diff --git a/apod/randomdate.go b/apod/randomdate.go new file mode 100644 index 0000000..743b447 --- /dev/null +++ b/apod/randomdate.go @@ -0,0 +1,31 @@ +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() + + // We are taking max from UTC but it could fail if the + // timezone on NASA APOD server is any different. They don't + // mention the timezone api server uses so we use UTC & the + // probability of this function failing because of this issue + // is very low. + max = time.Now().UTC().Unix() + delta = max - min + + sec = rand.Int63n(delta) + min + date = time.Unix(sec, 0).Format("2006-01-02") + + return date +} -- cgit 1.4.1-2-gfad0