api

package
v1.3.2-0...-05700ea Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 14, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

package api contains the code required to make requests, fetch/stream posts and other things from reddit. This package is heavily inspired by https://github.com/vartanbeno/go-reddit/, you should check it out. But, this package is simpler, smaller and more specialized to the use-case required by me.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	Subreddit *SubredditService
	// contains filtered or unexported fields
}

This is the client used to make requests in RedditStreamer.Stream().

func DefaultClient

func DefaultClient() *Client

func (*Client) BaseImageURL

func (c *Client) BaseImageURL() *url.URL

func (*Client) BaseURL

func (c *Client) BaseURL() *url.URL

func (*Client) BaseVideoURL

func (c *Client) BaseVideoURL() *url.URL

func (*Client) Do

func (c *Client) Do(ctx context.Context, opts *RequestOptions, method string, body io.Reader) (*http.Response, error)

func (*Client) GetImageByURL

func (c *Client) GetImageByURL(ctx context.Context, surl string) (*http.Response, error)

func (*Client) GetURL

func (c *Client) GetURL(ctx context.Context, surl string) (*http.Response, error)

func (*Client) GetVideoByURL

func (c *Client) GetVideoByURL(ctx context.Context, surl string) (*http.Response, error)

func (*Client) WithBaseImageURL

func (c *Client) WithBaseImageURL(u *url.URL) *Client

func (*Client) WithBaseURL

func (c *Client) WithBaseURL(u *url.URL) *Client

func (*Client) WithBaseVideoURL

func (c *Client) WithBaseVideoURL(u *url.URL) *Client

func (*Client) WithTimeout

func (c *Client) WithTimeout(timeout time.Duration) *Client

type Image

type Image struct {
	Source *struct {
		URL    string `json:"url"`
		Height int    `json:"height"`
		Width  int    `json:"width"`
	} `json:"source"`
}

type Item

type Item struct {
	Name        string
	Extension   string
	URL         string
	Orientation string
	Type        string

	Bytes []byte

	Width  int
	Height int

	IsOver18 bool
}

type Post

type Post struct {
	Data struct {
		Media struct {
			RedditVideo *Video `json:"reddit_video"`
		} `json:"media"`
		Title     string `json:"title"`
		URL       string `json:"url"`
		PostHint  string `json:"post_hint"`
		Subreddit string `json:"subreddit"`
		Preview   struct {
			Images []Image `json:"images"`
		}
		Over18  bool `json:"over_18"`
		IsVideo bool `json:"is_video"`
	} `json:"data"`
}

func (*Post) Dimensions

func (p *Post) Dimensions() (w, h int)

Dimensions calls p.Width() and p.Height(), then return.

func (*Post) Height

func (p *Post) Height() int

Height returns either the height of video/image, or 0.

func (*Post) Orientation

func (p *Post) Orientation() string

Orientation calculates the orientation of video/image.

func (*Post) Title

func (p *Post) Title() string

Title is just the post title.

func (*Post) Type

func (p *Post) Type() string

func (*Post) URL

func (p *Post) URL() string

URL returns an automatically formatted url of the post.

func (*Post) Width

func (p *Post) Width() int

Width returns either the width of video/image, or 0.

type Posts

type Posts struct {
	Data struct {
		After    string `json:"after"`
		Children []Post `json:"children"`
	} `json:"data"`
}

type RequestOptions

type RequestOptions struct {
	After     string
	Sorting   string
	Timeframe string
	Subreddit string
	Count     int64
}

type SubredditService

type SubredditService struct {
	// contains filtered or unexported fields
}

func (*SubredditService) GetItems

func (s *SubredditService) GetItems(ctx context.Context, opts *RequestOptions) ([]Item, string, error)

GetItems return a slice of items, "after" string (consult reddit api), or an error.

func (*SubredditService) GetPosts

func (s *SubredditService) GetPosts(ctx context.Context, opts *RequestOptions) ([]Post, string, error)

func (*SubredditService) PostToItem

func (s *SubredditService) PostToItem(ctx context.Context, p *Post) (*Item, error)

type Video

type Video struct {
	ScrubberMediaURL string `json:"scrubber_media_url"`
	Height           int    `json:"height"`
	Width            int    `json:"width"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL