Skip to content

Commit

Permalink
Merge pull request #25 from DanielFillol/daniel
Browse files Browse the repository at this point in the history
implement SaveImageBase64
  • Loading branch information
DanielFillol authored Jun 14, 2024
2 parents 80ec5ce + fc8d5a1 commit cdf82ac
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 1 deletion.
63 changes: 62 additions & 1 deletion goSpider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package goSpider
import (
"bufio"
"context"
"encoding/base64"
"errors"
"fmt"
"github.com/DanielFillol/goSpider/htmlQuery"
Expand Down Expand Up @@ -126,7 +127,7 @@ func (nav *Navigator) GetCurrentURL() (string, error) {
// Login logs into a website using the provided credentials and selectors.
// Example:
//
// err := nav.Login("https://www.example.com/login", "username", "password", "#username", "#password", "#login-button", "Login failed")
// err := nav.Login("https://www.example.com/login", "username", "password", "#username", "#password", "#login-button", "#login-message-fail")
func (nav *Navigator) Login(url, username, password, usernameSelector, passwordSelector, loginButtonSelector string, messageFailedSuccess string) error {
nav.Logger.Printf("Logging into URL: %s\n", url)

Expand Down Expand Up @@ -792,6 +793,66 @@ func (nav *Navigator) GetElement(selector string) (string, error) {
return content, nil
}

// SaveImageBase64 extracts the base64 image data from the given selector and saves it to a file.
//
// Parameters:
// - selector: the CSS selector of the CAPTCHA image element
// - outputPath: the file path to save the image
// - prefixClean: the prefix to clear form the source, if any
//
// Example:
//
// err := nav.SaveImageBase64("#imagemCaptcha", "captcha.png", "data:image/png;base64,")
func (nav *Navigator) SaveImageBase64(selector, outputPath, prefixClean string) error {
var imageData string

// Run the tasks
err := chromedp.Run(nav.Ctx,
chromedp.AttributeValue(selector, "src", &imageData, nil),
)
if err != nil {
nav.Logger.Printf("Error - Failed to get image data: %v\n", err)
return fmt.Errorf("error - failed to get image data: %w", err)
}

var base64Data string
if prefixClean != "" {
// Check if the image data is in base64 format
if !strings.HasPrefix(imageData, prefixClean) {
nav.Logger.Printf("Error - Unexpected image format: %v\n", err)
return fmt.Errorf("error - unexpected image format")
}

// Remove the data URL prefix
base64Data = strings.TrimPrefix(imageData, prefixClean)
}

// Remove any newlines or spaces (just in case)
base64Data = strings.ReplaceAll(base64Data, "\n", "")
base64Data = strings.ReplaceAll(base64Data, "\r", "")
base64Data = strings.TrimSpace(base64Data)

// Decode the base64 data
imageBytes, err := base64.StdEncoding.DecodeString(base64Data)
if err != nil {
return fmt.Errorf("failed to decode base64 image: %w", err)
}

// Check if decoded bytes are non-zero
if len(imageBytes) == 0 {
return fmt.Errorf("decoded image bytes are zero, something went wrong with extraction or decoding")
}

// Save the image to a file
err = ioutil.WriteFile(outputPath, imageBytes, 0644)
if err != nil {
return fmt.Errorf("failed to save image: %w", err)
}

nav.Logger.Printf("Captcha image saved successfully to %s", outputPath)
return nil
}

// Close closes the Navigator instance and releases resources.
// Example:
//
Expand Down
30 changes: 30 additions & 0 deletions goSpider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,36 @@ func TestGetCurrentURL(t *testing.T) {
//
//}

func TestSaveCaptchaImage(t *testing.T) {
nav := NewNavigator("", false)
defer nav.Close()

err := nav.OpenURL("https://pje.trt2.jus.br/consultaprocessual/")
if err != nil {
t.Errorf("OpenURL error: %v", err)
}

err = nav.FillField("#nrProcessoInput", "1000113-34.2018.5.02.0386")
if err != nil {
t.Errorf("FillField error: %v", err)
}

err = nav.ClickButton("#btnPesquisar")
if err != nil {
t.Errorf("ClickButton error: %v", err)
}

err = nav.WaitForElement("#imagemCaptcha", 10*time.Second)
if err != nil {
t.Errorf("WaitForElement error: %v", err)
}

err = nav.SaveImageBase64("#imagemCaptcha", "image.png", "data:image/png;base64,")
if err != nil {
t.Errorf("SaveImageBase64 error: %v", err)
}
}

func TestParallelRequests(t *testing.T) {
users := []Request{
{SearchString: "1017927-35.2023.8.26.0008"},
Expand Down

0 comments on commit cdf82ac

Please sign in to comment.