func GetAdv(url string, parameters Parameters, cookies Cookies, f func(*http.Request)) ([]byte, error) { Debug(url) var httpReq *http.Request if len(parameters) == 0 { httpReq, _ = http.NewRequest("GET", url, nil) } else { httpReq, _ = http.NewRequest("GET", url+"?"+parameters.Encode(), nil) } if len(cookies) != 0 { httpReq.Header.Add("Cookie", cookies.Encode()) } f(httpReq) for i := 0; ; { httpResp, err := client.Do(httpReq) //fmt.Println(err) //fmt.Println(httpResp, err) defer func() { if err == nil && httpResp != nil && httpResp.Body != nil { httpResp.Body.Close() } }() if err == nil { bs, _ := ioutil.ReadAll(httpResp.Body) return bs, nil } else if RetryTimes < 0 { if !ShutUp { logs.Warn("第", i+1, "次获取响应失败,无重试次数上限.", err.Error()) } i++ } else if i == RetryTimes { if !ShutUp { logs.Warn("第", i+1, "次获取响应失败,到达重试次数上限.", err.Error()) } return []byte(""), err } else { if !ShutUp { logs.Warn("第", i+1, "次获取响应失败,最多重试", RetryTimes, "次.ERROR:", err.Error()) } i++ } } }
func PostAdv(url string, parameters Parameters, cookies Cookies, f func(*http.Request)) ([]byte, error) { Debug(url) var httpReq *http.Request if len(parameters) == 0 { httpReq, _ = http.NewRequest("POST", url, nil) } else { httpReq, _ = http.NewRequest("POST", url, bytes.NewReader([]byte(parameters.Encode()))) } if len(cookies) != 0 { httpReq.Header.Add("Cookie", cookies.Encode()) } httpReq.Header.Add("Content-Type", "application/x-www-form-urlencoded") f(httpReq) for i := 0; ; { httpResp, err := client.Do(httpReq) defer func() { if err == nil && httpResp != nil && httpResp.Body != nil { httpResp.Body.Close() } }() if err == nil { bs, _ := ioutil.ReadAll(httpResp.Body) return bs, nil } else if RetryTimes < 0 { if !ShutUp { logs.Warn("第", i+1, "次获取响应失败,无重试次数上限.", err.Error()) } i++ } else if i == RetryTimes { if !ShutUp { logs.Warn("第", i+1, "次获取响应失败,到达重试次数上限.", err.Error()) } return []byte(""), err } else { if !ShutUp { logs.Warn("第", i+1, "次获取响应失败,最多重试", RetryTimes, "次.ERROR:", err.Error()) } i++ } } }
func getFid(forumName string) uint64 { var fid uint64 for i := 0; ; { _fid, err, pberr := apis.GetFid(forumName) if err != nil { continue } else if pberr != nil && pberr.ErrorCode != 0 { if i < 10 { i++ continue } logs.Fatal("方案A未能获得到fid,进行下一步尝试.", pberr) break } else if _fid == 0 { logs.Warn("方案A未能获得到fid,进行下一步尝试.") break } else { fid = _fid break } } if fid == 0 { for i := 0; ; { results, err, pberr := apis.SearchForum(forumName) if err != nil { continue } else if pberr != nil && pberr.ErrorCode != 0 { if i < 10 { i++ continue } logs.Fatal("方案B未能获得到fid,放弃.", pberr) return 0 } else if len(results) == 0 { logs.Fatal("未找到该贴吧,放弃.", pberr) return 0 } else { for _, result := range results { if result.ForumName == forumName { return result.ForumID } } logs.Fatal("未找到该贴吧,放弃.", pberr) return 0 } } } return fid }
func TryCreateFile(name string) *os.File { var err1, err2 error var f *os.File f, err1 = os.Create(name + ".") if err1 != nil { logs.Warn(name+".", "创建失败:\n", err1.Error(), "\n尝试创建为", name+strconv.FormatInt(time.Now().Unix(), 10)+".") f, err2 = os.Create(name + strconv.FormatInt(time.Now().Unix(), 10) + ".") if err2 != nil { logs.Fatal(name+".", "创建失败:\n", err1.Error(), "\n跳过写入此文件") } } return f }
func main() { var logDir = time.Now().Format("log/20060102-150405-post-deleter/") os.MkdirAll(logDir, 0644) logs.Info("删贴机启动", time.Now()) var settings = keepUpdatingSettings() { var BDUSS = settings.BDUSS if BDUSS != "" { settings.BDUSS = "***" } logs.Info(settings) settings.BDUSS = BDUSS } if settings == nil { os.Exit(1) } if settings.BDUSS == "" { logs.Warn("未设置BDUSS.") } if settings.DebugPort != 0 { go func() { http.ListenAndServe(fmt.Sprintf("localhost:%d", settings.DebugPort), nil) }() } var accAndr = postbar.NewDefaultAndroidAccount("") var accWin8 = postbar.NewDefaultWindows8Account("") accWin8.BDUSS = settings.BDUSS accAndr.BDUSS = settings.BDUSS if settings.ForumID == 0 { logs.Info("设置中未提供ForumID,自动获取.") settings.ForumID = getFid(settings.ForumName) if settings.ForumID == 0 { logs.Fatal("未能获得到fid,退出.") os.Exit(1) } } //mrb := initMRuby(logs.DefaultLogger) if d, err := postdeleter.NewPostDeleter( postdeleter.PostDeleterBuildingParameters{ AccWin8: accWin8, AccAndr: accAndr, ForumName: settings.ForumName, ForumID: settings.ForumID, ConfgiFileName: postdeleter.ConfgiFileName{ ContentRegexp: settings.ContentRegexpFilePath, UserNameRegexp: settings.UserNameRegexpFilePath, TidWhiteList: settings.TidWhiteListFilePath, UserNameWhiteList: settings.UserNameWhiteListFilePath, BawuList: settings.BawuListFilePath, }, Debugging: settings.DebugPort != 0, LogDir: logDir, }); err != nil { logs.Fatal("无法启动删贴机,退出.", err) os.Exit(1) } else { d.Run(time.Second) } <-make(chan struct{}) }