Skip to content

Commit

Permalink
.
Browse files Browse the repository at this point in the history
  • Loading branch information
s3ththompson committed Feb 20, 2019
0 parents commit cf6f6fc
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
![Example Image](northern-territory-australia-2260.jpg)

## Earth Catcher

Simple CLI to download images from [Google Earth View](https://chrome.google.com/webstore/detail/earth-view-from-google-ea/bhloflhklmhfpedakmangadcdofhnnoh?hl=en).

### Installation

```
$ go get github.com/s3ththompson/earth-catcher
```

### Usage

```
earth [options...] EARTH_VIEW_URL
Options:
-o name of output file
```

### Example

```
$ earth https://g.co/ev/2260
Northern Territory, Australia – Earth View from Google
Lat: -11.992309, Lng: 131.807527, ©2014 CNES / Astrium, Cnes/Spot Image, DigitalGlobe, Landsat, Sinclair Knight Merz, Sinclair Knight Merz & Fugro
Downloaded https://www.gstatic.com/prettyearth/assets/full/2260.jpg to northern-territory-australia-2260.jpg (1 file, 261.3 kB)
```
123 changes: 123 additions & 0 deletions earth.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package main

import (
"encoding/json"
"flag"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path"
)

type EarthViewItem struct {
Id string `json:"id"`
Slug string `json:"slug"`
Title string `json:"title"`
Lat string `json:"lat"`
Lng string `json:"lng"`
PhotoURL string `json:"photoUrl"`
Attribution string `json:"attribution"`
MapsLink string `json:"mapsLink"`
EarthLink string `json:"earthLink"`
}

const Usage = `Usage: earth [options...] EARTH_VIEW_URL
Options:
-o name of output file
`

var (
o = flag.String("o", "", "")
)

func main() {
flag.Usage = func() {
fmt.Fprint(os.Stderr, Usage)
}

flag.Parse()

earthURL := ""
if flag.NArg() > 0 {
earthURL = flag.Args()[0]
}
output := *o

if earthURL == "" {
fmt.Fprintln(os.Stderr, Usage)
return
}

resp, err := http.Get(earthURL)
if err != nil {
fmt.Fprintln(os.Stderr, "error requesting url, ", earthURL)
return
}

expandedURL := resp.Request.URL.String()
apiURL, err := url.ParseRequestURI(expandedURL)
if err != nil || apiURL.Host != "earthview.withgoogle.com" {
fmt.Fprintln(os.Stderr, "error resolving url, expecting host earthview.withgoogle.com")
return
}

apiURL.Path = path.Join("_api", apiURL.Path + ".json")
resp, err = http.Get(apiURL.String())
if err != nil {
fmt.Fprintln(os.Stderr, "error requesting api, ", apiURL.String())
return
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Fprintln(os.Stderr, "error reading api request body")
return
}

var item EarthViewItem
err = json.Unmarshal(body, &item)
if err != nil {
fmt.Fprintln(os.Stderr, "error decoding json")
}

if output == "" {
output = item.Slug + ".jpg"
}
out, err := os.Create(output)
if err != nil {
fmt.Fprintln(os.Stderr, "error writing output, ", output)
}
defer out.Close()

resp, err = http.Get(item.PhotoURL)
if err != nil {
fmt.Fprintln(os.Stderr, "error downloading file, ", item.PhotoURL)
}
defer resp.Body.Close()

n, err := io.Copy(out, resp.Body)
if err != nil {
fmt.Fprintln(os.Stderr, "error downloading file, ", item.PhotoURL)
}
fmt.Println(item.Title)
fmt.Printf("Lat: %s, Lng: %s, %s\n", item.Lat, item.Lng, item.Attribution)
fmt.Printf("Downloaded %s to %s (1 file, %s)\n", item.PhotoURL, output, byteCountDecimal(n))
}

// programming.guide/go/formatting-byte-size-to-human-readable-format.html
func byteCountDecimal(b int64) string {
const unit = 1000
if b < unit {
return fmt.Sprintf("%d B", b)
}
div, exp := int64(unit), 0
for n := b / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(b)/float64(div), "kMGTPE"[exp])
}
Binary file added northern-territory-australia-2260.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cf6f6fc

Please sign in to comment.