feat: переход на Go
This commit is contained in:
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@@ -0,0 +1,3 @@
|
||||
.idea/
|
||||
out/
|
||||
!out/.gitkeep
|
||||
7
Makefile
Normal file
7
Makefile
Normal file
@@ -0,0 +1,7 @@
|
||||
#!make
|
||||
|
||||
build-linux:
|
||||
GOOS=linux go build -o out/ff_renamer main.go
|
||||
|
||||
build-windows:
|
||||
GOOS=windows go build -o out/ff_renamer.exe main.go
|
||||
106
main.go
Normal file
106
main.go
Normal file
@@ -0,0 +1,106 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"slices"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Pair struct {
|
||||
originFull string
|
||||
newName string
|
||||
newFull string
|
||||
skip bool
|
||||
}
|
||||
|
||||
var (
|
||||
exts = []string{
|
||||
".jpg", ".jpeg", ".jfif",
|
||||
".png", ".gif", ".webp",
|
||||
".mp4", ".MP4", ".webm",
|
||||
}
|
||||
|
||||
renameSlice []Pair
|
||||
|
||||
pattern, _ = regexp.Compile("^\\d+(-\\d)?$") // 123456789, 123456789-1
|
||||
)
|
||||
|
||||
func generateName(baseName string) string {
|
||||
counter := 1
|
||||
name := baseName
|
||||
|
||||
loop:
|
||||
for {
|
||||
for _, pair := range renameSlice {
|
||||
if pair.newName == name {
|
||||
name = fmt.Sprintf("%s-%d", baseName, counter)
|
||||
counter++
|
||||
continue loop
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
return name
|
||||
}
|
||||
|
||||
func main() {
|
||||
entries, err := os.ReadDir(".")
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
renameSlice = make([]Pair, 0)
|
||||
|
||||
for _, entry := range entries {
|
||||
if entry.IsDir() {
|
||||
continue
|
||||
}
|
||||
|
||||
ext := filepath.Ext(entry.Name())
|
||||
extL := strings.ToLower(ext)
|
||||
if !slices.Contains(exts, extL) {
|
||||
continue
|
||||
}
|
||||
|
||||
if pattern.MatchString(strings.TrimSuffix(entry.Name(), ext)) {
|
||||
renameSlice = append(renameSlice, Pair{
|
||||
originFull: entry.Name(),
|
||||
skip: true})
|
||||
continue
|
||||
}
|
||||
|
||||
info, err := entry.Info()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
newName := generateName(strconv.FormatInt(info.ModTime().Unix(), 10))
|
||||
|
||||
renameSlice = append(renameSlice, Pair{
|
||||
originFull: entry.Name(),
|
||||
newName: newName,
|
||||
newFull: newName + extL})
|
||||
}
|
||||
|
||||
counter := 0
|
||||
for i, pair := range renameSlice {
|
||||
if pair.skip || pair.originFull == pair.newFull {
|
||||
continue
|
||||
}
|
||||
|
||||
fmt.Printf("%d. %s --> %s\n", i+1, pair.originFull, pair.newFull)
|
||||
err := os.Rename(pair.originFull, pair.newFull)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
counter++
|
||||
}
|
||||
|
||||
fmt.Printf("Total: %d\n", len(renameSlice))
|
||||
fmt.Printf("Renamed: %d\n", counter)
|
||||
}
|
||||
34
renamer.py
34
renamer.py
@@ -1,34 +0,0 @@
|
||||
#!/usr/bin/python3
|
||||
# vi: set tabstop=4 shiftwidth=4 expandtab : #
|
||||
# version: 3.20220218
|
||||
|
||||
import re
|
||||
import os
|
||||
|
||||
pattern = re.compile('^\d{10}$')
|
||||
unique_names = set()
|
||||
list_files = sorted(os.listdir(path = '.'))
|
||||
|
||||
for filename in list_files:
|
||||
if not os.path.isfile(filename):
|
||||
continue
|
||||
|
||||
(original_name, ext) = os.path.splitext(filename)
|
||||
if not (ext in ['.jpg', '.jpeg', '.jfif', '.png', '.gif', '.webp', '.mp4', '.MP4', '.webm']):
|
||||
continue
|
||||
|
||||
if pattern.match(original_name) != None:
|
||||
unique_names.add(original_name)
|
||||
continue
|
||||
|
||||
file_mtime = int(os.stat(filename).st_mtime)
|
||||
new_name = file_mtime
|
||||
suffix = 1
|
||||
while new_name in unique_names:
|
||||
new_name = '%s-%d' % (file_mtime, suffix)
|
||||
suffix = suffix + 1
|
||||
unique_names.add(new_name)
|
||||
full_new_name = '%s%s' % (new_name, ext)
|
||||
print('%s -> %s' % (filename, full_new_name))
|
||||
os.rename(filename, full_new_name)
|
||||
|
||||
Reference in New Issue
Block a user