网站屏幕截图在Golang中变得容易
#go #backend #developers #screenshot

你好,
因此,我懒惰地发表一篇文章大约一个月或更长时间,但今天是那天:)。
这篇文章是关于我最近的项目。想知道那是什么?
好吧,这是关于Urlbox
我想实现网站屏幕截图功能。因此,我进入了谷歌搜索,最后,我遇到了Urlbox。这是关于URLBOX的内容的一些解释。
URLBOX是一种轻巧的REST API,旨在自动化产品上的网站屏幕截图。
我对这个项目感到非常兴奋的原因是,URLBOX似乎是我手头上问题的答案。但是,我意识到URLBOX没有Golang库。因此,就像我往常一样,我决定构建one。现在,您可以将此功能添加到您的应用程序中。好吧,无论如何,对于Golang开发人员而言。你现在应该认识我。我都是关于戈兰的;)。
以下是图书馆拍摄的示例屏幕截图:

Urlbox website Image screenshot

Iqquee github's profile Image screenshot

Youtube website image screenshot

因此,这是在Golang应用程序中设置URLBOX的方法:
注意:此Golang库支持了将URLBOX支持的网站屏幕截图的两种方法。

安装

此软件包可以在下面的GO命令中安装。

go get github.com/iqquee/urlbox

您可以在请求{} struct中的格式字段中使用的文件格式列表。

const (
    //FileFormatPng for png file format
    FileFormatPng string = "png"
    //FileFormatJpeg for jpeg file format
    FileFormatJpeg string = "jpeg"
    // FileFormatAvif for avif file format
    FileFormatAvif string = "avif"
    //FileFormatWebp for webp file format
    FileFormatWebp string = "webp"
    //FileFormatWebm for webm file format
    FileFormatWebm string = "webm"
    // FileFormatPdf for pdf file format
    FileFormatPdf string = "pdf"
    //FileFormatSvg for svg file format
    FileFormatSvg string = "svg"
    // FileFormatHtml for html file format
    FileFormatHtml string = "html"
    // FileFormatMd for md file format
    FileFormatMd string = "md"
    // FileFormatMp4 for mp4 file format
    FileFormatMp4 string = "mp4"
)

截屏

屏幕截图同步使用网站的屏幕截图。
用另外的话说,每当您使用此方法提出请求时,您都会等待从服务器获取屏幕截图数据([]字节)。

此方法将请求{} struct作为参数。

请求{} struct中可用的所有字段的列表。

type (
    // Request parameter for the ScreenshotAsync method
    Request struct {
        Url     string  `json:"url"`    // url of website to screenshot
        Format  string  `json:"format"` // screenshot file format
        Options Options // optional params for the request
    }
    // optional parameter
    Options struct {
        FullPage        bool // for full page screenshot
        Width           int  // the viewport width of the browser, in pixels
        Height          int // the viewport height of the browser, in pixels
        BlockingOptions Blocking // options for blocking or dismissing certain page elements, such as cookie banners
        SelectorOption  Selector // take a screenshot of the element that matches this selector. By default, if the selector is not found, Urlbox will take a normal viewport screenshot. If you prefer Urlbox to fail the request when the selector is not found, pass fail_if_selector_missing=true.
        ImageOption     Image    // options relating to the outputted PNG, WebP or JPEG file
        WaitOption      Wait // options relating to waiting before taking the screenshot 
    }
    // blocking option parameter
    Blocking struct {
        BlockAds          bool `json:"block_ads"`           // remove ads from page
        HideCookieBanners bool `json:"hide_cookie_banners"` // remove cookie banners if any
        ClickAccept       bool `json:"click_accept"`        // click accept buttons to dismiss pop-upsSelector
    }
    // selector option parameter
    Selector struct {
        Selector              string `json:"selector"`                 // for css selectors e.g #playground for id of playground
        FailIfSelectorMissing bool   `json:"fail_if_selector_missing"` // fail the request when the selector is not found
    }
    // image option parameter
    Image struct {
        Retina      bool `json:"retina"`      // take a 'retina' or high-definition screenshot, equivalent to setting a device pixel ratio of 2.0 or @2x. Please note that retina screenshots will be double the normal dimensions and will normally take slightly longer to process due to the much bigger image size.
        Quality     int  `json:"quality"`     // the image quality of the resulting screenshot (JPEG/WebP only)
        Transparent bool `json:"transparent"` // if a website has no background color set, the image will have a transparent background (PNG/WebP only)
    }

    // wait option parameter
    Wait struct {
        Delay   int // the amount of time to wait before Urlbox takes the screenshot or PDF, in milliseconds.
        TimeOut int // the amount of time to wait for the requested URL to respond, in milliseconds.
    }
)
package main

import (
    "fmt"
    "log"
    "net/http"
    "os"
    "strings"
    "time"

    "github.com/iqquee/urlbox"
)

func main() {
    apiKey := ""
    secreteKey := ""
    client := urlbox.New(*http.DefaultClient, apiKey, secreteKey)

    request := urlbox.Request{
        Url:    "https://urlbox.io",
        Format: urlbox.FileFormatPng,
        Options: urlbox.Options{
            ImageOption: urlbox.Image{
                Retina:  true,
                Quality: 100,
            },
        },
    }

    data, err := client.Screenshot(request)
    if err != nil {
        fmt.Println("an error occured: ", err)
        return
    }

    fmt.Println("This is the response byte: ", data)

    filename := fmt.Sprintf("%s-%d.%s", strings.Replace(request.Url, "/", "-", -1), time.Now().UTC().Unix(), request.Format)

    if err := os.WriteFile(filename, data, 0666); err != nil {
        fmt.Println("error writing to disk: ", err)
        return
    }
}

屏幕快照

ScreenShotasync允许您的应用程序在呈现屏幕截图后接收信息。
这使您可以异步渲染屏幕截图。

此方法将requestAsync {} struct作为参数。

使用此对象有效载荷实现ScreenShotAsync()方法

type RequestAsync struct {
    Url        string `json:"url"`         // url of website to screenshot
    WebhookUrl string `json:"webhook_url"` // Pass a webhook URL in as the webhook_url option and Urlbox will send a POST request back to that URL with data about the screenshot in JSON format once it has completed rendering
}
package main

import (
    "fmt"
    "net/http"

    "github.com/iqquee/urlbox"
)

func main() {
    apiKey := ""
    secreteKey := ""
    client := urlbox.New(*http.DefaultClient, apiKey, secreteKey)

    request := urlbox.RequestAsync{
        Url:        "https://urlbox.io",
        WebhookUrl: "https://example.com/webhooks/urlbox",
    }

    message, err := client.ScreenshotAsync(request)
    if err != nil {
        fmt.Println("an error occured: ", err)
        return
    }

    fmt.Println("This is the response message: ", message)

}

我已经做了一些工作,以确保正确记录了此library,以便外面的Golang开发人员可以发现它很容易使用并且没有太大的压力。因此,为每种方法添加了示例代码。
确实有美好的一天。