forked from Pomax/node-flickrapi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.js
More file actions
executable file
·147 lines (130 loc) · 4.81 KB
/
test.js
File metadata and controls
executable file
·147 lines (130 loc) · 4.81 KB
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
/**
* This is a test runner that sets up the Flickr
* API, and uses its connect/express proxy function
* to listen for API calls that are POST'ed to the
* API url. In this case:
*
* http://127.0.0.1:3000/service/rest/flickr.method.name
*
* A test page for this API can be accessed by
* loading http://127.0.0.1:3000 in your browser.
*/
var habitat = require("habitat"),
env = habitat.load(),
Flickr = require("./src/FlickrApi"),
FlickrOptions = env.get("FLICKR"),
// node test => auth test; node test false => token only test
testAuthenticated = (process.argv[2] != "false"),
server;
// Set up a temporary oauth callback server if
// we do not have authentication credentials yet.
if(!FlickrOptions.access_token || !FlickrOptions.access_token_secret) {
var express = require("express"),
app = express(),
server;
app.configure(function(){
app.get("/", function(req, res){
res.write("<!doctype html><html><head><meta charset='utf-8'><title>Credentials received</title></head>"+
"<body><h1>oauth credentials received</h1><p>You can close this window/tab now.</p></body></html>");
res.end();
FlickrOptions.exchange(req.query);
// we no longer need the auth server
server.close();
// reload environment
env = habitat.load();
FlickrOptions = env.get("FLICKR");
FlickrOptions.api_key = FlickrOptions.key;
delete FlickrOptions.key;
});
});
server = app.listen(4321, function(err, result) {
if (err) { console.error(err); process.exit(1); }
console.log("auth server listening on port 4321");
});
}
// set up a simple http server using Express.js
var setupApp = function(flickr, next) {
var express = require("express"),
app = express(),
server;
app.configure(function() {
app.disable('x-powered-by');
app.use(express.compress());
app.use(express.json());
app.use(express.static("browser"));
flickr.proxy(app, "/service/rest/");
});
server = app.listen(3000, next);
};
// Start up the Flickr API proxy, and call the test.echo
// method to make sure we are actually able to talk to Flickr.
if(testAuthenticated) Flickr.authenticate(FlickrOptions, function(error, flickr) {
setupApp(flickr, function(err, result) {
if (err) { console.error(err); process.exit(1); }
else {
console.log("listening on port 3000 (press ctrl-c to exit).");
flickr.test.echo({"test": "test"}, function(err,result) {
if(err) console.log("note: error connecting to the flickr API", err);
});
/**
*
* Drop in any code you want to test at this point.
*
*/
flickr.photos.search({ tags: "red+panda" }, function(err,result) {
if(err) { return console.log("error:", err); }
console.log(result.photos.photo.length + " results found. First result:");
console.log(JSON.stringify(result.photos.photo[0],false,2));
});
/**
*
* The code above simply searches for red panda pictures
*
*/
// Simple test code: downsync the user's content,
// if the --downsync runtime argument was passed.
if (process.argv.indexOf("--downsync")>-1) {
console.log("Starting downsync...");
// make sure we grab public + private data for a downsync
FlickrOptions.force_auth = true;
FlickrOptions.afterDownsync = function() {
console.log("\nDownsync finished.");
server.close();
process.exit(0);
};
var user = FlickrOptions.user_id.replace("%40","-"),
downsync = Flickr.downsync("data/" + user);
console.log("Downsyncing for user: " + user);
downsync(false, flickr);
}
}
});
});
// Start up the Flickr API proxy, and call the test.echo
// method to make sure we are actually able to talk to Flickr.
// *** THIS PATH DOES NOT USE ANY AUTHENTICATION ***
if(!testAuthenticated) Flickr.tokenOnly(FlickrOptions, function(error, flickr) {
setupApp(flickr, function(err, result) {
if (err) { console.error(err); process.exit(1); }
else {
console.log("listening on port 3000 (press ctrl-c to exit).");
flickr.test.echo({"test": "test"}, function(err,result) {
if(err) { return console.log("note: error connecting to the flickr API"); }
/**
*
* Drop in any code you want to test at this point.
*
*/
flickr.photos.search({ tags: "red+panda" }, function(err,result) {
if(err) { return console.log("error:", err); }
console.log(result.photos.photo.length + " results found. First result:");
console.log(JSON.stringify(result.photos.photo[0],false,2));
});
/**
*
* The code above simply searches for red panda pictures
*/
});
}
});
});