def list_bitcasa_files(base64="/"): bitcasa_utils = BitcasaUtils() client = bitcasa_utils.create_client() if not client: redirect("../auth") else: if not base64: base64 = "/" try: folder = client.get_folder(base64) except BitcasaException as e: if bitcasa_utils.test_auth(): auth_name="View Files" auth_url="/bitcasafilelister/files/" msg = "You access token is stored locally. To retrieve base64 paths click view files below" else: auth_name="Login" auth_url="/bitcasafilelister/auth/" msg = "Your authentication token is either invalid or not set. Please set one by logging in. After authentication, you will also be able to view your files" return template("bitcasafilelister", is_error=True, error_msg=e, auth_url=auth_url, authorization_code="", auth_name=auth_name, msg=msg) parent_path = folder.path[:folder.path.rfind('/')] if not parent_path: parent_path = "/" download_url = "https://developer.api.bitcasa.com/v1/files/" return template("fileslist", folder=folder, access_token=client.access_token, parent_path=parent_path, download_url=download_url, BitcasaFile=BitcasaFile)
def do_bitcasa_auth(): authorization_code = request.query.authorization_code bitcasa_utils = BitcasaUtils() client = bitcasa_utils.create_client(force=True, redirect_uri=utils.REDIRECT_URI) auth_name="View Files" auth_url="/bitcasafilelister/files/" error_msg = "" msg = "You access token is stored locally. To retrieve base64 paths click view files below" if authorization_code: try: client.authenticate(authorization_code) except BitcasaException: auth_name="Login" auth_url="/bitcasafilelister/auth" is_error=True error_msg = "Failed to authenticate access token %s" % authorization_code msg = "Your authentication token is either invalid or not set. Please set one by logging in. After authentication, you will also be able to view your files" log.exception(error_msg) else: is_error=False error_msg = "Storing permanent token %s" % client.access_token log.info(error_msg) try: with open(utils.BITCASA_TOKEN, "r") as tokenfile: json_token = json.loads(tokenfile.read()) with open(utils.BITCASA_TOKEN, "w") as tokenfile: json_token["bitcasa"]["TOKEN"] = client.access_token tokenfile.write(json.dumps(json_token, indent=4)) except Exception as e: auth_name="Login" auth_url="/bitcasafilelister/auth" is_error = True error_msg = "Failed to save permanent token" msg = "Your authentication token is either invalid or not set. Please set one by logging in. After authentication, you will also be able to view your files" log.exception(error_msg) return template("bitcasafilelister", is_error=is_error, error_msg=error_msg, authorization_code=authorization_code, auth_url=auth_url, auth_name=auth_name, msg=msg) else: redirect(client.login_url)
def main(): global log, utils, bitc args = Args() args.parse() log = logger.create("BitcasaFileFetcher", args) from helpers import utils from bitcasadownload import BitcasaDownload from lib import BitcasaUtils from lib.gdrive import GoogleDrive if args.run_level == Args.RUN_LEVEL_MAIN: client = None if not args.args.upload or not args.args.local: bitcasa_utils = BitcasaUtils() if bitcasa_utils.test_auth(): args.args.token = bitcasa_utils.token else: log.error("Bitcasa Access token not set or invalid. Use the following commands to get one.") log.info("python BitcasaFileLister") return client = bitcasa_utils.create_client() if args.args.upload: if args.args.provider == "gdrive": g = GoogleDrive() if not g.test_auth(): log.error("Google Drive Access token not set or invalid. Use the following command to get one.") log.info("python BitcasaFileFetcher oauth --provider gdrive") return log.debug("Initializing download") bitc = BitcasaDownload(args.args, client, should_exit) if should_exit.is_set(): log.info("Exiting") return input_thread = threading.Thread(target=handle_input, name="Handle Exit") input_thread.daemon = True input_thread.start() if args.args.single: bitc.process_single() else: bitc.process() elif args.run_level == Args.RUN_LEVEL_OAUTH: if args.args.provider == "bitcasa": run_server(args.args.nolaunch) elif args.args.provider == "gdrive": g = GoogleDrive() try: g.get_service(True, not args.args.nolaunch) except: log.exception("Error authenticating to Google drive") elif args.run_level == Args.RUN_LEVEL_TEST: if args.args.provider == "bitcasa": bitcasa_utils = BitcasaUtils() if bitcasa_utils.test_auth(): log.info("Connected to Bitcasa successfully") else: log.info("Error connecting to Bitcasa") elif args.args.provider == "gdrive": g = GoogleDrive() if g.test_auth(): log.info("Connected to Google Drive successfully") else: log.info("Error connecting to Google drive") else: log.error("An error occurred processing your command. Please check the syntax and try again") log.info("Done")