-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeedReader.go
More file actions
139 lines (130 loc) · 2.54 KB
/
feedReader.go
File metadata and controls
139 lines (130 loc) · 2.54 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package main
import (
"archive/zip"
"bytes"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/go-redis/redis"
)
const (
httpDirectory = "http://bitly.com/nuvi-plz"
listName = "NEWS_XML"
redisHost = "localhost:6379"
redisPass = ""
redisDB = 0
)
func getZipListings(directory string) (downloadList []string, err error) {
response, err := http.Get(directory)
if err != nil {
return
}
URL := response.Request.URL.String()
d, err := goquery.NewDocumentFromResponse(response)
if err != nil {
return
}
d.Find("td a").Each(func(i int, s *goquery.Selection) {
name, ok := s.Attr("href")
if !ok {
return
}
correctFileType := strings.Contains(name, ".zip")
if !correctFileType {
return
}
downloadList = append(downloadList, URL+name)
})
return
}
func getZip(URL string) (zipFile *os.File, err error) {
resp, err := http.Get(URL)
if err != nil {
return
}
defer resp.Body.Close()
zipFile, err = ioutil.TempFile("./", "download")
if err != nil {
return
}
defer zipFile.Close()
_, err = io.Copy(zipFile, resp.Body)
return
}
func openZip(zipFile *os.File) (zipReader *zip.ReadCloser, err error) {
zipReader, err = zip.OpenReader(zipFile.Name())
return
}
func pushXML(f *zip.File, client *redis.Client) (index int64, err error) {
rc, err := f.Open()
if err != nil {
return
}
defer rc.Close()
buff := bytes.NewBuffer(nil)
_, err = io.Copy(buff, rc)
if err != nil {
return
}
length, err := client.LLen(listName).Result()
if err != nil {
return
}
listItems, err := client.LRange(listName, 0, length).Result()
if err != nil {
return
}
for _, v := range listItems {
if buff.String() == v {
return
}
}
index, err = client.RPush(listName, buff.Bytes()).Result()
if err != nil {
return
}
return
}
func setupRedis() (client *redis.Client, err error) {
client = redis.NewClient(&redis.Options{
Addr: redisHost,
Password: redisPass,
DB: redisDB,
})
_, err = client.Ping().Result()
return
}
func main() {
downloadList, err := getZipListings(httpDirectory)
if err != nil {
log.Print(err)
return
}
rclient, err := setupRedis()
if err != nil {
log.Print(err)
return
}
for _, v := range downloadList {
zipFile, err := getZip(v)
if err != nil {
log.Print(err)
return
}
defer zipFile.Close()
defer os.Remove(zipFile.Name())
zipReader, err := openZip(zipFile)
if err != nil {
log.Print(err)
return
}
defer zipReader.Close()
for _, f := range zipReader.File {
_, err = pushXML(f, rclient)
}
}
}