Using chromedp to render PNG object from HTML string - chromedp

I am very new to Chromedp. I know how to render image from an url for Chromdp but is there a way to render it from a HTML string itself using Chromedp? I am able to convert an url to image using the following:
func fullScreenshot(urlstr string, quality int64, res *[]byte) chromedp.Tasks {
return chromedp.Tasks{
chromedp.Navigate(urlstr),
chromedp.Sleep(3 * time.Second),
chromedp.ActionFunc(func(ctx context.Context) error {
// get layout metrics
_, _, contentSize, err := page.GetLayoutMetrics().Do(ctx)
if err != nil {
return err
}
width, height := int64(math.Ceil(contentSize.Width)), int64(math.Ceil(contentSize.Height))
// force viewport emulation
err = emulation.SetDeviceMetricsOverride(width, height, 1, false).
WithScreenOrientation(&emulation.ScreenOrientation{
Type: emulation.OrientationTypePortraitPrimary,
Angle: 0,
}).
Do(ctx)
if err != nil {
return err
}
// capture screenshot
*res, err = page.CaptureScreenshot().
WithQuality(quality).
WithClip(&page.Viewport{
X: contentSize.X,
Y: contentSize.Y,
Width: contentSize.Width,
Height: contentSize.Height,
Scale: 1,
}).Do(ctx)
if err != nil {
return err
}
return nil
}),
}
}

Related

How to get the page name in go?

I have a function which should get the page name and print it, for example, if the URL is http://localhost:8080/login.html the function should print login.html
If you only need to parse the URL you can use below:
package main
import (
"fmt"
"net/url"
)
func main() {
URL := "http://localhost:8080/login.html"
name, err := getPageName(URL)
if err != nil {
panic(err)
}
fmt.Println(name)
}
func getPageName(URL string) (string, error) {
u, err := url.Parse(URL)
if err != nil {
return "", err
}
return u.Path[1:], nil // To remove initial /
}
If you need to get page's HTML and parse the title from <head> you can use go-query
package main
import (
"fmt"
"log"
"net/http"
"github.com/PuerkitoBio/goquery"
)
func main() {
URL := "https://stackoverflow.com"
res, err := http.Get(URL)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
title := doc.Find("title").Text()
fmt.Println(title)
}
You can do this to get the page name:
func GetPageName(address string) (string, error) {
u, err := url.Parse(address)
if err != nil {
return "", err
}
params := strings.Split(u.Path, "/")
// Index page
if len(params) == 0 || (len(params) == 1 && params[0] == "") {
return "", nil
} else {
pageName := params[len(params)-1]
// Has trailing slash
if pageName == "" {
return params[len(params)-2], nil
}
// Doesn't have trailing slash
return pageName, nil
}
}
If the url address is the index page address, for example host.com or host.com/ it returns an empty string.
Otherwise returns the page name for the given url. For example test for host.com/test and host.com/test/ and host.com/path/test.

Convert map[string][]string into a yaml structure

