styx/models/pastebin.go
2020-06-08 10:49:19 +02:00

105 lines
2.8 KiB
Go

package models
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"github.com/sirupsen/logrus"
)
// PasteMeta is a set of descriptive information on a paste.
// Camel case in the marshaling because it's the Pastebin API structure.
type PasteMeta struct {
ScrapeURL string `json:"scrape_url,omitempty"`
FullURL string `json:"full_url,omitempty"`
Date string `json:"date,omitempty"`
Key string `json:"key,omitempty"`
Size string `json:"size,omitempty"`
Expire string `json:"expire,omitempty"`
Title string `json:"title,omitempty"`
Syntax string `json:"syntax,omitempty"`
User string `json:"user,omitempty"`
}
// PasteFull extends PasteMeta by the actual content.
// Not used in our code.
type PasteFull struct {
ScrapeURL string `json:"scrapeUrl,omitempty"`
FullURL string `json:"fullUrl,omitempty"`
Date string `json:"date,omitempty"`
Key string `json:"key,omitempty"`
Size string `json:"size,omitempty"`
Expire string `json:"expire,omitempty"`
Title string `json:"title,omitempty"`
Syntax string `json:"syntax,omitempty"`
User string `json:"user,omitempty"`
Data string `json:"data,omitempty"`
RFC3339 string `json:"time,omitempty"`
}
// Meta Information: https://pastebin.com/api_scraping.php
// Content: http://pastebin.com/api_scrape_item.php
// QueryPastes returns metadata for the last 100 public pastes.
func QueryPastes() ([]PasteMeta, error) {
server := "scrape.pastebin.com"
req, err := http.NewRequest("GET", fmt.Sprintf("https://%s/api_scraping.php?limit=100", server), nil)
if err != nil {
logrus.Fatal("Could not build http request", err)
return nil, err
}
req.Header.Set("Content-Type", "application/json")
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
logrus.Error("Could not do request due to", err)
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
logrus.Error("Could not fetch response due to", err)
return nil, err
}
var pastes []PasteMeta
if err := json.Unmarshal(body, &pastes); err != nil {
logrus.Error("Could not decode response due to ", err, " body", string(body))
return nil, err
}
return pastes, err
}
// FetchPaste fetches paste contents via the web API.
func FetchPaste(paste PasteMeta) (string, error) {
url := paste.ScrapeURL
req, err := http.NewRequest("GET", url, nil)
if err != nil {
logrus.Error("Could build request", req, " due to", err)
return "", err
}
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Printf("Could not do request %v due to %v", req, err)
return "", err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Printf("Could not read response body %v due to %v", resp.Body, err)
return "", err
}
return string(body), nil
}