-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewsy.go
110 lines (88 loc) · 2.31 KB
/
newsy.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
package main
import (
"os"
"fmt"
"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 newHnStories() []Story {
var stories []Story
changes, err := hackerNewsClient.GetChanges()
if err != nil {
fmt.Println(err)
return nil
}
for _, id := range changes.Items {
story, err := hackerNewsClient.GetStory(id)
if err != nil {
continue
}
newStory := Story{
title: story.Title,
url: story.URL,
author: story.By,
source: "HackerNews",
}
stories = append(stories, newStory)
}
return stories
}
func newRedditStories() []Story {
var stories []Story
sort := geddit.PopularitySort(geddit.NewSubmissions)
var listingOptions geddit.ListingOptions
submissions, err := redditSession.SubredditSubmissions("programming", sort, listingOptions)
if err != nil {
fmt.Println(err)
return nil
}
for _, s := range submissions {
newStory := Story {
title: s.Title,
url: s.URL,
author: s.Author,
source: "Reddit /r/programming",
}
stories = append(stories, newStory)
}
return stories
}
func main() {
hnStories :=newHnStories()
redditStories := newRedditStories()
var stories []Story
if hnStories != nil {
stories = append(stories, hnStories...)
}
if redditStories != nil {
stories = append(stories, redditStories...)
}
file, err := os.Create("data/stories.txt")
if err != nil {
fmt.Println(err)
os.Exit(1)
}
defer file.Close()
for _, s := range stories {
fmt.Fprintf(file, "%s: %s \nby %s on %s\n\n", s.title, s.url, s.author, s.source)
}
}