Пример #1
0
// threadRequest creates an http request that represents a call for a specific
// thread comment listing from Reddit.
func threadRequest(permalink string) (*http.Request, error) {
	if permalink == "" {
		return nil, fmt.Errorf("no permalink with which to find thread")
	}

	return request.New(
		"GET",
		fmt.Sprintf("%s%s.json", baseURL, permalink),
		nil,
	)
}
Пример #2
0
// threadsRequest creates an http request that represents a by_id api call to
// Reddit.
func threadsRequest(fullnames []string) (*http.Request, error) {
	if len(fullnames) == 0 {
		return nil, fmt.Errorf("no threads provided")
	}

	return request.New(
		"GET",
		fmt.Sprintf(
			"%s/by_id/%s",
			baseURL,
			strings.Join(fullnames, ","),
		),
		nil,
	)
}
Пример #3
0
// MarkAsRead marks inbox items as read, so they are no longer returned by calls
// to Inbox().
func (o *operator) MarkAsRead(fullnames ...string) error {
	req, err := request.New(
		"POST",
		fmt.Sprintf("%s/api/read_message", baseURL),
		&url.Values{
			"id": []string{strings.Join(fullnames, ",")},
		},
	)
	if err != nil {
		return err
	}

	_, err = o.cli.Do(req)
	return err
}
Пример #4
0
// scrapeRequests returns an http request representing a subreddit scrape query
// to the Reddit api.
func scrapeRequest(
	subreddit,
	sort,
	before,
	after string,
	limit uint,
) (*http.Request, error) {
	if limit > MaxLinks {
		return nil, fmt.Errorf(
			"%s links requested; max is %s",
			limit,
			MaxLinks)
	}

	if subreddit == "" {
		return nil, fmt.Errorf("no subreddit provided")
	}

	if sort == "" {
		return nil, fmt.Errorf("no sort provided")
	}

	if limit == 0 {
		return nil, fmt.Errorf("no request necessary for 0 links")
	}

	if before != "" && after != "" {
		return nil, fmt.Errorf("have both after and before ids; " +
			"this tells reddit to scrape in two directions.")
	}

	params := &url.Values{
		"limit": []string{strconv.Itoa(int(limit))},
	}
	if before != "" {
		params.Add("before", before)
	} else if after != "" {
		params.Add("after", after)
	}

	return request.New(
		"GET",
		fmt.Sprintf("%s/r/%s/%s.json", baseURL, subreddit, sort),
		params,
	)
}
Пример #5
0
// replyRequest creates an http request that represents a reply api call to
// Reddit.
func replyRequest(parent, content string) (*http.Request, error) {
	if parent == "" {
		return nil, fmt.Errorf("no parent provided to reply to")
	}

	if content == "" {
		return nil, fmt.Errorf("reply body empty")
	}

	return request.New(
		"POST",
		fmt.Sprintf("%s/api/comment", baseURL),
		&url.Values{
			"thing_id": []string{parent},
			"text":     []string{content},
		},
	)
}
Пример #6
0
// Inbox returns unread inbox items.
func (o *operator) Inbox() ([]*redditproto.Message, error) {
	req, err := request.New(
		"GET",
		fmt.Sprintf("%s/message/unread", baseURL),
		&url.Values{
			"limit": []string{"100"},
		},
	)
	if err != nil {
		return nil, err
	}

	response, err := o.cli.Do(req)
	if err != nil {
		return nil, err
	}

	return parseInbox(response)
}
Пример #7
0
// composeRequest creates an http request that represents a compose api call to
// Reddit.
func composeRequest(user, subject, content string) (*http.Request, error) {
	if user == "" {
		return nil, fmt.Errorf("no user to message")
	}

	if subject == "" {
		return nil, fmt.Errorf("no subject for message")
	}

	if content == "" {
		return nil, fmt.Errorf("no body for message")
	}

	return request.New(
		"POST",
		fmt.Sprintf("%s/api/compose", baseURL),
		&url.Values{
			"to":      []string{user},
			"subject": []string{subject},
			"text":    []string{content},
		},
	)
}
Пример #8
0
// submitRequest creates an http request that represents a submit api call to
// Reddit.
func submitRequest(
	subreddit,
	kind,
	title,
	content string,
) (*http.Request, error) {
	if subreddit == "" {
		return nil, fmt.Errorf("no subreddit provided")
	}

	if title == "" {
		return nil, fmt.Errorf("no title provided")
	}

	params := &url.Values{
		"sr":    []string{subreddit},
		"kind":  []string{kind},
		"title": []string{title},
	}
	if kind == "link" {
		if content == "" {
			return nil, fmt.Errorf("no url provided for link post")
		}
		params.Add("url", content)
	} else if kind == "self" {
		params.Add("text", content)
	} else {
		return nil, fmt.Errorf("unsupported post type")
	}

	return request.New(
		"POST",
		fmt.Sprintf("%s/api/submit", baseURL),
		params,
	)
}