2020-02-16 21:06:37 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2020-03-01 10:07:18 +00:00
|
|
|
"regexp"
|
2020-03-01 01:11:58 +00:00
|
|
|
"strconv"
|
2020-02-16 21:06:37 +00:00
|
|
|
"strings"
|
|
|
|
|
2020-02-26 00:01:35 +00:00
|
|
|
kingpin "gopkg.in/alecthomas/kingpin.v2"
|
2020-02-16 21:06:37 +00:00
|
|
|
)
|
|
|
|
|
2020-02-26 00:05:18 +00:00
|
|
|
// VERSION The version release
|
2020-03-01 10:09:36 +00:00
|
|
|
const VERSION = "0.1.2"
|
2020-02-26 00:01:35 +00:00
|
|
|
|
2020-02-26 00:05:18 +00:00
|
|
|
// AUTHOR The author name
|
2020-02-26 00:01:35 +00:00
|
|
|
const AUTHOR = "Elia el Lazkani"
|
|
|
|
|
2020-02-29 09:34:00 +00:00
|
|
|
var (
|
|
|
|
switchesFlags = []string{"m", "u", "M", "0", "1", "2", "A", "F", "n", "q", "Q", "T", "p"}
|
2020-02-16 21:06:37 +00:00
|
|
|
)
|
|
|
|
|
2020-02-26 00:01:35 +00:00
|
|
|
// We create a weather struct with the url
|
|
|
|
type weather struct {
|
|
|
|
url string
|
|
|
|
}
|
|
|
|
|
|
|
|
// `weather` method to handle creating new requests with proper headers
|
|
|
|
func (w weather) newRequest(headers map[string]string) *http.Request {
|
|
|
|
req, err := http.NewRequest("GET", w.url, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error reading request. ", err)
|
|
|
|
}
|
|
|
|
return w.formatHeader(req, headers)
|
|
|
|
}
|
|
|
|
|
|
|
|
// `weather` method to format the headers and inject required ones
|
|
|
|
func (w weather) formatHeader(req *http.Request, headers map[string]string) *http.Request {
|
|
|
|
req.Header.Set("Content-Type", "text/plain; charset=utf-8")
|
|
|
|
// I had to read the wttr.in code to figure this one out.
|
|
|
|
// It wasn't Golang, it was 2 wasted hours that I won't be getting back.
|
|
|
|
req.Header.Set("User-Agent", "curl")
|
|
|
|
|
|
|
|
for key, value := range headers {
|
2020-03-01 01:11:58 +00:00
|
|
|
req.Header.Set(key, value)
|
2020-02-26 00:01:35 +00:00
|
|
|
}
|
|
|
|
return req
|
|
|
|
}
|
|
|
|
|
|
|
|
// `weather` method to create a client and get a weather response
|
|
|
|
// This method handles printing or downloading the weather
|
|
|
|
func (w weather) get(req *http.Request, download bool) {
|
|
|
|
client := &http.Client{}
|
|
|
|
client.Jar = nil
|
|
|
|
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error reading response. ", err)
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if download {
|
|
|
|
w.download(resp)
|
|
|
|
} else {
|
|
|
|
w.print(resp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// `weather` method to print the weather
|
|
|
|
func (w weather) print(resp *http.Response) {
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal("Error reading body. ", err)
|
|
|
|
}
|
2020-02-26 23:15:41 +00:00
|
|
|
fmt.Printf("%s", body)
|
2020-02-26 00:01:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// `weather` method to download the weather
|
|
|
|
func (w weather) download(resp *http.Response) {
|
2020-02-29 11:17:14 +00:00
|
|
|
var fName string
|
|
|
|
if *fileName != "" {
|
|
|
|
fName = *fileName
|
|
|
|
} else {
|
|
|
|
splitURL := strings.Split(w.url, "/")
|
|
|
|
fName = splitURL[len(splitURL)-1]
|
|
|
|
}
|
2020-02-26 00:01:35 +00:00
|
|
|
|
2020-02-29 11:17:14 +00:00
|
|
|
f, err := os.Create(fName)
|
2020-02-26 00:01:35 +00:00
|
|
|
if err != nil {
|
2020-02-29 11:37:17 +00:00
|
|
|
log.Fatal("Error opening file. ", err)
|
2020-02-26 00:01:35 +00:00
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
_, err = io.Copy(f, resp.Body)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wraper function around the weather struct
|
|
|
|
func getWheather(url string, headers map[string]string, download bool) {
|
|
|
|
|
|
|
|
w := weather{url}
|
|
|
|
req := w.newRequest(headers)
|
|
|
|
w.get(req, download)
|
|
|
|
}
|
|
|
|
|
2020-02-16 21:06:37 +00:00
|
|
|
// Create a method to figure out the command parameters
|
|
|
|
// provided and translate them into uri parameters.
|
|
|
|
// This will figure out if we need to download the PNG image
|
|
|
|
// as well.
|
2020-03-01 01:11:58 +00:00
|
|
|
func generateParamFormat(switchesF []*bool, freeStyleF *string, formatF *string, oneLinerF *bool, pngF *bool, addFrameF *bool, transparencyF *int, downloadF *bool) (string, bool, error) {
|
2020-02-29 11:17:14 +00:00
|
|
|
var params strings.Builder
|
|
|
|
var prefix = "?"
|
2020-03-01 01:11:58 +00:00
|
|
|
var downloadMode = *downloadF
|
2020-02-16 21:06:37 +00:00
|
|
|
|
2020-02-29 11:17:14 +00:00
|
|
|
// If --free-syle is specified, let's not bother and simply return it
|
2020-03-01 01:11:58 +00:00
|
|
|
if *freeStyleF != "" {
|
|
|
|
return *freeStyleF, downloadMode, nil
|
2020-02-29 11:17:14 +00:00
|
|
|
}
|
2020-02-16 21:06:37 +00:00
|
|
|
|
2020-02-29 11:17:14 +00:00
|
|
|
// If --format is specified, let's return the format the user wants
|
2020-03-01 01:11:58 +00:00
|
|
|
if *formatF != "" {
|
2020-03-01 10:07:18 +00:00
|
|
|
re := regexp.MustCompile("\\s")
|
2020-02-29 11:17:14 +00:00
|
|
|
var str strings.Builder
|
2020-03-01 10:07:18 +00:00
|
|
|
|
|
|
|
str.WriteString("?format=")
|
|
|
|
str.WriteString(re.ReplaceAllString(*formatF, "%20"))
|
2020-02-29 11:17:14 +00:00
|
|
|
return str.String(), downloadMode, nil
|
2020-02-16 21:06:37 +00:00
|
|
|
}
|
2020-02-29 09:34:00 +00:00
|
|
|
// If --one-liner is specified, we know the format is set so let's return it
|
2020-03-01 01:11:58 +00:00
|
|
|
if *oneLinerF {
|
2020-02-29 11:17:14 +00:00
|
|
|
return "?format=3", downloadMode, nil
|
2020-02-16 21:06:37 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 01:11:58 +00:00
|
|
|
for i, s := range switchesF {
|
2020-02-29 11:50:37 +00:00
|
|
|
if *s {
|
2020-02-29 11:17:14 +00:00
|
|
|
params.WriteString(switchesFlags[i])
|
2020-02-29 09:34:00 +00:00
|
|
|
}
|
2020-02-16 21:06:37 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 01:11:58 +00:00
|
|
|
if *pngF {
|
2020-02-29 11:17:14 +00:00
|
|
|
prefix = "_"
|
2020-02-26 00:01:35 +00:00
|
|
|
|
2020-03-01 01:11:58 +00:00
|
|
|
if *addFrameF {
|
2020-02-29 11:17:14 +00:00
|
|
|
params.WriteString(switchesFlags[12])
|
2020-02-16 21:06:37 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 01:11:58 +00:00
|
|
|
if *transparencyF >= 0 && *transparencyF <= 255 {
|
2020-02-29 11:17:14 +00:00
|
|
|
params.WriteString("_transparency=")
|
2020-03-01 01:11:58 +00:00
|
|
|
params.WriteString(strconv.Itoa(*transparencyF))
|
2020-02-29 11:17:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
params.WriteString(".png")
|
|
|
|
downloadMode = true
|
2020-02-16 21:06:37 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 11:17:14 +00:00
|
|
|
var affix strings.Builder
|
|
|
|
affix.WriteString(prefix)
|
|
|
|
affix.WriteString(params.String())
|
2020-02-16 21:06:37 +00:00
|
|
|
|
2020-02-29 11:17:14 +00:00
|
|
|
return affix.String(), downloadMode, nil
|
2020-02-16 21:06:37 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 00:01:35 +00:00
|
|
|
// Defining the command-line interface
|
|
|
|
var (
|
2020-02-29 09:34:00 +00:00
|
|
|
app = kingpin.New("go-cmw", "A small terminal wrapper around the wttr.in weather endpoint.")
|
2020-02-29 11:37:17 +00:00
|
|
|
location = app.Flag("location", "Specify explicite location (--expert-mode)").Short('L').OverrideDefaultFromEnvar("GO_CMW_LOCATION").String()
|
|
|
|
language = app.Flag("language", "Specify explicite language (--expert-mode)").Short('l').OverrideDefaultFromEnvar("GO_CMW_LANGUAGE").String()
|
2020-02-29 11:17:14 +00:00
|
|
|
v2 = app.Flag("v2", "Display Data-Rich output").Default("false").Bool()
|
2020-02-29 11:50:37 +00:00
|
|
|
switches = []*bool{
|
2020-02-29 09:34:00 +00:00
|
|
|
app.Flag("metric", "Display weather in metric").Short('m').Default("false").Bool(),
|
|
|
|
app.Flag("uscs", "Display weather in imperial").Short('u').Default("false").Bool(),
|
|
|
|
app.Flag("meter-second", "Display wind in m/s").Short('M').Default("false").Bool(),
|
2020-02-29 11:17:14 +00:00
|
|
|
app.Flag("zero", "Display current weather").Short('z').Default("false").Bool(),
|
|
|
|
app.Flag("one", "Display current weather + 1 day").Short('o').Default("false").Bool(),
|
|
|
|
app.Flag("two", "Display current weather + 2 days").Short('w').Default("false").Bool(),
|
|
|
|
app.Flag("ignore-user-agent", "Ignore User-Agent and force ANSI output").Short('A').Default("false").Bool(),
|
|
|
|
app.Flag("follow-link", "Do not display the 'Follow' line").Short('F').Default("true").Bool(),
|
|
|
|
app.Flag("narrow", "Display narrow view (only day/night)").Short('n').Default("false").Bool(),
|
|
|
|
app.Flag("quiet", "Display quiet version (no weather report)").Short('q').Default("false").Bool(),
|
|
|
|
app.Flag("super-quiet", "Display super quiet version (no weather report and no city)").Short('Q').Default("false").Bool(),
|
|
|
|
app.Flag("no-colors", "Display no colors (always enabled on windows").Short('N').Default("false").Bool()}
|
|
|
|
|
|
|
|
png = app.Flag("png", "Download a weather PNG image").Short('P').Default("false").Bool()
|
|
|
|
addFrame = app.Flag("add-frame", "Add a frame to the output (PNG only)").Short('p').Default("false").Bool()
|
2020-03-06 19:15:46 +00:00
|
|
|
transparency = app.Flag("transparency", "Set transparency level (0-255) (PNG only) (--expert-mode)").OverrideDefaultFromEnvar("GO_CMW_TRANSPARENCY").Short('t').Default("255").Int()
|
2020-02-29 11:37:17 +00:00
|
|
|
download = app.Flag("download", "Enables download mode (--expert-mode)").Short('d').Default("false").Bool()
|
|
|
|
fileName = app.Flag("file-name", "Name download file (--expert-mode)").OverrideDefaultFromEnvar("GO_CMW_FILE_NAME").Default("").String()
|
2020-02-29 11:17:14 +00:00
|
|
|
oneLiner = app.Flag("one-liner", "One liner outpet (for the terminal multiplexer lovers out there)").Short('O').Default("false").Bool()
|
|
|
|
format = app.Flag("format", "Specify a format query (e.g. \"%l:+%c+%t\") (--expert-mode)").OverrideDefaultFromEnvar("GO_CMW_FORMAT").String()
|
|
|
|
freeStyle = app.Flag("free-style", "Specify a free-style API call (--expert-mode)").OverrideDefaultFromEnvar("GO_CMW_FREE_STYLE").String()
|
|
|
|
expertMode = app.Flag("expert-mode", "Print expert mode information").Default("false").Bool()
|
2020-02-26 00:01:35 +00:00
|
|
|
)
|
|
|
|
|
2020-02-26 19:07:27 +00:00
|
|
|
// Special help menu
|
2020-02-29 11:17:14 +00:00
|
|
|
func printExpertMode() {
|
2020-02-26 19:07:27 +00:00
|
|
|
eInfo := `
|
2020-02-29 11:17:14 +00:00
|
|
|
Expert Mode
|
|
|
|
-----------
|
|
|
|
|
|
|
|
All commands flagged with --expert-mode override
|
|
|
|
values/flags added by go-cmw.
|
|
|
|
|
|
|
|
This gives the user full control over
|
|
|
|
the queries requested.
|
|
|
|
|
|
|
|
Environment Variables
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
go-cmw makes use of the following environment
|
|
|
|
variables:
|
|
|
|
|
|
|
|
* GO_CMW_LOCATION for --location
|
|
|
|
* GO_CMW_LANGUAGE for --language
|
|
|
|
* GO_CMW_FORMAT for --format
|
|
|
|
* GO_CMW_FREE_STYLE for --free-style
|
|
|
|
* GO_CMW_FILE_NAME for --file-name
|
|
|
|
* GO_CMW_TRANSPARENCY for --transparency
|
|
|
|
|
2020-02-26 19:07:27 +00:00
|
|
|
Supported Location Types
|
|
|
|
------------------------
|
2020-02-29 11:17:14 +00:00
|
|
|
|
2020-02-26 19:07:27 +00:00
|
|
|
City name: Paris
|
|
|
|
Unicode name: Москва
|
|
|
|
Airport code (3 letters): muc
|
|
|
|
Domain name: @stackoverflow.com
|
|
|
|
Area code: 94107
|
|
|
|
GPS coordinates: -78.46,106.79
|
|
|
|
|
|
|
|
Special Location
|
|
|
|
----------------
|
2020-02-29 11:17:14 +00:00
|
|
|
|
2020-02-26 19:07:27 +00:00
|
|
|
Moon phase (add ,+US or
|
|
|
|
,+France for these cities): moon
|
|
|
|
Moon phase for a date: moon@2016-10-25
|
|
|
|
|
2020-02-29 11:17:14 +00:00
|
|
|
Format Flags (--format)
|
|
|
|
------------
|
|
|
|
|
|
|
|
c Weather condition
|
|
|
|
C Weather condition textual name
|
|
|
|
h Humidity
|
|
|
|
t Temperature
|
|
|
|
w Wind
|
|
|
|
l Location
|
|
|
|
m Moonphase
|
|
|
|
M Moonday
|
|
|
|
p precipitation (mm)
|
|
|
|
o Probability of Precipitation
|
|
|
|
P pressure (hPa)
|
|
|
|
|
2020-02-26 19:07:27 +00:00
|
|
|
Supported languages
|
|
|
|
-------------------
|
|
|
|
|
2020-02-29 11:17:14 +00:00
|
|
|
Supported: af be ca da de el et fr fa hu id it
|
|
|
|
nb nl pl pt-br ro ru tr th uk vi
|
|
|
|
zh-cn zh-tw
|
|
|
|
`
|
2020-02-26 19:07:27 +00:00
|
|
|
|
|
|
|
println(eInfo)
|
|
|
|
}
|
|
|
|
|
2020-02-16 21:06:37 +00:00
|
|
|
// Create a function to parse all the command line parameters
|
2020-02-29 11:37:17 +00:00
|
|
|
// provided.
|
2020-02-29 09:34:00 +00:00
|
|
|
func flagParser() {
|
2020-02-26 00:01:35 +00:00
|
|
|
|
|
|
|
app.Version(VERSION)
|
|
|
|
app.Author(AUTHOR)
|
|
|
|
|
|
|
|
kingpin.MustParse(app.Parse(os.Args[1:]))
|
2020-02-16 21:06:37 +00:00
|
|
|
|
2020-02-29 11:17:14 +00:00
|
|
|
if *expertMode {
|
|
|
|
printExpertMode()
|
2020-02-26 19:07:27 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2020-02-29 09:34:00 +00:00
|
|
|
|
2020-02-29 11:37:17 +00:00
|
|
|
// Windows terminal does not have color encoding
|
2020-02-19 22:36:38 +00:00
|
|
|
// so let's make sure windows users are happy
|
2020-02-29 11:37:17 +00:00
|
|
|
if os.Getenv("TERM") == "" && !*png && *format != "" && *freeStyle != "" {
|
2020-02-29 09:34:00 +00:00
|
|
|
*switches[11] = true
|
2020-02-19 22:36:38 +00:00
|
|
|
}
|
2020-02-16 21:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create a function to generate the url that we'll be calling shortly.
|
2020-02-29 11:17:14 +00:00
|
|
|
func generateURL(domain string, v2 bool, location string, lang string, affix string) (string, map[string]string) {
|
|
|
|
var link strings.Builder
|
2020-02-16 21:06:37 +00:00
|
|
|
var headers = make(map[string]string)
|
|
|
|
|
2020-02-29 11:17:14 +00:00
|
|
|
link.WriteString("https://")
|
2020-02-16 21:06:37 +00:00
|
|
|
|
|
|
|
if v2 {
|
2020-02-29 11:17:14 +00:00
|
|
|
link.WriteString("v2.")
|
2020-02-16 21:06:37 +00:00
|
|
|
}
|
|
|
|
|
2020-02-29 11:17:14 +00:00
|
|
|
link.WriteString(domain)
|
|
|
|
link.WriteString("/")
|
2020-02-16 21:06:37 +00:00
|
|
|
|
|
|
|
if location != "" {
|
2020-02-29 11:17:14 +00:00
|
|
|
link.WriteString(location)
|
2020-02-16 21:06:37 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 19:07:27 +00:00
|
|
|
if affix != "" {
|
2020-02-29 11:17:14 +00:00
|
|
|
link.WriteString(affix)
|
2020-02-16 21:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if lang != "" {
|
|
|
|
headers["Accept-Language"] = lang
|
|
|
|
}
|
|
|
|
|
2020-02-29 11:17:14 +00:00
|
|
|
return link.String(), headers
|
2020-02-16 21:06:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is the main function that glues everything together.
|
|
|
|
func main() {
|
|
|
|
var domain string = "wttr.in"
|
2020-02-29 09:34:00 +00:00
|
|
|
flagParser()
|
2020-03-01 01:11:58 +00:00
|
|
|
affix, downloadFile, _ := generateParamFormat(switches, freeStyle, format, oneLiner, png, addFrame, transparency, download)
|
2020-02-29 09:34:00 +00:00
|
|
|
link, headers := generateURL(domain, *v2, *location, *language, affix)
|
2020-02-29 11:17:14 +00:00
|
|
|
getWheather(link, headers, downloadFile)
|
2020-02-16 21:06:37 +00:00
|
|
|
}
|