-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwget.py
35 lines (26 loc) · 776 Bytes
/
wget.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# -*- coding: utf-8 -*-
import os
import requests
import shutil
class Downloadable(object):
def __init__(self, url):
self.url = url
def __repr__(self):
return 'OPUS API downloadable file:\n -> {}'.format(str(self))
def __str__(self):
return self.url
def download(self, out=None):
outdir = None
if out and os.path.isdir(out):
outdir = out
out = None
if out is None:
filename = self.url.split('/')[-1]
else:
filename = out
if outdir:
filename = os.path.join(outdir, filename)
r = requests.get(self.url, stream=True)
with open(filename, 'wb') as f:
shutil.copyfileobj(r.raw, f)
return filename