I try to convert a make(map[string]string) into a yaml like that:
Yaml Output desire:
items:
keys1:value1
keys2:value2
keys3:value3
keys4:value4
The keys,values are this listKey map of string. J = string = {"key1":"value1","key2":"value2" }
type Items struct {
items string
ItemsValues map[string][]string
}
func ConvertToYelm(j string){
y := Items{}
var dataJson map[string]string
err := json.Unmarshal([]byte(j), &dataJson)
if err != nil {
fmt.Println(err)
return
}
listKey := make(map[string]string)
for k := range dataJson{
listKey[k] = k
}
yelm, err := yaml.Marshal(listKey)
if err != nil {
fmt.Println(err)
return
}
err = yaml.Unmarshal(yelm, Items)
if err != nil {
fmt.Println(err)
return
}
yeml2, err := yaml.Marshal(&yelm)
fmt.Printf ("%s", string(yeml2))
To be honest, I'm a little bit lost here, thank you for the help
To get the exact YAML from your post:
items:
keys1:value1
keys2:value2
keys3:value3
keys4:value4
You can do this (Go Playground):
package main
import (
"fmt"
"gopkg.in/yaml.v2"
)
type ItemsStruct struct {
Items map[string]string `yaml:"items"`
}
func main() {
itms := &ItemsStruct{Items: map[string]string{
"keys1": "value1",
"keys2": "value2",
"keys3": "value3",
"keys4": "value4"}}
yamlBytes, err := yaml.Marshal(itms)
if err != nil {
//handle error
}
fmt.Println(string(yamlBytes))
}
And just to add, I see your code is decoding this JSON {"key1":"value1", "key2":"value2", ... } and then encoding it as YAML in your specified format. Here is the Go Playground for that.

Navigation into a map with a string path variable for golang

I would like to directly navigate to a value in a map. Lets be more specific with the following go code example which should give me the value of "Walter" directly: (https://play.golang.org/p/tYJsvp39hn)
type Signature struct{
Name string
Signed bool
}
path := "Document.Signatures.1.Name"
map := map[string]interface{}{
"Document": map[string]interface{}{
"Signatures": []interface{}{
Signature{ Name: "Hugo", Signed: false },
Signature{ Name: "Walter", Signed: false },
},
"Otherstuff": "asadwa",
},
"AlsoOtherStuff": "adwaw",
}
// map.giveMe(path)
// even better (if possible:) map.change(path,"ToThisNewValue")
I have searched for solutions, but I can't find any on the internet. Maybe one of you knows how to do this or knows a library to use for me.
Thank you so much in advance!
Quite a lot of reflect calls will be needed if there is no predefined struct.
That being said, you can do it by iterating through the map with type checking on every iteration and handling cases accordingly.
// Splitting the path into keys
keys := strings.Split(path, ".")
var value interface{} = map1
for _, key := range keys {
if value, err = Get(key, value); err != nil {
break
}
}
if err == nil {
fmt.Println("Value:", value)
} else {
fmt.Println("Error:", err)
}
func Get(key string, s interface{}) (v interface{}, err error) {
var (
i int64
ok bool
)
switch s.(type) {
case map[string]interface{}:
if v, ok = s.(map[string]interface{})[key]; !ok {
err = fmt.Errorf("Key not present. [Key:%s]", key)
}
case []interface{}:
if i, err = strconv.ParseInt(key, 10, 64); err == nil {
array := s.([]interface{})
if int(i) < len(array) {
v = array[i]
} else {
err = fmt.Errorf("Index out of bounds. [Index:%d] [Array:%v]", i, array)
}
}
case Signature:
r := reflect.ValueOf(s)
v = reflect.Indirect(r).FieldByName(key)
}
//fmt.Println("Value:", v, " Key:", key, "Error:", err)
return v, err
}
Playground code

Mcrypt from PHP to Go

Im using a class to encrypt/decrypt strings in PHP.
How could I encrypt/decrypt the strings in Go?
The PHP class:
class Crypto {
private $encryptKey = 'xxxxxxxxxxxxxxxx';
private $iv = 'xxxxxxxxxxxxxxxx';
private $blocksize = 16;
public function decrypt($data)
{
return $this->unpad(mcrypt_decrypt(MCRYPT_RIJNDAEL_128,
$this->encryptKey,
hex2bin($data),
MCRYPT_MODE_CBC, $this->iv), $this->blocksize);
}
public function encrypt($data)
{
//don't use default php padding which is '\0'
$pad = $this->blocksize - (strlen($data) % $this->blocksize);
$data = $data . str_repeat(chr($pad), $pad);
return bin2hex(mcrypt_encrypt(MCRYPT_RIJNDAEL_128,
$this->encryptKey,
$data, MCRYPT_MODE_CBC, $this->iv));
}
private function unpad($str, $blocksize)
{
$len = strlen($str);
$pad = ord($str[$len - 1]);
if ($pad && $pad <= $blocksize) {
if (substr($str, -$pad) === str_repeat(chr($pad), $pad)) {
return substr($str, 0, $len - $pad);
}
}
return $str;
}
}
What to be able to encrypt/decrypt same string in both PHP and Go.
Here is an example:
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"encoding/base64"
"errors"
"fmt"
"io"
"log"
)
func main() {
key := []byte("xxxxxxxxxxxxxxxx") // 32 bytes
plaintext := []byte("TEST")
fmt.Printf("%s\n", plaintext)
ciphertext, err := encrypt(key, plaintext)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%0x\n", ciphertext)
result, err := decrypt(key, ciphertext)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", result)
}
func encrypt(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
b := base64.StdEncoding.EncodeToString(text)
ciphertext := make([]byte, aes.BlockSize+len(b))
iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
return nil, err
}
cfb := cipher.NewCFBEncrypter(block, iv)
cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))
return ciphertext, nil
}
func decrypt(key, text []byte) ([]byte, error) {
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
if len(text) < aes.BlockSize {
return nil, errors.New("ciphertext too short")
}
iv := text[:aes.BlockSize]
text = text[aes.BlockSize:]
cfb := cipher.NewCFBDecrypter(block, iv)
cfb.XORKeyStream(text, text)
data, err := base64.StdEncoding.DecodeString(string(text))
if err != nil {
return nil, err
}
return data, nil
}
Mostly borrowed and adapted from: https://golang.org/src/crypto/cipher/example_test.go
// Input => TEST
// Output => 13360adba03733e11dd2702de441ff8bbb90676ad762fc83
UPDATE
To use a string as a parameter for decode function, you need to convert the string to byte using hex.Decodestring
data, _ := hex.DecodeString("1d6f12d3aa2353b23c6012dbc85816632129363d58a76063")
result, err := decrypt(key, data)
if err != nil {
log.Fatal(err)
}
fmt.Printf("%s\n", result)
Do not forget to include "encoding/hex" into package list.

How do i track the upload progress in golang

i'm trying to track the upload progress in GOLANG, that's what i got at the moment
func Upload(w http.ResponseWriter, req *http.Request) {
mr, err := req.MultipartReader()
if err != nil {
return
}
for {
// var part *multipart.Part
part, err := mr.NextPart()
mr.partsRead
if err == io.EOF {
break
}
println(part)
}
}
This will work, a stream to calc the bytes read and the total progress you need to point the stream somewhere, in this code example I pointed it to a file.
func Upload(w http.ResponseWriter, req *http.Request) {
mr, err := req.MultipartReader()
if err != nil {
return
}
length := req.ContentLength
for {
part, err := mr.NextPart()
if err == io.EOF {
break
}
var read int64
var p float32
dst, err := os.OpenFile("dstfile", os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return
}
for {
buffer := make([]byte, 100000)
cBytes, err := part.Read(buffer)
if err == io.EOF {
break
}
read = read + int64(cBytes)
//fmt.Printf("read: %v \n",read )
p = float32(read) / float32(length) *100
fmt.Printf("progress: %v \n",p )
dst.Write(buffer[0:cBytes])
}
}
}

Resources