-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
184 lines (159 loc) · 4.13 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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"strings"
"time"
"github.com/h2non/filetype"
"github.com/rwcarlsen/goexif/exif"
"github.com/rwcarlsen/goexif/mknote"
"gopkg.in/AlecAivazis/survey.v1"
)
const (
md = "markdown"
txt = "text"
stop = "(STOP)"
)
type Photo struct {
Name string
Date time.Time
FL string
ISO string
SS string
F string
Lens string
Body string
}
func main() {
imageList := fetchImageList("./")
fmt.Println("Number of photo files: ", len(imageList))
if len(imageList) == 0 {
return
}
outputType := selectOutputType()
if outputType == stop {
return
}
if outputType == md {
createMd(imageList)
return
}
if outputType == txt {
createTxt(imageList)
return
}
}
// DIR IMAGE LIST
func fetchImageList(dir string) []Photo {
paths, err := ioutil.ReadDir(dir)
if err != nil {
panic(err)
}
exif.RegisterParsers(mknote.All...)
var list []Photo
for _, path := range paths {
abspath := filepath.Join(dir, path.Name())
file, _ := os.Open(abspath)
head := make([]byte, 261)
file.Read(head)
if filetype.IsImage(head) {
file, _ = os.Open(abspath)
x, err := exif.Decode(file)
if err != nil {
fmt.Println("exif err:", err)
return list
}
bodyModelTag, _ := x.Get(exif.Model)
focal, _ := x.Get(exif.FocalLength)
lensModelTag, _ := x.Get(exif.LensModel)
ssTag, _ := x.Get(exif.ExposureTime)
fTag, _ := x.Get(exif.FNumber)
isoTag, _ := x.Get(exif.ISOSpeedRatings)
date, _ := x.DateTime()
bodyModel := strings.Replace(bodyModelTag.String(), "\"", "", -1)
numer, _, _ := focal.Rat2(0)
fl := strconv.FormatInt(numer, 10)
lensModel := strings.Replace(lensModelTag.String(), "\"", "", -1)
ss := strings.Replace(ssTag.String(), "\"", "", -1)
fStr := strings.Replace(fTag.String(), "\"", "", -1)
fs := strings.Split(fStr, "/")
f1, _ := strconv.Atoi(fs[0])
f2, _ := strconv.Atoi(fs[1])
f := strconv.FormatFloat(float64(f1)/float64(f2), 'f', 1, 64)
iso := isoTag.String()
list = append(list, Photo{Name: abspath, Date: date, FL: fl, SS: ss, F: f, ISO: iso, Body: bodyModel, Lens: lensModel})
}
}
return list
}
// OUTPUT FILE TYPE (select: markdown or text)
func selectOutputType() string {
outputType := ""
prompt := &survey.Select{
Message: "Output type:",
Options: []string{md, txt, stop},
}
survey.AskOne(prompt, &outputType, nil)
return outputType
}
func addLine(lines []byte, item string) []byte {
return append(lines[:], []byte(item + "\n")[:]...)
}
func createMd(imageList []Photo) {
file, err := os.OpenFile("exiflist.md", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
panic(err)
}
defer file.Close()
var lines []byte
// head
lines = addLine(lines, "# exif list")
lines = addLine(lines, "last updated: "+time.Now().String())
lines = addLine(lines, "---")
// body
for _, photo := range imageList {
lines = addLine(lines, "## "+photo.Name)
lines = addLine(lines, "<img src='"+photo.Name+"' alt='drawing' width='200'/>")
lines = addLine(lines, photo.Date.String())
lines = addLine(lines, "\n")
lines = addLine(lines, photo.Body)
lines = addLine(lines, photo.Lens)
lines = addLine(lines, photo.FL+"mm")
lines = addLine(lines, photo.SS+"sec")
lines = addLine(lines, "F"+photo.F)
lines = addLine(lines, "ISO "+photo.ISO)
lines = addLine(lines, "\n")
}
// write
file.Write(([]byte)(lines))
}
func createTxt(imageList []Photo) {
file, err := os.OpenFile("exiflist.txt", os.O_WRONLY|os.O_CREATE, 0666)
if err != nil {
panic(err)
}
defer file.Close()
var lines []byte
// head
lines = addLine(lines, "# exif list")
lines = addLine(lines, "last updated: "+"2019.4.11 20:20")
lines = addLine(lines, "---")
// body
for _, photo := range imageList {
lines = addLine(lines, "## "+photo.Name)
lines = addLine(lines, photo.Date.String())
lines = addLine(lines, "\n")
lines = addLine(lines, photo.Body)
lines = addLine(lines, photo.Lens)
lines = addLine(lines, photo.FL+"mm")
lines = addLine(lines, photo.SS+"sec")
lines = addLine(lines, "F"+photo.F)
lines = addLine(lines, "ISO "+photo.ISO)
lines = addLine(lines, "\n")
}
// write
file.Write(([]byte)(lines))
}