2018-09-19 03:41:16 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
2018-12-22 06:14:41 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/cdp"
|
|
|
|
"github.com/MontFerret/ferret/pkg/drivers/http"
|
2018-09-19 03:41:16 +02:00
|
|
|
"os"
|
2018-12-01 02:30:55 +02:00
|
|
|
|
|
|
|
"github.com/MontFerret/ferret/pkg/compiler"
|
2018-12-22 06:14:41 +02:00
|
|
|
"github.com/MontFerret/ferret/pkg/drivers"
|
2018-09-19 03:41:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Topic struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Description string `json:"description"`
|
2018-10-05 22:35:08 +02:00
|
|
|
URL string `json:"url"`
|
2018-09-19 03:41:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
topics, err := getTopTenTrendingTopics()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, topic := range topics {
|
2018-10-05 22:35:08 +02:00
|
|
|
fmt.Println(fmt.Sprintf("%s: %s %s", topic.Name, topic.Description, topic.URL))
|
2018-09-19 03:41:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getTopTenTrendingTopics() ([]*Topic, error) {
|
|
|
|
query := `
|
|
|
|
LET doc = DOCUMENT("https://github.com/topics")
|
|
|
|
|
|
|
|
FOR el IN ELEMENTS(doc, ".py-4.border-bottom")
|
|
|
|
LIMIT 10
|
|
|
|
LET url = ELEMENT(el, "a")
|
|
|
|
LET name = ELEMENT(el, ".f3")
|
|
|
|
LET desc = ELEMENT(el, ".f5")
|
|
|
|
|
|
|
|
RETURN {
|
|
|
|
name: TRIM(name.innerText),
|
|
|
|
description: TRIM(desc.innerText),
|
|
|
|
url: "https://github.com" + url.attributes.href
|
|
|
|
}
|
|
|
|
`
|
|
|
|
|
|
|
|
comp := compiler.New()
|
|
|
|
|
|
|
|
program, err := comp.Compile(query)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-12-01 02:30:55 +02:00
|
|
|
// create a root context
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
// enable HTML drivers
|
|
|
|
// by default, Ferret Runtime knows nothing about HTML drivers
|
|
|
|
// all HTML manipulations are done via functions from standard library
|
2018-12-22 06:14:41 +02:00
|
|
|
ctx = drivers.WithDynamic(ctx, cdp.NewDriver())
|
|
|
|
ctx = drivers.WithStatic(ctx, http.NewDriver())
|
2018-12-01 02:30:55 +02:00
|
|
|
|
|
|
|
out, err := program.Run(ctx)
|
2018-09-19 03:41:16 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make([]*Topic, 0, 10)
|
|
|
|
|
|
|
|
err = json.Unmarshal(out, &res)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|