-
Notifications
You must be signed in to change notification settings - Fork 0
/
unsplash.go
117 lines (103 loc) · 3.15 KB
/
unsplash.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
package main
import (
"encoding/json"
"log"
"net/http"
"net/url"
"os"
"strconv"
)
type UnsplashPhoto struct {
Id string `json:"id"`
Width float32 `json:"width"`
Height float32 `json:"height"`
Description string `json:"description"`
User UnsplashUser `json:"user"`
Urls UnsplashUrls `json:"urls"`
Links UnsplashPhotoLinks `json:"links"`
}
type UnsplashUser struct {
Id string `json:"id"`
Username string `json:"username"`
Name string `json:"name"`
Links UnsplashUserLinks `json:"links"`
}
type UnsplashPhotoLinks struct {
Self string `json:"self"`
Html string `json:"html"`
Download string `json:"download"`
}
type UnsplashUserLinks struct {
Self string `json:"self"`
Html string `json:"html"`
Photos string `json:"photos"`
Likes string `json:"likes"`
}
type UnsplashUrls struct {
Regular string `json:"regular"`
Raw string `json:"raw"`
}
type UnsplashSearchResult struct {
Total int `json:"total"`
TotalPages int `json:"total_pages"`
Results []UnsplashPhoto `json:"results"`
}
type UnsplashApi struct {
Http http.Client
cache *ReqCache
accessKey string
baseUrl string
log *log.Logger
}
func NewUnsplashApi(cfg *Config, cache *ReqCache) UnsplashApi {
return UnsplashApi{
cache: cache,
accessKey: cfg.Unsplash.AccessKey,
baseUrl: "https://api.unsplash.com/search/photos",
log: log.New(os.Stderr, "(unsplash)", log.LstdFlags),
}
}
func (unsp *UnsplashApi) Type() string {
return "unsplash"
}
func (unsp *UnsplashApi) TTL() int {
return 86400
}
func (unsp *UnsplashApi) PageSize() int { return 30 }
func (unsp *UnsplashApi) Search(page int, query string) ImageSearchResult {
qParam := url.Values{}
qParam.Add("query", query)
qParam.Add("page", strconv.Itoa(page))
qParam.Add("per_page", strconv.Itoa(unsp.PageSize()))
getReq, err := http.NewRequest(http.MethodGet, unsp.baseUrl+"?"+qParam.Encode(), nil)
if err != nil {
unsp.log.Println("Failed to create http request:", err.Error())
return ImageSearchResult{err: &err, images: []ImageData{}}
}
getReq.Header.Set("Accept-Version", "v1")
getReq.Header.Set("Authorization", "Client-ID "+unsp.accessKey)
req, err := unsp.cache.CachedFetch(getReq, &unsp.Http)
if err != nil {
unsp.log.Println("(unsplash) Failed to fetch:", err.Error())
return ImageSearchResult{err: &err, images: []ImageData{}}
}
defer req.Body.Close()
data := UnsplashSearchResult{}
err = json.NewDecoder(req.Body).Decode(&data)
if err != nil {
unsp.log.Println("Failed to decode response", err.Error())
return ImageSearchResult{err: &err, images: []ImageData{}}
}
output := make([]ImageData, len(data.Results))
for i, el := range data.Results {
output[i].Id = "unsplash/" + el.Id
output[i].Name = el.Description
output[i].Source = "Unsplash"
output[i].SourceUrl = el.Links.Html
output[i].Artist = el.User.Name
output[i].Aspect = el.Width / el.Height
output[i].DownloadUrl = el.Urls.Raw
output[i].PreviewUrl = el.Urls.Regular
}
return ImageSearchResult{err: nil, images: output}
}