Jake.codes

Dropbox Download, for Pythonista

Tuesday, February 25, 2014

Requires dropboxlogin.py by Omz

DropboxDownload works as a Hazel organizer for downloading files to Dropbox from your iOS device. Supports file urls and iOS’s Open In method of sending over files. More info at Jake.ch

# DropboxDownload.py for Pythonista
# by Jake Bilbrey
# Download and organize files in Dropbox automatically
#
# Bookmarklet:
# javascript:location.href='pythonista://DropboxDownload?action=run&argv='+encodeURIComponent(location.href);
import dropboxlogin
import requests
import re
import string
import urllib
import urllib2
import sys
import clipboard

defaultFolder = '/'
extensions = [
    ('avi', '/Shared/Convert Video/'),
    ('aax', '/Shared/Send to iTunes/'),
    ('aif', '/Shared/Send to iTunes/'),
    ('epub', '/Shared/Send to iTunes/'),
    ('ibooks', '/Shared/Send to iTunes/'),
    ('ipa', '/Shared/Send to iTunes/'),
    ('ipg', '/Shared/Send to iTunes/'),
    ('m4a', '/Shared/Send to iTunes/'),
    ('m4b', '/Shared/Send to iTunes/'),
    ('m4p', '/Shared/Send to iTunes/'),
    ('m4r', '/Shared/Send to iTunes/'),
    ('m4v', '/Shared/Send to iTunes/'),
    ('mov', '/Shared/Send to iTunes/'),
    ('mp3', '/Shared/Send to iTunes/'),
    ('mp4', '/Shared/Send to iTunes/'),
    ('pdf', '/Shared/Send to iTunes/'),
    ('wav', '/Shared/Send to iTunes/'),
    ('mobi', '/Shared/Send to Kindle/')
]

def cleanURL(redirect):
    # CleanURL by Viticci
    # https://gist.github.com/viticci/8586671
    # Regex Provided by Peter Hansen on StackOverflow:
    # http://stackoverflow.com/questions/1986059/grubers-url-regular-expression-in-python/1986151#1986151
    pat = r'\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^%s\s]|/)))'
    pat = pat % re.escape(string.punctuation)
    match = re.findall(pat, redirect)
    if match:
        for x in match:
            # Get the first match without redirects
            cleaned = urllib.urlopen(x[0]).geturl()
            # Get direct image link
            cleaned = cleaned.replace('www.dropbox.com', 'dl.dropboxusercontent.com')
            return cleaned
    elif not match:
        return 'No match found'

def sortFile(theFile):
    for ext, path in extensions:
        service_identifier = '.%s' % ext
        if not re.search(".torrent$", theFile):
            continue
        theFile = path + theFile
        break
    else:
        if re.search("\\.\\w+?$", theFile):
            theFile = defaultFolder + theFile
        else:
            theFile = defaultFolder + theFile + '.html'
    return theFile

def urlToDropbox(url):
    # mostly from ccc
    # http://omz-forums.appspot.com/pythonista/post/5041916064301056
    if url.startswith('/private/var'):
        file_url = url
        file_content = open(url, 'r')
    else:
        file_url = cleanURL(url)
        file_content = requests.get(file_url).content
    
    file_name = urllib2.unquote(file_url)
    file_name = re.search("[\s\w_.-]*?(?=[\?\#])|[\s\w_.-]*$", file_name).group(0)
    dropbox_client = dropboxlogin.get_client()
    response = dropbox_client.put_file(sortFile(file_name), file_content)

numArgs = len(sys.argv)
if numArgs == 1:
    urlToDropbox(clipboard.get())
elif numArgs == 2:
    urlToDropbox(sys.argv[1])
elif numArgs == 3:
    urlToDropbox(sys.argv[1])