-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
117 lines (104 loc) · 4.23 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
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
package main
import (
"encoding/json"
"fmt"
"net/http"
"strconv"
)
type redditResponseJSON struct {
Data struct {
After string `json:"after"`
Before interface{} `json:"before"`
Children []struct {
Data struct {
ApprovedBy interface{} `json:"approved_by"`
Archived bool `json:"archived"`
Author string `json:"author"`
AuthorFlairCSSClass interface{} `json:"author_flair_css_class"`
AuthorFlairText interface{} `json:"author_flair_text"`
BannedBy interface{} `json:"banned_by"`
Clicked bool `json:"clicked"`
Created int `json:"created"`
CreatedUtc int `json:"created_utc"`
Distinguished string `json:"distinguished"`
Domain string `json:"domain"`
Downs int `json:"downs"`
Edited int `json:"edited"`
From interface{} `json:"from"`
FromID interface{} `json:"from_id"`
FromKind interface{} `json:"from_kind"`
Gilded int `json:"gilded"`
Hidden bool `json:"hidden"`
HideScore bool `json:"hide_score"`
ID string `json:"id"`
IsSelf bool `json:"is_self"`
Likes interface{} `json:"likes"`
LinkFlairCSSClass string `json:"link_flair_css_class"`
LinkFlairText string `json:"link_flair_text"`
Locked bool `json:"locked"`
Media interface{} `json:"media"`
MediaEmbed struct{} `json:"media_embed"`
ModReports []interface{} `json:"mod_reports"`
Name string `json:"name"`
NumComments int `json:"num_comments"`
NumReports interface{} `json:"num_reports"`
Over18 bool `json:"over_18"`
Permalink string `json:"permalink"`
Quarantine bool `json:"quarantine"`
RemovalReason interface{} `json:"removal_reason"`
ReportReasons interface{} `json:"report_reasons"`
Saved bool `json:"saved"`
Score int `json:"score"`
SecureMedia interface{} `json:"secure_media"`
SecureMediaEmbed struct{} `json:"secure_media_embed"`
Selftext string `json:"selftext"`
SelftextHTML string `json:"selftext_html"`
Stickied bool `json:"stickied"`
Subreddit string `json:"subreddit"`
SubredditID string `json:"subreddit_id"`
SuggestedSort interface{} `json:"suggested_sort"`
Thumbnail string `json:"thumbnail"`
Title string `json:"title"`
Ups int `json:"ups"`
URL string `json:"url"`
UserReports []interface{} `json:"user_reports"`
Visited bool `json:"visited"`
} `json:"data"`
Kind string `json:"kind"`
} `json:"children"`
Modhash string `json:"modhash"`
} `json:"data"`
Kind string `json:"kind"`
}
func main() {
fmt.Println("Enter the name of subreddit:")
var subreddit string
fmt.Scanf("%s", &subreddit)
var limit int
fmt.Println("Enter number of links to get:")
fmt.Scanf("%d", &limit)
var subredditLink string
subredditLink = "http://www.reddit.com/r"
subredditLink += "/" + subreddit
subredditJSONlink := subredditLink + "/.json"
var lastPost string
count := 0
for count < limit {
fmt.Println(subredditJSONlink)
r, e := http.Get(subredditJSONlink)
if e != nil {
fmt.Println("Error:", e)
}
defer r.Body.Close()
var data redditResponseJSON
dec := json.NewDecoder(r.Body)
dec.Decode(&data)
for _, child := range data.Data.Children {
t := child.Data
fmt.Println(t.Author, " "+t.Name, " Over 18: "+strconv.FormatBool(t.Over18)+" ", t.URL)
lastPost = t.Name
}
count += 25
subredditJSONlink = subredditLink + "/.json" + "?count=" + strconv.FormatInt(int64(count), 10) + "&after=" + lastPost
}
}