-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
232 additions
and
89 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/* | ||
Copyright 2024 The GoPlus Authors (goplus.org) | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/goplus/hdq/fetcher" | ||
_ "github.com/goplus/hdq/fetcher/torch" | ||
) | ||
|
||
type module struct { | ||
Name string `json:"name"` | ||
Items []any `json:"items"` | ||
} | ||
|
||
// Usage: pysigfetch pageType [name ...] | ||
func main() { | ||
if len(os.Args) < 3 { | ||
fmt.Fprintln(os.Stderr, "Usage: pysigfetch pageType [name ...]") | ||
os.Exit(1) | ||
} | ||
pageType := os.Args[1] | ||
names := os.Args[2:] | ||
docs := make([]any, len(names)) | ||
for i, name := range names { | ||
docs[i] = fetcher.FromInput(pageType, name) | ||
} | ||
json.NewEncoder(os.Stdout).Encode(module{pageType, docs}) | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/* | ||
Copyright 2024 The GoPlus Authors (goplus.org) | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package fetcher | ||
|
||
import ( | ||
"reflect" | ||
|
||
"github.com/goplus/hdq" | ||
) | ||
|
||
// func(doc hdq.NodeSet) <any-object> | ||
type Conv = any | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
// Convert converts a html source to an object. | ||
func Convert(conv reflect.Value, in any) any { | ||
doc := reflect.ValueOf(hdq.Source(in)) | ||
out := conv.Call([]reflect.Value{doc}) | ||
return out[0].Interface() | ||
} | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
// New creates a new object from a html source by a registered converter. | ||
func New(pageType string, in any) any { | ||
page, ok := convs[pageType] | ||
if !ok { | ||
panic("fetcher: unknown pageType - " + pageType) | ||
} | ||
return Convert(page.Conv, in) | ||
} | ||
|
||
// FromInput creates a new object from the html source with the specified input name. | ||
func FromInput(pageType string, input string) any { | ||
page, ok := convs[pageType] | ||
if !ok { | ||
panic("fetcher: unknown pageType - " + pageType) | ||
} | ||
in := page.Input(input) | ||
return Convert(page.Conv, in) | ||
} | ||
|
||
// sitePageType represents a site page type. | ||
type sitePageType struct { | ||
Conv reflect.Value | ||
Input func(string) any | ||
} | ||
|
||
var ( | ||
convs = map[string]sitePageType{} | ||
) | ||
|
||
// Register registers a convType with a convert function. | ||
func Register(pageType string, conv Conv, input func(string) any) { | ||
vConv := reflect.ValueOf(conv) | ||
convs[pageType] = sitePageType{vConv, input} | ||
} | ||
|
||
// ----------------------------------------------------------------------------- |
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.