webshot

package module
v0.0.0-...-34e2362 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2024 License: MIT Imports: 6 Imported by: 0

README

webshot

webshot is a free website screenshotting library in golang

Get Started

In other to use this package, you need to first install tesseract on your machine and then download GeckoDriver for your os from https://github.com/mozilla/geckodriver/releases.

NOTE: The browser used in this package by default is firefox. Kindly install firefox if you don't have it on your machine already.

Installation

This package can be installed by using the go command below.

go get github.com/iqquee/webshot

Quick start

# assume the following codes in example.go file
$ touch example.go
# open the just created example.go file in the text editor of your choice

Screenshot

Screenshot is the default screenshotter which can take the screenshot of webpages

package main

import (
	"fmt"
	"os"

	"github.com/iqquee/webshot"
)

func main() {
	config := webshot.NewConfig{
		Address:     "http://localhost",
		Port:        4444, // you can change accordingly to which ever port you wish
		BrowserName: webshot.FirefoxBrowser,
		DebugMode:   true, // set to true if you want to get the logs
		DriverPath:  "", // your gekodriver path goes in here
	}

	driver, err := webshot.NewWebshot(config)
	if err != nil {
		fmt.Println(err)
		return
	}

	url := "https://google.com"

	byteImage, err := driver.Screenshot(url)
	if err != nil {
		fmt.Println(err)
		return
	}

	fileName := "screenshot" + ".png"
	pngData, _ := os.Create(fileName)
	pngData.Write([]byte(byteImage))
}

ImageProcessing

ImageProcessing does the optical character recognition(OCR)

This method processess an image and returns the text in that image in a .txt file.

package main

import (
	"fmt"

	"github.com/iqquee/webshot"
)

func main() {

    filePath := ""
    fileName := ""
	if err := webshot.ImageProcessing(filePath, fileName); err != nil {
		fmt.Println("Image processing err: \n", err)
		return
	}
}

Extend

Extend allow you to use use all of the functionalities provided by selenium

You can use the Extend() to pretty much do whatever you wish as long as it's a functionality selenium supports.

package main

import (
	"fmt"

	webshot "github.com/iqquee/webshot"
)

func main() {
    config := webshot.NewConfig{
		Address:     "http://localhost",
		Port:        4444, // you can change accordingly to which ever port you wish
		BrowserName: webshot.FirefoxBrowser,
		DebugMode:   true, // set to true if you want to get the logs
		DriverPath:  "", // your gekodriver path goes in here
	}

	driver, err := webshot.NewWebshot(config)
	if err != nil {
		fmt.Println(err)
		return
	}

	service := driver.Extend()
	imgBytes, err := service.Screenshot()
	if err != nil {
		fmt.Println(err)
		return
	}

	fmt.Println(imgBytes)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ChromeBrowser for the chrome browser
	ChromeBrowser = "chrome"
	// FirefoxBrowser for the firefox browser
	FirefoxBrowser = "firefox"
	// BrowserError is the error returned when the browser type entered by the user is not firefox
	ErrBrowser = errors.New("the supported browser is firefox")
)

Functions

func ImageProcessing

func ImageProcessing(filePath, saveAs string) error

ImageProcessing does the optical character recognition(OCR)

This method takes in two parameters which are: filePath and the name the processed image is to be saved as.

Types

type NewConfig

type NewConfig struct {
	// DriverPath is the webdriver for the browser
	DriverPath string //
	// Address for the address e.g 127.0.0.1
	Address string
	// Port for the port to run the selenium server on e.g 8080
	Port int
	// DebugMode turn this to true if you want to get logs for the process happening
	DebugMode bool
	// BrowserName is the name of the browser to use
	BrowserName string
}

NewConfig is the config that sets up webshot for use

type Webshot

type Webshot struct {
	// webdriver is the selenium we driver
	Webdriver selenium.WebDriver
	// Service is the selenuim service
	Service *selenium.Service
}

Webshot is the struct for the webdriver

func NewWebshot

func NewWebshot(c NewConfig) (*Webshot, error)

NewWebshot sets up a new browser with the config provided

func (*Webshot) Extend

func (w *Webshot) Extend() selenium.WebDriver

Extend allow you to use use all of the functionalities provided by selenium

func (*Webshot) Screenshot

func (p *Webshot) Screenshot(requestURL string) ([]byte, error)

Screenshot is the default screenshotter which can take the screenshot of webpages

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL