A Pastebin.com API wrapper in Go.
MIT License
A Pastebin.com API wrapper in Go.
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
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.
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)
There 3 ways to retrieve the content of a paste:
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)
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.
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)
Just like retrieving paste content, there are many ways to retrieve paste metadata.
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)
}
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.
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.