Browse Source

Support generating capture URL

This includes the basic captureas package, including the API client and
a Capture() func that generates capture URLs.
master
Matt Baer 1 year ago
parent
commit
4484809f0c
2 changed files with 69 additions and 0 deletions
  1. +44
    -0
      capture.go
  2. +25
    -0
      captureas.go

+ 44
- 0
capture.go View File

@@ -0,0 +1,44 @@
package captureas

import (
"crypto/md5"
"encoding/hex"
"fmt"
"io"
"net/url"
)

type CaptureParams struct {
URL string `field:"url"`
Width int `field:"w"`
Height int `field:"h"`
}

func (c *Client) Capture(cp *CaptureParams) string {
u, err := url.Parse(c.Config.BaseURL)
if err != nil {
return ""
}

// Build query string
q := u.Query()
q.Add("url", cp.URL)
if cp.Width > 0 {
q.Add("w", fmt.Sprintf("%d", cp.Width))
}
if cp.Height > 0 {
q.Add("h", fmt.Sprintf("%d", cp.Height))
}
u.RawQuery = q.Encode()

// Calculate token
h := md5.New()
ue, _ := url.QueryUnescape(u.RawQuery)
io.WriteString(h, "?"+ue)
io.WriteString(h, c.Secret)
token := hex.EncodeToString(h.Sum(nil))

u.Path = "/" + c.Username + "/" + token + "/png"

return u.String()
}

+ 25
- 0
captureas.go View File

@@ -0,0 +1,25 @@
// Package captureas provides the binding for the Capture.as API
package captureas

import (
"code.as/core/api"
)

const (
apiURL = "https://capture.as"
userAgent = "go-captureas"
)

type Client struct {
*as.Client
Username string
Secret string
}

func NewClient(username, secret string) *Client {
return &Client{
Client: as.NewClient(as.NewClientConfig(apiURL, userAgent)),
Username: username,
Secret: secret,
}
}

Loading…
Cancel
Save