-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenamer.go
114 lines (96 loc) · 3.07 KB
/
renamer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"strings"
"time"
MessagesModel "github.com/mrnegativetw/FacebookArchiveRenamer/models/messages"
Utils "github.com/mrnegativetw/FacebookArchiveRenamer/utils"
)
func getOriginalPhotoName(uri string) string {
fileName := strings.Split(uri, "/")
return fileName[4]
}
func getFileExtensionFromFileName(fileName string) string {
return strings.Split(fileName, ".")[1]
}
func convertUnixTimestampToIMGDateTime(photoCreationTimestamp int) string {
parsedTime := time.Unix(int64(photoCreationTimestamp), 0)
return fmt.Sprintf("IMG_%d%02d%02d_%02d%02d%02d",
parsedTime.Year(), parsedTime.Month(), parsedTime.Day(),
parsedTime.Hour(), parsedTime.Minute(), parsedTime.Second())
}
// [OK, but duplicated] File not foun.
func renamePhotos(originalPhotoName string, creationTimestamp int) {
// fmt.Printf("originalPhotoName: %s\n", originalPhotoName)
// fmt.Printf("with extension: %s\n", getFileExtensionFromFileName(originalPhotoName))
originalPath := fmt.Sprintf("%s%s%s",
baseFolderPath,
photosFolderPath,
originalPhotoName)
newPhotoName := convertUnixTimestampToIMGDateTime(creationTimestamp)
newPath := fmt.Sprintf("%s%s%s.%s",
baseFolderPath,
photosFolderPath,
newPhotoName,
getFileExtensionFromFileName(originalPhotoName))
// Check is file name duplicated, if so add timestamp by 1 sec.
for Utils.IsFileExist(newPath) {
creationTimestamp += 1
newPhotoName = convertUnixTimestampToIMGDateTime(creationTimestamp)
newPath = fmt.Sprintf("%s%s%s.%s",
baseFolderPath,
photosFolderPath,
newPhotoName,
getFileExtensionFromFileName(originalPhotoName))
}
// Rename photo.
if Utils.IsFileExist(originalPath) {
e := os.Rename(originalPath, newPath)
fmt.Printf("[OK] %s\n", newPhotoName)
if e != nil {
log.Fatal(e)
}
} else {
fmt.Printf("[Not Found] %s\n", originalPath)
}
}
func renamePhotosFromSingleJsonFile(messages MessagesModel.Messages) {
// Loop through all messages.
for i := 0; i < len(messages.Messages); i++ {
// Check message type is photo
if len(messages.Messages[i].Photos) != 0 {
// Loop through photos, sometimes a message has more than one photo.
for j := 0; j < len(messages.Messages[i].Photos); j++ {
// Passing original photo name and creation timestamp to rename
// photos.
renamePhotos(
getOriginalPhotoName(messages.Messages[i].Photos[j].Uri),
messages.Messages[i].Photos[j].CreationTimestamp)
}
}
}
}
func renamePhotosFromAllJsonFile() {
jsonFileCount := 1
filePath := fmt.Sprintf("%smessage_%d.json", baseFolderPath, jsonFileCount)
// Loop through all json files.
for Utils.IsFileExist(filePath) {
jsonFile, err := os.Open(filePath)
if err != nil {
fmt.Println(err)
} else {
fmt.Printf("%s has opend successfully!\n", filePath)
}
defer jsonFile.Close()
byteValue, _ := ioutil.ReadAll(jsonFile)
var messages MessagesModel.Messages
json.Unmarshal(byteValue, &messages)
renamePhotosFromSingleJsonFile(messages)
jsonFileCount++
filePath = fmt.Sprintf("%smessage_%d.json", baseFolderPath, jsonFileCount)
}
}