diff --git a/README.md b/README.md index 1068d1f..6bf9e19 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,122 @@ See [latest release](https://github.com/goark/apod/releases/latest). ## Usage +``` +$ apod -h +OpenPGP (RFC 4880) packet visualizer by golang. + +Usage: + apod [flags] + apod [command] + +Available Commands: + download Download NASA APOD data + help Help about any command + lookup Look up NASA APOD data + version Print the version number + +Flags: + --api-key string NASA API key + --config string Config file (default /home/spiegel/.config/apod/config.yaml) + --count int count randomly chosen images + --date string date of the APOD image to retrieve (YYYY-MM-DD) + --debug for debug + --end-date string end of a date range (YYYY-MM-DD) + -h, --help help for apod + --start-date string start of a date range (YYYY-MM-DD) + --thumbs return the URL of video thumbnail + +Use "apod [command] --help" for more information about a command. +``` + +### Config file + +```yaml:config.yaml +api-key: your_api_key_string +``` + +### Lookup APOD data + +``` +$ apod lookup -h +Look up NASA APOD data. + +Usage: + apod lookup [flags] + +Aliases: + lookup, look, l + +Flags: + -h, --help help for lookup + --raw Output raw data from APOD API + +Global Flags: + --api-key string NASA API key + --config string Config file (default /home/spiegel/.config/apod/config.yaml) + --count int count randomly chosen images + --date string date of the APOD image to retrieve (YYYY-MM-DD) + --debug for debug + --end-date string end of a date range (YYYY-MM-DD) + --start-date string start of a date range (YYYY-MM-DD) + --thumbs return the URL of video thumbnail + +$ apod lookup | jq . +[ + { + "copyright": "Serge\nBrunier, Jean-François Bax, David Vernet", + "date": "2023-02-24", + "explanation": "Planetary nebula Jones-Emberson 1 is the death shroud of a dying Sun-like star. It lies some 1,600 light-years from Earth toward the sharp-eyed constellation Lynx. About 4 light-years across, the expanding remnant of the dying star's atmosphere was shrugged off into interstellar space, as the star's central supply of hydrogen and then helium for fusion was finally depleted after billions of years. Visible near the center of the planetary nebula is what remains of the stellar core, a blue-hot white dwarf star. Also known as PK 164 +31.1, the nebula is faint and very difficult to glimpse at a telescope's eyepiece. But this deep broadband image combining 22 hours of exposure time does show it off in exceptional detail. Stars within our own Milky Way galaxy as well as background galaxies across the universe are scattered through the clear field of view. Ephemeral on the cosmic stage, Jones-Emberson 1 will fade away over the next few thousand years. Its hot, central white dwarf star will take billions of years to cool.", + "hdurl": "https://apod.nasa.gov/apod/image/2302/jonesemberson1.jpg", + "media_type": "image", + "service_version": "v1", + "title": "Jones-Emberson 1", + "url": "https://apod.nasa.gov/apod/image/2302/jonesemberson1_1024.jpg" + } +] +``` + +### Download APOD data + +``` +$ apod download -h +Download NASA APOD data. + +Usage: + apod download [flags] + +Aliases: + download, dl, d + +Flags: + -d, --base-dir string Base directory for daownload (default "./apod") + -h, --help help for download + --include-nopd Download no public domain images or videos + --overwrite Overwrite Download files + +Global Flags: + --api-key string NASA API key + --config string Config file (default /home/spiegel/.config/apod/config.yaml) + --count int count randomly chosen images + --date string date of the APOD image to retrieve (YYYY-MM-DD) + --debug for debug + --end-date string end of a date range (YYYY-MM-DD) + --start-date string start of a date range (YYYY-MM-DD) + --thumbs return the URL of video thumbnail + +$ apod download --include-nopd + +$ LANG=C ls -l ~/ws/work/apod +total 4 +drwxrwxr-x 2 spiegel spiegel 4096 Feb 24 19:58 2023-02-24 + +$ LANG=C ls -l ~/ws/work/apod/2023-02-24 +total 3376 +-rw-rw-r-- 1 spiegel spiegel 3094863 Feb 24 19:58 jonesemberson1.jpg +-rw-rw-r-- 1 spiegel spiegel 352679 Feb 24 19:58 jonesemberson1_1024.jpg +-rw-rw-r-- 1 spiegel spiegel 1365 Feb 24 19:58 metadata.json +``` + ## Modules Requirement Graph [![dependency.png](./dependency.png)](./dependency.png) diff --git a/facade/download.go b/facade/download.go new file mode 100644 index 0000000..beafd35 --- /dev/null +++ b/facade/download.go @@ -0,0 +1,69 @@ +package facade + +import ( + "context" + + "github.com/goark/apod/service/download" + "github.com/goark/gocli/rwi" + "github.com/spf13/cobra" + "github.com/spf13/viper" +) + +// newVersionCmd returns cobra.Command instance for show sub-command +func newDownload(ui *rwi.RWI) *cobra.Command { + downloadCmd := &cobra.Command{ + Use: "download", + Aliases: []string{"dl", "d"}, + Short: "Download NASA APOD data", + Long: "Download NASA APOD data.", + RunE: func(cmd *cobra.Command, args []string) error { + // global options + dir := viper.GetString("base-dir") + copyrightFlag := viper.GetBool("include-nopd") + overwriteFlag := viper.GetBool("overwrite") + cfg, err := makeAPODConfig() + if err != nil { + return debugPrint(ui, err) + } + + // download APOD data + if err := download.New(cfg, dir, copyrightFlag, overwriteFlag).Do(context.TODO()); err != nil { + return debugPrint(ui, err) + } + return nil + }, + } + downloadCmd.Flags().StringP("base-dir", "d", "./apod", "Base directory for daownload") + downloadCmd.Flags().BoolP("include-nopd", "", false, "Download no public domain images or videos") + downloadCmd.Flags().BoolP("overwrite", "", false, "Overwrite Download files") + + //Bind config file + _ = viper.BindPFlag("base-dir", downloadCmd.Flags().Lookup("base-dir")) + _ = viper.BindPFlag("include-nopd", downloadCmd.Flags().Lookup("include-nopd")) + _ = viper.BindPFlag("overwrite", downloadCmd.Flags().Lookup("overwrite")) + + return downloadCmd +} + +/* MIT License + * + * Copyright 2023 Spiegel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ diff --git a/facade/facade.go b/facade/facade.go index d27d700..e56b087 100644 --- a/facade/facade.go +++ b/facade/facade.go @@ -1,7 +1,10 @@ package facade import ( + "context" "fmt" + "os" + "os/signal" "runtime" "github.com/goark/errs" @@ -70,6 +73,7 @@ func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command { rootCmd.AddCommand( newVersionCmd(ui), newLookup(ui), + newDownload(ui), ) return rootCmd @@ -136,7 +140,9 @@ func Execute(ui *rwi.RWI, args []string) (exit exitcode.ExitCode) { //execution exit = exitcode.Normal - if err := newRootCmd(ui, args).Execute(); err != nil { + ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) + defer cancel() + if err := newRootCmd(ui, args).ExecuteContext(ctx); err != nil { exit = exitcode.Abnormal } return diff --git a/facade/lookup.go b/facade/lookup.go index ed3fb92..f1e4dc6 100644 --- a/facade/lookup.go +++ b/facade/lookup.go @@ -1,8 +1,6 @@ package facade import ( - "context" - "github.com/goark/apod/service/lookup" "github.com/goark/gocli/rwi" "github.com/spf13/cobra" @@ -28,7 +26,7 @@ func newLookup(ui *rwi.RWI) *cobra.Command { } // lookup APOD data - r, err := lookup.New(cfg).Do(context.TODO(), rawFlag) + r, err := lookup.New(cfg, rawFlag).Do(cmd.Context()) if err != nil { return debugPrint(ui, err) } diff --git a/nasaapi/apod/request.go b/nasaapi/apod/request.go index ff8276f..439d9ca 100644 --- a/nasaapi/apod/request.go +++ b/nasaapi/apod/request.go @@ -92,6 +92,9 @@ func WithAPIKey(apiKey string) Opts { // Encode returns JSON string. func (apod *Request) Encode() (string, error) { + if apod == nil { + return "", errs.Wrap(ecode.ErrNullPointer) + } b, err := json.Marshal(apod) if err != nil { return "", errs.Wrap(err) @@ -108,8 +111,11 @@ func (apod *Request) String() string { return s } -// Get method gets APOD data from NASA API, and returns []Response instance. -func (apod *Request) Get(ctx context.Context) ([]Response, error) { +// Get method gets APOD data from NASA API, and returns []*Response instance. +func (apod *Request) Get(ctx context.Context) ([]*Response, error) { + if apod == nil { + return nil, errs.Wrap(ecode.ErrNullPointer) + } resp, err := apod.GetRawData(ctx) if err != nil { return nil, err @@ -120,6 +126,9 @@ func (apod *Request) Get(ctx context.Context) ([]Response, error) { // GetRawData method gets APOD data from NASA API, and returns raw response string. func (apod *Request) GetRawData(ctx context.Context) (io.ReadCloser, error) { + if apod == nil { + return nil, errs.Wrap(ecode.ErrNullPointer) + } q, err := apod.makeQuery() if err != nil { return nil, errs.Wrap(err) @@ -158,9 +167,6 @@ func (apod *Request) makeQuery() (url.Values, error) { v.Set("end_date", apod.EndDate.Format(time.DateOnly)) } if apod.Count > 0 { - if !apod.Date.IsZero() || !apod.StartDate.IsZero() || !apod.EndDate.IsZero() { - return nil, errs.Wrap(ecode.ErrCombination, errs.WithContext("config", apod)) - } v.Set("count", strconv.Itoa(apod.Count)) } if apod.Thumbs { diff --git a/nasaapi/apod/request_test.go b/nasaapi/apod/request_test.go new file mode 100644 index 0000000..bcad07b --- /dev/null +++ b/nasaapi/apod/request_test.go @@ -0,0 +1,187 @@ +package apod + +import ( + "errors" + "testing" + + "github.com/goark/apod/ecode" + "github.com/goark/apod/nasaapi" +) + +func dateFromMust(s string) nasaapi.Date { + dt, err := nasaapi.DateFrom(s) + if err != nil { + panic(err) + } + return dt +} + +func TestDate(t *testing.T) { + testCases := []struct { + date nasaapi.Date + startDate nasaapi.Date + endDate nasaapi.Date + count int + thumbs bool + apiKey string + err error + want string + }{ + { + date: dateFromMust(""), + startDate: dateFromMust(""), + endDate: dateFromMust(""), + count: 0, + thumbs: false, + apiKey: "", + err: nil, + want: `{"date":"","start_date":"","end_date":"","api_key":""}`, + }, + { + date: dateFromMust("2023-02-22"), + startDate: dateFromMust(""), + endDate: dateFromMust(""), + count: 0, + thumbs: false, + apiKey: "", + err: nil, + want: `{"date":"2023-02-22","start_date":"","end_date":"","api_key":""}`, + }, + { + date: dateFromMust(""), + startDate: dateFromMust("2023-02-22"), + endDate: dateFromMust(""), + count: 0, + thumbs: false, + apiKey: "", + err: nil, + want: `{"date":"","start_date":"2023-02-22","end_date":"","api_key":""}`, + }, + { + date: dateFromMust(""), + startDate: dateFromMust("2023-02-22"), + endDate: dateFromMust("2023-02-22"), + count: 0, + thumbs: false, + apiKey: "", + err: nil, + want: `{"date":"","start_date":"2023-02-22","end_date":"2023-02-22","api_key":""}`, + }, + { + date: dateFromMust(""), + startDate: dateFromMust(""), + endDate: dateFromMust(""), + count: 1, + thumbs: false, + apiKey: "", + err: nil, + want: `{"date":"","start_date":"","end_date":"","count":1,"api_key":""}`, + }, + { + date: dateFromMust(""), + startDate: dateFromMust(""), + endDate: dateFromMust(""), + count: 0, + thumbs: true, + apiKey: "foo", + err: nil, + want: `{"date":"","start_date":"","end_date":"","thumbs":true,"api_key":"foo"}`, + }, + { + date: dateFromMust("2023-02-22"), + startDate: dateFromMust("2023-02-22"), + endDate: dateFromMust(""), + count: 0, + thumbs: false, + apiKey: "", + err: ecode.ErrCombination, + want: "", + }, + { + date: dateFromMust("2023-02-22"), + startDate: dateFromMust(""), + endDate: dateFromMust("2023-02-22"), + count: 0, + thumbs: false, + apiKey: "", + err: ecode.ErrCombination, + want: "", + }, + { + date: dateFromMust(""), + startDate: dateFromMust(""), + endDate: dateFromMust("2023-02-22"), + count: 0, + thumbs: false, + apiKey: "", + err: ecode.ErrCombination, + want: "", + }, + { + date: dateFromMust("2023-02-22"), + startDate: dateFromMust(""), + endDate: dateFromMust(""), + count: 1, + thumbs: false, + apiKey: "", + err: ecode.ErrCombination, + want: "", + }, + { + date: dateFromMust(""), + startDate: dateFromMust("2023-02-22"), + endDate: dateFromMust(""), + count: 1, + thumbs: false, + apiKey: "", + err: ecode.ErrCombination, + want: "", + }, + } + + for _, tc := range testCases { + req := New( + WithDate(tc.date), + WithStartDate(tc.startDate), + WithEndDate(tc.endDate), + WithCount(tc.count), + WithThumbs(tc.thumbs), + WithAPIKey(tc.apiKey), + ) + _, err := req.makeQuery() + if !errors.Is(err, tc.err) { + t.Errorf("makeQuery() is \"%v\", want \"%v\"", err, tc.err) + } + if err == nil { + if got, err := req.Encode(); err != nil { + t.Errorf("Encode() is \"%v\", want nil", err) + } else if got != tc.want { + t.Errorf("Encode() = \"%v\", want \"%v\"", got, tc.want) + } + + } + } +} + +/* MIT License + * + * Copyright 2023 Spiegel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ diff --git a/nasaapi/apod/response.go b/nasaapi/apod/response.go index 22f4598..c013f5a 100644 --- a/nasaapi/apod/response.go +++ b/nasaapi/apod/response.go @@ -22,8 +22,8 @@ type Response struct { ThumbnailUrl string `json:"thumbnail_url,omitempty"` } -func decode(r io.Reader, isSingle bool) ([]Response, error) { - var resps []Response +func decode(r io.Reader, isSingle bool) ([]*Response, error) { + var resps []*Response dec := json.NewDecoder(r) if isSingle { for { @@ -34,11 +34,11 @@ func decode(r io.Reader, isSingle bool) ([]Response, error) { } return nil, errs.Wrap(err) } - resps = append(resps, resp) + resps = append(resps, &resp) } } else { for { - var resp []Response + var resp []*Response if err := dec.Decode(&resp); err != nil { if errors.Is(err, io.EOF) { break diff --git a/nasaapi/date.go b/nasaapi/date.go index 6ea0a7b..632175c 100644 --- a/nasaapi/date.go +++ b/nasaapi/date.go @@ -18,6 +18,7 @@ func NewDate(tm time.Time) Date { return Date{tm} } +// Stringer with YYYY-MM-DD format. func (t Date) String() string { if t.IsZero() { return "" diff --git a/service/download/download.go b/service/download/download.go new file mode 100644 index 0000000..83c84e5 --- /dev/null +++ b/service/download/download.go @@ -0,0 +1,164 @@ +package download + +import ( + "context" + "encoding/json" + "errors" + "io" + "os" + "path" + "path/filepath" + + "github.com/goark/apod/ecode" + "github.com/goark/apod/nasaapi/apod" + "github.com/goark/errs" + "github.com/goark/fetch" +) + +const maxDataSize = 1024 * 1024 * 1024 //1GB + +// Download is configuration for download command. +type Download struct { + *apod.Request + baseDir string + copyrightFlag bool + overwriteFlag bool +} + +// New returns new Lookup instance. +func New(cfg *apod.Request, baseDir string, copyrightFlag, overwriteFlag bool) *Download { + if len(baseDir) == 0 { + baseDir = "." + } + return &Download{ + Request: cfg, + baseDir: baseDir, + copyrightFlag: copyrightFlag, + overwriteFlag: overwriteFlag, + } +} + +// Do method is downloading APOD data from NASA API. +func (dl *Download) Do(ctx context.Context) error { + if dl == nil { + return errs.Wrap(ecode.ErrNullPointer) + } + // get APOD data from NASA API + resps, err := dl.Get(ctx) + if err != nil { + return errs.Wrap(err) + } + + // make directory + if _, err := os.Stat(dl.baseDir); err != nil { // dirextory is not found + if err := os.MkdirAll(dl.baseDir, os.ModePerm); err != nil { + return errs.Wrap(err) + } + } + + for _, resp := range resps { + // make directory + dir := filepath.Join(dl.baseDir, resp.Date.String()) + if _, err := os.Stat(dir); err != nil { // dirextory is not found + if err := os.Mkdir(dir, os.ModePerm); err != nil { + return errs.Wrap(err, errs.WithContext("dir", dir)) + } + } else if !dl.overwriteFlag { + continue + } else { + if err := os.RemoveAll(dir); err != nil { + return errs.Wrap(err, errs.WithContext("dir", dir)) + } + if err := os.Mkdir(dir, 0755); err != nil { + return errs.Wrap(err, errs.WithContext("dir", dir)) + } + } + + // output metadata.json file + if err := saveMetadate(resp, filepath.Join(dir, "metadata.json")); err != nil { + return errs.Wrap(err) + } + // download image/video files + if len(resp.Copyright) > 0 && !dl.copyrightFlag { + continue + } + if len(resp.HdUrl) > 0 { + if err := downloadImage(ctx, resp.HdUrl, dir); err != nil { + return errs.Wrap(err, errs.WithContext("hdUrl", resp.HdUrl)) + } + } + if len(resp.Url) > 0 { + if err := downloadImage(ctx, resp.Url, dir); err != nil { + return errs.Wrap(err, errs.WithContext("url", resp.Url)) + } + } + if len(resp.ThumbnailUrl) > 0 { + if err := downloadImage(ctx, resp.ThumbnailUrl, dir); err != nil { + return errs.Wrap(err, errs.WithContext("thumbnailUrl", resp.ThumbnailUrl)) + } + } + + } + + return nil +} + +func saveMetadate(resp *apod.Response, path string) error { + file, err := os.Create(path) + if err != nil { + return errs.Wrap(err, errs.WithContext("path", path)) + } + defer file.Close() + enc := json.NewEncoder(file) + enc.SetIndent("", "\t") + return errs.Wrap(enc.Encode(resp)) +} + +func downloadImage(ctx context.Context, urlStr string, dir string) error { + u, err := fetch.URL(urlStr) + if err != nil { + return errs.Wrap(err, errs.WithContext("url", urlStr)) + } + _, fname := path.Split(u.Path) + resp, err := fetch.New().Get(u, fetch.WithContext(ctx)) + if err != nil { + return errs.Wrap(err, errs.WithContext("url", urlStr)) + } + defer resp.Close() + + path := filepath.Join(dir, fname) + file, err := os.Create(path) + if err != nil { + return errs.Wrap(err, errs.WithContext("path", path)) + } + defer file.Close() + if _, err := io.CopyN(file, resp.Body(), maxDataSize); err != nil { + if !errors.Is(err, io.EOF) { + return errs.Wrap(err, errs.WithContext("path", path)) + } + } + return nil +} + +/* MIT License + * + * Copyright 2023 Spiegel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ diff --git a/service/lookup/lookup.go b/service/lookup/lookup.go index 89bac19..f7d0113 100644 --- a/service/lookup/lookup.go +++ b/service/lookup/lookup.go @@ -6,22 +6,28 @@ import ( "encoding/json" "io" + "github.com/goark/apod/ecode" "github.com/goark/apod/nasaapi/apod" + "github.com/goark/errs" ) // Lookup is configuration for lookup command. type Lookup struct { *apod.Request + rawFlag bool } // New returns new Lookup instance. -func New(cfg *apod.Request) *Lookup { - return &Lookup{cfg} +func New(cfg *apod.Request, rawFlag bool) *Lookup { + return &Lookup{Request: cfg, rawFlag: rawFlag} } // Do method is looking up APOD data from NASA API. -func (l *Lookup) Do(ctx context.Context, rawFlag bool) (io.ReadCloser, error) { - if rawFlag { +func (l *Lookup) Do(ctx context.Context) (io.ReadCloser, error) { + if l == nil { + return nil, errs.Wrap(ecode.ErrNullPointer) + } + if l.rawFlag { return l.GetRawData(ctx) } resp, err := l.Get(ctx)