Skip to content

Commit

Permalink
chore: go layout
Browse files Browse the repository at this point in the history
  • Loading branch information
yzqzss committed Dec 12, 2024
1 parent 3e0da5b commit 4da2a0e
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 14 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
test/outcome/*
!test/outcome/.gitkeep
testoutcome_*
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

func main() {

}
6 changes: 4 additions & 2 deletions api.go → pkg/api/api.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package aixifan
package api

import (
"errors"
"io"
"net/http"
"strings"

"github.com/saveweb/aixifan/pkg/extractor"
)

// dougaId: int-string
Expand Down Expand Up @@ -34,5 +36,5 @@ func GetDouga(client *http.Client, dougaId string) (string, error) {
if err != nil {
return "", err
}
return html2json(body)
return extractor.Html2json(body)
}
9 changes: 5 additions & 4 deletions api_test.go → pkg/api/api_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package aixifan
package api

import (
"net/http"
Expand All @@ -7,6 +7,7 @@ import (
"testing"
"time"

"github.com/saveweb/aixifan/pkg/extractor"
"github.com/tidwall/gjson"
)

Expand All @@ -17,7 +18,7 @@ func Test_requestDouga(t *testing.T) {
t.Fatal(err)
return
}
json, err := html2json(body)
json, err := extractor.Html2json(body)
if err != nil {
t.Fatal(err)
return
Expand All @@ -37,7 +38,7 @@ func Test_getDougaMultiP(t *testing.T) {
t.Fatal(err)
return
}
os.WriteFile("test/outcome/DougaMultiP_1.json", []byte(json), 0644)
os.WriteFile("testoutcome_DougaMultiP_1.json", []byte(json), 0644)

if !strings.Contains(gjson.Get(json, "title").String(), "东方伪装天") {
t.Fatal("title not match")
Expand All @@ -51,7 +52,7 @@ func Test_getDougaMultiP(t *testing.T) {
t.Fatal(err)
return
}
os.WriteFile("test/outcome/DougaMultiP_2.json", []byte(json), 0644)
os.WriteFile("testoutcome_DougaMultiP_2.json", []byte(json), 0644)
if gjson.Get(json, "currentVideoId").Int() != 659502 {
t.Fatal("currentVideoId p2 not match")
return
Expand Down
4 changes: 2 additions & 2 deletions extractor.go → pkg/extractor/extractor.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package aixifan
package extractor

import (
"bytes"
Expand Down Expand Up @@ -41,7 +41,7 @@ func extractJsonFromHtml(_html string) (string, error) {
return "", errors.New("json not found")
}

func html2json(body []byte) (string, error) {
func Html2json(body []byte) (string, error) {
// trim the json body
// {...}/*<!-- fetch-stream -->*/
var body_trimed = body
Expand Down
10 changes: 6 additions & 4 deletions extractor_test.go → pkg/extractor/extractor_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package aixifan
package extractor

import (
"os"
Expand All @@ -8,19 +8,21 @@ import (
)

func Test_html2json(t *testing.T) {
raw, err := os.ReadFile("test/html2json.html")
raw, err := os.ReadFile("test_html2json.html")
if err != nil {
// print pwd
t.Fatal(os.Getwd())
t.Fatal(err)
return
}
_json, err := html2json(raw)
_json, err := Html2json(raw)
if err != nil {
t.Fatal(err)
return
}
t.Log(_json)

err = os.WriteFile("test/html2json.json", []byte(_json), 0644)
err = os.WriteFile("test_html2json.json", []byte(_json), 0644)
if err != nil {
t.Fatal(err)
return
Expand Down
File renamed without changes.
File renamed without changes.
Empty file removed test/outcome/.gitkeep
Empty file.

0 comments on commit 4da2a0e

Please sign in to comment.