Exemple #1
0
def render_file_with_template(target_file, target_template):
    """
    """
    client = dropbox.client.DropboxClient(CONFIG.DROPBOX_PRIVATE_TOKEN)

    file_response, dropbox_meta = client.get_file_and_metadata(
        target_file)

    file_content = file_response.read()

    f = process_markdown(
        target_file, file_content)

    log.debug(f)

    if 'meta' in f:
        fmeta = f['meta']
        fmeta.update(dropbox_meta)
        f['meta'] = fmeta
    else:
        f['meta'] = dropbox_meta

    # data['published'] = data['modified']
    # data['created'] = data['modified']

    # merge 'meta' values with other values
    f.update(f['meta'])

    # fix title
    if 'Title' in f:
        f['title'] = f['Title']

    # permalink
    f['permalink'] = url_for_path(f['path'])

    template_response, meta = client.get_file_and_metadata(
        'templates/%s' % target_template)
    template_content = template_response.read()

    page_content = render_template(template_content, {
            'page': f,
            'post': f,
        })

    return page_content
def dbx_run_app_msg():

    sess = session.DropboxSession(app_key, app_secret, ACCESS_TYPE)
    client = dropbox.client.DropboxClient("DTvqi-XTYtYAAAAAAAFwvLNDyaem_j-GoxdTtN5Y2bm5gD525HnWDk4fizjtfexQ")
    # print 'linked account: ', client.account_info()
    f, metadata = client.get_file_and_metadata(
        "/Programming/Python/APPS/DATA_MINING/CytoTissue-eX/src/app_msgs/ctpp_denied_msg.txt"
    )
    return f.read()
def access_dbx():
    authorize_url = flow.start()
    print authorize_url
    code = "DTvqi-XTYtYAAAAAAAFwuhgU-Jdp-7kYxhUPhv9T3h8"
    # This will fail if the user enters an invalid authorization code
    access_token, user_id = flow.finish(code)
    client = dropbox.client.DropboxClient(access_token)
    print "linked account: ", client.account_info()

    f, metadata = client.get_file_and_metadata("/TEMP/magnum-opus.txt")
    out = open("magnum-opus.txt", "wb")
    out.write(f.read())
    out.close()
    print metadata
Exemple #4
0
def load_dropbox_file():
    dropbox_access_token = session.get(DROPBOX_ACCESS_KEY, None)
    if dropbox_access_token is None:
        return "please log into dropbox first"

    filepath = request.args.get('filepath', None)
    if filepath is None:
        return "You didn't pass a filepath"

    dropbox_client = get_client(dropbox_access_token)
    try:
        f, metadata = dropbox_client.get_file_and_metadata(filepath)
        return str(f.read())
    except Exception, e:
        return e.message
    def auth_user(self):
        APP_KEY = 'xxxxxxxxxxxxxx'
        APP_SECRET = 'xxxxxxxxxxxxxx'
        ACCESS_TYPE = 'dropbox'
        TOKENS = 'dropbox_token.txt'

        token_file = open(TOKENS)
        token_key,token_secret = token_file.read().split('|')
        token_file.close()

        sess = session.DropboxSession(APP_KEY,APP_SECRET, ACCESS_TYPE)
        sess.set_token(token_key,token_secret)
        client = client.DropboxClient(sess)

        base, ext = file1.split('.')

        f, metadata = client.get_file_and_metadata(file1)
        out = open('/%s_COPY.%s' %(base, ext), 'w')
        out.write(f.read())
    def download(self,file,options):
        '''Plugin interface method to download a file and create a new dataset

        :param file: Path to the remote file to download
        :type file: str
        :param options: context parameters
        :type options: list
        :return: tmp file path
        '''
        from dropbox import client
        self.sess.set_token(options['drop_access_token'].key,options['drop_access_token'].secret)
        client = client.DropboxClient(self.sess)
        logging.warn("DropBox - download request for "+file)
        folder_metadata = client.metadata('/')
        logging.warn("/ content "+str(folder_metadata))
        f, metadata = client.get_file_and_metadata(file)
        (out,file_path) = tempfile.mkstemp()
        output_file = open(file_path, 'wb')
        output_file.write(f.read())
        return file_path
