This repository was archived by the owner on Jul 3, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfixes.go
More file actions
160 lines (127 loc) · 4.26 KB
/
fixes.go
File metadata and controls
160 lines (127 loc) · 4.26 KB
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
package main
import (
"fmt"
"strings"
"net/http"
"regexp"
"image"
"github.com/anthonynsimon/bild/transform"
"github.com/satori/go.uuid"
"github.com/gocraft/dbr"
_ "image/jpeg"
_ "image/png"
)
func fixURL(url string) string {
startsHTTP := strings.HasPrefix(url, "http:")
if startsHTTP == true {
return url[5:]
}
return url
}
func fixLinks(sess *dbr.Session) {
type Image struct {
ID string
SmallURL string `db:"small_url"`
BigURL string `db:"big_url"`
OriginalURL string `db:"original_url"`
}
var images []Image
sess.Select("id, small_url, big_url, original_url").From("images").Load(&images)
for _, image := range images {
fmt.Println(fixURL(image.SmallURL))
sess.Update("images").
Set("small_url", fixURL(image.SmallURL)).
Set("big_url", fixURL(image.BigURL)).
Set("original_url", fixURL(image.OriginalURL)).
Where("id = ?", image.ID).Exec()
}
}
func fixTags(sess *dbr.Session) {
type Tag struct {
Name string
Number int
}
var tags []Tag
sess.SelectBySql("SELECT `name`, COUNT(*) as `number` from `tags` GROUP BY `name` HAVING `number` > 1").Load(&tags)
fmt.Println("==============")
fmt.Println(len(tags))
fmt.Println("==============")
for _, tag := range tags {
var similarTags []string
sess.Select("id").From("tags").Where("name = ?", tag.Name).Load(&similarTags)
first := similarTags[0]
fmt.Println(first, similarTags[1:], tag.Name)
sess.Update("tags_images").Set("tag_id", first).Where("tag_id IN ?", similarTags[1:]).Exec()
sess.DeleteFrom("tags").Where("id IN ?", similarTags[1:]).Exec()
}
}
type Change struct {
Old string
New string
Result bool
}
func fixExternalImage(url string, folderFlag *string) Change {
filename := uuid.NewV4().String()
hasHTTP := strings.HasPrefix(url, "http://static.jess.gallery")
hasHTTPS := strings.HasPrefix(url, "https://static.jess.gallery")
alreadyFixed := strings.HasPrefix(url, "//static.jess.gallery")
if hasHTTP {
newURL := strings.Replace(url, "http://static.jess.gallery", "//static.jess.gallery", 1)
return Change{ Old: url, New: newURL, Result: true }
} else if hasHTTPS {
newURL := strings.Replace(url, "https://static.jess.gallery", "//static.jess.gallery", 1)
return Change{ Old: url, New: newURL, Result: true }
} else if alreadyFixed != true {
response, _ := http.Get(url)
defer response.Body.Close()
imageFile, _, _ := image.Decode(response.Body)
b := imageFile.Bounds()
fmt.Println(b.Max.X, b.Max.Y, b.Max.X/b.Max.Y)
var imageRatio = float64(b.Max.X) / float64(b.Max.Y)
// compress them to 1200px width and save to the static folder
largeImage := transform.Resize(imageFile, 1200, int(1200/imageRatio), transform.Linear)
finalLastName := saveFile(*folderFlag, filename + "_1200.jpg", largeImage)
fmt.Println(finalLastName, largeImage.Bounds())
newURL := prepareURL(finalLastName)
return Change{ Old: url, New: newURL, Result: true }
}
return Change{ Result: false }
}
func fixText(text string, folderFlag *string) (string, int) {
// find <img src="$EXT" />, where $EXT leads to other articles
// yes, I know the zen – https://blog.codinghorror.com/parsing-html-the-cthulhu-way/
r, _ := regexp.Compile("<img(.+?)src=\"(.+?)\"")
res := r.FindAllStringSubmatch(text, -1)
var changes = make([]Change, 0)
for _, value := range res {
url := value[2]
change := fixExternalImage(url, folderFlag)
if change.Result == true {
changes = append(changes, change)
}
}
articleText := text
// change link to the internal one
if len(changes) > 0 {
for _, change := range changes {
articleText = strings.Replace(articleText, change.Old, change.New, 1)
}
}
return articleText, len(changes)
}
func fixExternalImages(sess *dbr.Session, folderFlag *string) {
type Article struct {
ID string
Text string
}
// load all articles
var articles []Article
sess.Select("id, text").From("articles").Load(&articles)
// iterate through them and get their text
for _, article := range articles {
newText, numberOfChanges := fixText(article.Text, folderFlag)
if numberOfChanges > 0 {
sess.Update("articles").Set("text", newText).Where("id = ?", article.ID).Exec()
}
}
}