-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
134 lines (110 loc) · 3.27 KB
/
main.go
File metadata and controls
134 lines (110 loc) · 3.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package main
import (
"bufio"
"fmt"
"math"
"os"
"regexp"
"strconv"
"strings"
"github.com/MD-2016/TerminalWeather/formatweatherreport"
"github.com/gocolly/colly"
)
type WeatherReport struct {
Location string
Temp string
Condition string
}
const WEBSITE_URL = "https://www.wunderground.com/weather/"
func main() {
fmt.Println("TERMINAL WEATHER REPORT")
input, err := handleInput()
if err != nil {
panic(err.Error())
}
re, _ := regexp.Compile("[A-Za-z,]+")
if inputValid := re.MatchString(input); !inputValid {
panic("Input must only contain letters")
}
inputSlice := strings.Split(input, ",")
data, err := FormatReport(inputSlice)
if err != nil {
panic(err.Error())
}
if err := formatweatherreport.ValidateInput(data); err != nil {
panic(err.Error())
}
weatherReport := GetReport(data)
if weatherReport.Location == "undefined" {
fmt.Println("the location either doesn't exist or isn't supported by the site \n")
fmt.Println("check wunderground site for a PWS closest to that city")
os.Exit(0)
}
temp, err := strconv.ParseFloat(weatherReport.Temp, 64)
if err != nil {
fmt.Println("error processing temp")
}
celsius := GetCelsiusTemp(temp)
fmt.Printf("The temp in %s is %s° F or %d° C and conditions are %s\n", weatherReport.Location, weatherReport.Temp, celsius, weatherReport.Condition)
}
func handleInput() (string, error) {
read := bufio.NewReader(os.Stdin)
fmt.Println("Enter your country abbrevation followed by a comma and then city")
fmt.Println("US Cities require 3 inputs which are us, state, city")
input, err := read.ReadString('\n')
input = strings.ToLower(input)
if err != nil {
return input, fmt.Errorf("Input cannot be processed")
}
return input, err
}
func FormatReport(input []string) (formatweatherreport.ReportData, error) {
var rd = formatweatherreport.ReportData{}
if len(input) == 3 {
rd.CountryAbbrev = input[0]
rd.State = input[1]
rd.City = input[2]
} else if len(input) == 2 {
rd.CountryAbbrev = input[0]
rd.City = input[1]
if rd.CountryAbbrev == "us" {
return rd, fmt.Errorf("US requires 3 inputs")
}
} else {
return rd, fmt.Errorf("must have country abbrev, city or us, state, city")
}
formatweatherreport.FormatDataForReport(&rd)
return rd, nil
}
func GetReport(data formatweatherreport.ReportData) WeatherReport {
url := ""
report := WeatherReport{}
if data.State != "" {
url = fmt.Sprintf(WEBSITE_URL+"/%s/%s/%s", data.CountryAbbrev, data.State, data.City)
} else {
url = fmt.Sprintf(WEBSITE_URL+"/%s/%s", data.CountryAbbrev, data.City)
}
c := colly.NewCollector()
c.OnError(func(_ *colly.Response, err error) {
fmt.Println("Error processing request", err)
})
c.OnHTML(".city-header > h1:nth-child(2) > span:nth-child(1)",
func(e *colly.HTMLElement) {
res := strings.Split(e.Text, " Weather Conditions")
//fmt.Println(res)
report.Location = strings.Join(res, " ")
})
c.OnHTML(".condition-icon > p:nth-child(2)", func(e *colly.HTMLElement) {
report.Condition = e.Text
})
c.OnHTML(".current-temp > lib-display-unit:nth-child(1) > span:nth-child(1) > span:nth-child(1)",
func(e *colly.HTMLElement) {
report.Temp = e.Text
})
c.Visit(url)
return report
}
func GetCelsiusTemp(temp float64) int {
celsius := math.Ceil((temp - 32) * (5.0 / 9.0))
return int(celsius)
}