Skip to content

Instantly share code, notes, and snippets.

@qnub
Created March 25, 2016 09:10
Show Gist options
  • Save qnub/00bc3b8636d7443bf170 to your computer and use it in GitHub Desktop.
Save qnub/00bc3b8636d7443bf170 to your computer and use it in GitHub Desktop.
Yandex.Disk shared file download by link
import os
import httplib
import json
import urllib
import urlparse
from fabric.api import execute, local, run, lcd, cd, task
@task
def download(url, target='./', filename=None, locally=True):
if filename:
command = 'wget -O "%s" "%s"' % (os.path.join(target, filename), url)
else:
command = 'wget "%s"' % url
if locally:
with lcd(target):
local(command)
else:
with cd(target):
run(command)
@task
def disk_download(url, target='./', locally=True):
api = httplib.HTTPSConnection('cloud-api.yandex.net')
url ='/v1/disk/public/resources/download?public_key=%s' % urllib.quote(url)
api.request('GET', url)
resp = api.getresponse()
file_info = json.loads(resp.read())
api.close()
if resp.status == 200:
filename = urlparse.parse_qs(urlparse.urlparse(file_info['href']).query)['filename'][0]
execute('download', file_info['href'], target, filename, locally)
else:
print resp.status, resp.reason
print file_info['error'], '\n', file_info['description']
@task
def disk_downl(url, target='./'):
'''
Download locally
'''
execute('disk_download', url, target, True)
@task
def disk_down(url, target='./'):
'''
Download to remote host (won't forget set host)
'''
execute('disk_download', url, target, False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment