api

package
v0.0.0-...-106e1bf Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2022 License: BSD-3-Clause Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ApiDomain    = "a.4cdn.org"       // This domain serves all 4chan API endpoints in the form of static json files.
	MediaDomainA = "i.4cdn.org"       // This is the primary content domain used for serving user submitted media attached to posts.
	MediaDomainB = "is2.4chan.org"    // Some media files also served here
	StaticDomain = "s.4cdn.org"       // Serves all static site content including icons, banners, CSS and JavaScript files.
	BoardsDomain = "boards.4chan.org" // Serves the front-end html data
)

Variables

View Source
var (
	ErrInvalidSpoilerNum  = fmt.Errorf("invalid spoiler num, should be in the range 1-5 inclusive")
	ErrInvalidAssetFormat = fmt.Errorf("assets should be formatted as 'folder/name.ext' or 'folder/name.num.ext'")
)
View Source
var ErrNotFound = fmt.Errorf("404 not found")

Functions

func VerifyMD5

func VerifyMD5(p *Post, m *Media) bool

Types

type Archive

type Archive struct {
	Board   string `json:"board"`
	PostIDs []int  `json:"post_ids"` // Array of integers. These are the OP numbers of archived threads
	// contains filtered or unexported fields
}

func (*Archive) ClearLastModified

func (m *Archive) ClearLastModified()

type Board

type Board struct {
	Board           string `json:"board"`             // The directory the board is located in.
	Title           string `json:"title"`             // The readable title at the top of the board.
	WorkSafe        Bool   `json:"ws_board"`          // Is the board worksafe
	PerPage         int    `json:"per_page"`          // How many threads are on a single index page
	Pages           int    `json:"pages"`             // How many index pages does the board have
	MaxFilesize     int    `json:"max_filesize"`      // Maximum file size allowed for non .webm attachments (in KB)
	MaxWebmFilesize int    `json:"max_webm_filesize"` // Maximum file size allowed for .webm attachments (in KB)
	MaxCommentChars int    `json:"max_comment_chars"` // Maximum number of characters allowed in a post comment
	MaxWebmDuration int    `json:"max_webm_duration"` // Maximum duration of a .webm attachment (in seconds)
	BumpLimit       int    `json:"bump_limit"`        // Maximum number of replies allowed to a thread before the thread stops bumping
	ImageLimit      int    `json:"image_limit"`       // Maximum number of image replies per thread before image replies are discarded
	Cooldowns       struct {
		Threads int `json:"threads"` // Thread cooldown time
		Replies int `json:"replies"` // Reply cooldown time
		Images  int `json:"images"`  // Image cooldown time
	} `json:"cooldowns"`
	MetaDescription string `json:"meta_description"` // SEO meta description content for a board
	Spoilers        Bool   `json:"spoilers"`         // Are spoilers enabled
	CustomSpoilers  int    `json:"custom_spoilers"`  // How many custom spoilers does the board have
	IsArchived      Bool   `json:"is_archived"`      // Are archives enabled for the board
	TrollFlags      Bool   `json:"troll_flags"`      // Are troll flags enabled on the board
	CountryFlags    Bool   `json:"country_flags"`    // Are flags showing the poster's country enabled on the board
	UserIDs         Bool   `json:"user_ids"`         // Are poster ID tags enabled on the board
	Oekaki          Bool   `json:"oekaki"`           // Can users submit drawings via browser the Oekaki app
	SjisTags        Bool   `json:"sjis_tags"`        // Can users submit sjis drawings using the [sjis] tags
	CodeTags        Bool   `json:"code_tags"`        // Board supports code syntax highlighting using the [code] tags
	MathTags        Bool   `json:"math_tags"`        // Board supports [math] TeX and [eqn] tags
	TextOnly        Bool   `json:"text_only"`        // Is image posting disabled for the board
	ForcedAnon      Bool   `json:"forced_anon"`      // Is the name field disabled on the board
	WebmAudio       Bool   `json:"webm_audio"`       // Are webms with audio allowed?
	RequireSubject  Bool   `json:"require_subject"`  // Do OPs require a subject
	MinImageWidth   int    `json:"min_image_width"`  // What is the minimum image width (in pixels)
	MinImageHeight  int    `json:"min_image_height"` // What is the minimum image height (in pixels)
}

type Boards

type Boards struct {
	Boards     []Board           `json:"boards"`
	TrollFlags map[string]string `json:"troll_flags"`
	// contains filtered or unexported fields
}

func (*Boards) ClearLastModified

func (m *Boards) ClearLastModified()

type Bool

type Bool bool

Bool allows 0/1 to also become boolean.

func (*Bool) UnmarshalJSON

func (bit *Bool) UnmarshalJSON(b []byte) error

type Catalog

type Catalog struct {
	Board string `json:"board"`
	Pages []struct {
		Page    int     `json:"page"` // The page number that the following Threads array is on
		Threads []*Post `json:"threads"`
	} `json:"pages"`
	// contains filtered or unexported fields
}

