This commit is contained in:
Mikolaj 2018-11-21 20:31:05 +01:00
parent 02a74fe9b2
commit 8606fdf632

62
gotut.go Executable file
View File

@ -0,0 +1,62 @@
package main
import (
"encoding/xml"
"fmt"
"html/template"
"io/ioutil"
"net/http"
)
type NewsMap struct {
Keyword string
Location string
}
type NewsAggPage struct {
Title string
News map[string]NewsMap
}
type Sitemapindex struct {
Locations []string `xml:"sitemap>loc"`
}
type News struct {
Titles []string `xml:"url>news>title"`
Keywords []string `xml:"url>news>keywords"`
Locations []string `xml:"url>loc"`
}
func indexHandler(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "<h1>Whoa, Go is neat!</h1>")
}
func newsAggHandler(w http.ResponseWriter, r *http.Request) {
var s Sitemapindex
var n News
resp, _ := http.Get("https://www.washingtonpost.com/news-sitemap-index.xml")
bytes, _ := ioutil.ReadAll(resp.Body)
xml.Unmarshal(bytes, &s)
news_map := make(map[string]NewsMap)
for _, Location := range s.Locations {
resp, _ := http.Get(Location)
bytes, _ := ioutil.ReadAll(resp.Body)
xml.Unmarshal(bytes, &n)
for idx, _ := range n.Keywords {
news_map[n.Titles[idx]] = NewsMap{n.Keywords[idx], n.Locations[idx]}
}
}
p := NewsAggPage{Title: "Amazing News Aggregator", News: news_map}
t, _ := template.ParseFiles("newsaggtemplate.html")
t.Execute(w, p)
}
func main() {
//http.HandleFunc("/", indexHandler)
http.HandleFunc("/", newsAggHandler)
http.ListenAndServe(":8000", nil)
}