forked from piqoni/matcha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
189 lines (168 loc) · 4.31 KB
/
main.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
package main
import (
"database/sql"
"fmt"
"log"
"net/url"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/mmcdole/gofeed"
"github.com/savioxavier/termlink"
_ "modernc.org/sqlite"
)
var markdown_dir_path string
var mdPrefix, mdSuffix string
var terminal_mode bool = false
var currentDate = time.Now().Format("2006-01-02")
var lat, lon float64
var instapaper bool
var myMap []RSS
var db *sql.DB
type RSS struct {
url string
limit int
disabled bool
}
func check(e error) {
if e != nil {
panic(e)
}
}
func writeLink(title string, url string, newline bool) string {
var content string
if terminal_mode {
content = termlink.Link(title, url)
if newline {
content += "\n"
}
} else {
content = "[" + title + "](" + url + ")"
if newline {
content += "<br>"
}
}
return content
}
func favicon(s *gofeed.Feed) string {
if terminal_mode {
return ""
}
var src string
if s.FeedLink == "" {
// default feed favicon
return "🍵"
} else {
u, err := url.Parse(s.FeedLink)
if err != nil {
fmt.Println(err)
}
src = "https://www.google.com/s2/favicons?sz=32&domain=" + u.Hostname()
}
// if s.Title contains "hacker news"
if strings.Contains(s.Title, "Hacker News") {
src = "https://news.ycombinator.com/favicon.ico"
}
//return html image tag of favicon
return fmt.Sprintf("<img src=\"%s\" width=\"32\" height=\"32\" />", src)
}
func writeToMarkdown(body string) {
if terminal_mode {
fmt.Println(body)
} else {
markdown_file_name := mdPrefix + currentDate + mdSuffix + ".md"
f, err := os.OpenFile(filepath.Join(markdown_dir_path, markdown_file_name), os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
log.Fatal(err)
}
if _, err := f.Write([]byte(body)); err != nil {
log.Fatal(err)
}
if err := f.Close(); err != nil {
log.Fatal(err)
}
}
}
func parseFeedURL(fp *gofeed.Parser, url string) *gofeed.Feed {
feed, err := fp.ParseURL(url)
if err != nil {
return nil
}
return feed
}
func main() {
bootstrapConfig()
// Start writing to markdown
// Display weather if lat and lon are set
if lat != 0 && lon != 0 {
writeToMarkdown(getWeather(lat, lon))
}
fp := gofeed.NewParser()
for _, rss := range myMap {
if rss.disabled {
continue
}
feed := parseFeedURL(fp, rss.url)
if feed == nil {
continue
}
items := ""
for index, item := range feed.Items {
if index == rss.limit {
break
}
var url string
var date string
err := db.QueryRow("SELECT url, date FROM seen where url=?", item.Link).Scan(&url, &date)
if err != nil && err != sql.ErrNoRows {
fmt.Println(err)
}
if url != "" && date == currentDate {
// fmt.Println("Already seen: " + item.Title)
// Article is already in the database and it is for today's date so skip inserting it
} else if url != "" && date != currentDate {
// fmt.Println("Skipping: " + item.Link)
continue
} else {
stmt, err := db.Prepare("INSERT INTO seen(url, date) values(?,?)")
check(err)
res, err := stmt.Exec(item.Link, currentDate)
check(err)
_ = res
stmt.Close()
}
if strings.Contains(feed.Title, "Hacker News") {
// Find Comments URL
first_index := strings.Index(item.Description, "Comments URL") + 23
comments_url := item.Description[first_index : first_index+45]
// Find Comments number
first_comments_index := strings.Index(item.Description, "Comments:") + 10
// replace </p> with empty string
comments_number := strings.Replace(item.Description[first_comments_index:], "</p>\n", "", -1)
comments_number_int, _ := strconv.Atoi(comments_number)
if comments_number_int < 100 {
items += writeLink("💬 ", comments_url, false)
} else {
items += writeLink("🔥 ", comments_url, false)
}
}
if instapaper && !terminal_mode {
items += "[<img height=\"16\" src=\"https://staticinstapaper.s3.dualstack.us-west-2.amazonaws.com/img/favicon.png\">](https://www.instapaper.com/hello2?url=" + item.Link + ")"
}
title := item.Title
link := item.Link
// Support RSS with no Title (such as Mastdon), use Description instead
if title == "" {
title = stripHtmlRegex(item.Description)
}
items += writeLink(title, link, true)
}
if items != "" {
writeToMarkdown("\n### " + favicon(feed) + " " + feed.Title + "\n")
writeToMarkdown(items)
}
defer db.Close()
}
}