github.com/MontFerret/ferret@v0.18.0/examples/embedded/main.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"os"
     8  
     9  	"github.com/MontFerret/ferret/pkg/compiler"
    10  	"github.com/MontFerret/ferret/pkg/drivers"
    11  	"github.com/MontFerret/ferret/pkg/drivers/cdp"
    12  	"github.com/MontFerret/ferret/pkg/drivers/http"
    13  )
    14  
    15  type Topic struct {
    16  	Name        string `json:"name"`
    17  	Description string `json:"description"`
    18  	URL         string `json:"url"`
    19  }
    20  
    21  func main() {
    22  	topics, err := getTopTenTrendingTopics()
    23  
    24  	if err != nil {
    25  		fmt.Println(err)
    26  		os.Exit(1)
    27  	}
    28  
    29  	for _, topic := range topics {
    30  		fmt.Println(fmt.Sprintf("%s: %s %s", topic.Name, topic.Description, topic.URL))
    31  	}
    32  }
    33  
    34  func getTopTenTrendingTopics() ([]*Topic, error) {
    35  	query := `
    36  		LET doc = DOCUMENT("https://github.com/topics")
    37  
    38  		FOR el IN ELEMENTS(doc, ".py-4.border-bottom")
    39  			LIMIT 10
    40  			LET url = ELEMENT(el, "a")
    41  			LET name = ELEMENT(el, ".f3")
    42  			LET description = ELEMENT(el, ".f5")
    43  
    44  			RETURN {
    45  				name: TRIM(name.innerText),
    46  				description: TRIM(description.innerText),
    47  				url: "https://github.com" + url.attributes.href
    48  			}
    49  	`
    50  
    51  	comp := compiler.New()
    52  
    53  	program, err := comp.Compile(query)
    54  
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	// create a root context
    60  	ctx := context.Background()
    61  
    62  	// enable HTML drivers
    63  	// by default, Ferret Runtime does not know about any HTML drivers
    64  	// all HTML manipulations are done via functions from standard library
    65  	// that assume that at least one driver is available
    66  	ctx = drivers.WithContext(ctx, cdp.NewDriver())
    67  	ctx = drivers.WithContext(ctx, http.NewDriver(), drivers.AsDefault())
    68  
    69  	out, err := program.Run(ctx)
    70  
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	res := make([]*Topic, 0, 10)
    76  
    77  	err = json.Unmarshal(out, &res)
    78  
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	return res, nil
    84  }