-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
147 lines (121 loc) · 3.21 KB
/
Copy pathmain.go
File metadata and controls
147 lines (121 loc) · 3.21 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
135
136
137
138
139
140
141
142
143
144
145
146
147
package main
import (
"context"
_ "embed"
"encoding/base64"
"encoding/json"
"encoding/xml"
"fmt"
"log"
"net/http"
"net/url"
"os"
"sort"
"strings"
"time"
"golang.org/x/tools/blog/atom"
)
//go:embed index.html
var index []byte
func main() {
apiKey := os.Getenv("GOOGLE_MAPS_API_KEY")
feedUpdated := time.Date(2022, 10, 15, 0, 0, 0, 0, time.UTC)
mux := http.NewServeMux()
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write(index)
})
mux.HandleFunc("/atom.xml", func(w http.ResponseWriter, r *http.Request) {
ls := r.URL.Query()["l"]
if len(ls) == 0 {
http.Error(w, "need ls", http.StatusBadRequest)
return
}
sort.Strings(ls)
fenc := base64.RawURLEncoding.EncodeToString([]byte(strings.Join(ls, " ")))
feed := atom.Feed{
Title: "Street view updates",
ID: "urn:street-view-updates:feed:" + fenc,
Updated: atom.Time(feedUpdated),
}
for _, l := range ls {
enc := base64.RawURLEncoding.EncodeToString([]byte(l))
updated, err := check(r.Context(), apiKey, l)
if err != nil {
log.Println(l, err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
if !updated.IsZero() {
n := updated.Unix()
feed.Entry = append(feed.Entry, &atom.Entry{
Title: "Update for " + l,
ID: "urn:street-view-updates:item:" + enc + ":" + updated.Format("20060102"),
Link: []atom.Link{{Href: "https://www.google.com/maps/place/" + url.QueryEscape(l) + "?n=" + fmt.Sprint(n)}},
Content: &atom.Text{Type: "text", Body: l + " was updated " + updated.Format("2006-01-02")},
})
}
}
w.Header().Set("Content-Type", "application/atom+xml")
enc := xml.NewEncoder(w)
enc.Indent("", " ")
if err := enc.Encode(feed); err != nil {
log.Println(ls, err)
http.Error(w, "error", http.StatusInternalServerError)
return
}
})
addr := os.Getenv("ADDR")
port := os.Getenv("PORT")
if addr == "" {
if port != "" {
addr = ":" + port
} else {
addr = "127.0.0.1:8000"
}
}
srv := &http.Server{
Addr: addr,
Handler: mux,
}
log.Fatal(srv.ListenAndServe())
}
func check(ctx context.Context, apiKey, location string) (time.Time, error) {
v := make(url.Values)
v.Set("key", apiKey)
v.Set("location", location)
ctx, cancel := context.WithTimeout(ctx, 10*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://maps.googleapis.com/maps/api/streetview/metadata?"+v.Encode(), nil)
if err != nil {
return time.Time{}, err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return time.Time{}, err
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
return time.Time{}, fmt.Errorf("bad HTTP status %d", resp.StatusCode)
}
var body struct {
Status string
Date string
}
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil {
return time.Time{}, err
}
switch body.Status {
case "OK":
case "ZERO_RESULTS":
return time.Time{}, nil
default:
return time.Time{}, fmt.Errorf("bad body status %s", body.Status)
}
for _, l := range []string{"2006-01-02", "2006-01", "2006"} {
t, err := time.Parse(l, body.Date)
if err == nil {
return t, nil
}
}
return time.Time{}, fmt.Errorf("bad body date %s", body.Date)
}