mirror of
https://github.com/MontFerret/ferret.git
synced 2025-06-25 00:37:26 +02:00
Implement DOWNLOAD function (#132)
This commit is contained in:
@ -3,6 +3,8 @@ package html
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/ioutil"
|
||||||
|
"net/http"
|
||||||
"regexp"
|
"regexp"
|
||||||
|
|
||||||
"github.com/mafredri/cdp/protocol/page"
|
"github.com/mafredri/cdp/protocol/page"
|
||||||
@ -373,3 +375,28 @@ func PDF(ctx context.Context, args ...core.Value) (core.Value, error) {
|
|||||||
|
|
||||||
return pdf, nil
|
return pdf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Download a ressource from the given URL.
|
||||||
|
// @param URL (String) - URL to download.
|
||||||
|
// @returns data (Binary) - Returns a base64 encoded string in binary format.
|
||||||
|
func Download(_ context.Context, args ...core.Value) (core.Value, error) {
|
||||||
|
err := core.ValidateArgs(args, 1, 1)
|
||||||
|
if err != nil {
|
||||||
|
return values.None, err
|
||||||
|
}
|
||||||
|
|
||||||
|
arg1 := args[0]
|
||||||
|
err = core.ValidateType(arg1, core.StringType)
|
||||||
|
if err != nil {
|
||||||
|
return values.None, err
|
||||||
|
}
|
||||||
|
resp, err := http.Get(arg1.String())
|
||||||
|
if err != nil {
|
||||||
|
return values.None, err
|
||||||
|
}
|
||||||
|
data, err := ioutil.ReadAll(resp.Body)
|
||||||
|
if err != nil {
|
||||||
|
return values.None, err
|
||||||
|
}
|
||||||
|
return values.NewBinary(data), nil
|
||||||
|
}
|
||||||
|
@ -34,5 +34,6 @@ func NewLib() map[string]core.Function {
|
|||||||
"INNER_TEXT_ALL": InnerTextAll,
|
"INNER_TEXT_ALL": InnerTextAll,
|
||||||
"SCREENSHOT": Screenshot,
|
"SCREENSHOT": Screenshot,
|
||||||
"PDF": PDF,
|
"PDF": PDF,
|
||||||
|
"DOWNLOAD": Download,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user