Code Monkey home page Code Monkey logo

go-pastebin's Introduction

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.

go-pastebin's People

Contributors

brenol avatar dependabot[bot] avatar twin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

brenol

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.