This repository has been archived by the owner on Apr 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
gopher.go
71 lines (61 loc) · 2.24 KB
/
gopher.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
package goplus
import (
"net/http"
// Import appengine urlfetch package, that is needed to make http call to the api
"appengine"
"appengine/urlfetch"
// Import google api go client library
"code.google.com/p/google-api-go-client/googleapi/transport"
// Import Google+ package, the package will be named "plus"
"code.google.com/p/google-api-go-client/plus/v1"
)
// gopherFallback is the official gopher URL (in case we don't find any in the Google+ stream)
const gopherFallback = "http://golang.org/doc/gopher/gophercolor.png"
// init is called before the application start
func init() {
// Register a handler for /gopher URLs.
http.HandleFunc("/gopher", gopher)
}
// gopher is an HTTP handler that searches Google+ for an activity
// with a Gopher photo and redirects to the image thumbnail.
func gopher(w http.ResponseWriter, r *http.Request) {
// Create appengine context, needed to do urlfetch call
c := appengine.NewContext(r)
// Create a new http client, supplying the API key we
// generated to identify our application, and the urlfetch
// transport necessary to make HTTP calls on App Engine
transport := &transport.APIKey{
Key: apiKey,
Transport: &urlfetch.Transport{Context: c}}
client := &http.Client{Transport: transport}
// Create the plus service object with which we can make an API call
service, err := plus.New(client)
if err != nil {
// error creating the Google+ client
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Search the stream for "gopher" related activities
result, err := service.Activities.Search("#gopher").Do()
if err != nil {
// error searching Google+ for gopher
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
// Iterate through the activities search result until we find a photo or album attachment
for _, item := range result.Items {
for _, att := range item.Object.Attachments {
switch att.ObjectType {
case "photo":
// Redirect to the gopher thumbnail
http.Redirect(w, r, att.Image.Url, 302)
return
case "album":
http.Redirect(w, r, att.Thumbnails[0].Image.Url, 302)
return
}
}
}
// Fallback on the official gopher if we didn't find any gophers in the stream
http.Redirect(w, r, gopherFallback, http.StatusFound)
}