From 7017c1518f831515f331c715dcd5f5d24a1877f9 Mon Sep 17 00:00:00 2001 From: zevav Date: Sun, 17 Feb 2019 17:20:15 -0500 Subject: [PATCH] first --- .gitignore | 4 ++ README.md | 19 +++++++++ quickdrop/__init__.py | 0 quickdrop/quickdrop.py | 88 ++++++++++++++++++++++++++++++++++++++++++ setup.py | 28 ++++++++++++++ 5 files changed, 139 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100644 quickdrop/__init__.py create mode 100644 quickdrop/quickdrop.py create mode 100644 setup.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..84ee5e6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +dist/ +build/ +Pipfile* +*.egg-info/ diff --git a/README.md b/README.md new file mode 100644 index 0000000..b6356c6 --- /dev/null +++ b/README.md @@ -0,0 +1,19 @@ +# QuickDrop + +This is a simple CLI for quickly sharing a file or folder via Dropbox +that's already located in a Dropbox folder. + +It's as simple as + + $ export DROPBOX_ACCESS_TOKEN=yourdropboxaccesstoken + $ export DROPBOX_ROOT_PATH=yourdropboxrootpath + $ pip install quickdrop + + Collecting quickdrop + ... + Successfully installed quickdrop-x.y.z + + $ url + + Okay, is now shared, accessible via + https://www.dropbox.com/sh/bunchofrandomchars/morerandomcharsnstuff?dl=0. diff --git a/quickdrop/__init__.py b/quickdrop/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/quickdrop/quickdrop.py b/quickdrop/quickdrop.py new file mode 100644 index 0000000..be95d83 --- /dev/null +++ b/quickdrop/quickdrop.py @@ -0,0 +1,88 @@ +import os +from pathlib import Path +import sys + +import click +import dropbox +import pyperclip + + +DROPBOX_ACCESS_TOKEN = os.getenv('DROPBOX_ACCESS_TOKEN') +DROPBOX_ROOT_PATH = os.getenv('DROPBOX_ROOT_PATH') +LB = '\n' + + +@click.command() +@click.argument('filepath', type=click.Path(exists=True)) +def cli(filepath): + check_for_env_vars() + dropbox_relative_path = get_relative_path(filepath) + url = share_file(dropbox_relative_path) + copy_to_clipboard(url) + print(f'Okay, {filepath} is now shared, accessible via {LB}{url}.') + print('This url was also copied to your clipboard for your convenience.') + + +def share_file(filepath): + try: + shared_link = get_client().sharing_create_shared_link(filepath) + except dropbox.exceptions.ApiError as e: + raise click.ClickException('There was a problem with the path.') + else: + return shared_link.url + + +def get_relative_path(filepath): + DROPBOX_ROOT = Path(DROPBOX_ROOT_PATH).expanduser() + + if '/' not in filepath: + filepath = f'/{filepath}' + + elif not filepath.startswith('/') and not filepath.startswith('~'): + *path_parts, filename = filepath.split('/') + relevant_path_parts = [] + for path_part in path_parts: + if path_part not in DROPBOX_ROOT_PATH: + relevant_path_parts.append(path_part) + filepath = os.path.join(*relevant_path_parts, f'/{filename}') + + filepath_expanded_user = Path(filepath).expanduser() + + path = Path(str(filepath_expanded_user).replace(str(DROPBOX_ROOT), '')) + + return str(path) + + +def check_for_valid_access_token(): + if not DROPBOX_ACCESS_TOKEN: + raise click.ClickException( + 'Please get an access token here and store it in an environment ' + 'variable called "DROPBOX_ACCESS_TOKEN": ' + ' https://www.dropbox.com/developers/apps') + try: + dbx = get_client() + dbx.users_get_current_account() + except dropbox.exceptions.AuthError as e: + raise click.ClickException(str(e)) + + +def check_for_env_vars(): + check_for_valid_access_token() + check_for_dropbox_root_path() + + +def check_for_dropbox_root_path(): + if not DROPBOX_ROOT_PATH: + raise click.ClickException( + 'Please create an environment variable called "DROPBOX_ROOT_PATH" ' + 'with the path to your computer\'s root Dropbox folder.') + if not Path(DROPBOX_ROOT_PATH).exists: + raise click.ClickException(f'{DROPBOX_ROOT_PATH} doesn\'t exist!') + + +def get_client(): + return dropbox.Dropbox(DROPBOX_ACCESS_TOKEN) + + +def copy_to_clipboard(url): + pyperclip.copy(url) diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..85a00ed --- /dev/null +++ b/setup.py @@ -0,0 +1,28 @@ +from setuptools import setup, find_packages + + +with open('README.md') as file: + long_description = file.read() + +setup( + name="quickdrop", + version="0.01", + py_modules=['quickdrop'], + url='https://github.com/zevaverbach/quickdrop', + install_requires=[ + 'dropbox', + 'Click', + 'pyperclip', + ], + include_package_data=True, + packages=find_packages(), + description=( + 'Quickly get a shared link for any file or folder in a Dropbox-synced ' + 'folder'), + long_description_content_type='text/markdown', + long_description=long_description, + entry_points=''' + [console_scripts] + url=quickdrop.quickdrop:cli + ''', + )