/
main.go
81 lines (65 loc) · 1.44 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
package main
import (
"fmt"
"log"
"strconv"
"sync"
"github.com/f2prateek/hn2instapaper/hn"
"github.com/f2prateek/hn2instapaper/instapaper"
"github.com/f2prateek/semaphore"
"github.com/tj/docopt"
)
const (
usage = `hn2instapaper.
Save top HN articles to Instapaper.
Usage:
hn2instapaper <username> <password> [--limit l]
hn2instapaper -h | --help
hn2instapaper --version
Options:
--limit l Number of articles to save [default: 500].
-h --help Show this screen.
--version Show version.`
version = "1.0.0"
)
func main() {
arguments, err := docopt.Parse(usage, nil, true, version, false)
check(err)
hnClient := hn.New()
instapaperClient := instapaper.New(arguments["<username>"].(string), arguments["<password>"].(string))
limit, err := strconv.Atoi(arguments["--limit"].(string))
check(err)
stories, err := hnClient.TopStories()
check(err)
s := semaphore.New(10)
var wg sync.WaitGroup
for i, id := range stories {
if i >= limit {
break
}
wg.Add(1)
s.Acquire(1)
go func(id int) {
defer wg.Done()
defer s.Release(1)
story, err := hnClient.GetPost(id)
check(err)
if story.URL == nil {
fmt.Println("Skipping", *story.Title)
return
}
_, err = instapaperClient.Add(instapaper.AddParams{
URL: *story.URL,
Title: story.Title,
})
check(err)
fmt.Println("Saved", *story.Title)
}(id)
}
wg.Wait()
}
func check(err error) {
if err != nil {
log.Fatal(err)
}
}