func (*Catalog) ClearLastModified

func (m *Catalog) ClearLastModified()

type Client

type Client struct {

	// SSL decides whether the API should make requests using HTTPS
	// Note:
	//   - Make API requests using the same protocol as the app.
	//   - Only use SSL when a user is accessing your app over HTTPS.
	SSL bool
	// Supplies the If-Modified-Since header, i.e. the time that the URL
	// was last accessed is sent to the API. A response with a 304 status
	// code is returned if no changes have occurred since then (meaning
	// no new content exists)
	IFMS bool
	// contains filtered or unexported fields
}

Client makes requests to the 4chan api and media/static endpoints

func DefaultClient

func DefaultClient() *Client

DefaultClient returns client with at most 1 request to the api per second and 8 requests per sec to media endpoints. It used SSL and the If-Modified-Since header by default.

func NewClient

func NewClient(apiPerSec, mediaPerSec int, ssl, ifms bool) *Client

func (*Client) GetArchive

func (c *Client) GetArchive(board string) (*Archive, bool, error)

func (*Client) GetBoards

func (c *Client) GetBoards() (*Boards, bool, error)

func (*Client) GetCatalog

func (c *Client) GetCatalog(board string) (*Catalog, bool, error)

func (*Client) GetCustomSpoiler

func (c *Client) GetCustomSpoiler(board string, num int) (*Media, error)

func (*Client) GetFile

func (c *Client) GetFile(p *Post) (*Media, error)

func (*Client) GetFlag

func (c *Client) GetFlag(flagCode string) (*Media, error)

func (*Client) GetPage

func (c *Client) GetPage(board string, page int) (*Page, bool, error)

func (*Client) GetStaticAsset

func (c *Client) GetStaticAsset(endpoint string) (*Media, error)

func (*Client) GetThread

func (c *Client) GetThread(board string, opID int) (*Thread, bool, error)

func (*Client) GetThreadHTML

func (c *Client) GetThreadHTML(t *Thread) (io.ReadCloser, error)

func (*Client) GetThreads

func (c *Client) GetThreads(board string) (*ThreadList, bool, error)

func (*Client) GetThumbnail

func (c *Client) GetThumbnail(p *Post) (*Media, error)

func (*Client) GetTrollFlag

func (c *Client) GetTrollFlag(flagCode string) (*Media, error)

func (*Client) RefreshArchive

func (c *Client) RefreshArchive(a *Archive) (*Archive, bool, error)

func (*Client) RefreshBoards

func (c *Client) RefreshBoards(b *Boards) (*Boards, bool, error)

func (*Client) RefreshCatalog

func (c *Client) RefreshCatalog(ctl *Catalog) (*Catalog, bool, error)

func (*Client) RefreshPage

func (c *Client) RefreshPage(p *Page) (*Page, bool, error)

func (*Client) RefreshThread

func (c *Client) RefreshThread(th *Thread) (*Thread, bool, error)

func (*Client) RefreshThreads

func (c *Client) RefreshThreads(tl *ThreadList) (*ThreadList, bool, error)

type Media

type Media struct {
	Body     io.ReadCloser // The response body
	Board    string        // Board the media is from
	ID, Ext  string        // The image ID and extension. The extension has the dot at the beginning.
	Filename string        // Filename of the image if applicable, only works ig FromPost() method is used
	URL      string        // URL to the image resource
	MD5      string        // Base64 encoded MD5 hash of the response content
}

type Page

type Page struct {
	No      int      `json:"no"`
	Board   string   `json:"board"`
	Threads []Thread `json:"threads"`
	// contains filtered or unexported fields
}

func (*Page) ClearLastModified

func (m *Page) ClearLastModified()

type Post

