/
rsschiba.go
116 lines (104 loc) · 2.58 KB
/
rsschiba.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package main
import (
"fmt"
"strings"
"time"
"github.com/junkichi/botpublisher/rss"
"github.com/junkichi/botpublisher/storage"
"github.com/mmcdole/gofeed"
)
var chibaRssURL = "https://www.pref.chiba.lg.jp/homepage/shinchaku/shinchaku.xml"
var chibaRssCOL = "rss"
// ChibaRSS is the value of image directory
type ChibaRSS struct {
imgdir string
}
// GetImageDir the stored balue
func (r ChibaRSS) GetImageDir() string {
return r.imgdir
}
// Init the RSS
func (ChibaRSS) Init() bool {
s := storage.GetInstance()
var items []*gofeed.Item
err := rss.RetrieveRSS(chibaRssURL, &items)
if err != nil {
fmt.Println("retrieve err:", err)
return false
}
n := 0
skip := 0
for _, item := range items {
pubdate, _ := time.Parse("Mon, 02 Jan 2006 15:04:05 MST", item.Published)
found, err := storage.FindRSS(s, chibaRssCOL, item.Link, pubdate)
if found == true {
skip++
continue
}
found = strings.Contains(item.Title, "新型コロナウイルス")
if found == false {
skip++
continue
}
err = storage.InsertRSS(s, chibaRssCOL, item.Title, item.Link, item.Description, pubdate)
if err != nil {
fmt.Println("insert error:", err)
continue
}
n++
}
fmt.Println("[crss] skipped:", skip)
fmt.Println("[crss] inserted:", n)
return true
}
// Collect the RSS
func (ChibaRSS) Collect(imgdir string) bool {
s := storage.GetInstance()
var items []*gofeed.Item
err := rss.RetrieveRSS(chibaRssURL, &items)
if err != nil {
fmt.Println("retrieve err:", err)
return false
}
n := 0
skip := 0
old := 0
for _, item := range items {
pubdate, _ := time.Parse("Mon, 02 Jan 2006 15:04:05 MST", item.Published)
found, err := storage.FindRSS(s, chibaRssCOL, item.Link, pubdate)
if found == true {
skip++
continue
}
found = strings.Contains(item.Title, "新型コロナウイルス")
if found == false {
skip++
continue
}
err = storage.InsertRSS(s, chibaRssCOL, item.Title, item.Link, item.Description, pubdate)
if err != nil {
fmt.Println("insert error:", err)
continue
}
imgpath := fmt.Sprintf("%s/%d.png", imgdir, time.Now().Unix())
err = rss.TakeScreenShot(item.Link, `#tmp_contents`, imgpath)
if err != nil {
imgpath = ""
}
limitdate := time.Now().Add(-24 * 7 * time.Hour)
if pubdate.Before(limitdate) {
old++
continue
}
desc := fmt.Sprintln("(千葉県)", item.Title, item.Link)
err = storage.InsertPublish(s, publishCOL, desc, imgpath)
if err != nil {
fmt.Println("insert error:", err)
}
n++
}
fmt.Println("[crss] skipped:", skip)
fmt.Println("[crss] old:", old)
fmt.Println("[crss] inserted:", n)
return true
}