Exemple #7
0
def main():
	# Include the Dropbox SDK libraries
	from dropbox import client, rest, session

	# Get your app key and secret from the Dropbox developer website
	APP_KEY = 'INSERT_APP_KEY_HERE'
	APP_SECRET = 'INSERT_SECRET_HERE'

	# ACCESS_TYPE should be 'dropbox' or 'app_folder' as configured for your app
	ACCESS_TYPE = 'INSERT_ACCESS_TYPE_HERE'

	sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)

	request_token = sess.obtain_request_token()

	# Make the user sign in and authorize this token
	url = sess.build_authorize_url(request_token)
	print "url:", url
	print "Please authorize in the browser. After you're done, press enter."
	raw_input()

	# This will fail if the user didn't visit the above URL and hit 'Allow'
	access_token = sess.obtain_access_token(request_token)

	client = client.DropboxClient(sess)
	print "linked account:", client.account_info()

	f = open('working-draft.txt')
	response = client.put_file('/magnum-opus.txt', f)
	print "uploaded:", response

	folder_metadata = client.metadata('/')
	print "metadata:", folder_metadata

	f, metadata = client.get_file_and_metadata('/magnum-opus.txt')
	out = open('magnum-opus.txt', 'w')
	out.write(f.read())
	out.close()
	print(metadata)
def recursive_get_folder(client, folder):
    folder_metadata = client.metadata(folder)

    for c in folder_metadata["contents"]:
        if c["is_dir"]:
            dirname = BASE_PATH + c["path"]
            sys.stdout.write("Found directory %s ... " % dirname)
            sys.stdout.flush()

            if os.path.exists(dirname):
                sys.stdout.write("Exists local.\n")
            else:
                sys.stdout.write("Creating local...")
                sys.stdout.flush()
                os.makedirs(dirname)
                sys.stdout.write("Done.\n")

            sys.stdout.flush()

            previous_cwd = os.getcwd()
            os.chdir(dirname)
            recursive_get_folder(client, c["path"])
            os.chdir(previous_cwd)
        else:
            # Check if the file exists
            download = True
            local_filename = BASE_PATH + c["path"]
            sys.stdout.write("Checking %s ... " % c["path"])
            sys.stdout.flush()

            if os.path.exists(local_filename):
                sys.stdout.write("Exists ")
                sys.stdout.flush()
                # Compare if they are the same
                if os.path.getsize(local_filename) == c["bytes"]:

                # FIXME: Improve reliability
                #if os.path.getmtime(filename) == c.client_mtime:

                    sys.stdout.write("and are the same.\n")
                    sys.stdout.flush()
                    download = False
                else:
                    sys.stdout.write("but aren't the same. ")
            else:
                sys.stdout.write("Does not exist. ")
                sys.stdout.flush()

            if download:
                # Download the file
                sys.stdout.write("\nDownloading.. ")
                sys.stdout.flush()

                out = open(local_filename, "wb")
                f, metadata = client.get_file_and_metadata(c["path"])
                total_size = metadata["bytes"]
                written = 0
                while True:
                    data = f.read(CHUNK_SIZE)
                    if data == '':
                        break
                    else:
                        out.write(data)
                        written = written + len(data)

                    percent = written * 30 / total_size

                    #percent_line = "[" + ("*" * percent) +
                    #               (" " * (30 - percent)) +
                    #                "] " + str(written) + "/" +
                    #                str(total_size)
                    percent_line = "[%s%s] %d/%d" % (("*" * percent),
                                                     (" " * (30 - percent)),
                                                     written, total_size)

                    sys.stderr.write(percent_line)
                    sys.stderr.write("\b" * (len(percent_line)))
                    sys.stderr.flush()

                out.close()

                sys.stdout.write("Done. \n")
    
    token_key = access_token.key
    token_secret = access_token.secret
    
    f = open(TOKEN_FILE, 'w')
    f.write("%s|%s" % (token_key, token_secret) )
    f.close()
    print "wrote new token"
else:
    sess.set_token(token_key, token_secret)

client = client.DropboxClient(sess)

print "client constructed"

recentf, recentmeta = client.get_file_and_metadata(FILE_PATH)

revisions = client.revisions(FILE_PATH)

print "files/revisions obtained"

DAT_PATH = "/.DocumentRevisions-V100/DAT/"

if not os.path.exists(DAT_PATH):
    os.makedirs(DAT_PATH)

for i in revisions:
    rev = i['rev']
    print "revision", rev, "obtained"
    f = client.get_file(FILE_PATH, rev)
    outfile = open(DAT_PATH + rev + "_dat.txt", 'w')
# To store time stamps of each revision
dictTimeStamps={}

