-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
144 lines (116 loc) · 3.2 KB
/
main.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"facette.io/natsort"
"fmt"
"log"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/spf13/cobra"
"github.com/valyala/fastjson"
)
func main() {
var input string
var outDir string
var dirFilter string
var rootCmd = &cobra.Command{
Use: "gphotos-takeout",
RunE: func(cmd *cobra.Command, args []string) error {
return layout(input, outDir, dirFilter)
},
}
rootCmd.Flags().StringVarP(&input, "input", "i", "", "path to Takeout\\Google Photos directory")
err := rootCmd.MarkFlagRequired("input")
if err != nil {
panic(err)
}
rootCmd.Flags().StringVarP(&outDir, "output", "o", "", "path to output directory")
err = rootCmd.MarkFlagRequired("output")
if err != nil {
panic(err)
}
rootCmd.Flags().StringVarP(&dirFilter, "filter", "f", "", "directory names pattern (e.g. '2020-08-2*')")
err = rootCmd.Execute()
if err != nil {
log.Fatal(err)
}
}
func layout(rootDir string, outDir string, dirFilter string) error {
log.Printf("start (dir=%s, dirFilter=%s, outDir=%s)\n", rootDir, dirFilter, outDir)
yearMap := make(map[int]map[time.Month]map[ItemKey]ItemValue)
var jsonParser fastjson.Parser
yearOrAlbumNames, err := readDir(rootDir)
// constant order of processing to avoid any random bugs
// nat sort not suitable - albums should be always after year dirs
sort.Strings(yearOrAlbumNames)
if err != nil {
return err
}
start := time.Now()
var years []int
for _, file := range yearOrAlbumNames {
if strings.HasSuffix(file, ".json") || strings.HasPrefix(file, ".") {
continue
}
if dirFilter != "" {
match, err := filepath.Match(dirFilter, file)
if err != nil {
return err
}
if !match {
continue
}
}
yearOrAlbumDir := filepath.Join(rootDir, file)
log.Print("scan dir: " + file)
itemNames, err := readDir(yearOrAlbumDir)
// for items use nat sort
natsort.Sort(itemNames)
if err != nil {
return err
}
nameMap := make(map[string]string)
for _, name := range itemNames {
nameMap[strings.ToLower(name)] = name
}
for _, name := range itemNames {
if !strings.HasSuffix(name, ".json") {
continue
}
path := filepath.Join(yearOrAlbumDir, name)
err = processMetaItem(name, path, yearOrAlbumDir, nameMap, &jsonParser, yearMap, &years)
if err != nil {
return fmt.Errorf("cannot process %s: %w", path, err)
}
}
}
log.Printf("scanning complete (%s)", time.Since(start).Round(time.Millisecond).String())
sort.Ints(years)
start = time.Now()
err = deduplicate(years, yearMap, rootDir)
if err != nil {
return err
}
log.Printf("deduplicating complete (%s)", time.Since(start).Round(time.Millisecond).String())
start = time.Now()
err = linkItems(years, yearMap, outDir)
if err != nil {
return err
}
log.Printf("linking complete (%s)", time.Since(start).Round(time.Millisecond).String())
return nil
}
func readDir(dir string) ([]string, error) {
f, err := os.Open(dir)
if err != nil {
return nil, err
}
list, err := f.Readdirnames(-1)
_ = f.Close()
if err != nil {
return nil, err
}
return list, nil
}