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
}
Esempio n. 2
0
func initLoggers(pf *PostFinder, logDir string) (ok bool) {
	os.MkdirAll(logDir, 0644)

	initFunc := func(loggerName string) *logs.Logger {
		logFile, err := os.Create(logDir + loggerName)
		if err != nil {
			logs.Fatal("无法创建log文件.", err)
			return nil
		}
		return logs.NewLogger(logs.DebugLevel, os.Stdout, logFile)
	}

	var loggers [4]*logs.Logger

	for i, loggerName := range []string{
		"post-finder-日志.log",
		"post-finder-延时搜索日志.log",
		"post-finder-解组错误记录.log",
	} {
		loggers[i] = initFunc(loggerName)
		if loggers[i] == nil {
			return false
		}
	}

	logger = loggers[0]
	delayerLogger = loggers[1]
	unmarshallerLogger = loggers[2]

	return true

}
Esempio n. 3
0
func initLogger(pd *PostDeleter, logDir string) error {
	logFile, err := os.Create(logDir + "post-deleter-日志.log")
	if err != nil {
		logs.Fatal("无法创建log文件.", err)
		return err
	}
	pd.Logger = logs.NewLogger(logs.DebugLevel, os.Stdout, logFile)
	logs.DefaultLogger = pd.Logger
	return nil
}
func keepUpdatingSettings() *Settings {
	var settings Settings
	var fileName string
	if len(os.Args) == 1 {
		fileName = "删贴机设置.json"
	} else {
		fileName = os.Args[1]
	}

	var lastModTime time.Time
	ticker := time.NewTicker(time.Second)
	var isFirstTime bool = true
	var firstTimeWaitChan = make(chan bool)
	go func() {
		for {

			info, err := os.Stat(fileName)
			if err != nil {
				if isFirstTime {
					panic(err)
				}
				continue
			}

			if modTime := info.ModTime(); modTime.After(lastModTime) {
				lastModTime = modTime
				_settings, err := LoadSettings(fileName)
				if err != nil {
					logs.Fatal("更新设置文件失败,将继续使用旧设置:", err)
				} else {
					logs.Info("更新设置文件成功(然而这并没有什么用(除了第一次之外)).")
					settings = *_settings
				}
			}

			if isFirstTime {
				firstTimeWaitChan <- true
				isFirstTime = false
			}

			<-ticker.C
		}

	}()

	<-firstTimeWaitChan
	close(firstTimeWaitChan)

	return &settings
}
Esempio n. 5
0
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 NewOperationLogger(logDir string) (*OperationLogger, error) {
	var logger OperationLogger
	logFile, err := os.Create(logDir + "post-deleter-操作记录.csv")
	if err != nil {
		logs.Fatal("无法创建log文件.", err)
		return nil, err
	}

	logFile.Write([]byte{0xEF, 0xBB, 0xBF})

	logger.writer = csv.NewWriter(logFile)

	logger.writer.Write([]string{"操作", "状态", "操作来源", "原因", "本地操作时间", "发贴时间",
		"发贴人", "uid", "主题标题", "tid(主题id)", "pid(楼层id/楼中楼id)", "spid(楼中楼id)", "贴子内容", "其他"})

	return &logger, nil
}
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{})

}