-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathmorningpaper.go
251 lines (207 loc) · 5.95 KB
/
morningpaper.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package main
import (
"fmt"
"io"
"net/http"
"os"
"path/filepath"
"strings"
"time"
"github.com/PuerkitoBio/goquery"
"github.com/mmcdole/gofeed"
"github.com/sirupsen/logrus"
)
func getFiles() error {
// Iterate over however many pages we want to display.
for i := 1; i <= maxPages; i++ {
if err := downloadFilesFromFeed(i); err != nil {
return err
}
}
return nil
}
// downloadFilesFromFeed parses the RSS feed for the morning paper and downloads the links for
// papers.
func downloadFilesFromFeed(page int) error {
// Parse the feed.
fp := gofeed.NewParser()
feed, err := fp.ParseURL(fmt.Sprintf(morningPaperRSSFeedURL, page))
if err != nil {
return fmt.Errorf("parsing the url %s failed: %v", morningPaperRSSFeedURL, err)
}
// Iterate over the items.
for _, item := range feed.Items {
if item == nil || item.PublishedParsed == nil {
// Continue early.
continue
}
// Ignore End of Term
if strings.HasPrefix("end of term", strings.ToLower(item.Title)) {
continue
}
// Ignore The Year Ahead
if strings.HasPrefix("the year ahead", strings.ToLower(item.Title)) {
continue
}
logrus.WithFields(logrus.Fields{
"title": item.Title,
"published": item.PublishedParsed.String(),
}).Debug("parsing article")
// Try to get the first link to the paper from the content.
doc, err := goquery.NewDocumentFromReader(strings.NewReader(item.Content))
if err != nil {
return fmt.Errorf("parsing article %q content as HTML failed: %v", item.Title, err)
}
paper := doc.Find("a")
paperLink, ok := paper.Attr("href")
if !ok {
return fmt.Errorf("paper link for article %q does not have an href", item.Title)
}
logrus.WithFields(logrus.Fields{
"title": item.Title,
"paper": paper.Text(),
"link": paperLink,
}).Debug("found paper link")
for !strings.HasSuffix(paperLink, ".pdf") && !strings.HasSuffix(paperLink, "/REF") {
// Handle arxiv papers.
if strings.HasPrefix(paperLink, "https://arxiv.org") {
// Get the pdf link for arxiv.org.
parts := strings.Split(strings.Trim(paperLink, "/"), "/")
paperLink = fmt.Sprintf("https://arxiv.org/pdf/%s.pdf", parts[len(parts)-1])
continue
}
// Try to see if we have a link for it in our known papers.
pl, ok := knownPapersDownloadLinks[paperLink]
if ok {
paperLink = pl
break
}
// Try to find the link on the next page.
paperLink, err = tryToFindPDFLink(paperLink)
if err != nil {
return err
}
// Bail.
break
}
if len(paperLink) < 1 {
// Maybe throw an error?
logrus.WithFields(logrus.Fields{
"title": item.Title,
"paper": paper.Text(),
"link": paperLink,
}).Warn("could not find PDF to download")
continue
}
// Download the pdf.
logrus.WithFields(logrus.Fields{
"link": paperLink,
}).Debug("downloading paper")
// Create a name for the resulting file from the title.
name := getNameForPaperFile(item.Title, item.PublishedParsed)
// Use the item title here because Adrian uses better titles than
// what is usually in the link for the paper.
file := filepath.Join(dataDir, name)
if paperAlreadySynced(file) {
logrus.WithFields(logrus.Fields{
"paper": paper.Text(),
"link": paperLink,
}).Info("skipping paper (already synced)")
continue
}
if err := downloadPaper(paperLink, file); err != nil {
return err
}
logrus.WithFields(logrus.Fields{
"paper": paper.Text(),
"link": paperLink,
"file": file,
}).Info("downloaded paper to file")
// Sync the file with remarkable cloud.
if err := rmAPI.SyncFileAndRename(file, fmt.Sprintf("%s (%s)", strings.TrimSpace(item.Title), item.PublishedParsed.Format("2006-01-02"))); err != nil {
return err
}
// Continue here.
continue
}
return nil
}
func paperAlreadySynced(file string) bool {
// check if file exists
_, err := os.Stat(file)
return err == nil
}
func downloadPaper(link, file string) error {
// Open the file.
f, err := os.OpenFile(file, os.O_RDWR|os.O_CREATE, 0755)
if err != nil {
return fmt.Errorf("opening file %s failed: %v", file, err)
}
defer f.Close()
// Get the file contents.
resp, err := http.Get(link)
if err != nil {
f.Close()
os.Remove(file)
return fmt.Errorf("getting %s failed: %v", link, err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
// Delete the file.
f.Close()
os.Remove(file)
return fmt.Errorf("status code for getting %s error: %d %s", link, resp.StatusCode, resp.Status)
}
// Copy the contents.
if _, err := io.Copy(f, resp.Body); err != nil {
f.Close()
os.Remove(file)
return fmt.Errorf("writing file %s failed: %v", file, err)
}
return nil
}
func getNameForPaperFile(title string, published *time.Time) string {
parts := strings.Split(title, "http")
title = parts[0]
name := strings.Replace(strings.Replace(strings.ToLower(title), " ", "-", -1), ":", "", -1)
parts = strings.Split(name, "/")
// Return the last part.
return fmt.Sprintf("%s-%s.pdf", published.Format("2006-01-02"), parts[len(parts)-1])
}
func tryToFindPDFLink(link string) (string, error) {
// Request the HTML page.
resp, err := http.Get(link)
if err != nil {
return "", fmt.Errorf("getting %s failed: %v", link, err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return "", fmt.Errorf("status code for getting %s error: %d %s", link, resp.StatusCode, resp.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(resp.Body)
if err != nil {
return "", fmt.Errorf("parsing link %s failed: %v", link, err)
}
// Iterate over all the links.
doc.Find("a").EachWithBreak(func(i int, s *goquery.Selection) bool {
href, ok := s.Attr("href")
if !ok {
return true
}
text := s.Text()
if text == "PDF" && strings.HasPrefix(link, "https://dl.acm.org") {
// Return false to break.
// Cannot download from ACM.
return false
}
// Found a link to a pdf.
if strings.HasPrefix(href, ".pdf") {
link = href
// Return false to break.
return false
}
return true
})
return "", nil
}