Created
March 27, 2020 09:25
-
-
Save runo280/41c41343e164bf91894b959f209ac320 to your computer and use it in GitHub Desktop.
photo rename
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"path/filepath" | |
"strings" | |
"time" | |
"github.com/rwcarlsen/goexif/exif" | |
ptime "github.com/yaa110/go-persian-calendar" | |
) | |
type Photo struct { | |
fileName string | |
filePath string | |
ext string | |
date time.Time | |
persianDate ptime.Time | |
newFileName string | |
persianYear string | |
persianMonth string | |
} | |
var dayOfWeek = [7]string{ | |
"0Shan", | |
"1Shan", | |
"2Shan", | |
"3Shan", | |
"4Shan", | |
"5Shan", | |
"6Jome", | |
} | |
func getPhotoDate(file string) time.Time { | |
f, err := os.Open(file) | |
if err != nil { | |
log.Fatal(err) | |
} | |
pic, err := exif.Decode(f) | |
if err != nil { | |
fmt.Println(err) | |
os.Exit(1) | |
} | |
time, _ := pic.DateTime() | |
return time | |
} | |
func process(photo Photo) Photo { | |
photo.persianDate = dateToPersianDate(photo.date) | |
photo.newFileName = persianDateToString(photo.persianDate) | |
photo.persianYear = photo.persianDate.Year() | |
photo.persianMonth = photo.persianDate.Month() | |
return strings.Replace(date, "*", dayOfWeek[int(pt.Weekday())], -1) | |
} | |
func dateToPersianDate(date time.Time) ptime.Time { | |
d := time.Date(date.Year(), date.Month(), date.Day(), date.Hour(), date.Minute(), date.Second(), 0, ptime.Iran()) | |
return ptime.New(d) | |
} | |
func persianDateToString(date ptime.Time) string { | |
date := date.Format("yyyy-MM-dd_*_HH-mm-ss") | |
return strings.Replace(date, "*", dayOfWeek[int(pt.Weekday())], -1) | |
} | |
func main() { | |
var files []string | |
root := "." | |
err := filepath.Walk(root, visit(&files)) | |
if err != nil { | |
panic(err) | |
} | |
for _, file := range files { | |
fmt.Println(dateToPersianDate(getPhotoDate(file))) | |
} | |
} | |
func visit(files *[]string) filepath.WalkFunc { | |
return func(path string, info os.FileInfo, err error) error { | |
if err != nil { | |
log.Fatal(err) | |
} | |
if info.Mode().IsRegular() { | |
if strings.HasSuffix(strings.ToLower(info.Name()), ".jpg") || strings.HasSuffix(strings.ToLower(info.Name()), ".jpeg") { | |
*files = append(*files, path) | |
} | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment