-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
109 lines (100 loc) · 2.88 KB
/
main.go
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
package main
import (
"fmt"
"io/ioutil"
"net/http"
"regexp"
"strconv"
"time"
"github.com/tidwall/gjson"
"github.com/tidwall/sjson"
)
func main() {
// https://www.timeanddate.com/weather/@z-us-08008/climate
url := "https://www.timeanddate.com/weather/@z-us-"
client := &http.Client{}
counter := 499
max := 100000
for ; counter <= max; counter++ {
time.Sleep(200 * time.Millisecond)
zipCode := ""
if counter < 10 {
zipCode = "0000" + strconv.Itoa(counter)
} else if counter < 100 {
zipCode = "000" + strconv.Itoa(counter)
} else if counter < 1000 {
zipCode = "00" + strconv.Itoa(counter)
} else if counter < 10000 {
zipCode = "0" + strconv.Itoa(counter)
} else if counter < 100000 {
zipCode = strconv.Itoa(counter)
}
req, err := http.NewRequest("GET", url+zipCode+"/climate", nil)
if err != nil {
fmt.Println(zipCode)
continue
}
response, err := client.Do(req)
if err != nil {
fmt.Printf("error getting response from client when streaming from location:\n%v\n%s", response, err.Error())
fmt.Println(zipCode)
continue
}
if response.StatusCode != 200 {
fmt.Printf("zip: %v failed stauts: %v\n", zipCode, response.StatusCode)
continue
}
content, err := ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("error reading data from response body:\n%s", err.Error())
fmt.Println(zipCode)
continue
}
strContent := string(content)
re := regexp.MustCompile(`var data.*`)
climateData := re.FindString(strContent)
urlZipCode := "https://api.promaptools.com/service/us/zip-lat-lng/get/?zip="
key := "&key=17o8dysaCDrgv1c"
req, err = http.NewRequest("GET", urlZipCode+zipCode+key, nil)
if err != nil {
fmt.Printf("error requesting: %v %v\n", zipCode, err.Error())
continue
}
response, err = client.Do(req)
if err != nil {
fmt.Printf("error getting response from client when streaming from location:\n%v\n%s", response, err.Error())
fmt.Println(zipCode)
continue
}
if response.StatusCode != 200 {
fmt.Printf("zip: %v status: %v\n", zipCode, response.StatusCode)
continue
}
content, err = ioutil.ReadAll(response.Body)
if err != nil {
fmt.Printf("error reading data from response body:\n%s", err.Error())
fmt.Println(zipCode)
continue
}
status := gjson.Get(string(content), "status").String()
if status != "1" {
continue
}
coords := gjson.Get(string(content), "output").Array()[0]
lat := coords.Get("latitude").String()
long := coords.Get("longitude").String()
if len(string(climateData)) > 0 {
fmt.Printf("zip:%v\n", zipCode)
value, err := sjson.Set(string(climateData)[9:], "lat", lat)
if err != nil {
fmt.Printf("error on zip:%v\n", zipCode)
}
value, _ = sjson.Set(value, "long", long)
err = ioutil.WriteFile("./data/"+zipCode+".txt", []byte(value), 0644)
if err != nil {
fmt.Printf("error writing %v\n", zipCode)
continue
}
}
}
}