var querystring = require("querystring");
var Rx = require("rx-dom");
var API_KEY = "???";
var PROXY_SERVER = "http://localhost:9999";
function photoUrl (photo) {
return PROXY_SERVER+"/?"+querystring.stringify({
farm: photo.farm,
server: photo.server,
id: photo.id,
secret: photo.secret
});
}
function searchPhotosFlickrURL (nb, text) {
return "https://www.flickr.com/services/rest/?"+querystring.stringify({
jsoncallback: "JSONPCallback",
method: "flickr.photos.search",
text: text,
per_page: nb,
format: "json",
api_key: API_KEY
});
}
function fetchPhotos (nb) {
return Rx.DOM.jsonpRequest(searchPhotosFlickrURL(nb, "Landscape"))
.map(function (json) {
return json.response.photos.photo.map(photoUrl);
});
}
Last active
August 29, 2015 14:26
-
-
Save gre/41255c316fbe213c03d5 to your computer and use it in GitHub Desktop.
proxy of flickr images to workaround the CORS issue on images (can't draw them on canvas / webgl)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"io/ioutil" | |
"net/http" | |
) | |
func main() { | |
http.HandleFunc("/", GetImageFromFlickr) | |
fmt.Println("Up and listening on port 9999") | |
http.ListenAndServe(":9999", nil) | |
} | |
func GetImageFromFlickr(rw http.ResponseWriter, request *http.Request) { | |
farm := request.FormValue("farm") | |
server := request.FormValue("server") | |
id := request.FormValue("id") | |
secret := request.FormValue("secret") | |
if farm == "" || server == "" || id == "" || secret == "" { | |
rw.WriteHeader(400) | |
} else { | |
url := fmt.Sprintf("http://farm%s.static.flickr.com/%s/%s_%s_z.jpg", farm, server, id, secret) | |
response, err := http.Get(url) | |
if err != nil { | |
fmt.Fprintf(rw, "Error fetching the image") | |
} | |
defer response.Body.Close() | |
body, err := ioutil.ReadAll(response.Body) | |
if err != nil { | |
fmt.Fprintf(rw, "Error while reading the body of the response; %s", err) | |
} | |
rw.Header().Set("Access-Control-Allow-Origin", "*") | |
_, err = rw.Write(body) | |
if err != nil { | |
panic("Couldn't write the response. Exiting.") | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require("http"); | |
var request = require("request"); | |
var URL = require("url"); | |
http.createServer(function (req, res) { | |
var url = URL.parse(req.url, true); | |
var farm = url.query.farm; | |
var server = url.query.server; | |
var id = url.query.id; | |
var secret = url.query.secret; | |
if (url && farm && server && id && secret) { | |
var src = "http://farm"+farm+".static.flickr.com/"+server+"/"+id+"_"+secret+"_z.jpg"; | |
res.setHeader("access-control-allow-origin", "*"); | |
request.get(src).pipe(res); | |
} | |
else { | |
res.statusCode = 404; | |
res.end(); | |
} | |
}).listen(9999); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment