/
instragram.go
197 lines (178 loc) · 4.97 KB
/
instragram.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
185
186
187
188
189
190
191
192
193
194
195
196
197
package datastore
import (
"bytes"
"encoding/json"
"fmt"
"time"
"github.com/ahmdrz/goinsta/v2"
"github.com/dgraph-io/badger"
"github.com/disintegration/imaging"
"github.com/robrotheram/gogallery/worker"
)
type Instagram struct {
insta *goinsta.Instagram
GalleryPath string
db *badger.DB
}
func (i *Instagram) SetUpAlbum() {
worker.MakeThumbnail("themes/default/static/img/instagram.png")
p := Picture{
Id: "themes/default/static/img/instagram.png",
Name: "instagram",
Path: "themes/default/static/img/instagram.png",
}
err := Cache.Tables("PICTURE").Save(p)
fmt.Println(err)
Cache.Tables("ALBUM").Save(Album{
Id: i.GalleryPath + "/instagram",
Name: "instagram",
ProfileIMG: &p,
ModTime: time.Now(),
Parent: ""})
}
func (i *Instagram) Connect(username, password string) error {
insta := goinsta.New(username, password)
if err := insta.Login(); err != nil {
fmt.Println(err)
return err
}
i.insta = insta
i.db = createDatastore("instagram")
return nil
}
func (i *Instagram) UploadPhoto(path, caption string) error {
img, err := imaging.Open(path)
if err != nil {
return err
}
maxSize := img.Bounds().Dy()
if img.Bounds().Dx() < img.Bounds().Dy() {
maxSize = img.Bounds().Dx()
}
fmt.Printf("width: %d, height: %d, max: %d \n", img.Bounds().Dx(), img.Bounds().Dy(), maxSize)
// crop from center
centercropimg := imaging.CropCenter(img, maxSize, maxSize)
dstImage128 := imaging.Resize(centercropimg, 1024, 1024, imaging.Lanczos)
buf := new(bytes.Buffer)
imaging.Encode(buf, dstImage128, imaging.JPEG)
post, uploadErr := i.insta.UploadPhoto(buf, caption, 50, 0)
if uploadErr != nil {
return uploadErr
}
i.SavePost(post)
return nil
}
func (i *Instagram) getImagePathName(item goinsta.Item) string {
return fmt.Sprintf("%s-%s.jpg", "ig-post", time.Unix(item.TakenAt, 0).Format("02-01-2006 15:04:05"))
}
func (i *Instagram) getImageName(item goinsta.Item) string {
return fmt.Sprintf("%s-%s", "ig-post", time.Unix(item.TakenAt, 0).Format("02-01-2006 15:04:05"))
}
func (i *Instagram) SyncFrom() error {
latest := i.insta.Account.Feed()
for latest.Next(false) {
for _, item := range latest.Items {
if item.MediaType == 1 {
p, e := i.GetPost(item.ID)
if (e == nil || e == badger.ErrKeyNotFound) && len(p.ID) == 0 {
_, _, downloadERR := item.Download(i.GalleryPath+"/instagram", i.getImagePathName(item))
if downloadERR != nil {
fmt.Printf("Error Downloading from Instagram: %v", downloadERR)
} else {
fmt.Printf("Downloading Post ID : %s \n", item.ID)
i.SavePost(item)
p := Picture{
Id: i.GalleryPath + "/instagram/images/" + i.getImagePathName(item),
Name: i.getImageName(item),
Path: i.GalleryPath + "/instagram/images/" + i.getImagePathName(item),
Album: "instagram",
PostedToIG: true,
Caption: item.Caption.Text,
Exif: Exif{
Camera: "Instagram",
DateTaken: time.Unix(item.TakenAt, 0),
}}
Cache.Tables("PICTURE").Save(p)
worker.MakeThumbnail(i.GalleryPath + "/instagram/images/" + i.getImageName(item))
worker.MakeLargeThumbnail(i.GalleryPath + "/instagram/images/" + i.getImageName(item))
}
} else {
pics, err := Cache.Tables("PICTURE").Query("Name", i.getImageName(item), 1)
if err != nil {
break
}
if len(pics.([]Picture)) == 0 {
break
}
picture := pics.([]Picture)[0]
picture.Caption = item.Caption.Text
Cache.Tables("PICTURE").Save(picture)
}
}
}
if err := latest.Error(); err != nil {
if err := latest.Error(); err == goinsta.ErrNoMore {
break
}
}
}
return nil
}
func (i *Instagram) SavePost(original goinsta.Item) error {
err := i.db.Update(func(tx *badger.Txn) error {
return tx.Set([]byte(original.ID), serialize(original))
})
return err
}
func (i *Instagram) GetPost(id string) (goinsta.Item, error) {
post := goinsta.Item{}
err := i.db.View(func(tx *badger.Txn) error {
item, err := tx.Get([]byte(id))
if err != nil {
return err
}
valCopy, err := item.ValueCopy(nil)
post, _ = deserialize(valCopy)
return nil
})
if err != nil {
return goinsta.Item{}, err
}
return post, nil
}
func (i *Instagram) GetAllPosts() ([]goinsta.Item, error) {
posts := []goinsta.Item{}
err := i.db.View(func(txn *badger.Txn) error {
opts := badger.DefaultIteratorOptions
opts.PrefetchSize = 10
it := txn.NewIterator(opts)
defer it.Close()
for it.Rewind(); it.Valid(); it.Next() {
item := it.Item()
var data []byte
err := item.Value(func(v []byte) error {
data = v
return nil
})
if err != nil {
return err
}
u, error := deserialize(data)
if error != nil {
return error
}
posts = append(posts, u)
}
return nil
})
return posts, err
}
func serialize(u goinsta.Item) []byte {
b, _ := json.Marshal(u)
return b
}
func deserialize(b []byte) (goinsta.Item, error) {
var u = goinsta.Item{}
err := json.Unmarshal(b, &u)
return u, err
}