about summary refs log tree commit diff stats
path: root/README.md
Commit message (Expand)AuthorAgeFilesLines
* Add notmuch docsReto Brunner2019-08-081-0/+6
* Update README.mdDrew DeVault2019-07-141-0/+2
* Document collecting log outputLyle Hanson2019-06-141-0/+4
* Update docs per filters rewriteDrew DeVault2019-06-071-8/+0
* Clarify socksify dependencyDrew DeVault2019-06-041-1/+1
* Update README.md with better dependency listDrew DeVault2019-06-041-1/+9
* Update to the latest go-libvtermDrew DeVault2019-05-261-3/+2
* Add aerc-announce to README.mdDrew DeVault2019-05-261-3/+6
* Update README.mdDrew DeVault2019-05-251-1/+1
* Update README.mdDrew DeVault2019-05-251-13/+4
* Update README.mdDrew DeVault2019-05-251-1/+3
* Document dependenciesDrew DeVault2019-05-171-0/+6
* Add man pagesDrew DeVault2019-05-171-2/+7
* Add IRC link to README.mdDrew DeVault2019-05-141-0/+2
* Move aerc to dedicated mailing listDrew DeVault2019-05-111-1/+1
* Update README.mdDrew DeVault2019-03-211-3/+7
* Add link to todo trackerDrew DeVault2019-03-171-0/+2
* Add README.md, update license (MIT -2019)Drew DeVault2019-01-131-0/+20
nterpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
package apod

import (
	"encoding/json"
	"fmt"
	"regexp"

	"tildegit.org/andinus/cetus/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 *APOD, 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
}