def process(self): bc = BitcasaClient("758ab3de", "5669c999ac340185a7c80c28d12a4319", "https://rosekings.com/bitcasafilelist/", self.at) logger("Getting base folder\n") base = bc.get_folder(self.baseFolder) #initialize logfiles try: if not os.path.isdir(self.tmp): os.makedirs(self.tmp) except OSError as exc: pass myFile = file("%ssuccessfiles.txt" % self.tmp, 'w+') myFile.write(time.strftime("%Y-%m-%d %H:%M:%S") + "\n") myFile.close() myFile = file("%serrorfiles.txt" % self.tmp, 'w+') myFile.write(time.strftime("%Y-%m-%d %H:%M:%S") + "\n") myFile.close() myFile = file("%sskippedfiles.txt" % self.tmp, 'w+') myFile.write(time.strftime("%Y-%m-%d %H:%M:%S") + "\n") myFile.close() logger("Starting recursion\n") self.folderRecurse(base, "", 0,0) #wait for threads to finish downoading for thread in self.threads: thread.join() #Log final speed and statistics logger("finished %s at %s\n" % (convertSize(self.bytestotal),getSpeed(self.bytestotal,time.time()-self.st)))
def t_est_authentication(self): #Requests makes this difficult to test correctly, so just drop it for now. redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect) response = requests.get(client.login_url) self.assertEqual(response.status_code, 200) pattern = re.compile( r'<input type="hidden" name="csrf_token" value="(.+?)"/>') matches = pattern.findall(response.content) self.assertEqual(len(matches), 1) csrf_token = matches[0] params = { 'user': self.username, 'password': self.password, 'redirect': redirect, 'csrf_token': csrf_token } response = requests.post(client.login_url, params=params) #, allow_redirects=False) self.assertEqual(response.status_code, 200) parsed_url = urlparse.urlparse(response.url) parsed_qs = urlparse.parse_qs(parsed_url.query) self.assertIn('authorization_code', parsed_qs) code = parsed_qs['authorization_code'] client.authenticate(code)
def test_add_folder(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') unittest_folder.add_folder('new folder') self.assertEqual(len(unittest_folder.items), 1) self.assertEqual(unittest_folder.items[0].name, 'new folder')
def main(): client_id = os.environ.get("BITCASA_CLIENT_ID", "") client_secret = os.environ.get("BITCASA_CLIENT_SECRET", "") access_token = os.environ.get("BITCASA_ACCESS_TOKEN", "") assert client_id, 'Please set "BITCASA_CLIENT_ID".' assert client_secret, 'Please set "BITCASA_CLIENT_SECRET".' assert access_token, 'Please set "BITCASA_ACCESS_TOKEN".' bitcasa = BitcasaClient(client_id, client_secret, access_token) r = bitcasa.get('folders/', params={'depth': 1}) pprint(r.json())
def main(): client_id = os.environ.get("BITCASA_CLIENT_ID", "") client_secret = os.environ.get("BITCASA_CLIENT_SECRET", "") access_token = os.environ.get("BITCASA_ACCESS_TOKEN", "") assert client_id, 'Please set "BITCASA_CLIENT_ID".' assert client_secret, 'Please set "BITCASA_CLIENT_SECRET".' assert access_token, 'Please set "BITCASA_ACCESS_TOKEN".' bitcasa = BitcasaClient(client_id, client_secret, access_token) items = bitcasa.list_item() for i in items: print(i.name)
def test_delete_folder(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path)
def main(): client_id = os.environ.get("BITCASA_CLIENT_ID", "") client_secret = os.environ.get("BITCASA_CLIENT_SECRET", "") access_token = os.environ.get("BITCASA_ACCESS_TOKEN", "") assert client_id, 'Please set "BITCASA_CLIENT_ID".' assert client_secret, 'Please set "BITCASA_CLIENT_SECRET".' assert access_token, 'Please set "BITCASA_ACCESS_TOKEN".' bitcasa = BitcasaClient(client_id, client_secret, access_token) path = bitcasa.infinite_drive.path print('Upload to InfiniteDrive: ' + path) item = bitcasa.upload_file(path, 'upload.py') print(item.data)
def test_rename(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') unittest_folder_renamed = client.create_folder('/', 'renamed unittest') client.delete_folder(unittest_folder.path) client.delete_folder(unittest_folder_renamed.path) unittest_folder = client.create_folder('/', 'unittest') new_name = 'renamed unittest' unittest_folder.rename(new_name) self.assertEqual(unittest_folder.name, new_name)
def test_add_file(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') contents = 'this is a test' f = StringIO(contents) filename = 'test_file.txt' unittest_folder.add_file(f, len(contents), filename) self.assertEqual(len(unittest_folder.items), 1) read_iter = unittest_folder.items[0].read() read_contents = '' for chunk in read_iter: read_contents += chunk self.assertEqual(contents, read_contents)
def test_delete_file(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') original_contents = 'this is a test' f = StringIO(original_contents) filename = 'test_file.txt' result = client.upload_file(f, filename, unittest_folder.path, 'overwrite') client.delete_file(result.path)
def test_move_folder(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') sub_folder = client.create_folder(unittest_folder.path, 'sub_folder') unittest_folder_two = client.create_folder('/', 'unittest_two') moved_folder = client.move_folder(unittest_folder_two.path, sub_folder.path, 'moved_sub_folder', 'rename') paths = [i.path for i in sub_folder.items] self.assertIn(moved_folder.path, paths)
def test_copy_folder(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') sub_folder = client.create_folder(unittest_folder.path, 'sub_folder') copy_folder = client.copy_folder(sub_folder.path, unittest_folder.path, 'sub_folder_copy') self.assertIn(unittest_folder.path, copy_folder.path)
def test_refresh(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') self.assertEqual(len(unittest_folder.items), 0) client.create_folder(unittest_folder.path, 'new folder') unittest_folder.refresh() self.assertEqual(len(unittest_folder.items), 1) self.assertEqual(unittest_folder.items[0].name, 'new folder')
def test_login_url(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect) given_login_url = client.login_url query_string = urlencode({ 'client_id': self.client_id, 'redirect': redirect }) expected_login_url = '{0}oauth2/authenticate?{1}'.format( BASEURL, query_string)
def process(self): bitc = BitcasaClient("758ab3de", "5669c999ac340185a7c80c28d12a4319", "https://rose-llc.com/bitcasafilelist/", self.accesstoken) log.debug("Getting base folder") base = None while base is None: try: base = bitc.get_folder(self.basefolder) except (BitcasaException, ValueError) as e: log.error("Bitcasa API Exception: %s. Retrying" % e) time.sleep( 10 ) # sleep here so that we don't exhaust api rate limit too fast except KeyboardInterrupt: raise SystemExit #initialize logfiles try: if not os.path.isdir(self.tmp): os.makedirs(self.tmp) except OSError as exc: pass myfile = file(self.successfiles, 'w+') myfile.write(time.strftime("%Y-%m-%d %H:%M:%S") + " Start\n") myfile.close() myfile = file(self.errorfiles, 'w+') myfile.write(time.strftime("%Y-%m-%d %H:%M:%S") + " Start\n") myfile.close() myfile = file(self.skippedfiles, 'w+') myfile.write(time.strftime("%Y-%m-%d %H:%M:%S") + " Start\n") myfile.close() log.debug("Starting recursion") self.folderRecurse(base, "", 0, 0) #wait for threads to finish downoading for thread in self.threads: thread.join() #Log final speed and statistics log.info("finished %s at %s\n" % (convertSize( self.bytestotal), getSpeed(self.bytestotal, time.time() - self.st)))
def test_rename_folder(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') sub_folder = client.create_folder(unittest_folder.path, 'sub_folder') renamed_folder = client.rename_folder(sub_folder.path, 'renamed_sub_folder', 'overwrite') self.assertEqual(renamed_folder.name, 'renamed_sub_folder') self.assertEqual(sub_folder.path, renamed_folder.path)
def test_move(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') sub_folder = client.create_folder(unittest_folder.path, 'sub_folder') move_folder = sub_folder.move_to(unittest_folder, 'sub_folder_move') unittest_folder.refresh() self.assertIn(unittest_folder.path, move_folder.path)
def test_rename_file(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') f = StringIO('this is a test') filename = 'test_file.txt' result = client.upload_file(f, filename, unittest_folder.path, 'overwrite') new_result = client.rename_file(result.path, 'renamed_file', 'overwrite') self.assertEqual(new_result.name, 'renamed_file') self.assertEqual(result.path, new_result.path)
def t_est_authentication(self): #Requests makes this difficult to test correctly, so just drop it for now. redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect) response = requests.get(client.login_url) self.assertEqual(response.status_code, 200) pattern = re.compile(r'<input type="hidden" name="csrf_token" value="(.+?)"/>') matches = pattern.findall(response.content) self.assertEqual(len(matches), 1) csrf_token = matches[0] params = { 'user': self.username, 'password': self.password, 'redirect': redirect, 'csrf_token': csrf_token } response = requests.post(client.login_url, params=params)#, allow_redirects=False) self.assertEqual(response.status_code, 200) parsed_url = urlparse.urlparse(response.url) parsed_qs = urlparse.parse_qs(parsed_url.query) self.assertIn('authorization_code', parsed_qs) code = parsed_qs['authorization_code'] client.authenticate(code)
def process(self): bitc = BitcasaClient("758ab3de", "5669c999ac340185a7c80c28d12a4319", "https://rose-llc.com/bitcasafilelist/", self.accesstoken) log.debug("Getting base folder") base = None while base is None: try: base = bitc.get_folder(self.basefolder) except (BitcasaException, ValueError) as e: log.error("Bitcasa API Exception: %s. Retrying" % e) time.sleep(10) # sleep here so that we don't exhaust api rate limit too fast except KeyboardInterrupt: raise SystemExit #initialize logfiles try: if not os.path.isdir(self.tmp): os.makedirs(self.tmp) except OSError as exc: pass myfile = file(self.successfiles, 'w+') myfile.write(time.strftime("%Y-%m-%d %H:%M:%S") + " Start\n") myfile.close() myfile = file(self.errorfiles, 'w+') myfile.write(time.strftime("%Y-%m-%d %H:%M:%S") + " Start\n") myfile.close() myfile = file(self.skippedfiles, 'w+') myfile.write(time.strftime("%Y-%m-%d %H:%M:%S") + " Start\n") myfile.close() log.debug("Starting recursion") self.folderRecurse(base, "", 0, 0) #wait for threads to finish downoading for thread in self.threads: thread.join() #Log final speed and statistics log.info("finished %s at %s\n" % (convertSize(self.bytestotal), getSpeed(self.bytestotal, time.time() - self.st)))
def clientFactory(configObj): """ Create a BitcasaClient with an access-token attached """ conf = dict(configObj.items('bitcasa')) token = config.readTokenFile('~/.bitcasa-token') client = BitcasaClient(conf['client-id'], conf['secret'], conf['redirect-url'], access_token=token) if token: if configObj.opt.verbose: print("-- Found ~/.bitcasa-token, using...") return client # If we don't already have an access-token, authenticate to get one return authenticate(client, conf)
def test_move(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') sub_folder = client.create_folder(unittest_folder.path, 'sub_folder') contents = 'this is a test' f = StringIO(contents) filename = 'test_file.txt' unittest_folder.add_file(f, len(contents), filename) uploaded_file = unittest_folder['test_file.txt'] new_file = uploaded_file.move_to(sub_folder, 'something new') self.assertIn(sub_folder.path, new_file.path)
def test_iter(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') f = StringIO('this is a test') filename = 'test_file.txt' result = client.upload_file(f, filename, unittest_folder.path, 'overwrite') items = [i for i in unittest_folder] self.assertEqual(len(items), 1) self.assertEqual(items[0].path, result.path)
def test_read_file(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') original_contents = 'this is a test' f = StringIO(original_contents) filename = 'test_file.txt' result = client.upload_file(f, filename, unittest_folder.path, 'overwrite') self.assertIn(unittest_folder.path, result.path) self.assertEqual(result.name, filename) returned_contents = '' for chunk in client.get_file_contents('test_download.txt', result.path): returned_contents += chunk self.assertEqual(original_contents, returned_contents)
def test_rename(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') contents = 'this is a test' f = StringIO(contents) filename = 'test_file.txt' unittest_folder.add_file(f, len(contents), filename) uploaded_file = unittest_folder['test_file.txt'] uploaded_file.rename('something new.txt') unittest_folder.refresh() self.assertEqual(unittest_folder.items[0].name, 'something new.txt')
def test_move_file(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') f = StringIO('this is a test') filename = 'test_file.txt' result = client.upload_file(f, filename, unittest_folder.path, 'overwrite') moved_result = client.move_file(result.path, unittest_folder.path, 'test_file_copy.txt', 'overwrite') result_iter = result.read() contents = '' moved_contents = '' for chunk in result_iter: contents += chunk moved_iter = moved_result.read() for chunk in moved_iter: moved_contents += chunk self.assertEqual(contents, moved_contents)
def test_read(self): redirect = 'http://example.com' client = BitcasaClient(self.client_id, self.secret, redirect, self.access_token) unittest_folder = client.create_folder('/', 'unittest') client.delete_folder(unittest_folder.path) unittest_folder = client.create_folder('/', 'unittest') contents = 'this is a test' f = StringIO(contents) filename = 'test_file.txt' unittest_folder.add_file(f, len(contents), filename) self.assertEqual(len(unittest_folder.items), 1) read_iter = unittest_folder.items[0].read() read_contents = '' for chunk in read_iter: read_contents += chunk self.assertEqual(contents, read_contents)
def get_args(): bitc = BitcasaClient(CLIENTID, CLIENTSECRET, "https://rose-llc.com/bitcasafilelist/") if "--oauth" in sys.argv: sys.stdout.write("%s\n" % bitc.login_url) webbrowser.open(bitc.login_url) sys.exit(1) elif "--clientcreds" in sys.argv: print "CLIENTID %s" % CLIENTID print "CLIENTSECRET %s" % CLIENTSECRET sys.exit(1) elif "--settoken" in sys.argv: token = sys.argv[2] try: bitc.authenticate(token) except BitcasaException: sys.stderr.write("Failed to authenticate access token %s\n" % token) sys.stderr.write("Navigate to the below url to get a new one\n") sys.stderr.write("%s\n" % bitc.login_url) else: sys.stdout.write("Storing permanent token %s\n" % bitc.access_token) try: with open("token.ini", "w") as tokenfile: tokenfile.write(bitc.access_token) except OSError: sys.stderr.write("Failed to save permanent token\n") raise finally: sys.exit(1) parser = argparse.ArgumentParser() parser.add_argument("--settoken", dest="token", help="Set the access token from Bitcasa. You only need to do this once.") parser.add_argument("src", help="The Bitcasa base64 path for file source") parser.add_argument("dst", help="The final destination root dir or your files") parser.add_argument("-t", "--temp", help="The temp dir to store downloaded files. (Should be a local folder)") parser.add_argument("-l", "--log", help="Full path to log file") parser.add_argument("--depth", type=int, help="Specify depth of folder traverse. 0 is same as --norecursion") parser.add_argument("-m", "--threads", type=int, help="Specify the max number of threads to use for downloading. Default is 5") parser.add_argument("--norecursion", dest="rec", help="Do not go below the src folder. (Same as --depth=0)", action="store_true") parser.add_argument("--noconsole", dest="console", help="do not log to console", action="store_true") parser.add_argument("--oauth", help="Get the url to authenticate and retrieve an access token", action="store_true") parser.add_argument("--verbose", help="increase output verbosity", action="store_true") parser.add_argument("--testauth", dest="test", help="test capability to connect to infinite drive", action="store_true") parser.add_argument("-p","--progress", dest="progress", help="Log download progress every 60 secs", action="store_true") parser.add_argument('--version', help="Displays version and exits", action='version', version='%(prog)s 0.5.4') args = parser.parse_args() if not args.dst or not args.src: sys.stderr.write("Source and destination are required.\n") sys.exit(2) if not os.path.isfile("token.ini"): sys.stderr.write("Please retrive an access token using the following command.\n") sys.stderr.write("python getfiles.py --oauth\n") sys.exit(2) else: try: with open("token.ini", "r") as tokenfile: args.token = tokenfile.read() except OSError: sys.stderr.write("Failed to retrieve permanent token\n") raise if not args.temp: args.local = True if not args.log and args.temp: args.log = os.path.join(args.temp, "runlog.txt") args.logdir = args.temp elif not args.log and not args.temp: args.log = os.path.join(args.dst, "runlog.txt") args.logdir = args.dst #initialize temp dir try: if not os.path.isdir(args.logdir): os.makedirs(args.logdir) except: sys.stderr.write("Error creating log directory\n") raise args.rec = not args.rec args.console = not args.console if args.depth > 0 and not args.rec: sys.stdout.write("Note: Non 0 depth and --no-recursion parameter present. Assuming recusion") args.rec = True return args
def create_client(self, force=False, redirect_uri=utils.REDIRECT_URI): if (self.token or force) and not self.client: self.client = BitcasaClient(self.client_id, self.client_secret, redirect_uri, self.token) return self.client
class BitcasaUtils(object): def __init__(self): self.client = None self.token = None self.client_id = None self.client_secret = None self.get_bitcasa_token() def get_bitcasa_token(self): if os.path.isfile(utils.BITCASA_TOKEN): try: with open(utils.BITCASA_TOKEN, "r") as tokenfile: self.token = tokenfile.read().rstrip() try: tokens_json = json.loads(self.token) except ValueError: log.info("Converting bitcasa.ini") log.info("If you are using a custom CLIENTID and CLIENTSECRET please put them in bitcasa.ini") with open(utils.BITCASA_SAMPLE_TOKEN, "r") as sample, open(utils.BITCASA_TOKEN, "w") as tokenfile: json_sample = json.loads(sample.read()) self.client_id = json_sample["bitcasa"]["CLIENTID"] self.client_secret = json_sample["bitcasa"]["CLIENTSECRET"] json_sample["bitcasa"]["TOKEN"] = self.token tokenfile.write(json.dumps(json_sample, indent=4)) else: self.client_id = tokens_json["bitcasa"]["CLIENTID"] self.client_secret = tokens_json["bitcasa"]["CLIENTSECRET"] self.token = tokens_json["bitcasa"]["TOKEN"] if self.token: log.debug("Got token") else: log.error("No token stored") except: log.exception("Failed to read Bitcasa token file") elif os.path.isfile(utils.BITCASA_SAMPLE_TOKEN): log.info("Creating bitcasa.ini") try: with open(utils.BITCASA_SAMPLE_TOKEN, "r") as sample, open(utils.BITCASA_TOKEN, "w") as tokenfile: json_sample = json.loads(sample.read()) self.client_id = json_sample["bitcasa"]["CLIENTID"] self.client_secret = json_sample["bitcasa"]["CLIENTSECRET"] tokenfile.write(json.dumps(json_sample, indent=4)) except: log.exception("Error writing bitcasa.ini") else: log.info("No auth token file found at %s or %s ", utils.BITCASA_TOKEN, utils.BITCASA_SAMPLE_TOKEN) return self.token def create_client(self, force=False, redirect_uri=utils.REDIRECT_URI): if (self.token or force) and not self.client: self.client = BitcasaClient(self.client_id, self.client_secret, redirect_uri, self.token) return self.client def test_auth(self): self.create_client() if not self.client: return False try: self.client.get_user_profile() except BitcasaException as e: if e.code == 9006: return True else: log.exception("Error connecting to bitcasa %s" % e.code) return False else: return True def do_oauth(self): if not self.client: self.create_client(True) log.info("Please navigate to the following url:") log.info(self.client.login_url) time.sleep(3) webbrowser.open(self.client.login_url)