-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconewsy.go
141 lines (119 loc) · 3.04 KB
/
conewsy.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
package main
import (
"os"
"fmt"
"sync"
"github.com/caser/gophernews"
"github.com/jzelinskie/geddit"
)
var redditSession *geddit.LoginSession
var hackerNewsClient *gophernews.Client
func init() {
hackerNewsClient = gophernews.NewClient()
// todo: switch to OAuth2 method
// see https://github.com/jzelinskie/geddit/blob/master/example_test.go
var err error
redditSession, err = geddit.NewLoginSession("redditUsername", "redditPassword", "customUserAgent")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
}
type Story struct {
title string
url string
author string
source string
}
func getHnStoryDetails(id int, c chan<- Story, wg *sync.WaitGroup) {
defer wg.Done()
story, err := hackerNewsClient.GetStory(id)
if err != nil {
return
}
newStory := Story{
title: story.Title,
url: story.URL,
author: story.By,
source: "HackerNews",
}
c <- newStory
}
func newHnStories(c chan<- Story) {
defer close(c)
changes, err := hackerNewsClient.GetChanges()
if err != nil {
fmt.Println(err)
return
}
var wg sync.WaitGroup
for _, id := range changes.Items {
wg.Add(1)
go getHnStoryDetails(id, c, &wg)
}
wg.Wait()
}
func newRedditStories(c chan<- Story) {
defer close(c)
sort := geddit.PopularitySort(geddit.NewSubmissions)
var listingOptions geddit.ListingOptions
submissions, err := redditSession.SubredditSubmissions("programming", sort, listingOptions)
if err != nil {
fmt.Println(err)
return
}
for _, s := range submissions {
newStory := Story {
title: s.Title,
url: s.URL,
author: s.Author,
source: "Reddit /r/programming",
}
c <- newStory
}
}
func outputToConsole(c <-chan Story) {
for s := range c {
fmt.Printf("%s: %s \nby %s on %s\n\n", s.title, s.url, s.author, s.source)
}
}
func outputToFile(c <-chan Story, file *os.File) {
for s := range c {
fmt.Fprintf(file, "%s: %s \nby %s on %s\n\n", s.title, s.url, s.author, s.source)
}
}
func main() {
fromHn := make(chan Story, 8)
fromReddit := make(chan Story, 8)
toFile := make(chan Story, 8)
toConsole := make(chan Story, 8)
go newHnStories(fromHn)
go newRedditStories(fromReddit)
file, err := os.Create("codata/stories.txt")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
go outputToConsole(toConsole)
go outputToFile(toFile, file)
hnOpen := true
redditOpen := true
for hnOpen || redditOpen {
select {
case story, open:= <- fromHn:
if open {
toFile <-story
toConsole <-story
} else {
hnOpen = false
}
case story, open := <- fromReddit:
if open {
toFile <-story
toConsole <-story
} else {
redditOpen = false
}
}
}
}