# To Get All Files in a Folder you can use:
#folder_metadata = client.metadata('/Research/ThesisOzanSener/')#name of the folder we are interested
#for i in range(1,len(folder_metadata['contents'])):
#	if folder_metadata['contents'][i]['is_dir']==False:

#Full path of the files we are interested in their stories
filel = ['file1','file2']
for fN in filel:
	dictFS = {} #Processed value of a single file
	dictFSStr = {} #Yet another value
	revs = client.revisions(fN)
	for i in range(len(revs)): #Maximum number of revision is 1000 in default case
		f, metadata = client.get_file_and_metadata( revs[i]['path'],revs[i]['rev']) Fetch data
		# Write revision of the file
		out = open("RESULT_DIR" + revs[i]['path'] + str(revs[i]['revision']), 'w')
		out.write(f.read())
		out.close()
		dictTimeStamps[revs[i]['revision']]=revs[i]['modified']
		# Process your files to get necessary data
		wcBeforeStrip=int(commands.getstatusoutput('wc -w '+"RESULT_DIR" + revs[i]['path'] + str(revs[i]['revision'])+' |cut -f1 -d" "')[1])
		os.system("./StripLatexComments.pl "+"RESULT_DIR" + revs[i]['path'] + str(revs[i]['revision'])+" >tmp.txt")
		wcAfterStrip=int(commands.getstatusoutput('wc -w '+"tmp.txt"+' |cut -f1 -d" "')[1])
		# Store the data
		dictFS[revs[i]['revision']]=wcBeforeStrip
		dictFSStr[revs[i]['revision']]=wcAfterStrip
	# Save the story of a single file to a CSV file
	w2 = csv.writer(open( "RESULT_DIR" + revs[i]['path']+".csv", "w"),delimiter=';')
	for key, val in dictFS.items():
print 'selected file: ', filename

# upfile = open('E:/workspace/CSE6331/upload/working-draft.txt', 'rb')
# upfile = open(filename, 'rb')
# l = []
# l = filename.split('/')
# # print l[-1]

encfile = encrypt_file(key, filename)

upfile = open(encfile, 'rb')
response = client.put_file('/' + filename.split('/')[-1], upfile)
print 'uploaded: ', response
upfile.close()

new_name = '../../upload/' + filename.split('/')[-1]
os.rename(encfile, new_name)
print 'encrypted file: ', new_name

# folder_metadata = client.metadata('/')
# print 'metadata: ', folder_metadata
# 
downfile, metadata = client.get_file_and_metadata('/' + filename.split('/')[-1])
# print 'downfile: ', downfile
out = open('../../download/' + filename.split('/')[-1] + '.enc', 'wb')
out.write(downfile.read())
out.close()
decrypt_file(key, '../../download/' + filename.split('/')[-1] + '.enc')
os.remove('../../download/' + filename.split('/')[-1] + '.enc')
print 'done'
# print 'metadata 2: ', metadata
Exemple #12
0
# upfile = open('E:/workspace/CSE6331/upload/working-draft.txt', 'rb')
# upfile = open(filename, 'rb')
# l = []
# l = filename.split('/')
# # print l[-1]

encfile = encrypt_file(key, filename)

upfile = open(encfile, 'rb')
response = client.put_file('/' + filename.split('/')[-1], upfile)
print 'uploaded: ', response
upfile.close()

new_name = '../../upload/' + filename.split('/')[-1]
os.rename(encfile, new_name)
print 'encrypted file: ', new_name

# folder_metadata = client.metadata('/')
# print 'metadata: ', folder_metadata
#
downfile, metadata = client.get_file_and_metadata('/' +
                                                  filename.split('/')[-1])
# print 'downfile: ', downfile
out = open('../../download/' + filename.split('/')[-1] + '.enc', 'wb')
out.write(downfile.read())
out.close()
decrypt_file(key, '../../download/' + filename.split('/')[-1] + '.enc')
os.remove('../../download/' + filename.split('/')[-1] + '.enc')
print 'done'
# print 'metadata 2: ', metadata
def get_music():
    f, _ = client.get_file_and_metadata("/Public/quotes/music.txt")
    return f.read().split("\r\n\r\n")
Exemple #14
0
print '1. Go to: ' + authorize_url
print '2. Click "Allow" (you might have to log in first)'
print '3. Copy the authorization code.'
code = raw_input("Enter the authorization code here: ").strip()

