Skip to content

Commit

Permalink
Add download command
Browse files Browse the repository at this point in the history
  • Loading branch information
spiegel-im-spiegel committed Feb 24, 2023
1 parent aa06aad commit dc80491
Show file tree
Hide file tree
Showing 10 changed files with 570 additions and 17 deletions.
116 changes: 116 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
69 changes: 69 additions & 0 deletions facade/download.go
Original file line number Diff line number Diff line change
@@ -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.
*/
8 changes: 7 additions & 1 deletion facade/facade.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package facade

import (
"context"
"fmt"
"os"
"os/signal"
"runtime"

"github.com/goark/errs"
Expand Down Expand Up @@ -70,6 +73,7 @@ func newRootCmd(ui *rwi.RWI, args []string) *cobra.Command {
rootCmd.AddCommand(
newVersionCmd(ui),
newLookup(ui),
newDownload(ui),
)

return rootCmd
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions facade/lookup.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package facade

import (
"context"

"github.com/goark/apod/service/lookup"
"github.com/goark/gocli/rwi"
"github.com/spf13/cobra"
Expand All @@ -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)
}
Expand Down
16 changes: 11 additions & 5 deletions nasaapi/apod/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down
Loading

0 comments on commit dc80491

Please sign in to comment.