From 3c3cceab763640443f5fd6fa63294303afca0590 Mon Sep 17 00:00:00 2001 From: Patrick Wang Date: Tue, 3 Apr 2012 00:19:08 +0800 Subject: [PATCH] 1. Store expiration timestamp of access token. 2. Implement basic command line handling mechinism. --- src/config.js | 13 +++++++++- src/fb_oauth_client.js | 27 ++++++++++++++------ src/index.js | 57 ++++++++++++++++++++++++++++++++++++++---- src/upload_photo.js | 5 ++++ 4 files changed, 88 insertions(+), 14 deletions(-) create mode 100644 src/upload_photo.js diff --git a/src/config.js b/src/config.js index 9486020..3bfa9f0 100644 --- a/src/config.js +++ b/src/config.js @@ -19,7 +19,18 @@ var app_cfg = { 'fb_auth_scope' : '', // Default configure file name - 'default_config_file' : HOME() + '/.0xfb' + 'default_config_file' : HOME() + '/.0xfb', + + // Available program options + // 'option name' : (true if it has a value) + // Ex: + // If you expect user to type "-p 60" + // you should set + // '-p' : true + 'program_options' : { + '--website' : true, // Example + '--silence' : false + } }; diff --git a/src/fb_oauth_client.js b/src/fb_oauth_client.js index 1cf520f..e36f420 100644 --- a/src/fb_oauth_client.js +++ b/src/fb_oauth_client.js @@ -59,14 +59,25 @@ var getTokenWithUsersHelp = function(success_callback, exports.getAuthToken = function(success_callback, failure_callback) { var USER_CONFIG = require('./config.js').user_config(); - if (USER_CONFIG.hasOwnProperty('fb_auth_token')) { - success_callback(USER_CONFIG['fb_auth_token'], 0); + + if (USER_CONFIG.hasOwnProperty('fb_auth_token') + && USER_CONFIG['fb_auth_token_expire'] > Date.now()/1000) { + // Success callback + success_callback(USER_CONFIG['fb_auth_token'], + USER_CONFIG['fb_auth_token_expire']); } else { - getTokenWithUsersHelp(function(access_token, expires_in) { // When success. - USER_CONFIG['fb_auth_token'] = access_token; - USER_CONFIG['fb_auth_token_expire'] = - Date.now() / 1000 + expires_in; - success_callback(access_token, expires_in); // To caller's callback - }, failure_callback); + getTokenWithUsersHelp( + function(access_token, expires_in) { // When success. + + // Record to user config. + USER_CONFIG['fb_auth_token'] = access_token; + USER_CONFIG['fb_auth_token_expire'] = + parseInt(Date.now()/1000) + parseInt(expires_in); + + // Success callback + success_callback(USER_CONFIG['fb_auth_token'], + USER_CONFIG['fb_auth_token_expire']); + }, + failure_callback); } }; \ No newline at end of file diff --git a/src/index.js b/src/index.js index dc7b23c..95eedae 100644 --- a/src/index.js +++ b/src/index.js @@ -1,8 +1,55 @@ +// Command format: +// ./0xfb [Program option] [Command argument] + + +var CONFIG = require('./config.js').app_config(); + +// prog_command: +// Map command to module that run the command. +// Each module must has run() function. And the first argument of this +// function would be program option, while the second one would be +// command argument. Move to ./config.js ? +var progCommand = { + // Update photo + 'uploadphoto' : './upload_photo.js' +}; + +// It is a chance for program to make global setting before and command +// is issued. Command will be executed right after this step. +var handleSetting = function(progOpt, command, cmdArg) { + // So far it's empty XD. +} + +// Parse command line, collect program option, command, command argrment. +var initRun = function() { + + var program_option = []; + var command = ''; + var command_argument = []; + + // [TODO Patrick] Here I assume there is no program options, so we just + // use that first argument as command + + command = process.argv[2]; + command_argument = process.argv.slice(3); + + if (!progCommand.hasOwnProperty(command)) { + // Invalid command. + process.exit(1); + } + + handleSetting(program_option, command, command_argument); + + // Go + require(progCommand[command]).run(program_option, command_argument); +} + +// *** Start with fetching access token *** var fbOauthClient = require('./fb_oauth_client.js'); +fbOauthClient.getAuthToken(function(access_token, expires) { + // Debug !! + console.log(" Access token: %s", access_token); + console.log(" Expire Time: %d, now: %d", expires, parseInt(Date.now()/1000)); -fbOauthClient.getAuthToken(function(access_token, expires_in) { - console.log("We got token:"); - console.log(" access_token: %s", access_token); - console.log(" expires_in: %s", expires_in); - process.exit(0); + initRun(); }); \ No newline at end of file diff --git a/src/upload_photo.js b/src/upload_photo.js new file mode 100644 index 0000000..90b64c8 --- /dev/null +++ b/src/upload_photo.js @@ -0,0 +1,5 @@ +exports.run = function(progOpt, cmdArgs) { + console.log(progOpt); + console.log(cmdArgs); + process.exit(0); +} \ No newline at end of file