-
Notifications
You must be signed in to change notification settings - Fork 2
/
atom_crawler.go
101 lines (89 loc) · 2.09 KB
/
atom_crawler.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
package feeder
import (
"encoding/xml"
"fmt"
"net/http"
"time"
"github.com/p1ass/feeds"
"github.com/pkg/errors"
)
type atomCrawler struct {
URL string
}
// NewAtomCrawler returns atomCrawler
func NewAtomCrawler(url string) Crawler {
return &atomCrawler{URL: url}
}
// Crawl is crawl entry items from atom file
func (crawler *atomCrawler) Crawl() ([]*Item, error) {
resp, err := http.Get(crawler.URL)
if err != nil {
return nil, errors.Wrap(err, "Failed to get response from rss.")
}
defer resp.Body.Close()
var atom feeds.AtomFeed
err = xml.NewDecoder(resp.Body).Decode(&atom)
if err != nil {
return nil, errors.Wrap(err, "Failed to decode response body.")
}
items := []*Item{}
for _, e := range atom.Entries {
item, err := convertAtomEntryToItem(e)
if err != nil {
return nil, errors.Wrap(err, "Failed to convert RSSItem to Item.")
}
items = append(items, item)
}
return items, nil
}
func convertAtomEntryToItem(e *feeds.AtomEntry) (*Item, error) {
p, err := time.Parse(time.RFC3339, e.Published)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("Failed to parse published time. published=%v", e.Published))
}
u, err := time.Parse(time.RFC3339, e.Updated)
if err != nil {
return nil, errors.Wrap(err, fmt.Sprintf("Failed to parse updated time. updated=%v", e.Updated))
}
var description string
if e.Summary != nil{
description = e.Summary.Content
}
i := &Item{
Title: e.Title,
Description: description,
ID: e.Id,
Created: &p,
Updated: &u,
}
var name, email string
if e.Author != nil {
name, email = e.Author.Name, e.Author.Email
}
if len(name) > 0 || len(email) > 0 {
i.Author = &Author{
Name: e.Author.Name,
Email: e.Author.Email,
}
}
if e.Content != nil {
i.Content = e.Content.Content
}
for _, link := range e.Links {
if link.Rel == "enclosure" {
i.Enclosure = &Enclosure{
URL: link.Href,
Length: link.Length,
Type: link.Type,
}
} else {
i.Link = &Link{
Href: link.Href,
Rel: link.Rel,
Type: link.Type,
Length: link.Length,
}
}
}
return i, nil
}