-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (146 loc) · 4.46 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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"bufio"
"encoding/xml"
"html/template"
"net/http"
"os"
"time"
"github.com/apex/log"
"github.com/apex/log/handlers/logfmt"
"github.com/dustin/go-humanize"
)
var funcs = template.FuncMap{
"humanize_bytes": humanize.Bytes,
}
var views = template.Must(template.New("").Funcs(funcs).ParseGlob("views/*.html"))
func main() {
log.SetHandler(logfmt.New(os.Stdout))
addr := ":" + os.Getenv("PORT")
http.HandleFunc("/submit", submit)
http.HandleFunc("/", index)
if err := http.ListenAndServe(addr, nil); err != nil {
log.Fatalf("error listening: %s", err)
}
}
func index(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
views.ExecuteTemplate(w, "index.html", nil)
}
func submit(w http.ResponseWriter, r *http.Request) {
file, hdr, err := r.FormFile("xml")
if err != nil {
log.WithError(err).Error("parsing form")
http.Error(w, "Error parsing form.", http.StatusBadRequest)
return
}
defer file.Close()
reader := bufio.NewReader(file)
decoder := xml.NewDecoder(reader)
var item EPGtv
for {
token, _ := decoder.Token()
if token == nil {
break
}
switch se := token.(type) {
case xml.StartElement:
decoder.DecodeElement(&item, &se)
}
}
w.Header().Set("Content-Type", "text/html")
err = views.ExecuteTemplate(w, "index.html", struct {
Name string
Size uint64
Type string
EPGtv EPGtv
}{
Name: hdr.Filename,
Size: uint64(hdr.Size),
Type: hdr.Header.Get("Content-Type"),
EPGtv: item,
})
if err != nil {
log.WithError(err).Error("parsing form")
http.Error(w, "Error parsing form.", http.StatusBadRequest)
return
}
}
type EPGcatchup struct {
Text string `xml:",chardata" json:",omitempty"`
}
type EPGcategory struct {
Text string `xml:",chardata" json:",omitempty"`
}
type EPGcna_rating struct {
Text string `xml:",chardata" json:",omitempty"`
}
type EPGcredits struct {
}
type EPGdesc struct {
Text string `xml:",chardata" json:",omitempty"`
}
type EPGepisode_num struct {
Text string `xml:",chardata" json:",omitempty"`
}
type EPGformat struct {
Text string `xml:",chardata" json:",omitempty"`
}
type EPGicon struct {
Attr_src string `xml:" src,attr" json:",omitempty"`
}
type EPGprogramme struct {
Attr_channel string `xml:" channel,attr" json:",omitempty"`
Attr_id string `xml:" id,attr" json:",omitempty"`
Start customTime `xml:" start,attr" json:",omitempty"`
Stop customTime `xml:" stop,attr" json:",omitempty"`
EPGcatchup *EPGcatchup `xml:" catchup,omitempty" json:"catchup,omitempty"`
EPGcategory *EPGcategory `xml:" category,omitempty" json:"category,omitempty"`
EPGcna_rating *EPGcna_rating `xml:" cna-rating,omitempty" json:"cna-rating,omitempty"`
EPGcredits *EPGcredits `xml:" credits,omitempty" json:"credits,omitempty"`
EPGdesc *EPGdesc `xml:" desc,omitempty" json:"desc,omitempty"`
EPGformat *EPGformat `xml:" format,omitempty" json:"format,omitempty"`
EPGicon *EPGicon `xml:" icon,omitempty" json:"icon,omitempty"`
EPGreplay *EPGreplay `xml:" replay,omitempty" json:"replay,omitempty"`
EPGseries *EPGseries `xml:" series,omitempty" json:"series,omitempty"`
EPGtitle *EPGtitle `xml:" title,omitempty" json:"title,omitempty"`
}
type EPGreplay struct {
Text string `xml:",chardata" json:",omitempty"`
}
type EPGroot struct {
EPGtv *EPGtv `xml:" tv,omitempty" json:"tv,omitempty"`
}
type EPGseason_num struct {
Text string `xml:",chardata" json:",omitempty"`
}
type EPGseries struct {
EPGepisode_num *EPGepisode_num `xml:" episode-num,omitempty" json:"episode-num,omitempty"`
EPGseason_num *EPGseason_num `xml:" season-num,omitempty" json:"season-num,omitempty"`
EPGseries_id *EPGseries_id `xml:" series-id,omitempty" json:"series-id,omitempty"`
EPGseries_name *EPGseries_name `xml:" series-name,omitempty" json:"series-name,omitempty"`
}
type EPGseries_id struct {
Text string `xml:",chardata" json:",omitempty"`
}
type EPGseries_name struct {
Text string `xml:",chardata" json:",omitempty"`
}
type EPGtitle struct {
Text string `xml:",chardata" json:",omitempty"`
}
type EPGtv struct {
EPGprogramme []*EPGprogramme `xml:" programme,omitempty" json:"programme,omitempty"`
}
type customTime struct {
time.Time
}
func (c *customTime) UnmarshalXMLAttr(attr xml.Attr) error {
const shortForm = "20060102150405"
parse, err := time.Parse(shortForm, attr.Value)
if err != nil {
return err
}
*c = customTime{parse}
return nil
}