-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
114 lines (99 loc) · 2.71 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
110
111
112
113
114
package main
import (
"fmt"
"log"
"net/http"
"regexp"
"strconv"
"time"
"github.com/gorilla/feeds"
"golang.org/x/net/html"
)
var regexInsee = regexp.MustCompile(`^\/(\d{5})$`)
var regexPP = regexp.MustCompile(`^\/panneaupocket\/(\d*)$`)
func createFeed(city *City) (string, error) {
requestURL := fmt.Sprintf("https://app.panneaupocket.com/embeded/%d?mode=widgetTv", city.Id)
resp, err := http.Get(requestURL)
if err != nil {
return "", fmt.Errorf("error while accessing embed api : %s", err)
}
defer resp.Body.Close()
doc, err := html.Parse(resp.Body)
if err != nil {
return "", fmt.Errorf("error while reading embed api : %s", err)
}
header := NewHeader(doc)
feed := &feeds.Feed{
Title: header.Title,
Description: "Actualités de " + city.Name,
Link: &feeds.Link{Href: "https://info-communes.fr"},
Created: time.Now().UTC(),
Id: fmt.Sprint(city.Id),
Items: []*feeds.Item{},
}
for _, s := range header.Article {
item := &feeds.Item{
Title: s.Title,
Description: s.Message,
Created: s.Date,
Id: feed.Id + "/" + s.Id,
Author: &feeds.Author{Name: s.Location},
}
feed.Items = append(feed.Items, item)
}
rss, err := feed.ToRss()
if err != nil {
return "", fmt.Errorf("error while creating rss : %s", err)
}
return rss, nil
}
func renderGeneral(w http.ResponseWriter, r *http.Request) {
insee := regexInsee.FindStringSubmatch(r.URL.Path)
if insee == nil || len(insee) < 2 {
http.Redirect(w, r, "https://info-communes.fr", http.StatusSeeOther)
return
}
city := NewCity(insee[1])
err := city.Populate(true)
if err != nil {
log.Println(err)
http.Redirect(w, r, "https://info-communes.fr", http.StatusSeeOther)
return
}
err = city.GetIdFromPP(true)
if err != nil {
log.Println(err)
http.Redirect(w, r, "https://info-communes.fr", http.StatusSeeOther)
return
}
renderFeed(w, r, city)
}
func renderPanneaupocket(w http.ResponseWriter, r *http.Request) {
pp := regexPP.FindStringSubmatch(r.URL.Path)
if pp == nil || len(pp) < 2 {
http.Redirect(w, r, "https://info-communes.fr", http.StatusSeeOther)
return
}
idPP, err := strconv.Atoi(pp[1])
if err != nil {
log.Println(err)
http.Redirect(w, r, "https://info-communes.fr", http.StatusSeeOther)
return
}
city := &City{Id: idPP}
renderFeed(w, r, city)
}
func renderFeed(w http.ResponseWriter, r *http.Request, city *City) {
rss, err := createFeed(city)
if err != nil {
log.Println(err)
http.Redirect(w, r, "https://info-communes.fr", http.StatusSeeOther)
return
}
w.Write([]byte(rss))
}
func main() {
http.HandleFunc("/panneaupocket/", renderPanneaupocket)
http.HandleFunc("/", renderGeneral)
log.Fatal(http.ListenAndServe("0.0.0.0:8080", nil))
}