114 lines
3.0 KiB
Go
114 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"path"
|
|
"regexp"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/yinhui87/go-component/language"
|
|
)
|
|
|
|
func main() {
|
|
dataSource := new(BilibiliSource)
|
|
episodeProvider := NewEpisodeDataProvider(dataSource)
|
|
params := os.Args[1:]
|
|
var epdatas []episode
|
|
if len(params) > 0 {
|
|
var skipEps []int64
|
|
var err error
|
|
if len(params) > 2 {
|
|
// 处理如果有跳过的情况
|
|
for _, param := range params[2:] {
|
|
if sped := strings.Split(param, "-"); len(sped) == 2 {
|
|
// 兼容1-10这样的批量写法
|
|
skepStart, err := strconv.ParseInt(sped[0], 10, 64)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
skepEnd, err := strconv.ParseInt(sped[1], 10, 64)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
for i := skepStart; i <= skepEnd; i++ {
|
|
skipEps = append(skipEps, i)
|
|
}
|
|
} else {
|
|
skipEp, err := strconv.ParseInt(param, 10, 64)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
skipEps = append(skipEps, skipEp)
|
|
}
|
|
}
|
|
}
|
|
epdatas, err = episodeProvider.GetEpisodeData(params[0], skipEps...)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
// epdatas, err := episodeProvider.GetEpisodeData("av1742161", 143, 147)
|
|
// epdatas, err := episodeProvider.GetEpisodeData("ep173262")
|
|
for i, epdata := range epdatas {
|
|
fmt.Printf("%dep %d Cid: %v, Title: %s\n", i, epdata.Index, epdata.Cid, epdata.Title)
|
|
}
|
|
// 获取到所有集信息,开始准备文件
|
|
filenames, err := getFileList()
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
length := len(filenames)
|
|
if len(epdatas) < length {
|
|
length = len(epdatas)
|
|
}
|
|
for i := 0; i < length; i++ {
|
|
epdata := epdatas[i]
|
|
fmt.Printf("file %s cid[%d] title is %s\n", filenames[i], epdata.Cid, epdata.Title)
|
|
newName := fmt.Sprintf("%s%s", epdata.Title, path.Ext(filenames[i]))
|
|
if len(params) > 1 {
|
|
if match, _ := regexp.MatchString(".*%\\d*d.*", params[1]); match {
|
|
newName = fmt.Sprintf(params[1], i+1, newName)
|
|
} else {
|
|
newName = fmt.Sprintf(params[1], newName)
|
|
}
|
|
}
|
|
fmt.Println("rename ", filenames[i], " to ", newName)
|
|
os.Rename(filenames[i], newName)
|
|
}
|
|
}
|
|
|
|
func getFileList() (filenames []string, err error) {
|
|
pwd, err := os.Open(".")
|
|
if err != nil {
|
|
return
|
|
}
|
|
defer pwd.Close()
|
|
files, err := pwd.Readdir(0)
|
|
if err != nil {
|
|
return
|
|
}
|
|
fileinfoTimeMap := make(map[int]os.FileInfo)
|
|
var fileTimes []int
|
|
for _, fileInfo := range files {
|
|
if !fileInfo.IsDir() {
|
|
fileinfoTimeMap[int(fileInfo.ModTime().Unix())] = fileInfo
|
|
fileTimes = append(fileTimes, int(fileInfo.ModTime().Unix()))
|
|
}
|
|
}
|
|
fileTimes = language.ArraySort(fileTimes).([]int)
|
|
for i, t := range fileTimes {
|
|
fileinfo := fileinfoTimeMap[t]
|
|
filenames = append(filenames, fileinfo.Name())
|
|
if i > 0 {
|
|
// 检测是否有相同大小的相邻文件,极有可能为下载错误下了一样的
|
|
oldinfo := fileinfoTimeMap[fileTimes[i-1]]
|
|
if fileinfo.Size() == oldinfo.Size() {
|
|
fmt.Printf("warning: %s(%s) size is same with %s(%s)\n", oldinfo.Name(), oldinfo.ModTime().Format("2006-01-02 15:04:05"), fileinfo.Name(), fileinfo.ModTime().Format("2006-01-02 15:04:05"))
|
|
}
|
|
}
|
|
}
|
|
return
|
|
}
|