Wire everything together, now it's amen

This commit is contained in:
2024-11-06 00:40:28 +01:00
parent 0cdc57bf9a
commit 7a04d16a2e
4 changed files with 49 additions and 24 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
main.log main.log
test.html test.html
doc.html doc.html
out

View File

@@ -4,6 +4,7 @@ import (
_ "embed" _ "embed"
"fmt" "fmt"
"os" "os"
"path/filepath"
"text/template" "text/template"
) )
@@ -44,12 +45,12 @@ type (
func (f *Function) ResolveFileName() string { func (f *Function) ResolveFileName() string {
return f.Name + ".lua" return f.Name + ".lua"
} }
func (f *Function) WriteFile() error { func (f *Function) WriteFile(root string) error {
if f.Name == "" { if f.Name == "" {
return fmt.Errorf("function name is empty of %+v", f) return fmt.Errorf("function name is empty of %+v", f)
} }
file, err := os.Create(f.ResolveFileName()) file, err := os.Create(filepath.Join(root, f.ResolveFileName()))
if err != nil { if err != nil {
return err return err
} }

View File

@@ -1,6 +1,7 @@
package main package main
import ( import (
"context"
"fmt" "fmt"
"io" "io"
"net/http" "net/http"
@@ -8,9 +9,14 @@ import (
"golang.org/x/time/rate" "golang.org/x/time/rate"
) )
var LIMITER = rate.NewLimiter(rate.Limit(1), 2) var LIMITER = rate.NewLimiter(rate.Limit(8), 16)
func Fetch(url string) (string, error) {
err := LIMITER.Wait(context.Background())
if err != nil {
return "", fmt.Errorf("Error waiting for rate limiter: %v", err)
}
func FetchFull(url string) (string, error) {
res, err := http.Get(url) res, err := http.Get(url)
if err != nil { if err != nil {
return "", fmt.Errorf("Error fetching %s: %v", url, err) return "", fmt.Errorf("Error fetching %s: %v", url, err)

51
main.go
View File

@@ -6,9 +6,11 @@ import (
"io" "io"
"log" "log"
"os" "os"
"regexp"
) )
var url = `https://wowprogramming.com/docs/api.html` var rootUrl = `https://wowprogramming.com/`
var apiUrl = fmt.Sprintf("%s/docs/api.html", rootUrl)
var Error *log.Logger var Error *log.Logger
var Warning *log.Logger var Warning *log.Logger
@@ -37,30 +39,45 @@ var html string
//go:embed doc.html //go:embed doc.html
var doc string var doc string
var pageNameExtractor = regexp.MustCompile(`\/([^/]+).html`)
var outDir = "out"
func main() { func main() {
//res, err := FetchFull(url) res, err := Fetch(apiUrl)
//if err != nil { if err != nil {
// Error.Printf("Error fetching %s: %v", url, err) Error.Printf("Error fetching %s: %v", apiUrl, err)
// return return
//} }
//os.WriteFile("test.html", []byte(res), 0644) //os.WriteFile("test.html", []byte(res), 0644)
// foo, err := ParseHTML(html) pages, err := ParseHTML(res)
// if err != nil {
// Error.Printf("Error parsing HTML: %v", err)
// return
// }
// log.Printf("%#v", foo)
foo, err := ParseDoc(doc)
if err != nil { if err != nil {
Error.Printf("Error parsing HTML: %v", err) Error.Printf("Error parsing HTML: %v", err)
return return
} }
foo.Name = "JoinPermanentChannel" for _, page := range pages {
log.Printf("%#v", foo) log.Printf("Processing page %s", page)
err = foo.WriteFile() pname := pageNameExtractor.FindStringSubmatch(page)
if len(pname) != 2 {
Error.Printf("Failed to extract page name from %s", page)
continue
}
res, err := Fetch(rootUrl + page)
if err != nil {
Error.Printf("Error fetching %s: %v", rootUrl+page, err)
}
function, err := ParseDoc(res)
if err != nil {
Error.Printf("Error parsing HTML: %v", err)
continue
}
function.Name = pname[1]
err = function.WriteFile(outDir)
if err != nil { if err != nil {
Error.Printf("Error writing file: %v", err) Error.Printf("Error writing file: %v", err)
return continue
}
} }
} }