Created
January 19, 2014 14:49
-
-
Save anonymous/8505899 to your computer and use it in GitHub Desktop.
Archive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PLEASE READ THESE FEW LINES | |
## You can contact at @theknowsbest on Twitter if you need help (though admittedly, I'm far from an expert on this stuff) | |
## Before you can use this script, you need to do a few things... | |
## 1. Create a Dropbox app for Python through 'http://dropbox.com/developers/apps' | |
## 2. Run the first script on this site 'http://omz-software.com/pythonista/docs/ios/dropbox.html' | |
## Make sure to insert your app key and secret, | |
## and follow the instructions you're given in the console slavishly | |
## 3. Download this 'https://gist.github.com/4034526' script and name it 'dropboxlogin.py' | |
## 4. Run 'dropboxlogin.py' and authenticate your app through | |
## | |
## To use this script you need to do the following: | |
## 1. Open Pythonista’s settings | |
## 2. Tap "'Open In…' Menu" | |
## 3. Choose this file | |
## 4. Exit Pythonista | |
## 5. Send any kind of file from any app to Pythonista, and this magic will commence automagically | |
## 6. Squeal in joy - SQUEAL!!! | |
## | |
## Well, not entirely done yet, as you might need to tinker with quite a lot in this script for it to become perfect | |
import os, sys, datetime, string, dropboxlogin, notification, console, unicodedata, clipboard, webbrowser | |
from os.path import basename | |
# Prepare the console, so things look pretty | |
## Pretty is nice | |
console.clear() | |
# Get name and path of incoming file | |
filename_unprepared = sys.argv[1] | |
# Strip away the path | |
filename = basename(filename_unprepared) | |
# Get all necessary date and time information | |
date = datetime.datetime.now().day | |
month = datetime.datetime.now().month | |
year = datetime.datetime.now().year | |
mydate = datetime.datetime.now() | |
month_name_temp = mydate.strftime("%B") | |
month_name = string.capwords(month_name_temp) | |
# Change month name to local language | |
## Uncomment and change 'real_name' to your own language | |
## add as many months as you like! | |
## If you use this: substitute 'month_name' for 'real_month' in the definition of 'newfile' | |
# | |
# if 'January' in month_name: | |
# real_month = 'Januar' | |
# elif 'February' in month_name: | |
# real_month = 'Februar' | |
# else: | |
# real_month = month_name | |
# Separate name from extension | |
extension = filename[filename.find('.'):] | |
# Find filenamecore (name sans extension *and* path) | |
core = filename[0:filename.find("%s" % extension)] | |
clipboard.set(core) | |
# Ask for new filename | |
i = console.input_alert('Name your file', 'Category followed by title', '', 'Proceed') | |
if i.count('') < 2: | |
console.hud_alert('Archives with original name', 'success') | |
base = core.lower() | |
# Choose Proceed to just use the name iOS has assigned to the file | |
else: | |
string = unicode(i, 'utf8') | |
base = string.encode('utf8', 'replace') | |
# Categorisation | |
## Substitute keywords for categories from input with searchable filename-prefixes | |
## (Mini-version of TextExpander) | |
## for example: type 'bill energy' in the preceding prompt | |
## get this output as 'full_name': 'finance_invoice - energy' | |
## thus, the file will be easy to find with search on iOS and Mac or whatever else you may prefer | |
## | |
## Example code (append to code beneath): | |
## | |
## elif 'bill ' in base: | |
## def bill(): | |
## cut = "bill " | |
## new_base = re.sub(cut, '', base) | |
## name = "finance_invoice- '%s'" % new_base | |
## return name | |
## full_name = category1() | |
if 'category1 ' in base: | |
def category1(): | |
cut = "category1 " | |
new_base = re.sub(cut, '', base) | |
name = "category1- '%s'" % new_base | |
return name | |
full_name = category1() | |
elif 'category2 ' in base: | |
def category2(): | |
cut = "category2 " | |
new_base = re.sub(cut, '', base) | |
name = "category2- '%s'" % new_base | |
return name | |
full_name = category2() | |
else: | |
full_name = base | |
# If you don't want any of this; replace all of it with 'full_name = base' | |
# Why wouldn't you want this?! | |
# Combine new destination and filename | |
## Example result: "/myfolder/01 - January/2014-01-19 - 'filename'.ext" | |
## The script will create a folder if it doesn't exist | |
## and do change 'myfolder' to something meaningful! | |
newfile = "/myfolder/%02d/%02d - %s/%02d-%02d-%02d - %s%s" % (year, month, month_name, year, month, date, full_name, extension) | |
# Find and temporarily open the local file | |
file_location = 'Inbox/%s' % filename | |
# Open the file | |
f = open(file_location) | |
# Send it up to the almighty clouds | |
dropbox_client = dropboxlogin.get_client() | |
response = dropbox_client.put_file(newfile, f) | |
# YES! Success! | |
success = console.hud_alert('"%s" was archived in Dropbox' % full_name, 'success') | |
# Now find its link and copy it | |
## In case you want to share it | |
get_share = dropbox_client.share(newfile) | |
get_link = get_share.get('url') | |
clipboard.set(get_link) | |
# Clean up the mess | |
## comment out the next line if you encounter an error and need to troubleshoot | |
console.clear() | |
## Closes and deletes the file from Pythonista - we don't need an archived file anymore, do we? | |
f.close() | |
s = os.remove(file_location) | |
# Open the file in the browser to verify that everything went well | |
webbrowser.open('safari-%s' % get_link) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A few things to consider:
Consider using len(i) instead of i.count('').
Fully leverage the strftime() call:
Month names in strftime('%B') are already localized to the local language by Python's Standard Library.