pastebin

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2024 License: MIT Imports: 11 Imported by: 1

README

go-pastebin

build Go Report Card codecov Go version Go Reference

A Pastebin.com API wrapper in Go.

Table of Contents

Usage

go get -u github.com/TwiN/go-pastebin
Function Client Description PRO
NewClient n/a Creates a new Client no
CreatePaste yes Creates a new paste and returns the paste key no
DeletePaste yes Removes a paste that belongs to the authenticated user no
GetAllUserPastes yes Retrieves a list of pastes owned by the authenticated user no
GetUserPasteContent yes Retrieves the content of a paste owned by the authenticated user no
GetPasteContent no Retrieves the content of a paste using the raw endpoint. This does not require authentication, but only works with public and unlisted pastes. Using this excessively could lead to your IP being blocked. You may want to use GetPasteContentUsingScrapingAPI instead. no
GetPasteContentUsingScrapingAPI no Retrieves the content of a paste using Pastebin's scraping API yes*
GetPasteUsingScrapingAPI no Retrieves the metadata of a paste using Pastebin's scraping API yes*
GetRecentPastesUsingScrapingAPI no Retrieves a list of recent pastes using Pastebin's scraping API yes*

*To use Pastebin's Scraping API, you must link your IP to your account

Creating a paste

You can create a paste by using pastebin.Client's CreatePaste function:

client, err := pastebin.NewClient("username", "password", "token")
if err != nil {
	panic(err)
}
pasteKey, err := client.CreatePaste(pastebin.NewCreatePasteRequest("title", "content", pastebin.ExpirationTenMinutes, pastebin.VisibilityUnlisted, "go"))
if err != nil {
	panic(err)
}
fmt.Println("Created paste:", pasteKey)

To view the paste on your browser, you can simply append the returned pasteKey to https://pastebin.com/.

Passing an empty string as username and as password for the client will result in the creation of a guest paste rather than a paste owned by a user. Note that only authenticated users may create private pastes.

Deleting a paste

You can delete a paste owned by the user configured in the client by using the DeletePaste function:

client, err := pastebin.DeleteClient("username", "password", "token")
if err != nil {
	panic(err)
}
pasteKey, err := client.CreatePaste(pastebin.NewCreatePasteRequest("title", "content", pastebin.ExpirationTenMinutes, pastebin.VisibilityUnlisted, "go"))
if err != nil {
	panic(err)
}
fmt.Println("Created paste:", pasteKey)
Retrieving the content of a paste

There 3 ways to retrieve the content of a paste:

GetUserPasteContent

If you own the paste, you should use this.

client, err := pastebin.NewClient("username", "password", "token")
if err != nil {
	panic(err)
}
pasteContent, err := client.GetUserPasteContent("abcdefgh")
if err != nil {
	panic(err)
}
println(pasteContent)
GetPasteContent

The paste is public or unlisted, and you don't have a Pastebin PRO account.

pasteContent, err := pastebin.GetPasteContent("abcdefgh")
if err != nil {
	panic(err)
}
println(pasteContent)

WARNING: Using this excessively could lead to your IP being blocked. You may want to use GetPasteContentUsingScrapingAPI instead.

GetPasteContentUsingScrapingAPI

The paste is public or unlisted and you have a Pastebin PRO account with your IP linked.

pasteContent, err := pastebin.GetPasteContentUsingScrapingAPI("abcdefgh")
if err != nil {
	panic(err)
}
println(pasteContent)
Retrieving paste metadata

Just like retrieving paste content, there are many ways to retrieve paste metadata.

List of fields available in paste metadata
  • key
  • title
  • user
  • url
  • hits
  • size
  • date
  • expiration date
  • visibility (public, unlisted, private)
  • syntax
GetAllUserPastes

This will return a list of pastes owned by the user.

client, err := pastebin.NewClient("username", "password", "token")
if err != nil {
	panic(err)
}
pastes, err := client.GetAllUserPastes()
if err != nil {
	panic(err)
}
for _, paste := range pastes {
	fmt.Printf("key=%s title=%s hits=%d visibility=%d url=%s syntax=%s\n", paste.Key, paste.Title, paste.Hits, paste.Visibility, paste.URL, paste.Syntax)
}
GetPasteUsingScrapingAPI

This will return the metadata of a single paste.

paste, err := pastebin.GetPasteUsingScrapingAPI("abcdefgh")
if err != nil {
	panic(err)
}
fmt.Printf("key=%s title=%s hits=%d visibility=%d url=%s syntax=%s\n", paste.Key, paste.Title, paste.Hits, paste.Visibility, paste.URL, paste.Syntax)

Because this function doesn't require authentication, it only works for public and unlisted pastes.

GetRecentPastesUsingScrapingAPI

This will return a list of the most recent pastes on Pastebin.

recentPastes, err := pastebin.GetRecentPastesUsingScrapingAPI("", 30)
if err != nil {
	panic(err)
}
for _, paste := range recentPastes { 
    fmt.Printf("key=%s title=%s hits=%d visibility=%d url=%s syntax=%s\n", paste.Key, paste.Title, paste.Hits, paste.Visibility, paste.URL, paste.Syntax)
}

This method takes in syntax and limit as parameters. Leaving the syntax string empty applies no filtering. The full list of supported values can be found here.

Documentation

Index

Constants

