Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v0.2.0 #2

Merged
merged 8 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ imdb/.imdbcache/

# Sample output files
output.html
output.json

# Api key files
apikey.txt
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,9 @@ root of each package.
```go
import "github.com/Jisin0/filmigo/imdb"

client := imdb.NewClient()

func main() {
client := imdb.NewClient()

movie, _ := client.GetMovie("t1375666")
movie.PrettyPrint()
}
Expand All @@ -52,9 +52,9 @@ func main() {
```go
import "github.com/Jisin0/filmigo/omdb"

client := omdb.NewClient("your_api_key")

func main() {
client := omdb.NewClient("your_api_key")

movie, _ := client.GetMovie("t1375666")
movie.PrettyPrint()
}
Expand All @@ -67,9 +67,9 @@ of the movie which is more common within justwatch.
```go
import "github.com/Jisin0/filmigo/justwatch"

client := justwatch.NewClient()

func main() {
client := justwatch.NewClient()

movie, _ := client.GetTitle("tm92641")
movie.PrettyPrint()
}
Expand All @@ -78,7 +78,7 @@ func main() {

## Disclaimer
- This product is only for educational purposes and is not meant for commercial usage .
- This product uses the apis of imdb, omdb and juswatch but is by no means endorsed or certified by any of them.
- This product uses the apis of imdb, omdb and juswatch but is by no means endorsed nor certified by any of them.
- This product uses apis not intended for public use.
- This product **does not** use justwatch's official [partners api](https://www.justwatch.com/us/JustWatch-Streaming-API).
- This product comes with **no warranty**.
Expand Down
2 changes: 1 addition & 1 deletion encode/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
)

// Returns a list of Link by searching for all a tags.
func getLinks(node *html.Node) types.Links {
func GetXpathLinks(node *html.Node) types.Links {
ls, e := htmlquery.QueryAll(node, ".//a")
if e != nil || len(ls) < 1 {
return []types.Link{}
Expand Down
45 changes: 20 additions & 25 deletions encode/xpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package encode

import (
"errors"
"reflect"
"strings"

Expand All @@ -21,21 +22,18 @@ var linkStructType = reflect.TypeOf(types.Link{})
// - val interface : input data type.
//
// See https://github.com/Jisin/filmigo/xpath for examples and full reference.
func Xpath(doc *html.Node, val any) any {
st := reflect.TypeOf(val)

// https://stackoverflow.com/questions/63421976
// v is the interface{}
v := reflect.ValueOf(&val).Elem()
// Allocate a temporary variable with type of the struct.
//
// v.Elem() is the vale contained in the interface.
tmp := reflect.New(v.Elem().Type()).Elem()
// Copy the struct value contained in interface to
// the temporary variable.
tmp.Set(v.Elem())

for i := 0; i < tmp.NumField(); i++ {
func Xpath(doc *html.Node, target any) error {
rv := reflect.ValueOf(target)
if rv.Kind() != reflect.Pointer || rv.IsNil() {
return errors.New("input type is not a pointer")
}

st := reflect.TypeOf(target).Elem()

// Access the struct value within the interface
v := reflect.ValueOf(target).Elem()

for i := 0; i < v.NumField(); i++ {
field := st.Field(i)

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

node, err := htmlquery.Query(doc, path)
if node == nil || err != nil {
if err != nil || node == nil {
continue
}

fieldType := field.Type

// Extra options are passed with a separator | in the xpath struct tag, for ex. src to get the src attr of a node
switch method {
case "attr":
for _, a := range node.Attr {
if a.Key == attr {
tmp.FieldByName(field.Name).SetString(a.Val)
v.FieldByName(field.Name).SetString(a.Val)
break
}
}
Expand All @@ -81,20 +78,18 @@ func Xpath(doc *html.Node, val any) any {
// If the field is of type []Link all inner a tags are extracted
// If field type is []string innertex of each li tag is extracted
if fieldType.Kind() == reflect.Slice && fieldType.Elem() == linkStructType {
links := getLinks(node)
links := GetXpathLinks(node)
lVal := reflect.Append(reflect.ValueOf(links))
tmp.FieldByName(field.Name).Set(lVal)
v.FieldByName(field.Name).Set(lVal)
} else if fieldType.Kind() == reflect.Slice && fieldType.Elem().Kind() == reflect.String {
list := getTextList(node)
lVal := reflect.Append(reflect.ValueOf(list))
tmp.FieldByName(field.Name).Set(lVal)
v.FieldByName(field.Name).Set(lVal)
} else {
tmp.FieldByName(field.Name).SetString(htmlquery.InnerText(node))
v.FieldByName(field.Name).SetString(htmlquery.InnerText(node))
}
}
}

v.Set(tmp)

return val
return nil
}
10 changes: 4 additions & 6 deletions encode/xpath_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,16 @@ func TestXpath(t *testing.T) {
}

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

res, ok := encode.Xpath(doc, sampleData{}).(sampleData)
if !ok {
t.Errorf("unknown type returned")
t.FailNow()
}
var res sampleType

encode.Xpath(doc, &res)

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