Capture.as API client
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
783 B

  1. package captureas
  2. import (
  3. "crypto/md5"
  4. "encoding/hex"
  5. "fmt"
  6. "io"
  7. "net/url"
  8. )
  9. type CaptureParams struct {
  10. URL string `field:"url"`
  11. Width int `field:"w"`
  12. Height int `field:"h"`
  13. }
  14. func (c *Client) Capture(cp *CaptureParams) string {
  15. u, err := url.Parse(c.Config.BaseURL)
  16. if err != nil {
  17. return ""
  18. }
  19. // Build query string
  20. q := u.Query()
  21. q.Add("url", cp.URL)
  22. if cp.Width > 0 {
  23. q.Add("w", fmt.Sprintf("%d", cp.Width))
  24. }
  25. if cp.Height > 0 {
  26. q.Add("h", fmt.Sprintf("%d", cp.Height))
  27. }
  28. u.RawQuery = q.Encode()
  29. // Calculate token
  30. h := md5.New()
  31. ue, _ := url.QueryUnescape(u.RawQuery)
  32. io.WriteString(h, ue)
  33. io.WriteString(h, c.Secret)
  34. token := hex.EncodeToString(h.Sum(nil))
  35. u.Path = "/" + c.Username + "/" + token + "/png"
  36. return u.String()
  37. }