-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathfilename.go
168 lines (140 loc) · 4.33 KB
/
filename.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
package instago
// This file provides methods to create file names of posts, stories, and
// postlives.
import (
"path"
"regexp"
"strconv"
"strings"
)
func BuildStoryFilename2(url, username, id, timef, times string) string {
url, err := StripQueryString(url)
if err != nil {
panic(err)
}
ext := path.Ext(path.Base(url))
return username + "-" +
id +
"-story-" +
timef + "-" +
times +
ext
}
func BuildStoryFilename(url, username, id string, timestamp int64) string {
return BuildStoryFilename2(url, username, id, FormatTimestamp(timestamp), strconv.FormatInt(timestamp, 10))
}
func BuildFilename(url, username, id, middle, last string, timestamp int64) string {
url, err := StripQueryString(url)
if err != nil {
panic(err)
}
ext := path.Ext(path.Base(url))
return username + "-" +
id +
middle +
FormatTimestamp(timestamp) + "-" +
last +
strconv.FormatInt(timestamp, 10) +
ext
}
func GetPostFilename(username, id, code, url string, timestamp int64, taggedusers []IGTaggedUser) (filename string) {
filename = BuildFilename(url, username, id, "-post-", code+"-", timestamp)
filename = AppendTaggedUsersToFilename(username, id, filename, taggedusers)
return
}
func AppendTaggedUsersToFilename(username, id, filename string, appendIdUsernames []IGTaggedUser) string {
// cannot use 256 here. will get filename too long error.
// use 240
filenameLimit := 240
prefix := username + "-" + id
usednames := make(map[string]string)
usednames[username] = id
// append tagged name
for _, n := range appendIdUsernames {
taggedname := n.Username
newprefix := prefix + "-" + taggedname
newfilename := strings.Replace(filename, prefix, newprefix, 1)
if len(newfilename) > filenameLimit {
continue
}
if _, ok := usednames[taggedname]; ok {
continue
} else {
usednames[taggedname] = n.Id
}
prefix = newprefix
filename = newfilename
}
// append tagged id
for name2, id2 := range usednames {
if name2 == username {
continue
}
newfilename := strings.Replace(filename, name2, name2+"-"+id2, 1)
if len(newfilename) > filenameLimit {
continue
}
filename = newfilename
}
return filename
}
// GetStoryFilename is the same as getStoryFilePath, except adding usernames in reel_mentions
func GetStoryFilename(username, id, code, url string, timestamp int64, rms []ItemReelMention) (filename string) {
//filename = BuildFilename(url, username, id, "-story-", code+"-", timestamp)
filename = BuildStoryFilename(url, username, id, timestamp)
var appendIdUsernames []IGTaggedUser
for _, rm := range rms {
pair := IGTaggedUser{Id: rm.GetUserId(), Username: rm.GetUsername()}
appendIdUsernames = append(appendIdUsernames, pair)
}
filename = AppendTaggedUsersToFilename(username, id, filename, appendIdUsernames)
return
}
// AppendIndexToFilename appends index to the filename. For example, filename
// "abcdef.jpg" with index "1" will return "abcdef-1.jpg", and filename
// "123456.mp4" with index "2" will return "123456-2.mp4".
// Only used for single post/item with several photos/videos of the same TakenAt
// time.
func AppendIndexToFilename(filename string, index int) string {
ext := path.Ext(filename)
fne := strings.TrimSuffix(filename, ext)
return fne + "-" + strconv.Itoa(index) + ext
}
// GetRFC3339String returns RFC3339 time string, given the input string.
func GetRFC3339String(s string) string {
// Google search: regex rfc3339 golang
pattern := regexp.MustCompile(`([0-9]+)-(0[1-9]|1[012])-(0[1-9]|[12][0-9]|3[01])[Tt]([01][0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9]|60)(\.[0-9]+)?(([Zz])|([\+|\-]([01][0-9]|2[0-3]):[0-5][0-9]))`)
return string(pattern.Find([]byte(s)))
}
func ExtractPostCodeFromFilename(filename string) (code string) {
// remove ext
f1 := strings.TrimSuffix(filename, path.Ext(filename))
rfc3339s := GetRFC3339String(f1)
pieces := strings.Split(f1, "-"+rfc3339s+"-")
if len(pieces) != 2 {
return
}
f2 := pieces[1]
pieces = strings.Split(f2, "-")
if len(pieces) < 2 {
return
}
utime := pieces[len(pieces)-1]
if len(utime) < 3 && len(pieces) > 2 {
utime = pieces[len(pieces)-2]
}
pieces = strings.Split(f2, "-"+utime)
if len(pieces) < 1 {
return
}
return pieces[0]
}
func ExtractUsernameIdFromFilename(filename string) (username, id string) {
pieces := strings.Split(filename, "-")
if len(pieces) < 2 {
return
}
username = pieces[0]
id = pieces[1]
return
}