Skip to content

Instantly share code, notes, and snippets.

@onjin
Last active August 29, 2015 13:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onjin/10270135 to your computer and use it in GitHub Desktop.
Save onjin/10270135 to your computer and use it in GitHub Desktop.
Push and publish on dropbox from shell
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Put and publish given file to dropbox.
Install
-------
1. open https://www.dropbox.com/developers/apps and register app
2. pip install dropbox
3. run droppub.py to create default config file ~/.droppub.cfg
4. edit ~/.droppub.cfg and put valid key/secret
Run
---
At first run you will have to authorize app using given url.
$ droppub.py ./last_facepalm.jpg
https://db.tt/sByvFJiD
"""
import os
import sys
import json
import dropbox
config_file = os.path.join(
os.path.expanduser("~"), '.droppub.cfg'
)
if not os.path.isfile(config_file):
cfg = {"key": "app_key", "secret": "app_secret"}
json.dump(cfg, open(config_file, 'w'))
print("Default config has been created {}".format(config_file))
sys.exit(0)
if len(sys.argv) != 2:
print("Usage: {} path_to_file_to_push_and_publish".format(sys.argv[0]))
sys.exit(0)
settings = json.load(open(config_file, 'r'))
def write_config(settings):
json.dump(settings, open(config_file, 'w'))
def authorize(settings):
flow = dropbox.client.DropboxOAuth2FlowNoRedirect(
settings['key'], settings['secret']
)
authorize_url = flow.start()
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()
# This will fail if the user enters an invalid authorization code
access_token, user_id = flow.finish(code)
settings['access_token'] = access_token
write_config(settings)
if 'access_token' not in settings:
authorize(settings)
client = dropbox.client.DropboxClient(settings['access_token'])
source_file = sys.argv[1]
with open(source_file, 'rb') as f:
dest_file = os.path.basename(source_file)
response = client.put_file(dest_file, f)
response = client.share(response['path'], short_url=True)
print response['url']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment