@@ -12,6 +12,9 @@ import (
1212 "encoding/hex"
1313 "encoding/json"
1414 "fmt"
15+ "github.com/go-git/go-git/v5"
16+ "github.com/go-git/go-git/v5/plumbing"
17+ "github.com/go-git/go-git/v5/plumbing/object"
1518 "github.com/otiai10/copy"
1619 "github.com/robfig/cron/v3"
1720 "io"
@@ -729,19 +732,40 @@ func LogAnalysis(fileName string) map[string]int {
729732}
730733
731734func FindLogFiles (dirPath string ) ([]string , error ) {
732-
733- date := time .Now ().Format ("20060102" )
734- prefix := fmt .Sprintf ("better-genshin-impact%s" , date )
735- pattern := dirPath + "\\ " + prefix + "*.log" // logs 为日志目录
735+ pattern := filepath .Join (dirPath , "*.log" )
736736
737737 files , err := filepath .Glob (pattern )
738738 if err != nil {
739739 return nil , err
740740 }
741741
742- var filenames []string
742+ // 保存文件名和时间
743+ type fileInfo struct {
744+ name string
745+ time time.Time
746+ }
747+
748+ var fileInfos []fileInfo
743749 for _ , f := range files {
744- filenames = append (filenames , filepath .Base (f ))
750+ info , err := os .Stat (f )
751+ if err != nil {
752+ continue // 读取失败跳过
753+ }
754+ fileInfos = append (fileInfos , fileInfo {
755+ name : filepath .Base (f ),
756+ time : info .ModTime (),
757+ })
758+ }
759+
760+ // 按时间倒序排序
761+ sort .Slice (fileInfos , func (i , j int ) bool {
762+ return fileInfos [i ].time .After (fileInfos [j ].time )
763+ })
764+
765+ // 只返回文件名
766+ var filenames []string
767+ for _ , fi := range fileInfos {
768+ filenames = append (filenames , fi .name )
745769 }
746770
747771 return filenames , nil
@@ -1010,19 +1034,27 @@ type GroupDetail struct {
10101034 EndTime string
10111035 // 执行时间
10121036 ExecuteTime string
1013- //摩拉
1014- MoLa int
10151037}
10161038
1017- func GroupTime () ([]GroupMap , error ) {
1018- nowTime := time .Now ()
1019- today := nowTime .Format ("2006-01-02" )
1039+ func GroupTime (fileName string ) ([]GroupMap , error ) {
10201040 layoutFull := "2006-01-02 15:04:05"
10211041
1022- filePath := filepath .Clean (fmt .Sprintf ("%s\\ log" , Config .BetterGIAddress )) // 本地日志路径
1023- files , err := FindLogFiles (filePath )
1024- //获取最后一个文件
1025- filename := filepath .Clean (fmt .Sprintf ("%s\\ log\\ %s" , Config .BetterGIAddress , files [len (files )- 1 ]))
1042+ today := time .Now ().Format ("2006-01-02" )
1043+
1044+ //提取文件名字的数字
1045+ // 正则表达式匹配数字
1046+ re := regexp .MustCompile (`\d+` )
1047+ // 查找所有匹配项
1048+ matches := re .FindAllString (fileName , - 1 )
1049+ // 检查是否找到匹配项
1050+ if len (matches ) > 0 {
1051+ //格式化转换
1052+ formatted := matches [0 ][:4 ] + "-" + matches [0 ][4 :6 ] + "-" + matches [0 ][6 :]
1053+
1054+ today = formatted
1055+ }
1056+
1057+ filename := filepath .Clean (fmt .Sprintf ("%s\\ log\\ %s" , Config .BetterGIAddress , fileName ))
10261058
10271059 file , err := os .Open (filename )
10281060 if err != nil {
@@ -1045,25 +1077,7 @@ func GroupTime() ([]GroupMap, error) {
10451077 scanner := bufio .NewScanner (file )
10461078 var prevLine string
10471079
1048- var asyncList []config.TravelsDiaryDetailList
1049-
1050- fmt .Println (Config .IsMoLaSum )
1051-
1052- if Config .IsMoLaSum {
1053- async , err := config .GetTravelsDiaryDetailAsync (int (nowTime .Month ()), 2 , 1 )
1054- if err == nil {
1055- time .Sleep (3 * time .Second )
1056- async1 , _ := config .GetTravelsDiaryDetailAsync (int (nowTime .Month ()), 2 , 2 )
1057- time .Sleep (3 * time .Second )
1058- async2 , _ := config .GetTravelsDiaryDetailAsync (int (nowTime .Month ()), 2 , 3 )
1059-
1060- asyncList = append (async .List , async1 .List ... )
1061- asyncList = append (async .List , async2 .List ... )
1062- }
1063- }
1064-
10651080 var sunTime time.Duration
1066- var sumMoLa int
10671081
10681082 for scanner .Scan () {
10691083 line := scanner .Text ()
@@ -1092,12 +1106,6 @@ func GroupTime() ([]GroupMap, error) {
10921106 // 过滤收益
10931107 startStr := temp .StartTime .Format ("2006-01-02 15:04:05" )
10941108 endStr := endTime .Format ("2006-01-02 15:04:05" )
1095- filtered := config .FilterByTime (asyncList , startStr , endStr )
1096- var totalMoLa int
1097- for _ , item := range filtered {
1098- totalMoLa += item .Num
1099- }
1100- sumMoLa += totalMoLa
11011109
11021110 // 组装
11031111 results = append (results , GroupMap {
@@ -1106,7 +1114,6 @@ func GroupTime() ([]GroupMap, error) {
11061114 StartTime : startStr ,
11071115 EndTime : endStr ,
11081116 ExecuteTime : duration .String (),
1109- MoLa : totalMoLa ,
11101117 },
11111118 })
11121119
@@ -1129,7 +1136,6 @@ func GroupTime() ([]GroupMap, error) {
11291136 StartTime : "00:00:00" ,
11301137 EndTime : "00:00:00" ,
11311138 ExecuteTime : sunTime .String (),
1132- MoLa : sumMoLa ,
11331139 },
11341140 })
11351141
@@ -1143,7 +1149,7 @@ func CheckConfig() (bool, error) {
11431149 fmt .Println ("Bgi安装目录设置正确" )
11441150 }
11451151 if os .IsNotExist (err ) {
1146- return false , fmt .Errorf ("Bgi安装目录设置错误目录设置错误,请检查配置文件BetterGIAddress:例子:D: \\ subject \\ BetterGI " )
1152+ return false , fmt .Errorf ("Bgi安装目录设置错误目录设置错误,请检查配置文件BetterGIAddress:你有没有加双斜杠呀,没有看网站说明 " )
11471153 }
11481154 names := Config .ConfigNames
11491155 if len (names ) == 7 {
@@ -1180,41 +1186,163 @@ func GetGroupPInfo() string {
11801186 return string (s1 )
11811187}
11821188
1183- func AutoJs () string {
1189+ type GitLogStruct struct {
1190+ //提交时间
1191+ CommitTime string
1192+ //作者
1193+ Author string
1194+ //更新内容
1195+ Message string
1196+ }
11841197
1185- url := "https://github.com/babalae/bettergi-scripts-list/archive/refs/heads/main.zip"
1186- zipFile := "main.zip"
1187- err := downloadFile (zipFile , url )
1198+ // 查询git日志
1199+ func GitLog () []GitLogStruct {
1200+ localPath := "./bettergi-scripts-list"
1201+
1202+ // 打开仓库
1203+ repo , err := git .PlainOpen (localPath )
11881204 if err != nil {
1189- return "下载失败"
1205+ log . Fatal ( "打开仓库失败:" , err )
11901206 }
1191- err2 := unzipRepo (zipFile , "repo" , "repo/" )
1192- if err2 != nil {
1193- return "解压失败"
1207+
1208+ // 获取 HEAD 引用
1209+ ref , err := repo .Head ()
1210+ if err != nil {
1211+ log .Fatal ("获取 HEAD 失败:" , err )
11941212 }
11951213
1196- jsNames := Config .JsName
1197- repoDir := "repo/js"
1214+ // 获取日志迭代器
1215+ commitIter , err := repo .Log (& git.LogOptions {From : ref .Hash ()})
1216+ if err != nil {
1217+ log .Fatal ("获取日志失败:" , err )
1218+ }
1219+
1220+ var logs []GitLogStruct
1221+ // 迭代前 10 条
1222+ count := 0
1223+ err = commitIter .ForEach (func (c * object.Commit ) error {
1224+
1225+ var gitLogStruct GitLogStruct
1226+ gitLogStruct .CommitTime = c .Author .When .Format ("2006-01-02 15:04:05" )
1227+ gitLogStruct .Author = c .Author .Name
1228+ gitLogStruct .Message = c .Message
1229+ logs = append (logs , gitLogStruct )
1230+ count ++
1231+ if count >= 10 {
1232+ return fmt .Errorf ("done" ) // 手动中断
1233+ }
1234+ return nil
1235+ })
1236+
1237+ if err != nil && err .Error () != "done" {
1238+ log .Fatal ("遍历日志失败:" , err )
1239+ }
1240+
1241+ // 按时间倒序排序
1242+ sort .Slice (logs , func (i , j int ) bool {
1243+ ti , _ := time .Parse ("2006-01-02 15:04:05" , logs [i ].CommitTime )
1244+ tj , _ := time .Parse ("2006-01-02 15:04:05" , logs [j ].CommitTime )
1245+ return ti .After (tj )
1246+ })
1247+
1248+ return logs
1249+ }
11981250
1251+ // git拉取代码
1252+ func GitPull () error {
1253+ localPath := "./bettergi-scripts-list"
1254+ repoURL := "https://gitcode.com/huiyadanli/bettergi-scripts-list.git"
1255+
1256+ // 尝试打开本地仓库
1257+ repo , err := git .PlainOpen (localPath )
1258+ if err == git .ErrRepositoryNotExists {
1259+ // 本地不存在,克隆
1260+ autoLog .Sugar .Info ("本地不存在,开始克隆..." )
1261+ repo , err = git .PlainClone (localPath , false , & git.CloneOptions {
1262+ URL : repoURL ,
1263+ ReferenceName : plumbing .NewBranchReferenceName ("main" ),
1264+ SingleBranch : true ,
1265+ Progress : nil ,
1266+ })
1267+ if err != nil {
1268+ autoLog .Sugar .Errorf ("克隆失败: %v" , err )
1269+ return fmt .Errorf ("克隆失败: %v" , err )
1270+ }
1271+ autoLog .Sugar .Info ("克隆完成" )
1272+ } else if err == nil {
1273+ // 已存在,拉取最新
1274+ autoLog .Sugar .Info ("仓库存在,拉取最新代码..." )
1275+ w , err := repo .Worktree ()
1276+ if err != nil {
1277+ return fmt .Errorf ("获取工作区失败: %v" , err )
1278+ }
1279+ err = w .Pull (& git.PullOptions {
1280+ RemoteName : "origin" ,
1281+ ReferenceName : plumbing .NewBranchReferenceName ("main" ),
1282+ Force : false ,
1283+ })
1284+ if err != nil && err != git .NoErrAlreadyUpToDate {
1285+ autoLog .Sugar .Errorf ("拉取失败: %v" , err )
1286+ return fmt .Errorf ("拉取失败: %v" , err )
1287+ }
1288+ autoLog .Sugar .Info ("拉取完成或已是最新" )
1289+ } else {
1290+ return fmt .Errorf ("打开仓库失败: %v" , err )
1291+ }
1292+ return nil
1293+ }
1294+
1295+ func AutoJs () (string , error ) {
1296+
1297+ err := GitPull ()
1298+ if err != nil {
1299+ return err .Error (), err
1300+ }
1301+
1302+ jsNames := Config .JsName
1303+ repoDir := "bettergi-scripts-list/repo/js"
1304+ //
11991305 for _ , jsName := range jsNames {
12001306 subFolderPath , err := findSubFolder (repoDir , jsName )
12011307 if err != nil {
12021308 autoLog .Sugar .Errorf ("查找子文件夹失败: %v" , err )
1203- return fmt .Sprintf ("未找到子文件夹: %s" , jsName )
1309+ return fmt .Sprintf ("未找到子文件夹: %s" , jsName ), err
12041310 }
12051311
12061312 // 找到子文件夹后,执行复制操作
12071313 targetPath := filepath .Join (Config .BetterGIAddress , "User" , "JsScript" , jsName )
1208- err2 := copy .Copy (subFolderPath , targetPath )
1209- if err2 != nil {
1210- fmt .Println (err2 )
1314+
1315+ //狗粮特殊处理
1316+ if jsName == "AutoArtifactsPro" {
1317+ autoLog .Sugar .Infof ("狗粮pro脚本特殊处理" )
1318+ autoLog .Sugar .Infof ("开始备份日志文件" )
1319+ copy .Copy (filepath .Join (targetPath , "records" ), "./backups/AutoArtifactsPro/" )
1320+ //清理原文件
1321+ os .RemoveAll (targetPath )
1322+ autoLog .Sugar .Infof ("更新脚本" )
1323+ err2 := copy .Copy (subFolderPath , targetPath )
1324+ if err2 != nil {
1325+ autoLog .Sugar .Errorf ("更新脚本失败: %v" , err2 )
1326+ }
1327+ autoLog .Sugar .Infof ("恢复日志文件" )
1328+ err := copy .Copy ("./backups/AutoArtifactsPro/" , filepath .Join (targetPath , "records" ))
1329+ if err != nil {
1330+ autoLog .Sugar .Errorf ("恢复日志文件失败: %v" , err )
1331+ }
1332+
1333+ } else {
1334+ //清理原文件
1335+ os .RemoveAll (targetPath )
1336+ err2 := copy .Copy (subFolderPath , targetPath )
1337+ if err2 != nil {
1338+ return err2 .Error (), err2
1339+ }
12111340 }
1341+
12121342 autoLog .Sugar .Infof ("Js脚本: %s 已更新" , subFolderPath )
12131343 }
1214- os .RemoveAll ("repo" )
1215- os .RemoveAll ("main.zip" )
12161344
1217- return "备份成功"
1345+ return "备份成功" , nil
12181346}
12191347
12201348// 查找 repo 目录下是否存在名为 targetFolder 的子文件夹
0 commit comments