-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6bfecb9
commit 2a45db8
Showing
4 changed files
with
86 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package sling | ||
|
||
import ( | ||
"github.com/PuerkitoBio/goquery" | ||
"github.com/spf13/cast" | ||
"strings" | ||
) | ||
|
||
type HTMLData struct { | ||
Status int | ||
Message string | ||
Location string | ||
ParentLocation string | ||
Path string | ||
Referer string | ||
ChangeLog string | ||
} | ||
|
||
func HtmlData(html string) (data HTMLData, err error) { | ||
doc, err := goquery.NewDocumentFromReader(strings.NewReader(html)) | ||
if err != nil { | ||
return data, err | ||
} | ||
|
||
data.Status = cast.ToInt(htmlElementText(doc, "#Status", "0")) | ||
data.Message = htmlElementText(doc, "#Message", "Unknown error") | ||
data.Location = htmlElementHref(doc, "#Location", "") | ||
data.ParentLocation = htmlElementHref(doc, "#ParentLocation", "") | ||
data.Path = htmlElementText(doc, "#Path", "") | ||
data.Referer = htmlElementHref(doc, "#Referer", "") | ||
data.ChangeLog = htmlElementText(doc, "#ChangeLog", "") | ||
|
||
return data, nil | ||
} | ||
|
||
func (d HTMLData) IsError() bool { | ||
return d.Status <= 0 || d.Status > 399 | ||
} | ||
|
||
func htmlElementText(doc *goquery.Document, selector string, defaultValue string) string { | ||
selection := doc.Find(selector) | ||
if len(selection.Nodes) > 0 { | ||
return selection.Text() | ||
} | ||
return defaultValue | ||
} | ||
|
||
func htmlElementHref(doc *goquery.Document, selector string, defaultValue string) string { | ||
selection := doc.Find(selector) | ||
if len(selection.Nodes) > 0 { | ||
href, _ := selection.Attr("href") | ||
return href | ||
} | ||
return defaultValue | ||
} |