added flags!
This commit is contained in:
parent
1993a207de
commit
e53081277d
102
main.go
102
main.go
@ -2,7 +2,11 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"log"
|
||||||
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -67,49 +71,33 @@ type Sys struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
/*
|
//set up the flags for the program to use!
|
||||||
//setting up openweathermap api
|
var city, code string
|
||||||
lat := "56.122970"
|
flag.StringVar(&city, "city", "London", "Enter city e.g. London, Glasgow etc...")
|
||||||
lon := "-3.932390"
|
flag.StringVar(&code, "code", "uk", "Enter Code e.g. uk, us etc...")
|
||||||
//city := "Stirling, uk"
|
|
||||||
api_key := "29e5139b86638988d333a28ba360bfd9"
|
|
||||||
full := "https://api.openweathermap.org/data/2.5/weather?lat=" + lat + "&lon=" + lon + "&appid=" + api_key + ""
|
|
||||||
fmt.Printf("\n------\nWeather url: %+s\n---------\n", full)
|
|
||||||
|
|
||||||
//read webpage
|
flag.Parse()
|
||||||
url := full
|
//setting up openweathermap api
|
||||||
resp, err := http.Get(url)
|
api_key := "29e5139b86638988d333a28ba360bfd9"
|
||||||
//reads html as a slice of bytes
|
full := "https://api.openweathermap.org/data/2.5/weather?q=" + city + "," + code + "&units=metric" + "&appid=" + api_key + ""
|
||||||
html, err := ioutil.ReadAll(resp.Body)
|
fmt.Printf("\n------\nWeather url: %+s\n---------\n", full)
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
//print the html
|
|
||||||
//fmt.Printf("%s\n", html)
|
|
||||||
|
|
||||||
*/
|
//read webpage
|
||||||
/*
|
url := full
|
||||||
jsonFile, err := os.Open("stirling.json")
|
resp, err := http.Get(url)
|
||||||
// if we os.Open returns an error then handle it
|
//reads html as a slice of bytes
|
||||||
if err != nil {
|
html, err := ioutil.ReadAll(resp.Body)
|
||||||
fmt.Println(err)
|
if err != nil {
|
||||||
}
|
panic(err)
|
||||||
fmt.Println("Successfully Opened stirling.json")
|
}
|
||||||
defer jsonFile.Close()
|
//print the html
|
||||||
byteValue, _ := ioutil.ReadAll(jsonFile)
|
fmt.Printf("%s\n", html)
|
||||||
|
fmt.Println("---------- JSON PARSED -----------\n\n")
|
||||||
|
|
||||||
var data WeatherAPI
|
//output the html to stirling.json
|
||||||
|
CreateFile(string(html))
|
||||||
|
|
||||||
error := json.Unmarshal([]byte(byteValue), &data)
|
data, _ := LoadConfiguration("weather.json")
|
||||||
if error != nil {
|
|
||||||
fmt.Println(error)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
//json.Unmarshal(byteValue, &data)
|
|
||||||
|
|
||||||
fmt.Println(data)
|
|
||||||
*/
|
|
||||||
data, _ := LoadConfiguration("./stirling.json")
|
|
||||||
fmt.Println(data)
|
fmt.Println(data)
|
||||||
}
|
}
|
||||||
func LoadConfiguration(file string) (WeatherAPI, error) {
|
func LoadConfiguration(file string) (WeatherAPI, error) {
|
||||||
@ -122,3 +110,39 @@ func LoadConfiguration(file string) (WeatherAPI, error) {
|
|||||||
jsonParser.Decode(&config)
|
jsonParser.Decode(&config)
|
||||||
return config, err
|
return config, err
|
||||||
}
|
}
|
||||||
|
func CreateFile(fileContents string) {
|
||||||
|
|
||||||
|
// fmt package implements formatted
|
||||||
|
// I/O and has functions like Printf
|
||||||
|
// and Scanf
|
||||||
|
fmt.Printf("Writing to a file in Go lang\n")
|
||||||
|
|
||||||
|
// in case an error is thrown it is received
|
||||||
|
// by the err variable and Fatalf method of
|
||||||
|
// log prints the error message and stops
|
||||||
|
// program execution
|
||||||
|
file, err := os.Create("weather.json")
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed creating file: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Defer is used for purposes of cleanup like
|
||||||
|
// closing a running file after the file has
|
||||||
|
// been written and main //function has
|
||||||
|
// completed execution
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
// len variable captures the length
|
||||||
|
// of the string written to the file.
|
||||||
|
len, err := file.WriteString(fileContents)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
log.Fatalf("failed writing to file: %s", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Name() method returns the name of the
|
||||||
|
// file as presented to Create() method.
|
||||||
|
fmt.Printf("\nFile Name: %s", file.Name())
|
||||||
|
fmt.Printf("\nLength: %d bytes", len)
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user