View Source
const (
	LoginApiUrl = "https://pastebin.com/api/api_login.php"
	PostApiUrl  = "https://pastebin.com/api/api_post.php"
	RawApiUrl   = "https://pastebin.com/api/api_raw.php"

	// ScrapingApiUrl is one of the URLs of Pastebin's scraping API
	//
	// Requires PRO Pastebin account with a linked IP
	// See https://pastebin.com/doc_scraping_api
	ScrapingApiUrl = "https://scrape.pastebin.com/api_scraping.php"

	// ScrapeItemApiUrl is one of the URLs for Pastebin's scraping API
	//
	// Requires PRO Pastebin account with a linked IP
	// See https://pastebin.com/doc_scraping_api
	ScrapeItemApiUrl = "https://scrape.pastebin.com/api_scrape_item.php"

	// ScrapeItemMetadataApiUrl is one of the URLs for Pastebin's scraping API
	//
	// Requires PRO Pastebin account with a linked IP
	// See https://pastebin.com/doc_scraping_api
	ScrapeItemMetadataApiUrl = "https://scrape.pastebin.com/api_scrape_item_meta.php"

	// RawUrlPrefix is not part of the supported API, but can still be used to fetch raw pastes.
	//
	// See GetPasteContent
	RawUrlPrefix = "https://pastebin.com/raw"
)

Variables

View Source
var (
	ErrNotAuthenticated = errors.New("must be authenticated to perform this action")
)

Functions

func GetPasteContent

func GetPasteContent(pasteKey string) (string, error)

GetPasteContent retrieves the content of a paste by using the raw endpoint (https://pastebin.com/raw/{pasteKey}) This does not require authentication, but only works with public and unlisted pastes.

WARNING: Using this excessively could lead to your IP being blocked. You may want to use GetPasteContentUsingScrapingAPI instead.

func GetPasteContentUsingScrapingAPI

func GetPasteContentUsingScrapingAPI(pasteKey string) (string, error)

GetPasteContentUsingScrapingAPI retrieves the content of a paste by using the Scraping API (ScrapingApiUrl) This does not require authentication, but only works with public and unlisted pastes.

To use the scraping API, you must link your IP to your Pastebin account, or it will not work. See https://pastebin.com/doc_scraping_api

Types

type Client

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

Client is the Pastebin client for performing operations that require authentication

func NewClient

func NewClient(username, password, developerApiKey string) (*Client, error)

NewClient creates a new Client and authenticates said client before returning if the username parameter is passed.

Note that the only thing you can do without providing a username and a password is creating a new guest paste.

func (*Client) CreatePaste

func (c *Client) CreatePaste(request *CreatePasteRequest) (string, error)

CreatePaste creates a new paste and returns the paste key If the client was only provided with a developer API key, a guest paste will be created. You can get the URL by simply appending the output key to "https://pastebin.com/"

func (*Client) DeletePaste

func (c *Client) DeletePaste(pasteKey string) error

DeletePaste removes a paste owned by the authenticated user

func (*Client) GetAllUserPastes

func (c *Client) GetAllUserPastes() ([]*Paste, error)

GetAllUserPastes retrieves a list of pastes owned by the authenticated user

func (*Client) GetUserPasteContent

func (c *Client) GetUserPasteContent(pasteKey string) (string, error)

GetUserPasteContent retrieves the content of a paste owned by the authenticated user Unlike GetPasteContent, this function can only get the content of a paste that belongs to the authenticated user, even if the paste is public.

type CreatePasteRequest

type CreatePasteRequest struct {
	Title      string
	Code       string
	Expiration Expiration

	// Visibility of the paste that will be created.
	// Note that a Client configured without username/password cannot create a private paste
	Visibility Visibility

	// Syntax is the format of the paste (e.g. go, javascript, json, ...)
	// See https://pastebin.com/doc_api#5 for a full list of supported values
	Syntax string
}

func NewCreatePasteRequest

func NewCreatePasteRequest(title, code string, expiration Expiration, visibility Visibility, syntax string) *CreatePasteRequest

NewCreatePasteRequest creates a new CreatePasteRequest struct

Should be used as parameter to the Client.CreatePaste method

type Expiration

type Expiration string
const (
	ExpirationTenMinutes Expiration = "10M"
	ExpirationOneHour    Expiration = "1H"
	ExpirationOneDay     Expiration = "1D"
	ExpirationOneWeek    Expiration = "1W"
	ExpirationTwoWeeks   Expiration = "2W"
	ExpirationOneMonth   Expiration = "1M"
	ExpirationSixMonth   Expiration = "6M"
	ExpirationOneYear    Expiration = "1Y"
	ExpirationNever      Expiration = "N"
)

type Paste

type Paste struct {
	Key        string
	Title      string
	User       string
	URL        string
	Hits       int
	Size       int
	Date       time.Time
	ExpireDate time.Time
	Visibility Visibility
	Syntax     string
}

func GetPasteUsingScrapingAPI

func GetPasteUsingScrapingAPI(pasteKey string) (*Paste, error)

GetPasteUsingScrapingAPI retrieves the metadata of a paste by using the Scraping API (ScrapingApiUrl) This does not require authentication, but only works with public and unlisted pastes.

To use the scraping API, you must link your IP to your Pastebin account, or it will not work. See https://pastebin.com/doc_scraping_api

func GetRecentPastesUsingScrapingAPI

func GetRecentPastesUsingScrapingAPI(syntax string, limit int) ([]*Paste, error)

GetRecentPastesUsingScrapingAPI retrieves the most recent pastes using Pastebin's scraping API If you don't want to filter by language, you can pass an empty string as syntax. The maximum value for the limit parameter is 250.

To use the scraping API, you must link your IP to your Pastebin account, or it will not work. See https://pastebin.com/doc_scraping_api

type Visibility

type Visibility int
const (
	VisibilityPublic   Visibility = 0
	VisibilityUnlisted Visibility = 1
	VisibilityPrivate  Visibility = 2
)

func (Visibility) String

func (v Visibility) String() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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