api

package
v0.0.0-...-117c90c Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2018 License: BSD-2-Clause Imports: 7 Imported by: 8

Documentation

Overview

Package api pulls 4chan board and thread data from the JSON API into native Go data structures.

Index

Examples

Constants

View Source
const (
	APIURL    = "a.4cdn.org"
	ImageURL  = "i.4cdn.org"
	StaticURL = "s.4cdn.org"
)

Variables

View Source
var (
	// Whether or not to use HTTPS for requests.
	SSL bool = false
	// Cooldown time for updating threads using (*Thread).Update().
	// If it is set to less than 10 seconds, it will be re-set to 10 seconds
	// before being used.
	UpdateCooldown time.Duration = 15 * time.Second
)
View Source
var Boards []Board

Board names/descriptions will be cached here after a call to LookupBoard or GetBoards

Functions

func GetThreads

func GetThreads(board string) ([][]int64, error)

GetThreads hits the API for a list of the thread IDs of all the active threads on a given board.

Types

type Board

type Board struct {
	Board string `json:"board"`
	Title string `json:"title"`
}

A Board is the name and title of a single board.

func GetBoards

func GetBoards() ([]Board, error)

Get the list of boards.

func LookupBoard

func LookupBoard(name string) (Board, error)

LookupBoard returns the Board corresponding to the board name (without slashes)

type Catalog

type Catalog []struct {
	Page    int
	Threads []*Thread
}

A Catalog contains a list of (truncated) threads on each page of a board.

func GetCatalog

func GetCatalog(board string) (Catalog, error)

GetCatalog hits the API for a catalog listing of a board.

type File

type File struct {
	Id          int64  // Id is what 4chan renames images to (UNIX + microtime, e.g. 1346971121077)
	Name        string // Original filename
	Ext         string
	Size        int
	MD5         []byte
	Width       int
	Height      int
	ThumbWidth  int
	ThumbHeight int
	Deleted     bool
	Spoiler     bool
}

A File represents an uploaded file's metadata.

func (*File) String

func (self *File) String() string

type Post

type Post struct {
	// Post info
	Id           int64
	Thread       *Thread
	Time         time.Time
	Subject      string
	LastModified int64

	// Poster info
	Name    string
	Trip    string
	Email   string
	Special string
	Capcode string

	// Country and CountryName are empty unless the board uses country info
	Country     string
	CountryName string

	// Message body
	Comment string

	// File info if any, otherwise nil
	File *File

	// only when they do this on /q/
	CapcodeReplies map[string][]int
	// contains filtered or unexported fields
}

A Post represents all of the attributes of a 4chan post, organized in a more directly usable fashion.

func (*Post) CountryFlagURL

func (self *Post) CountryFlagURL() string

CountryFlagURL returns the URL of the post's country flag icon, if enabled on the board in question.

func (*Post) ImageURL

func (self *Post) ImageURL() string

ImageURL constructs and returns the URL of the attached image. Returns the empty string if there is none.

func (*Post) String

func (self *Post) String() (s string)

func (*Post) ThumbURL

func (self *Post) ThumbURL() string

ThumbURL constructs and returns the thumbnail URL of the attached image. Returns the empty string if there is none.

type Thread

type Thread struct {
	Posts []*Post
	OP    *Post
	Board string // without slashes ex. "g" or "ic"
	// contains filtered or unexported fields
}

A Thread represents a thread of posts. It may or may not contain the actual replies.

Example
thread, err := GetThread("a", 77777777)
if err != nil {
	panic(err)
}
// will block until the cooldown is reached
thread.Update()
fmt.Println(thread)
Output:

func GetIndex

func GetIndex(board string, page int) ([]*Thread, error)

GetIndex hits the API for an index of thread stubs from the given board and page.

Example
threads, err := GetIndex("a", 0)
if err != nil {
	panic(err)
}
for _, thread := range threads {
	fmt.Println(thread)
}
Output:

func GetThread

func GetThread(board string, thread_id int64) (*Thread, error)

GetThread hits the API for a single thread and all its replies. board is just the board name, without the surrounding slashes. If a thread is being updated, use an existing thread's Update() method if possible because that uses If-Modified-Since in the request, which reduces unnecessary server load.

func ParseIndex

func ParseIndex(r io.Reader, board string) ([]*Thread, error)

ParseIndex converts a JSON response for multiple threads into a native Go data structure

func ParseThread

func ParseThread(r io.Reader, board string) (*Thread, error)

ParseThread converts a JSON response for one thread into a native Go data structure.

func (*Thread) BumpLimit

func (self *Thread) BumpLimit() bool

BumpLimit returns true if the thread is at its bump limit, or false otherwise.

func (*Thread) Closed

func (self *Thread) Closed() bool

Closed returns true if the thread is closed for replies, or false otherwise.

func (*Thread) CustomSpoiler

func (self *Thread) CustomSpoiler() int

CustomSpoiler returns the ID of its custom spoiler image, if there is one.

func (*Thread) CustomSpoilerURL

func (self *Thread) CustomSpoilerURL(id int, ssl bool) string

CustomSpoilerURL builds and returns the URL of the custom spoiler image, or an empty string if none exists.

func (*Thread) Id

func (self *Thread) Id() int64

Id returns the thread OP's post ID.

func (*Thread) ImageLimit

func (self *Thread) ImageLimit() bool

ImageLimit returns true if the thread can no longer accept image posts, or false otherwise.

func (*Thread) Images

func (self *Thread) Images() int

Images returns the number of images in the thread.

func (*Thread) OmittedImages

func (self *Thread) OmittedImages() int

OmittedImages returns the number of image posts omitted in a thread list overview.

func (*Thread) OmittedPosts

func (self *Thread) OmittedPosts() int

OmittedPosts returns the number of posts omitted in a thread list overview.

func (*Thread) Replies

func (self *Thread) Replies() int

Replies returns the number of replies the thread OP has.

func (*Thread) Sticky

func (self *Thread) Sticky() bool

Sticky returns true if the thread is stickied, or false otherwise.

func (*Thread) String

func (self *Thread) String() (s string)

func (*Thread) Update

func (self *Thread) Update() (new_posts, deleted_posts int, err error)

Update an existing thread in-place.

Jump to

Keyboard shortcuts

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