-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdouban.go
79 lines (67 loc) · 1.65 KB
/
douban.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
package main
import (
"encoding/xml"
"fmt"
"io/ioutil"
"net/http"
"strings"
"sync"
)
const (
feedUrl = "https://www.douban.com/feed/people/debuginn/interests"
)
type MoviesData struct {
Movies map[int]interface{}
sync.RWMutex
}
// SetMoviesData .
func (m *MoviesData) SetMoviesData() {
data, err := m.GetXMLData(feedUrl)
if err != nil {
fmt.Printf("read xml file faild, err:%v\n", err)
}
v := Attributes{}
unMarshalErr := xml.Unmarshal(data, &v)
if unMarshalErr != nil {
fmt.Printf("xml unmarshal faild, err:%v\n", err)
}
movieItem := v.Channel.MovieItem
MoviesMap := make(map[int]interface{}, len(movieItem))
for i := 0; i < len(movieItem); i++ {
movie := make(map[string]string)
description := strings.Split(movieItem[i].Description, "\"")
movie["Title"] = string([]rune(movieItem[i].Title)[2:])
movie["Link"] = movieItem[i].Link
movie["Img"] = description[7]
movie["Pubdate"] = movieItem[i].Pubdate
MoviesMap[i] = movie
}
m.Lock()
m.Movies = MoviesMap
m.Unlock()
}
// GetMoviesData .
func (m *MoviesData) GetMoviesData() map[int]interface{} {
m.RLock()
defer m.RUnlock()
return m.Movies
}
// GetXMLData 获取 xml 文件数据
func (m *MoviesData) GetXMLData(url string) (data []byte, err error) {
// 读取 xml 文件
client := &http.Client{}
req, _ := http.NewRequest("GET", url, nil)
// 自定义Header
req.Header.Set("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)")
resp, err := client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close() // 关闭文件
// 读取所有文件内容保存至 []byte
data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
return
}