Skip to content
Browse files

1. Use array to store permission scope.

2. View photo with ./0xfb album
3. Add parameter support of uploadphoto.
4. Upload photo with a description:
  $ ./0xfb uploadphoto -m "This is message"
  • Loading branch information...
1 parent 758aa0d commit 31b5156346bc51762053af443d879e20b5d7b609 @kk1fff kk1fff committed Apr 6, 2012
Showing with 124 additions and 7 deletions.
  1. +56 −0 src/album.js
  2. +5 −1 src/config.js
  3. +1 −1 src/fb_oauth_client.js
  4. +2 −1 src/index.js
  5. +60 −4 src/upload_photo.js
View
56 src/album.js
@@ -0,0 +1,56 @@
+var https = require('https');
+
+var USERCONFIG = require('./config.js').user_config();
+
+var error = function(data) {
+ console.log(JSON.stringify(data));
+};
+
+var listAlbum = function(data) {
+ if (data.hasOwnProperty('data')) {
+ var d = data['data'];
+ d.forEach(function(v) {
+ console.log('%d - %s', v.id, v.name);
+ });
+ } else if (data.hasOwnProperty('error')) {
+ // Print error message
+ console.log('Error!');
+ console.log('Message: %s', data.error.message);
+ } else {
+ console.log('error');
+ }
+ process.exit(0);
+};
+
+var getUserAlbum = exports.getUserAlbum = function(success_callback,
+ failure_callback) {
+ var url = "https://graph.facebook.com/me/albums?access_token="
+ + USERCONFIG['fb_auth_token'];
+
+ var opt = {
+ host: 'graph.facebook.com',
+ path: '/me/albums?access_token=' + USERCONFIG['fb_auth_token']
+ };
+
+ https.get(opt, function(resp) {
+ var jsonData = '';
+ var cb;
+ if (resp.statusCode == 200) {
+ cb = success_callback;
+ } else {
+ cb = failure_callback;
+ }
+ resp.on('data', function(d) {
+ jsonData = jsonData + d.toString('utf8');
+ });
+ resp.on('end', function() {
+ cb(JSON.parse(jsonData));
+ });
+ });
+};
+
+exports.run = function(progOpt, cmdArg) {
+ if (cmdArg.length == 0) {
+ getUserAlbum(listAlbum, error);
+ }
+};
View
6 src/config.js
@@ -16,7 +16,11 @@ var app_cfg = {
// Facebook app id.
'fb_client_id' : '370477596318204',
- 'fb_auth_scope' : 'user_photos,publish_stream',
+ 'fb_auth_scope' : [
+ 'user_photos',
+ 'publish_stream',
+ 'friends_photos'
+ ],
// Default configure file name
'default_config_file' : HOME() + '/.0xfb',
View
2 src/fb_oauth_client.js
@@ -34,7 +34,7 @@ var httpServer = http.createServer(
response.setHeader('Location',
CONFIG['fb_authclient_pathname']
+ "#client_id=" + CONFIG['fb_client_id']
- + "&scope=" + CONFIG['fb_auth_scope']);
+ + "&scope=" + CONFIG['fb_auth_scope'].join(','));
response.end();
} else {
// Error, return 404
View
3 src/index.js
@@ -11,7 +11,8 @@ var CONFIG = require('./config.js').app_config();
// command argument. Move to ./config.js ?
var progCommand = {
// Update photo
- 'uploadphoto' : './upload_photo.js'
+ 'uploadphoto' : './upload_photo.js',
+ 'album' : './album.js'
};
// It is a chance for program to make global setting before and command
View
64 src/upload_photo.js
@@ -3,16 +3,29 @@ var http = require('http');
var crypto = require('crypto');
var USERCONFIG = require('./config.js').user_config();
+var fieldMapper = {
+ 'message' : 'message'
+};
+
// Make a multipart boundary.
var makeBoundary = function() {
var hash = crypto.createHash('sha1');
hash.update(Date.now() + '');
return '----' + hash.digest('base64');
}
-var uploadStdin = function(albumId) {
+var makePostData = function(cfg) {
+ var kvp = [];
+ if (cfg.hasOwnProperty('message')) {
+ kvp.push('message='+encodeURIComponent(cfg['message']));
+ }
+ return kvp.join('&');
+}
+
+var uploadStdin = function(config) {
- var uploadPath = (albumId === undefined) ? '/me/photos' : '/'+albumId+'/photos';
+ var uploadPath = !config.hasOwnProperty('albumId') ?
+ '/me/photos' : '/' + config['albumId'] + '/photos';
var multipartBoundary = makeBoundary();
@@ -54,9 +67,20 @@ var uploadStdin = function(albumId) {
});
});
+ // Send data section.
+ Object.keys(config).forEach(function(v) {
+ if (fieldMapper.hasOwnProperty(v)) {
+ req.write('--' + multipartBoundary + '\r\n');
+ req.write('Content-Disposition: form-data; name="' + fieldMapper[v] + '"\r\n');
+ req.write('\r\n');
+ req.write(config[v]);
+ req.write('\r\n');
+ }
+ });
+
// Here we start to redirect data to server.
req.write('--' + multipartBoundary + '\r\n');
- req.write('Content-Disposition: form-data; name="picture"; filename="1.jpg"\r\n');
+ req.write('Content-Disposition: form-data; name="image"; filename="1.jpg"\r\n');
req.write('Content-Type: image/jpeg\r\n');
req.write('\r\n');
@@ -72,5 +96,37 @@ var uploadStdin = function(albumId) {
};
exports.run = function(progOpt, cmdArgs) {
- uploadStdin();
+ var expecting = 'key';
+ var key = ''
+ var parsed = {};
+ for (var i = 0; i < cmdArgs.length; ++i) {
+ if (expecting == 'key') {
+ expecting = 'value';
+ key = cmdArgs[i];
+ } else {
+ // If this key expects value.
+ if ((key == '--albumId')
+ || (key == '--msg')
+ || (key == '-m')) {
+ parsed[key] = cmdArgs[i];
+ expecting = 'key';
+ } else {
+ parsed[key] = '';
+ key = cmdArgs[i];
+ }
+ }
+ }
+
+ var cfg = {};
+ Object.keys(parsed).forEach(function(v) {
+ if (v == '--album') {
+ cfg['albumId'] = parsed[v];
+ } else if (v == '--msg' || v == '-m') {
+ cfg['message'] = parsed[v];
+ }
+ });
+
+ console.log(JSON.stringify(cfg));
+
+ uploadStdin(cfg);
}

0 comments on commit 31b5156

Please sign in to comment.
Something went wrong with that request. Please try again.