Skip to content

Commit 970e58d

Browse files
authored
Merge pull request #2646 from xushiwei/q
cmd: hdq
2 parents e500c37 + c0085da commit 970e58d

File tree

6 files changed

+275
-0
lines changed

6 files changed

+275
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ xgo_autogen*.go
3939
!dql/fetcher/**/xgo_autogen.go
4040
!cmd/xgo/gop_autogen*.go
4141
!cmd/xgo/xgo_autogen*.go
42+
!cmd/hdq/xgo_autogen*.go
4243
!demo/fullspec/mixgo-complex/xgo_autogen.go
4344
!demo/mixgo/xgo_autogen.go
4445
go.json

cmd/hdq/fetch_cmd.gox

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and limitations under the License.
14+
*/
15+
16+
import (
17+
"encoding/json"
18+
"io"
19+
"log"
20+
"os"
21+
"xgo/dql/fetcher"
22+
)
23+
24+
use "fetch [flags] fetchType [input ... | -]"
25+
26+
short "Fetch objects from the html source with the specified fetchType and inputs"
27+
28+
run args => {
29+
if args.len < 1 {
30+
help
31+
return
32+
}
33+
fetchType := args[0]
34+
inputs := args[1:]
35+
if len(inputs) == 1 && inputs[0] == "-" {
36+
inputs = string(io.readAll(os.Stdin)!).fields
37+
}
38+
docs := make([]any, 0, len(inputs))
39+
for input in inputs {
40+
log.println "==> Fetch", input
41+
doc := fetcher.do(fetchType, input)!
42+
docs <- doc
43+
}
44+
enc := json.newEncoder(os.Stdout)
45+
enc.setIndent "", " "
46+
enc.encode! docs
47+
}

cmd/hdq/list_cmd.gox

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and limitations under the License.
14+
*/
15+
16+
import (
17+
"xgo/dql/fetcher"
18+
)
19+
20+
use "list"
21+
22+
short "List all supported fetchTypes."
23+
24+
run => {
25+
for ft in fetcher.list {
26+
echo ft
27+
}
28+
}

cmd/hdq/main_app.gox

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2026 The XGo Authors (xgo.dev). All rights reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and limitations under the License.
14+
*/
15+
16+
import (
17+
_ "github.com/qiniu/x/stream/http/cached"
18+
19+
_ "xgo/dql/fetcher/github.com/issueTask"
20+
_ "xgo/dql/fetcher/github.com/repoList"
21+
_ "xgo/dql/fetcher/hrefs"
22+
_ "xgo/dql/fetcher/pkg.go.dev/importedBy"
23+
_ "xgo/dql/fetcher/pytorch.org/fndoc"
24+
)
25+
26+
use "hdq"
27+
28+
short "hdq - An HTML DOM Query Tool (powered by XGo DQL)"

cmd/hdq/xgo_autogen.go

Lines changed: 160 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dql/fetcher/fetch.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package fetcher
1919
import (
2020
"errors"
2121
"reflect"
22+
"sort"
2223

2324
"github.com/goplus/xgo/dql/html"
2425
)
@@ -83,4 +84,14 @@ func Register(fetchType string, conv Conv, urlOf func(input any) string) {
8384
convs[fetchType] = fetchInfo{vConv, urlOf}
8485
}
8586

87+
// List returns a list of registered fetch types.
88+
func List() []string {
89+
keys := make([]string, 0, len(convs))
90+
for k := range convs {
91+
keys = append(keys, k)
92+
}
93+
sort.Strings(keys)
94+
return keys
95+
}
96+
8697
// -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)