-
Notifications
You must be signed in to change notification settings - Fork 0
/
story.go
113 lines (88 loc) · 2.75 KB
/
story.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
package hkn
import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)
const (
createStoryFormRegex = `<input\stype=['"]hidden['"]\s+name=['"]fnid['"]\s+(?:[^>]*?\s+)?value=['"]([^'"]*)['"]`
)
// Given a number a limit and a url, fetch from the url and
// return the number requested if it is >= 0 and <= limit
func getNumber(number int, limit int, url string) ([]int, error) {
if number > limit || number < 0 {
return nil, ErrInvalidNumber
}
resp, err := getBody(url + jsonSuffix)
var top []int
if err != nil {
return top, err
}
err = json.Unmarshal(resp, &top)
if err != nil {
return top, err
}
if len(top) < number {
return top, nil
}
return top[:number], nil
}
// Get top stories given a number
func getTopStories(number int, apiURL string) ([]int, error) {
resource := fmt.Sprintf("%s/%s", apiURL, "topstories")
return getNumber(number, 500, resource)
}
// Get new stories given a number
func getNewStories(number int, apiURL string) ([]int, error) {
resource := fmt.Sprintf("%s/%s", apiURL, "newstories")
return getNumber(number, 500, resource)
}
// Get best stories given a number
func getBestStories(number int, apiURL string) ([]int, error) {
resource := fmt.Sprintf("%s/%s", apiURL, "beststories")
return getNumber(number, 500, resource)
}
// Get latest ask stories given a number
func getLatestAskStories(number int, apiURL string) ([]int, error) {
resource := fmt.Sprintf("%s/%s", apiURL, "askstories")
return getNumber(number, 200, resource)
}
// Get latest show stories given a number
func getLatestShowStories(number int, apiURL string) ([]int, error) {
resource := fmt.Sprintf("%s/%s", apiURL, "showstories")
return getNumber(number, 200, resource)
}
// Get latest job stories given a number
func getLatestJobStories(number int, apiURL string) ([]int, error) {
resource := fmt.Sprintf("%s/%s", apiURL, "jobstories")
return getNumber(number, 200, resource)
}
// Create a story given a title, content, cookie and content key (either "text" or "url")
func createStory(title string, content string, cookie *http.Cookie, webURL string, contentKey string) (bool, error) {
if len(title) == 0 {
return false, ErrEmptyTitle
}
submitFormURL := fmt.Sprintf("%s/%s", webURL, "submit")
fnID, err := matchRegexFromBody(submitFormURL, createStoryFormRegex, cookie)
if err != nil {
return false, err
}
submitURL := fmt.Sprintf("%s/%s", webURL, "r")
body := url.Values{}
body.Set("fnid", fnID)
body.Set("fnop", "submit-page")
body.Set("title", title)
if contentKey == "text" {
body.Set("url", "")
body.Set("text", content)
} else if contentKey == "url" {
body.Set("url", content)
body.Set("text", "")
}
resp, err := postWithCookie(submitURL, body, cookie)
if err == nil && resp != nil {
return true, nil
}
return false, err
}