Skip to content

Commit 2ce0065

Browse files
authored
Merge pull request #2 from Jisin0/v2
v0.2.0
2 parents c18f214 + 6fa9ed4 commit 2ce0065

File tree

16 files changed

+503
-300
lines changed

16 files changed

+503
-300
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ imdb/.imdbcache/
2626

2727
# Sample output files
2828
output.html
29+
output.json
2930

3031
# Api key files
3132
apikey.txt

README.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ root of each package.
3939
```go
4040
import "github.com/Jisin0/filmigo/imdb"
4141

42-
client := imdb.NewClient()
43-
4442
func main() {
43+
client := imdb.NewClient()
44+
4545
movie, _ := client.GetMovie("t1375666")
4646
movie.PrettyPrint()
4747
}
@@ -52,9 +52,9 @@ func main() {
5252
```go
5353
import "github.com/Jisin0/filmigo/omdb"
5454

55-
client := omdb.NewClient("your_api_key")
56-
5755
func main() {
56+
client := omdb.NewClient("your_api_key")
57+
5858
movie, _ := client.GetMovie("t1375666")
5959
movie.PrettyPrint()
6060
}
@@ -67,9 +67,9 @@ of the movie which is more common within justwatch.
6767
```go
6868
import "github.com/Jisin0/filmigo/justwatch"
6969

70-
client := justwatch.NewClient()
71-
7270
func main() {
71+
client := justwatch.NewClient()
72+
7373
movie, _ := client.GetTitle("tm92641")
7474
movie.PrettyPrint()
7575
}
@@ -78,7 +78,7 @@ func main() {
7878

7979
## Disclaimer
8080
- This product is only for educational purposes and is not meant for commercial usage .
81-
- This product uses the apis of imdb, omdb and juswatch but is by no means endorsed or certified by any of them.
81+
- This product uses the apis of imdb, omdb and juswatch but is by no means endorsed nor certified by any of them.
8282
- This product uses apis not intended for public use.
8383
- This product **does not** use justwatch's official [partners api](https://www.justwatch.com/us/JustWatch-Streaming-API).
8484
- This product comes with **no warranty**.

encode/helpers.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
)
1313

1414
// Returns a list of Link by searching for all a tags.
15-
func getLinks(node *html.Node) types.Links {
15+
func GetXpathLinks(node *html.Node) types.Links {
1616
ls, e := htmlquery.QueryAll(node, ".//a")
1717
if e != nil || len(ls) < 1 {
1818
return []types.Link{}

encode/xpath.go

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package encode
55

66
import (
7+
"errors"
78
"reflect"
89
"strings"
910

@@ -21,21 +22,18 @@ var linkStructType = reflect.TypeOf(types.Link{})
2122
// - val interface : input data type.
2223
//
2324
// See https://github.com/Jisin/filmigo/xpath for examples and full reference.
24-
func Xpath(doc *html.Node, val any) any {
25-
st := reflect.TypeOf(val)
26-
27-
// https://stackoverflow.com/questions/63421976
28-
// v is the interface{}
29-
v := reflect.ValueOf(&val).Elem()
30-
// Allocate a temporary variable with type of the struct.
31-
//
32-
// v.Elem() is the vale contained in the interface.
33-
tmp := reflect.New(v.Elem().Type()).Elem()
34-
// Copy the struct value contained in interface to
35-
// the temporary variable.
36-
tmp.Set(v.Elem())
37-
38-
for i := 0; i < tmp.NumField(); i++ {
25+
func Xpath(doc *html.Node, target any) error {
26+
rv := reflect.ValueOf(target)
27+
if rv.Kind() != reflect.Pointer || rv.IsNil() {
28+
return errors.New("input type is not a pointer")
29+
}
30+
31+
st := reflect.TypeOf(target).Elem()
32+
33+
// Access the struct value within the interface
34+
v := reflect.ValueOf(target).Elem()
35+
36+
for i := 0; i < v.NumField(); i++ {
3937
field := st.Field(i)
4038

4139
args := strings.Split(field.Tag.Get("xpath"), "|")
@@ -61,18 +59,17 @@ func Xpath(doc *html.Node, val any) any {
6159
path := args[0]
6260

6361
node, err := htmlquery.Query(doc, path)
64-
if node == nil || err != nil {
62+
if err != nil || node == nil {
6563
continue
6664
}
6765

6866
fieldType := field.Type
6967

70-
// Extra options are passed with a separator | in the xpath struct tag, for ex. src to get the src attr of a node
7168
switch method {
7269
case "attr":
7370
for _, a := range node.Attr {
7471
if a.Key == attr {
75-
tmp.FieldByName(field.Name).SetString(a.Val)
72+
v.FieldByName(field.Name).SetString(a.Val)
7673
break
7774
}
7875
}
@@ -81,20 +78,18 @@ func Xpath(doc *html.Node, val any) any {
8178
// If the field is of type []Link all inner a tags are extracted
8279
// If field type is []string innertex of each li tag is extracted
8380
if fieldType.Kind() == reflect.Slice && fieldType.Elem() == linkStructType {
84-
links := getLinks(node)
81+
links := GetXpathLinks(node)
8582
lVal := reflect.Append(reflect.ValueOf(links))
86-
tmp.FieldByName(field.Name).Set(lVal)
83+
v.FieldByName(field.Name).Set(lVal)
8784
} else if fieldType.Kind() == reflect.Slice && fieldType.Elem().Kind() == reflect.String {
8885
list := getTextList(node)
8986
lVal := reflect.Append(reflect.ValueOf(list))
90-
tmp.FieldByName(field.Name).Set(lVal)
87+
v.FieldByName(field.Name).Set(lVal)
9188
} else {
92-
tmp.FieldByName(field.Name).SetString(htmlquery.InnerText(node))
89+
v.FieldByName(field.Name).SetString(htmlquery.InnerText(node))
9390
}
9491
}
9592
}
9693

97-
v.Set(tmp)
98-
99-
return val
94+
return nil
10095
}

encode/xpath_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,16 @@ func TestXpath(t *testing.T) {
1616
}
1717

1818
if doc != nil {
19-
type sampleData struct {
19+
type sampleType struct {
2020
InnerText string `xpath:"//p[contains(@class, 'substring')]"`
2121
Attribute string `xpath:"//p[last()]|attr_my-attr"`
2222
LinkList types.Links `xpath:"//span[@class='sample']"`
2323
StringList []string `xpath:"//ul"`
2424
}
2525

26-
res, ok := encode.Xpath(doc, sampleData{}).(sampleData)
27-
if !ok {
28-
t.Errorf("unknown type returned")
29-
t.FailNow()
30-
}
26+
var res sampleType
27+
28+
encode.Xpath(doc, &res)
3129

3230
if res.Attribute == "" || res.InnerText == "" || len(res.LinkList) < 3 || len(res.StringList) < 3 {
3331
t.Errorf("xpath failed with output : %+v", res)

0 commit comments

Comments
 (0)