This repository was archived by the owner on Oct 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathflickr.go
107 lines (95 loc) · 2.59 KB
/
flickr.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
// Copyright 2014 James McGuire. All rights reserved.
// Use of this source code is governed by the MIT
// license that can be found in the LICENSE file.
package main
import (
"encoding/xml"
"log"
"math/big"
"math/rand"
"net/http"
"net/url"
"strings"
irc "github.com/fluffle/goirc/client"
"github.com/tv42/base58"
)
const flickrApiUrl = "https://api.flickr.com/services/rest/"
type Setresp struct {
Sets []Set `xml:"collections>collection>set"`
}
type Set struct {
Id string `xml:"id,attr"`
Title string `xml:"title,attr"`
Description string `xml:"description,attr"`
}
type Photoresp struct {
Photos []Photo `xml:"photoset>photo"`
}
type Photo struct {
Id int64 `xml:"id,attr"`
Secret string `xml:"secret,attr"`
Server string `xml:"server,attr"`
Farm string `xml:"farm,attr"`
Title string `xml:"title,attr"`
Isprimary string `xml:"isprimary,attr"`
}
// Fetch a random picture from one of Haata's keyboard sets
func haata(conn *irc.Conn, line *irc.Line) {
if !strings.HasPrefix(line.Text(), "!haata") {
return
}
flickrUrl, err := url.Parse(flickrApiUrl)
if err != nil {
log.Println(err)
return
}
v := flickrUrl.Query()
v.Set("method", "flickr.collections.getTree")
v.Set("api_key", config.FlickrAPIKey)
// triplehaata's user_id
v.Set("user_id", "57321699@N06")
// Only the keyboard pics
v.Set("collection_id", "57276377-72157635417889224")
flickrUrl.RawQuery = v.Encode()
sets, err := http.Get(flickrUrl.String())
if err != nil {
log.Println(err)
return
}
defer sets.Body.Close()
var setresp Setresp
err = xml.NewDecoder(sets.Body).Decode(&setresp)
if err != nil {
log.Println(err)
return
}
randsetindex := rand.Intn(len(setresp.Sets))
randset := setresp.Sets[randsetindex].Id
flickrUrl, err = url.Parse(flickrApiUrl)
if err != nil {
log.Println(err)
return
}
v = flickrUrl.Query()
v.Set("method", "flickr.photosets.getPhotos")
v.Set("api_key", config.FlickrAPIKey)
v.Set("photoset_id", randset)
flickrUrl.RawQuery = v.Encode()
pics, err := http.Get(flickrUrl.String())
if err != nil {
log.Println(err)
return
}
defer pics.Body.Close()
var photoresp Photoresp
err = xml.NewDecoder(pics.Body).Decode(&photoresp)
if err != nil {
log.Println(err)
return
}
randpic := rand.Intn(len(photoresp.Photos))
// flickr's short url's are encoded using base58... this seems messy
// Maybe use the proper long url?
photostring := string(base58.EncodeBig([]byte{}, big.NewInt(photoresp.Photos[randpic].Id)))
conn.Privmsg(line.Target(), strings.TrimSpace(setresp.Sets[randsetindex].Title)+`: http://flic.kr/p/`+photostring)
}