Created
November 21, 2021 11:29
-
-
Save pallove/b37161eb545b07e708557c0c099ae430 to your computer and use it in GitHub Desktop.
an alfredworkflow suggestion about gushiwen.cn
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
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"html" | |
"log" | |
"net/http" | |
"os" | |
"strings" | |
"github.com/PuerkitoBio/goquery" | |
) | |
type AlfredItem struct { | |
Type string `json:"type"` | |
Title string `json:"title"` | |
Subtitle string `json:"subtitle"` | |
Arg string `json:"arg"` | |
Icon struct { | |
Path string `json:"path"` | |
} `json:"icon"` | |
} | |
func getItems(query string) { | |
res, err := http.Get(fmt.Sprintf("https://www.gushiwen.cn/shiwen2017/ajaxSearchSo.aspx?valuekey=%s", html.EscapeString(query))) | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer res.Body.Close() | |
doc, err := goquery.NewDocumentFromReader(res.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
r := make([]AlfredItem, 0) | |
doc.Find(".main").Each(func(i int, cs *goquery.Selection) { | |
category := cs.Find(".mleft > span").Text() | |
cs.Find(".mright > div > a").Each(func(i int, s *goquery.Selection) { | |
r = append(r, AlfredItem{ | |
Type: "file", | |
Title: strings.ReplaceAll(s.Text(), "\n", ""), | |
Subtitle: category, | |
Arg: s.AttrOr("href", ""), | |
Icon: struct { | |
Path string `json:"path"` | |
}{ | |
Path: "icon.png", | |
}, | |
}) | |
}) | |
}) | |
output, _ := json.Marshal(struct { | |
Items []AlfredItem `json:"items"` | |
}{ | |
Items: r, | |
}) | |
fmt.Println(string(output)) | |
} | |
func main() { | |
getItems(os.Args[1]) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment