-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
71 lines (61 loc) · 1.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
package main
import (
"encoding/xml"
"html/template"
"io/ioutil"
"net/http"
"github.com/prometheus/common/log"
)
type SitemapIndex struct {
Locations []string `xml:"sitemap>loc"`
}
type NewsMap struct {
Loc []string `xml:"url>loc"`
UpdateSchedule []string `xml:"url>changefreq"`
}
type News struct {
Loc string
UpdateSchedule string
}
type NewsAggPAge struct {
Title string
News map[int]News
}
func main() {
http.HandleFunc("/news", newsAggHandler)
log.Fatal(http.ListenAndServe(":9000", nil))
}
func newsAggHandler(w http.ResponseWriter, r *http.Request) {
var siteMap SitemapIndex
var newMap NewsMap
n1 := make(map[int]News)
res, err := http.Get("https://timesofindia.indiatimes.com/travel/staticsitemap/htpoi/sitemap-index.xml")
if err != nil {
panic(err)
}
bytes, _ := ioutil.ReadAll(res.Body)
//fmt.Fprint(w, string(bytes))
xml.Unmarshal(bytes, &siteMap)
//fmt.Fprintf(w,"%v",siteMap )
for _, x := range siteMap.Locations {
//fmt.Print(x)
resp, err := http.Get(x)
if err != nil {
panic(err)
}
bytes, _ := ioutil.ReadAll(resp.Body)
xml.Unmarshal(bytes, &newMap)
//fmt.Fprint(w, string(bytes))
for i, _ := range newMap.Loc {
//fmt.Print("idhr aya")
n1[i] = News{newMap.Loc[i], newMap.UpdateSchedule[i]}
}
}
//fmt.Fprintf(w, "%v", n1)
news := NewsAggPAge{"Awesome NewsAggPAge", n1}
t := template.Must(template.ParseFiles("index.html"))
err = t.Execute(w, news)
if err != nil {
panic(err)
}
}