client = dropbox.client.DropboxClient(code)
print 'linked account: ', client.account_info()

#view folders
folder_metadata = client.metadata('/')
print 'metadata: ', folder_metadata

#download first image file
print " "
filelocation = raw_input("Enter file location: (example: /Home/13-15-00.jpg) ") 
f, metadata = client.get_file_and_metadata(filelocation)
print metadata
im = Image.open(f)
im.show()
print client.share(filelocation, short_url = False)
fileurl = client.share(filelocation, short_url = False)
print fileurl.get('url')
lasturl = fileurl.get('url')

#examine first image file
instance = vr(api_key='put your watson ibm api key here', version='2016-05-20')

img = instance.classify(images_url = lasturl)

a = 0
for things in img['images'][0]['classifiers'][0]['classes']:
Exemple #15
0
def get_music():
    f, _ = client.get_file_and_metadata('/Public/quotes/music.txt')
    return f.read().split('\r\n\r\n')
Exemple #16
0
def blog_index_handle(template='index.html', content_type='text/html'):
    log.debug('blog_index_handle()')

    target_file = "posts"

    client = dropbox.client.DropboxClient(CONFIG.DROPBOX_PRIVATE_TOKEN)

    dropbox_response = client.metadata(
        target_file, list=True)

    files = dropbox_response['contents']

    files = sorted(
        files,
        key=lambda f: f['path'],
        reverse=True)
    files = files[:10]

    log.debug(files)

    for f in files:
        log.debug(f['path'])

        file_response, dropbox_meta = client.get_file_and_metadata(
            f['path'])

        f.update(dropbox_meta)
        log.debug(f['path'])

        file_content = file_response.read()

        fdata = process_markdown(
            target_file, file_content)

        log.debug(fdata)

        f.update(fdata)
        log.debug(f['path'])

        # fix title
        f.update(f['meta'])
        if 'Title' in f:
            f['title'] = f['Title']

        # permalink
        f['permalink'] = url_for_path(f['path'])

        log.debug(f)

    # log.debug(files)

    template_response, meta = client.get_file_and_metadata(
        'templates/%s' % template)
    template_content = template_response.read()

    page_content = render_template(template_content, {
            'posts': files,
        })

    resp = make_response(page_content)
    resp.headers["Content-Type"] = content_type

    return resp
Exemple #17
0
		print "File uploaded successfully:", response
		try: 	
			os.remove(f_new_name)
		except Exception, e:
			print e


	if(opt=="D"):
		f_name = raw_input("Enter filename (with file extension):")
		key_opt = raw_input("Use a specific key or the default ? (S/D)")
		if(key_opt=="S"):
			opt1=raw_input("Enter key used to encrypt the file ?")
		else:
			opt1 = "1234567891234567"
		f_new_name="/"+f_name
		f, metadata = client.get_file_and_metadata(f_new_name)
		out = open(f_name, 'w')
		out.write(f.read())
		out.close()
		decrypt_file(opt1,f_name)
		f_new = os.path.splitext(f_name)[0]
		print "File decrypted successfully and saved as "+f_new
		try: 	
			os.remove(f_name)
		except Exception, e:
			print e
	
	if(opt=="E"):
		break
	
	
# Make the user sign in and authorize this token
url = sess.build_authorize_url(request_token)
print "url:", url
print "Please authorize in the browser. After you're done, press enter."
raw_input()

# This will fail if the user didn't visit the above URL and hit 'Allow'
access_token = sess.obtain_access_token(request_token)
TOKENS = 'dropbox_token.txt'
token_file = open(TOKENS,'w')
token_file.write("%s|%s" % (access_token.key,access_token.secret) )
token_file.close()
token_file = open(TOKENS)
token_key,token_secret = token_file.read().split('|')
token_file.close()
sess.set_token(token_key,token_secret)
client = client.DropboxClient(sess)
#stored_creds = open(CONF_DIR + self.TOKEN_FILE).read()
uploading of a file
f = open('today.txt')
response = client.put_file('/magnum_opus.txt', f)
#listing of the files
folder_metadata = client.metadata('/')

#downloading the files
f, metadata = client.get_file_and_metadata('/magnum_opus.txt')
out = open('magnum_opus.txt', 'wb')
out.write(f.read())
out.close()

def download(file_name):
    f, metadata = client.get_file_and_metadata(file_name).read()
    out = open(file_name, 'w')
    out.write(f.read())
    print(metadata)
    return true