webrequest pkg, web request bar

This commit is contained in:
Sebastian Frank 2019-03-27 12:52:30 +01:00
parent 4cb09fb81f
commit 5d6d03702e
Signed by: apairon
GPG Key ID: 7270D06DDA7FE8C3
5 changed files with 30 additions and 14 deletions

View File

@ -18,6 +18,7 @@ import (
"gitbase.de/apairon/mark2web/pkg/jobm" "gitbase.de/apairon/mark2web/pkg/jobm"
"gitbase.de/apairon/mark2web/pkg/logger" "gitbase.de/apairon/mark2web/pkg/logger"
"gitbase.de/apairon/mark2web/pkg/mark2web" "gitbase.de/apairon/mark2web/pkg/mark2web"
"gitbase.de/apairon/mark2web/pkg/webrequest"
"github.com/disintegration/imaging" "github.com/disintegration/imaging"
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
) )
@ -153,7 +154,7 @@ func ImageProcessFilter(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *
logger.Log.Noticef("processing image from %s to %s", imgSource, imgTarget) logger.Log.Noticef("processing image from %s to %s", imgSource, imgTarget)
if strings.HasPrefix(imgSource, "http://") || strings.HasPrefix(imgSource, "https://") { if strings.HasPrefix(imgSource, "http://") || strings.HasPrefix(imgSource, "https://") {
// remote file // remote file
img, p.Format, err = helper.ImageWebRequest(imgSource) img, p.Format, err = webrequest.GetImage(imgSource)
} else { } else {
img, err = imaging.Open(imgSource, imaging.AutoOrientation(true)) img, err = imaging.Open(imgSource, imaging.AutoOrientation(true))
} }

View File

@ -8,6 +8,7 @@ import (
"gitbase.de/apairon/mark2web/pkg/helper" "gitbase.de/apairon/mark2web/pkg/helper"
"gitbase.de/apairon/mark2web/pkg/logger" "gitbase.de/apairon/mark2web/pkg/logger"
"gitbase.de/apairon/mark2web/pkg/webrequest"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
) )
@ -57,7 +58,7 @@ func (node *TreeNode) handleCollections() {
cacheEntry.hit++ cacheEntry.hit++
} else { } else {
logger.Log.Noticef("reading collection from: %s", errSrcText) logger.Log.Noticef("reading collection from: %s", errSrcText)
colData = helper.JSONWebRequest(url) colData = webrequest.GetJSON(url)
colCache[url] = &colCacheEntry{ colCache[url] = &colCacheEntry{
data: colData, data: colData,
navnames: make([]string, 0), navnames: make([]string, 0),

View File

@ -1,14 +1,14 @@
package mark2web package mark2web
import ( import (
"gitbase.de/apairon/mark2web/pkg/helper" "gitbase.de/apairon/mark2web/pkg/webrequest"
"github.com/flosch/pongo2" "github.com/flosch/pongo2"
) )
// RequestFn will make a web request and returns map[string]interface form pongo2 // RequestFn will make a web request and returns map[string]interface form pongo2
func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value { func RequestFn(url *pongo2.Value, args ...*pongo2.Value) *pongo2.Value {
u := url.String() u := url.String()
return pongo2.AsValue(helper.JSONWebRequest(u)) return pongo2.AsValue(webrequest.GetJSON(u))
} }
// RenderFn renders a pongo2 template with additional context // RenderFn renders a pongo2 template with additional context

View File

@ -17,6 +17,12 @@ var bars = make(map[string]*bar)
var initialized = false var initialized = false
var terminalWidth = 80 var terminalWidth = 80
// OverallTotal is number of total jobs
var OverallTotal = 0
// OverallDone is number of done jobs
var OverallDone = 0
// Init initializes the bar drawing // Init initializes the bar drawing
func Init() { func Init() {
if t, err := tty.Open(); err == nil && t != nil { if t, err := tty.Open(); err == nil && t != nil {
@ -29,6 +35,7 @@ func Init() {
// IncrTotal increases the total jobs for the bar // IncrTotal increases the total jobs for the bar
func IncrTotal(barname string) { func IncrTotal(barname string) {
OverallTotal++
if initialized { if initialized {
_bar := bars[barname] _bar := bars[barname]
if _bar == nil { if _bar == nil {
@ -52,6 +59,7 @@ func IncrTotal(barname string) {
// IncrDone increases to done jobs counter // IncrDone increases to done jobs counter
func IncrDone(barname string) { func IncrDone(barname string) {
OverallDone++
if initialized { if initialized {
bars[barname].Bar.Incr() bars[barname].Bar.Incr()
bars[barname].Description = "" bars[barname].Description = ""

View File

@ -1,4 +1,4 @@
package helper package webrequest
import ( import (
"encoding/json" "encoding/json"
@ -8,6 +8,8 @@ import (
"net/http" "net/http"
"strings" "strings"
"gitbase.de/apairon/mark2web/pkg/progress"
"gitbase.de/apairon/mark2web/pkg/logger" "gitbase.de/apairon/mark2web/pkg/logger"
) )
@ -23,18 +25,22 @@ type wrJSONEntry struct {
var wrImageCache = make(map[string]*wrImageEntry) var wrImageCache = make(map[string]*wrImageEntry)
var wrJSONCache = make(map[string]*wrJSONEntry) var wrJSONCache = make(map[string]*wrJSONEntry)
// WebRequest will fetch an url and returns reponse // Get will fetch an url and returns reponse
func WebRequest(url string, opts interface{}) (*http.Response, error) { func Get(url string, opts interface{}) (resp *http.Response, err error) {
logger.Log.Noticef("requesting url via GET %s", url) logger.Log.Noticef("requesting url via GET %s", url)
return http.Get(url) progress.IncrTotal("web request")
progress.DescribeCurrent("web request", url)
resp, err = http.Get(url)
progress.IncrDone("web request")
return resp, err
} }
// JSONWebRequest will GET a json object/array from a given URL // GetJSON will GET a json object/array from a given URL
func JSONWebRequest(url string) interface{} { func GetJSON(url string) interface{} {
cached := wrJSONCache[url] cached := wrJSONCache[url]
if cached == nil { if cached == nil {
resp, err := WebRequest(url, nil) resp, err := Get(url, nil)
if err != nil { if err != nil {
logger.Log.Panicf("could not get url '%s': %s", url, err) logger.Log.Panicf("could not get url '%s': %s", url, err)
} }
@ -81,11 +87,11 @@ func JSONWebRequest(url string) interface{} {
return cached.data return cached.data
} }
// ImageWebRequest gets an image from an url // GetImage gets an image from an url
func ImageWebRequest(url string) (image.Image, string, error) { func GetImage(url string) (image.Image, string, error) {
cached := wrImageCache[url] cached := wrImageCache[url]
if cached == nil { if cached == nil {
resp, err := WebRequest(url, nil) resp, err := Get(url, nil)
if err != nil { if err != nil {
return nil, "", fmt.Errorf("could not get url '%s': %s", url, err) return nil, "", fmt.Errorf("could not get url '%s': %s", url, err)
} }