#!/usr/bin/env python
# NukeFS: simplest shortener-based file cloud storage for Python 2.6+
# by Multiversum
# Note: it gets damn slow on large files so plz don't upload >1M files
from requests import get
from json import dumps, loads
from urllib import quote_plus, unquote_plus
CHUNKLEN=22000 #current TinyURL effective chunk length limitation
def upload(localpath):
'Upload a file from localpath to cloud and get its access key'
filekeys = []
baseurl = 'http://tinyurl.com/api-create.php?url='
with open(localpath, 'rb') as fileobj:
while 1:
data = fileobj.read(CHUNKLEN)
if not data: break
encoded = quote_plus(''.join(data.encode('base64').splitlines()))
key = get(baseurl+encoded).text[19:]
filekeys.append(key)
fileobj.close()
return get(baseurl+quote_plus(dumps(filekeys))).text[19:]
def download(filekey, localpath):
'Download a file to localpath by its access key'
filekeys = loads(unquote_plus(get('http://tinyurl.com/'+filekey, allow_redirects=False).headers['location']))
with open(localpath, 'wb') as fileobj:
for key in filekeys:
data = get('http://tinyurl.com/'+key, allow_redirects=False).headers['location']
if not data: break
fileobj.write(unquote_plus(data).decode('base64'))
fileobj.close()
if __name__ == '__main__': #usage example
megakey = upload('uploaded.zip')
print 'File access key: ', megakey
download(megakey, 'downloaded.zip')
print 'Downloaded.'