Skip to content

Commit

Permalink
Implemented @next & Add root selector
Browse files Browse the repository at this point in the history
  • Loading branch information
wspl committed Mar 4, 2017
1 parent 1a6ac6a commit 26f4df8
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 8 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,8 @@ There are all supported funs:

| Name | Parameters | Description |
| --------- | -------------------------------- | ---------------------------------------- |
| $ | (selector: string) | CSS selector |
| $ | (selector: string) | Relative CSS selector (select from parent node)|
| $root | (selector: string) | Absolute CSS selector (select from body)|
| html | | inner HTML |
| text | | inner text |
| outerHTML | | outer HTML |
Expand Down
1 change: 1 addition & 0 deletions eh.crs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ gallery(@page=0,code)="http://g.e-hentai.org/g/{code}/?p={@page}&hc=1"

# Crawler Struct
gallery[]: index -> $("table.itg tr.gtr0,tr.gtr1")
@next: $root("table.ptt td:last-child a").href
title: $("td.itd div div.it5 a").text
cover: $("td.itd div div.it2")
.html
Expand Down
2 changes: 1 addition & 1 deletion format.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Formatted struct {

var (
rx_isTown = regexp.MustCompile(`^\s*[a-zA-Z][a-zA-Z_-]{0,31}\s*(\(|=)`)
rx_isNode = regexp.MustCompile(`^\s*@?[a-zA-Z_-]{1,32}(\[]|\*)?:`)
rx_isNode = regexp.MustCompile(`^\s*@?[a-zA-Z_-]+(\[]|\*)?:`)
)

func Formatting(s string) *Formatted {
Expand Down
23 changes: 19 additions & 4 deletions fun.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ type Fun struct {
Document *goquery.Document
Selection *goquery.Selection
Result string
TempStop bool

PrevFun *Fun
NextFun *Fun
Expand Down Expand Up @@ -67,7 +68,6 @@ func (f *Fun) PageBody() (*goquery.Document, error) {

func (f *Fun) InitSelector(root bool) error {
var baseSel *goquery.Selection

if f.Node.Page != nil {
doc, err := f.PageBody()
if err != nil {
Expand All @@ -77,16 +77,29 @@ func (f *Fun) InitSelector(root bool) error {
baseSel = f.Document.Selection
} else {
f.Node.ParentNode.Fun.Invoke()
baseSel = f.Node.ParentNode.Fun.Selection
if root {
baseSel = f.Node.ParentNode.Fun.Document.Selection
} else {
baseSel = f.Node.ParentNode.Fun.Selection
}
}

if f.Node.IsArray {
bundle := PowerfulFind(baseSel, f.Params[0])
if len(bundle.Nodes) > f.Node.Index {
if len(bundle.Nodes) > f.Node.Index || f.TempStop {
f.Selection = PowerfulFind(baseSel, f.Params[0]).Eq(f.Node.Index)
f.TempStop = false
} else {
// overflow current page
f.Node.Page.Inc()
if f.Node.NextDirectorNode() != nil {
f.TempStop = true
np, err := f.Node.NextDirectorNode().Value()
if err != nil { return err }
f.Node.Page.NextMode = true
f.Node.Page.NextUrl = np
} else {
f.Node.Page.Inc()
}
f.Node.Reset()
f.InitSelector(root)
}
Expand All @@ -106,6 +119,8 @@ func (f *Fun) Invoke() (string, error) {
switch f.Name {
case "$":
err = f.InitSelector(false)
case "$root":
err = f.InitSelector(true)
case "attr":
f.Result, _ = f.PrevFun.Selection.Attr(f.Params[0])
case "text":
Expand Down
10 changes: 8 additions & 2 deletions page.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,11 @@ type Page struct {
Raw string
Node *Node

Town *Town
Ref string
Town *Town
Ref string

NextUrl string
NextMode bool

Index int
}
Expand All @@ -21,6 +24,9 @@ func (p *Page) Inc() {
}

func (p *Page) Url() (string, error) {
if p.NextMode {
return p.NextUrl, nil
}
if p.Town != nil {
p.Town.Attach()
if p.Index > -1 {
Expand Down
21 changes: 21 additions & 0 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,24 @@ func MD5(s string) string {
m.Write([]byte(s))
return hex.EncodeToString(m.Sum(nil))
}

type MonoStack struct {
value string
has bool
}

func (o *MonoStack) Set(s string) {
o.value = s
o.has = true
}

func (o *MonoStack) Has() bool {
return o.has
}

func (o *MonoStack) Value() string {
o.has = false
s := o.value
o.value = ""
return s
}

1 comment on commit 26f4df8

@wspl
Copy link
Owner Author

@wspl wspl commented on 26f4df8 Mar 4, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#3

Please sign in to comment.