diff --git a/README.md b/README.md index 8ebaac4..f627f73 100644 --- a/README.md +++ b/README.md @@ -12,10 +12,10 @@ - 运行方法二: 安装go(>=1.8)环境后, clone本repo源码到对应`$GOPATH/src/github.com/sundy-li/`下, 进入源码目录后,执行 `go run cmd/main.go` - 手机wifi设置代理为pc的ip和端口,启动小程序王者头脑 - ## 问题 -- ios端由于goproxy无法代理websocket问题,暂时无法使用,希望大家可以来完善这个问题,见[这个issue](https://github.com/sundy-li/wechat_brain/issues/18) +- 感谢@HsiangHo, @milkmeowo 的贡献,修复了ios代理问题,更新新版本后,最好重新安装证书,重启微信进程 + ~~ios端由于goproxy无法代理websocket问题,暂时无法使用,希望大家可以来完善这个问题,见[这个issue](https://github.com/sundy-li/wechat_brain/issues/18) ~~ ## 合并题库 diff --git a/cmd/question_utils.go b/cmd/question_utils.go index 2be0b88..8858641 100644 --- a/cmd/question_utils.go +++ b/cmd/question_utils.go @@ -1,33 +1,135 @@ package main import ( + "archive/zip" "flag" + "io" "log" + "net/http" + "os" + "path/filepath" "strings" brain "github.com/sundy-li/wechat_brain" + + "github.com/PuerkitoBio/goquery" ) var ( - action string - fs string + source string + fs string + issueUrl = "https://github.com/sundy-li/wechat_brain/issues/17" + tmpDir = "/data/tmp/" ) func init() { - flag.StringVar(&action, "a", "show", "action value -> show | merge") + flag.StringVar(&source, "s", "show", "source value -> show | file | issue") flag.StringVar(&fs, "fs", "", "merge data files") flag.Parse() } func main() { - if action == "merge" { + if source == "file" { files := strings.Split(fs, " ") if len(files) < 1 { log.Println("empty files") return } brain.MergeQuestions(files...) + } else if source == "issue" { + doc, _ := goquery.NewDocument(issueUrl) + doc.Find("div.comment").Each(func(index int, comment *goquery.Selection) { + comment.Find("td.d-block p a").Each(func(i int, s *goquery.Selection) { + if strings.Contains(s.Text(), "questions.zip") { + href, _ := s.Attr("href") + if href != "" { + handleZipUrl(href) + } + } + }) + }) } total := brain.CountQuestions() log.Println("total questions =>", total) } + +func handleZipUrl(url string) error { + resp, err := http.Get(url) + if err != nil { + return err + } + defer resp.Body.Close() + out, err := os.Create(tmpDir + "questions.zip") + if err != nil { + return err + } + defer out.Close() + io.Copy(out, resp.Body) + _, err = Unzip(tmpDir+"questions.zip", tmpDir) + if err != nil { + return err + } + + //merge data + brain.MergeQuestions(tmpDir + "questions.data") + log.Println("merged", url) + return nil +} + +// Unzip will un-compress a zip archive, +// moving all files and folders to an output directory +func Unzip(src, dest string) ([]string, error) { + + var filenames []string + + r, err := zip.OpenReader(src) + if err != nil { + return filenames, err + } + defer r.Close() + + for _, f := range r.File { + + rc, err := f.Open() + if err != nil { + return filenames, err + } + defer rc.Close() + + // Store filename/path for returning and using later on + fpath := filepath.Join(dest, f.Name) + filenames = append(filenames, fpath) + + if f.FileInfo().IsDir() { + // Make Folder + os.MkdirAll(fpath, os.ModePerm) + + } else { + + // Make File + var fdir string + if lastIndex := strings.LastIndex(fpath, string(os.PathSeparator)); lastIndex > -1 { + fdir = fpath[:lastIndex] + } + + err = os.MkdirAll(fdir, os.ModePerm) + if err != nil { + log.Fatal(err) + return filenames, err + } + f, err := os.OpenFile( + fpath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, f.Mode()) + if err != nil { + return filenames, err + } + defer f.Close() + + _, err = io.Copy(f, rc) + if err != nil { + return filenames, err + } + + } + } + return filenames, nil +} diff --git a/questions.data b/questions.data index dc5c2c7..65e5701 100644 Binary files a/questions.data and b/questions.data differ