class MediaFireConnection: def __init__(self, api=MediaFireApi(), email=properties.email, password=properties.password, app_id=properties.app_id): self.__api = api self.session = api.user_get_session_token(email=email, password=password, app_id=app_id) self.__api.session = self.session self.uploader = MediaFireUploader(self.__api) self.client = MediaFireClient() self.client.login(email=email, password=password, app_id=app_id) self.ROOT = ROOT def get_info(self): res = self.__api.system_get_info() return res def get_user_info(self): res = self.__api.user_get_info() return res def get_file_info(self, uploadresult): return self.__api.file_get_info(uploadresult.quickkey)['result'] def upload_file(self, source_path, source_filename, target_path, target_filename): result = None while not result: try: result = self.client.upload_file( os.path.join(source_path, source_filename), target_path + "/" + target_filename) except IsADirectoryError: result = None # Todo: idk why we need this... except ResourceNotFoundError: logging.info("%s path not found, lets create it." % target_path) self.client.create_folder(target_path, recursive=True) result = self.client.get_resource_by_key(result.quickkey) return result def do_ls(self, client, args): """List directory""" for item in client.get_folder_contents_iter(args.uri): # privacy flag if item['privacy'] == 'public': item['pf'] = '@' else: item['pf'] = '-' if isinstance(item, Folder): # type flag item['tf'] = 'd' item['key'] = item['folderkey'] item['size'] = '' else: item['tf'] = '-' item['key'] = item['quickkey'] item['name'] = item['filename'] print("{tf}{pf} {key:>15} {size:>10} {created} {name}".format( **item)) return True def to_mongo(self, db, folder_pair): path = folder_pair['mf'] ftp_root = folder_pair['ftp'] coll_name = folder_pair['name'] logging.info("Processing path: %s" % path) try: for item in self.client.get_folder_contents_iter(path): if type(item) is File: mf_path = (path + item['filename']) ftp_path = (ftp_root + item['filename']) logging.debug("Checking in Mongo for %s ==> %s" % (mf_path, ftp_path)) existing = db.find_by_ftp_path(ftp_path, coll_name=coll_name) if existing: logging.debug("updating Mongo") item['path'] = path item["mf_root"] = folder_pair['mf'] db.update_item(existing, properties={"mf": item}, coll_name=coll_name) else: logging.debug("inserting into Mongo") item['path'] = path db.insert_one({"mf": item}, coll_name=coll_name) elif type(item) is Folder: self.to_mongo( db, { "mf": path + item['name'] + '/', "ftp": folder_pair['ftp'] + item['name'] + '/', "name": coll_name }) except ResourceNotFoundError as rne: logging.error("Resource NOT Found: %s", exc_info=rne) return except Exception as e: logging.error("Error: %s", exc_info=e)
def main(argv): vnumber = "0.3" print('mfcmd.py v' + vnumber, file=sys.stderr) account = '' passphrase = '' filepath = '' filepathbase = '' filesize = '' localhash = '' uldir = '' try: opts, args = getopt.getopt(argv,"e:p:u:h:f:",["email=","password="******"upload-folder=","hash=","file="]) except getopt.GetoptError: print('Error!', file=sys.stderr) return for opt, arg in opts: if opt in ("-e", "--email"): account = arg elif opt in ("-p", "--password"): passphrase = arg elif opt in ("-u", "--upload-folder"): uldir = arg elif opt in ("-s", "--size"): filesize = arg elif opt in ("-h", "--hash"): localhash = arg elif opt in ("-f", "--file"): filepath = arg if account == '': print('Error:credentials') return else: print('Email: ' + account, file=sys.stderr) if passphrase == '': print('Error:credentials') return else: print('Password: '******'': print('Error:filepath') return else: if os.path.exists(filepath): if os.path.isfile(filepath): print('Filepath: ' + filepath, file=sys.stderr) filepathbase = os.path.basename(filepath) print('Filename: ' + filepathbase, file=sys.stderr) else: print('Error:noregularfile') return else: print('Error:nofile') return if uldir == '': uldir = 'mfcmd' print('Upload folder: ' + uldir, file=sys.stderr) if localhash == '': print('No SHA-256 specified: calculating...', file=sys.stderr) sha256_hash = hashlib.sha256() with open(filepath,"rb") as f: for byte_block in iter(lambda: f.read(4096),b""): sha256_hash.update(byte_block) localhash = sha256_hash.hexdigest() print('Checksum: ' + localhash, file=sys.stderr) api = MediaFireApi() try: session = api.user_get_session_token( email=account, password=passphrase, app_id='42511') print('MediaFire API: connected', file=sys.stderr) api.session = session except: print('Error:login') try: userinfo = api.user_get_info() print("Account holder: " + userinfo['user_info']['display_name'], file=sys.stderr) maxstore = userinfo['user_info']['storage_limit'] usedstore = userinfo['user_info']['used_storage_size'] freestore = int(maxstore)-int(usedstore) freestore_str = str(freestore) print("Maximum storage: " + maxstore, file=sys.stderr) print("Used storage: " + usedstore, file=sys.stderr) print("Free storage: " + freestore_str, file=sys.stderr) localsize = os.path.getsize(filepath) localsize_str = str(localsize) print("Local file size: " + localsize_str, file=sys.stderr) if freestore <= localsize: print("Error: available space will not suffice!", file=sys.stderr) print("Error:space") return else: print("Available filespace will suffice", file=sys.stderr) except: print('Error getting or parsing user info', file=sys.stderr) client = MediaFireClient() try: client.login(email=account, password=passphrase, app_id='42511') print('MediaFire Client: logged in', file=sys.stderr) except: print('Error:login') return try: client.get_resource_by_path("/" + uldir + "/") print('Detected upload folder ./' + uldir, file=sys.stderr) except: print('Error: upload folder ./' + uldir, 'does not exist!', file=sys.stderr) try: client.create_folder("mf:/" + uldir) except: print('Could not create upload folder: defaulting to root', file=sys.stderr) uldir = '' try: if uldir == '': fileinfo = client.get_resource_by_path("/" + filepathbase) remotehash = fileinfo['hash'] if localhash == remotehash: print('Same file already exists: no upload necessary!', file=sys.stderr) try: dlurl = fileinfo['links']['normal_download'] dlurl = dlurl.replace('http://www.', 'https://') print(dlurl) except: print('Error:link') return else: print('Error: filename already exists in upload folder', file=sys.stderr) posixtime = int(time.time()) posixtext = str(posixtime) uldir = posixtext print('Creating root subfolder: /' + uldir, file=sys.stderr) try: client.create_folder("mf:/" + uldir) except: print('Error:folder') return else: fileinfo = client.get_resource_by_path("/" + uldir + "/" + filepathbase) remotehash = fileinfo['hash'] if localhash == remotehash: print('Same file already exists: no upload necessary!', file=sys.stderr) try: dlurl = fileinfo['links']['normal_download'] dlurl = dlurl.replace('http://www.', 'https://') print(dlurl) except: print('Error:link') return else: print('Error: filename already exists in upload folder', file=sys.stderr) posixtime = int(time.time()) posixtext = str(posixtime) uldir = uldir + '/' + posixtext print('Creating subfolder: ./' + uldir, file=sys.stderr) try: client.create_folder("mf:/" + uldir) except: print('Could not create upload folder: defaulting to root', file=sys.stderr) uldir = '' except: print('File does not exist in upload folder', file=sys.stderr) try: if uldir == '': client.upload_file(filepath, "mf:/") else: client.upload_file(filepath, "mf:/" + uldir + "/") except: print('Error: upload function', file=sys.stderr) try: if uldir == '': fileinfo = client.get_resource_by_path("/" + filepathbase) else: fileinfo = client.get_resource_by_path("/" + uldir + "/" + filepathbase) dlurl = fileinfo['links']['normal_download'] dlurl = dlurl.replace('http://www.', 'https://') print(dlurl) except: print('Error:upload') return
def createfolder(foldername): client = MediaFireClient() client.login( email='*****@*****.**', password='******', app_id='42511') client.create_folder('/'+foldername)