forked from GliderGeek/pocket2rm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pocket2rm.go
262 lines (210 loc) · 6.06 KB
/
pocket2rm.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
252
253
254
255
256
257
258
259
260
261
262
package main
import (
"bufio"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"os/exec"
"os/user"
"path/filepath"
"runtime"
"strings"
"time"
"github.com/bmaupin/go-epub"
"github.com/go-shiori/go-readability"
"github.com/motemen/go-pocket/api"
"github.com/motemen/go-pocket/auth"
"gopkg.in/yaml.v3"
)
type pocketItem struct {
id string
url *url.URL
added time.Time
title string
}
func writeEpub(filePath string, title string, content string) error {
e := epub.NewEpub(title)
e.SetAuthor("pocket2rm")
_, err := e.AddSection(content, title, "", "")
if err != nil {
return err
}
err = e.Write(filePath)
if err != nil {
return err
}
return nil
}
func getReadableArticle(url *url.URL) (string, string, error) {
timeout, _ := time.ParseDuration("10s")
article, err := readability.FromURL(url.String(), timeout)
if err != nil {
return "", "", err
}
return article.Title, article.Content, nil
}
//get interactive input. whitespace is stripped from return
func input(text string) string {
reader := bufio.NewReader(os.Stdin)
fmt.Print(text)
text, _ = reader.ReadString('\n')
text = strings.Join(strings.Fields(text), "") //strip whitespace
return text
}
//obtain consumerKey, accessToken and write to credentialsPath
func setup(credentialsPath string) error {
consumerKey := input("Insert consumerKey: ")
redirectURL := "https://raw.githubusercontent.com/GliderGeek/pocket2rm/master/pocket_redirect.html"
requestToken, err := auth.ObtainRequestToken(consumerKey, redirectURL)
if err != nil {
fmt.Println("Could not obtain request token: ", err)
return err
}
//Open authorization URL in default browser for user to confirm application
authorizationURL := auth.GenerateAuthorizationURL(requestToken, redirectURL)
open(authorizationURL)
input("Press enter when authorized in browser")
authorization, err := auth.ObtainAccessToken(consumerKey, requestToken)
if err != nil {
fmt.Println("Could not obtain accessToken: ", err)
}
credentials := make(map[string]string)
credentials["consumerKey"] = consumerKey
credentials["accessToken"] = authorization.AccessToken
ymlContent, err := yaml.Marshal(credentials)
if err != nil {
return err
}
err = ioutil.WriteFile(credentialsPath, ymlContent, os.ModePerm)
if err != nil {
return err
}
fmt.Println("Setup successful. Wrote credentials to " + credentialsPath)
return nil
}
func getCredentials(credentialsPath string) (string, string, error) {
//return consumerKey, accessToken, error
fileContent, err := ioutil.ReadFile(credentialsPath)
if err != nil {
return "", "", err
}
var credentials map[string]string
yaml.Unmarshal(fileContent, &credentials)
return credentials["consumerKey"], credentials["accessToken"], nil
}
func getPocketItems(credentialsPath string) ([]pocketItem, error) {
consumerKey, accessToken, err := getCredentials(credentialsPath)
if err != nil {
return []pocketItem{}, nil
}
client := api.NewClient(consumerKey, accessToken)
var r *api.RetrieveOption
retrieveResult, err := client.Retrieve(r)
if err != nil {
return []pocketItem{}, nil
}
var items []pocketItem
for id, item := range retrieveResult.List {
parsedURL, _ := url.Parse(item.ResolvedURL)
items = append(items, pocketItem{id, parsedURL, time.Time(item.TimeAdded), item.Title()})
}
return items, nil
}
//generate filename from time added and title
func getFilename(timeAdded time.Time, title string, fileType string) string {
// fileType: "epub" or "pdf"
title = strings.Join(strings.Fields(title), "-")
title = strings.Replace(title, "/", "_", -1)
fileName := fmt.Sprintf("%s_%s", timeAdded.Format("20060102"), title)
if fileType == "epub" && filepath.Ext(fileName) != ".epub" {
fileName = fileName + ".epub"
} else if fileType == "pdf" && filepath.Ext(fileName) != ".pdf" {
fileName = fileName + ".pdf"
}
return fileName
}
func writePDF(filePath string, url string) error {
resp, err := http.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
f, err := os.Create(filePath)
if err != nil {
return err
}
defer f.Close()
_, err = io.Copy(f, resp.Body)
if err != nil {
return err
}
return nil
}
// open opens the specified URL in the default browser of the user.
func open(url string) error {
var cmd string
var args []string
switch runtime.GOOS {
case "windows":
cmd = "cmd"
args = []string{"/c", "start"}
case "darwin":
cmd = "open"
default: // "linux", "freebsd", "openbsd", "netbsd"
cmd = "xdg-open"
}
args = append(args, url)
return exec.Command(cmd, args...).Start()
}
func main() {
user, err := user.Current()
if err != nil {
fmt.Println("Could not get user")
panic(1)
}
credentialsPath := filepath.Join(user.HomeDir, ".pocket2rm")
argsWithProg := os.Args
if len(argsWithProg) > 1 {
if argsWithProg[1] == "setup" {
setup(credentialsPath)
}
os.Exit(0)
}
articleFolder := "articles"
err = os.MkdirAll(articleFolder, os.ModePerm)
if err != nil {
fmt.Println("Could not create article folder: ", err)
}
pocketArticles, err := getPocketItems(credentialsPath)
if err != nil {
fmt.Println("Could not get pocket articles: ", err)
}
for i, pocketItem := range pocketArticles {
fmt.Println(fmt.Sprintf("progress: %d/%d", i+1, len(pocketArticles)))
extension := filepath.Ext(pocketItem.url.String())
if extension == ".pdf" {
fileName := getFilename(pocketItem.added, pocketItem.title, "pdf")
filePath := filepath.Join(articleFolder, fileName)
err := writePDF(filePath, pocketItem.url.String())
if err != nil {
fmt.Println("Could not get PDF for ", pocketItem.url.String(), err)
}
} else {
fileName := getFilename(pocketItem.added, pocketItem.title, "epub")
title, content, err := getReadableArticle(pocketItem.url)
if err != nil {
fmt.Println("Could not get readable article for ", pocketItem.url.String(), err)
continue
}
filePath := filepath.Join(articleFolder, fileName)
err = writeEpub(filePath, title, content)
if err != nil {
fmt.Println("Could not write epub for ", pocketItem.url.String())
}
}
}
fmt.Println("Finished proces. Files written to: " + articleFolder)
}