type Post struct {
	// Custom fields implemented by crow
	Board   string `json:"board"`    // The directory the board is located in.
	HasFile bool   `json:"has_file"` // Whether the post has a file attached

	// Fields from the API
	No              int         `json:"no"`             // The numeric post ID
	RepliesTo       int         `json:"resto"`          // For replies: this is the ID of the thread being replied to. For OP: this value is zero
	Sticky          Bool        `json:"sticky"`         // If the thread is being pinned to the top of the page
	Closed          Bool        `json:"closed"`         // If the thread is closed to replies
	Now             string      `json:"now"`            // MM/DD/YY(Day)HH:MM (:SS on some boards), EST/EDT timezone
	Time            Timestamp   `json:"time"`           // UNIX timestamp the post was created
	Name            string      `json:"name"`           // Name user posted with. Defaults to Anonymous
	Trip            string      `json:"trip"`           // The user's tripcode, in format: !tripcode or !!securetripcode
	ID              string      `json:"id"`             // The poster's ID
	CapCode         string      `json:"cap_code"`       // The capcode identifier for a post
	Country         string      `json:"country"`        // Poster's ISO 3166-1 alpha-2 country code
	CountryName     string      `json:"country_name"`   // Poster's country name
	Subject         string      `json:"sub"`            // OP Subject text
	Comment         string      `json:"com"`            // Comment (HTML escaped)
	ImageID         json.Number `json:"tim"`            // Unix timestamp + microtime that an image was uploaded
	Filename        string      `json:"filename"`       // Filename as it appeared on the poster's device
	Ext             string      `json:"ext"`            // Filetype
	Filesize        int         `json:"fsize"`          // Size of uploaded file in bytes
	MD5             string      `json:"md5"`            // 24 character, packed base64 MD5 hash of file
	ImageWidth      int         `json:"w"`              // Image width dimension
	ImageHeight     int         `json:"h"`              // Image height dimension
	ThumbnailWidth  int         `json:"tn_w"`           // Thumbnail image width dimension
	ThumbnailHeight int         `json:"tn_h"`           // Thumbnail image height dimension
	FileDeleted     Bool        `json:"filedeleted"`    // If the file was deleted from the post
	ImageSpoiler    Bool        `json:"spoiler"`        // If the image was spoilered or not
	CustomSpoiler   int         `json:"custom_spoiler"` // The custom spoiler ID for a spoilered image
	OmittedPosts    int         `json:"omitted_posts"`  // Number of replies minus the number of previewed replies
	OmittedImages   int         `json:"omitted_images"` // Number of image replies minus the number of previewed image replies
	Replies         int         `json:"replies"`        // Total number of replies to a thread
	Images          int         `json:"images"`         // Total number of image replies to a thread
	BumpLimit       Bool        `json:"bump_limit"`     // If a thread has reached bumplimit, it will no longer bump
	ImageLimit      Bool        `json:"image_limit"`    // If an image has reached image limit, no more image replies can be made
	LastModified    Timestamp   `json:"last_modified"`  // The UNIX timestamp marking the last time the thread was modified (post added/modified/deleted, thread closed/sticky settings modified)
	Tag             string      `json:"tag"`            // The category of .swf upload
	SemanticURL     string      `json:"semantic_url"`   // SEO URL slug for thread
	Since4Pass      int         `json:"since4pass"`     // Year 4chan pass bought
	UniqueIPs       int         `json:"unique_ips"`     // Number of unique posters in a thread
	MImg            Bool        `json:"m_img"`          // Mobile optimized image exists for post
	LastReplies     []Post      `json:"last_replies"`   // JSON representation of the most recent replies to a thread
	Archived        Bool        `json:"archived"`       // Thread has reached the board's archive
	ArchivedOn      Timestamp   `json:"archived_on"`    // UNIX timestamp the post was archived
}

type Thread

type Thread struct {
	Board        string    `json:"board"`         // The board directory
	No           int       `json:"no"`            // The ID of the first post in the thread
	Closed       Bool      `json:"closed"`        // If the thread is closed to replies
	Subject      string    `json:"sub"`           // OP Subject text
	Comment      string    `json:"com"`           // Comment (HTML escaped)
	Replies      int       `json:"replies"`       // Total number of replies to a thread
	LastModified Timestamp `json:"last_modified"` // The UNIX timestamp marking the last time the thread was modified (post added/modified/deleted, thread closed/sticky settings modified)
	UniqueIPs    int       `json:"unique_ips"`    // Number of unique posters in a thread
	BumpLimit    Bool      `json:"bump_limit"`    // If a thread has reached bumplimit, it will no longer bump
	ImageLimit   Bool      `json:"image_limit"`   // If an image has reached image limit, no more image replies can be made
	Archived     Bool      `json:"archived"`      // Thread has reached the board's archive
	ArchivedOn   Timestamp `json:"archived_on"`   // UNIX timestamp the post was archived
	Sticky       Bool      `json:"sticky"`        // If the thread is being pinned to the top of the page
	Posts        []*Post   `json:"posts"`         // Posts in the thread
	// contains filtered or unexported fields
}

func (*Thread) ClearLastModified

func (m *Thread) ClearLastModified()

type ThreadList

type ThreadList struct {
	Board string `json:"board"`
	Pages []struct {
		Page    int `json:"page"` // The page number that the following Threads slice is on
		Threads []struct {
			No           int       `json:"no"`            // The OP ID of a thread
			LastModified Timestamp `json:"last_modified"` // The UNIX timestamp marking the last time the thread was modified (post added/modified/deleted, thread closed/sticky settings modified)
			Replies      int       `json:"replies"`       // A numeric count of the number of replies in the thread
		} `json:"threads"` // The threads on the page
	} `json:"pages"`
	// contains filtered or unexported fields
}

func (*ThreadList) ClearLastModified

func (m *ThreadList) ClearLastModified()

type Timestamp

type Timestamp struct {
	time.Time
}

Timestamp is a time.Time which unmarshalls from a UNIX timestamp

func (*Timestamp) UnmarshalJSON

func (p *Timestamp) UnmarshalJSON(bytes []byte) error

UnmarshalJSON decodes an int64 timestamp into a time.Time object

Jump to

Keyboard shortcuts

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