blob: f4d94cccb43637ad19a6a1069732779dfc8d0fe5 [file] [log] [blame]
Ilia Sergachevabfd60b2018-04-04 13:27:48 +02001#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
Ilia Sergachev51463352018-04-04 13:18:35 +02004import shutil
5import pathlib
6import os
7import argparse
8import hashlib
9import pprint
10import requests
11
Ilia Sergachevd7d23e62018-06-03 22:10:31 +020012API_ENDPOINT = 'https://cloud-api.yandex.net/v1/disk/public/resources/?public_key={}&path=/{}'
Ilia Sergachev51463352018-04-04 13:18:35 +020013
14
15def md5sum(filename):
16 md5 = hashlib.md5()
17 with open(filename, 'rb') as f:
18 for chunk in iter(lambda: f.read(128 * md5.block_size), b''):
19 md5.update(chunk)
20 return md5.hexdigest()
21
22
23def check_and_download_file(url, path, size, checksum, dry):
24 if os.path.isfile(path):
25 if size == os.path.getsize(path):
26 if checksum == md5sum(path):
27 print('skipping correctly downloaded file {}'.format(path))
28 return
29 if not dry:
30 print('downloading {}'.format(path))
31 r = requests.get(url, stream=True)
32 with open(path, 'wb') as f:
33 shutil.copyfileobj(r.raw, f)
34
35
Ilia Sergachevd7d23e62018-06-03 22:10:31 +020036def download_directory(dl_root_path, public_key, src_path, dry):
37 cur_path = os.path.join(dl_root_path, src_path)
38 pathlib.Path(cur_path).mkdir(parents=True, exist_ok=True)
39 jsn = requests.get(API_ENDPOINT.format(public_key, src_path)).json()
40 try:
41 items = jsn['_embedded']['items']
42 except KeyError:
43 pprint.pprint(jsn)
44 return
Ilia Sergachev51463352018-04-04 13:18:35 +020045 for i in items:
Ilia Sergachev51463352018-04-04 13:18:35 +020046 if 'file' in i:
Ilia Sergachevd7d23e62018-06-03 22:10:31 +020047 file_save_path = os.path.join(cur_path, i['name'])
48 check_and_download_file(i['file'], file_save_path, i['size'], i['md5'], dry)
Ilia Sergachev51463352018-04-04 13:18:35 +020049 else:
Ilia Sergachevd7d23e62018-06-03 22:10:31 +020050 subdir_path = os.path.join(src_path, i['name'])
51 print('entering folder {}'.format(subdir_path))
52 download_directory(dl_root_path, public_key, subdir_path, dry)
Ilia Sergachev51463352018-04-04 13:18:35 +020053
54
55parser = argparse.ArgumentParser(description='Yandex.Disk downloader.')
56parser.add_argument('url')
57parser.add_argument('-o', dest='output_path', default='output')
58parser.add_argument('--dry', action='store_const', const=True, default=False)
59args = parser.parse_args()
60
Ilia Sergachevd7d23e62018-06-03 22:10:31 +020061download_directory(args.output_path, args.url, '', args.dry)