/
client.go
74 lines (56 loc) · 1.5 KB
/
client.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
// Copyright 2014 Daniel Pupius
// A PubSubHubbub subscriber client that watches Medium's latest feed.
//
// Usage:
// $ go run example/medium.go --host=[host or ip] --port=[port number]
//
package main
import (
"encoding/xml"
"flag"
"fmt"
"log"
"time"
"github.com/dpup/gohubbub"
)
type Feed struct {
Status string `xml:"status>http"`
Entries []Entry `xml:"entry"`
}
type Entry struct {
URL string `xml:"id"`
Title string `xml:"title"`
Summary string `xml:"summary"`
Author Author `xml:"author"`
}
type Author struct {
Name string `xml:"name"`
}
var host = flag.String("host", "", "Host or IP to serve from")
var port = flag.Int("port", 3000, "The port to serve from")
func main() {
flag.Parse()
log.Println("Content Watcher Started...")
client := gohubbub.NewClient(fmt.Sprintf("%s:%d", *host, *port), "ContentWatcher")
err := client.DiscoverAndSubscribe("http://tantek.com/updates.atom", func(contentType string, body []byte) {
var feed Feed
xmlError := xml.Unmarshal(body, &feed)
if xmlError != nil {
log.Printf("XML Parse Error %v", xmlError)
} else {
for _, entry := range feed.Entries {
log.Printf("%s by %s (%s)", entry.Title, entry.Author.Name, entry.URL)
}
}
})
if err != nil {
log.Fatal(err)
}
go client.StartAndServe("", *port)
time.Sleep(time.Second * 5)
log.Println("Press Enter for graceful shutdown...")
var input string
fmt.Scanln(&input)
client.Unsubscribe("http://tantek.com/updates.atom")
time.Sleep(time.Second * 5)
}