Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix pastebin.com's URL #5

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions pastebin/pastebin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ package pastebin

import (
"errors"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
)

const (
baseURL = "https://pastebin.com/"
pastebinDevKey = "d06a9df64b29123b8eeda23f53d6535d"
)

Expand All @@ -36,24 +38,24 @@ func (p Pastebin) Put(text, title string) (id string, err error) {
data.Set("api_paste_private", "0") // Create a public paste.
data.Set("api_paste_expire_date", "N") // The paste should never expire.

resp, err := http.PostForm("http://pastebin.com/api/api_post.php", data)
resp, err := http.PostForm(baseURL+"api/api_post.php", data)
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", ErrPutFailed
}
defer resp.Body.Close()
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}
if resp.StatusCode != 200 {
return "", fmt.Errorf("%w: %s", ErrPutFailed, string(respBody))
}
return p.StripURL(string(respBody)), nil
}

// Get returns the text inside the paste identified by ID.
func (p Pastebin) Get(id string) (text string, err error) {
resp, err := http.Get("http://pastebin.com/raw.php?i=" + id)
resp, err := http.Get(baseURL + "raw.php?i=" + id)
if err != nil {
return "", err
}
Expand All @@ -70,10 +72,10 @@ func (p Pastebin) Get(id string) (text string, err error) {

// StripURL returns the paste ID from a pastebin URL.
func (p Pastebin) StripURL(url string) string {
return strings.Replace(url, "http://pastebin.com/", "", -1)
return strings.Replace(url, baseURL, "", -1)
}

// WrapID returns the pastebin URL from a paste ID.
func (p Pastebin) WrapID(id string) string {
return "http://pastebin.com/" + id
return baseURL + id
}