Last active
August 29, 2015 14:02
-
-
Save lavie/567c9dc6b61667da29d8 to your computer and use it in GitHub Desktop.
SnagURL uploads any file to S3 or imgur and puts a HTTP link to it in the clipboard
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
""" | |
Dependecies: | |
1. Install BOTO ("pip install boto") | |
2. Set the values for S3_BUCKET, BUCKET_WEB_PREFIX | |
3. Set the Amazon IAM access keys | |
""" | |
import sys | |
import subprocess | |
from os.path import splitext | |
from time import time | |
import os | |
import urllib2 | |
from json import loads | |
from urllib import urlencode | |
from Tkinter import Tk | |
import base64 | |
print sys.argv | |
import uuid | |
import boto | |
from datetime import datetime | |
LOG_FILE = 'uploads.log' | |
# You need to create the bucket first, and set its security policy to allow web access to all | |
S3_BUCKET = 'your_bucket_name_goes_here' | |
# This is the HTTP endpoint of your bucket | |
BUCKET_WEB_PREFIX = 'http://grab.this.from.your.s3.portal' | |
AWS_ACCESS_KEY_ID = '' | |
AWS_SECRET_ACCESS_KEY = '' | |
IMGUR_KEY = '' | |
def add_to_clipboard(str): | |
r = Tk() | |
r.withdraw() | |
r.clipboard_clear() | |
r.clipboard_append(str) | |
r.destroy() | |
def upload_imgur(local): | |
img = open(local, "rb").read() | |
b64 = base64.encodestring(img) | |
print "\n\nTitle: " | |
title = raw_input() | |
if not title: | |
print "Uploading to imgur. Clipboard will contain direct link." | |
else: | |
print "Uploading to imgur. Clipboard will contain link to imgur page." | |
data = urlencode({ | |
"image" : b64, | |
"key" : IMGUR_KEY, | |
"title" : title if title else "", | |
"description" : title if title else "", | |
}) | |
request = urllib2.Request("http://api.imgur.com/2/upload.json", data) | |
try: | |
response = urllib2.urlopen(request).read() | |
json = loads(response) | |
remote = '' | |
if title: | |
remote = json["upload"]["links"]["imgur_page"] | |
else: | |
remote = json["upload"]["links"]["original"] | |
return remote | |
except urllib2.HTTPError, e: | |
print "HTTP Error %d: %s" % (e.code, e.reason) | |
def upload_s3(local): | |
(root, ext) = splitext(local) | |
now = datetime.now() | |
t = time() | |
path_part = "u/%d/%02d/%d%s" % (now.year, now.month, t, ext) | |
remote_s3 = "s3://" + S3_BUCKET + "/" + path_part | |
conn = boto.connect_s3(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) | |
from boto.s3.bucket import Bucket | |
b = Bucket(conn, S3_BUCKET) | |
from boto.s3.key import Key | |
k = Key(b, path_part) | |
k.set_contents_from_filename(local) | |
remote_https = "%s/u/%d/%02d/%d%s" % (BUCKET_WEB_PREFIX, now.year, now.month, t, ext) | |
return remote_https | |
def main(): | |
local = None | |
if len(sys.argv) < 2: | |
print """ | |
Usage: | |
snagurl.py <local-file> | |
""" | |
return 1 | |
local = sys.argv[1] | |
useImgur = (len(sys.argv) > 2) and (sys.argv[2] == "imgur") | |
print "Processing %s" % local | |
if useImgur: | |
remote = upload_imgur(local) | |
else: | |
remote = upload_s3(local) | |
print "Uploaded to: %s" % remote | |
add_to_clipboard(remote) | |
f = open(LOG_FILE, 'a') | |
f.write("%s -> %s\n" % (local, remote)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment