Skip to content

Commit 5d0e9e3

Browse files
committed
Add download for image previews
1 parent 04f88bc commit 5d0e9e3

5 files changed

Lines changed: 76 additions & 9 deletions

File tree

opus/images.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# -*- coding: utf-8 -*-
22

3+
from .wget import download
4+
35
class Image(object):
46
def __init__(self, ring_obs_id, path, img):
57
self.ring_obs_id = ring_obs_id
@@ -16,6 +18,8 @@ def __str__(self):
1618
def url(self):
1719
return self.path + self.img
1820

21+
def download(self, out=None):
22+
download(self.url, out)
1923

2024
class Images(object):
2125
def __init__(self, json, size):

opus/wget.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# -*- coding: utf-8 -*-
2+
import os
3+
import requests
4+
import shutil
5+
6+
def download(url, out=None):
7+
outdir = None
8+
if out and os.path.isdir(out):
9+
outdir = out
10+
out = None
11+
12+
if out is None:
13+
filename = url.split('/')[-1]
14+
else:
15+
filename = out
16+
17+
if outdir:
18+
filename = os.path.join(outdir, filename)
19+
20+
r = requests.get(url, stream=True)
21+
with open(filename, 'wb') as f:
22+
shutil.copyfileobj(r.raw, f)
23+
24+
return filename

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
wget
21
requests
32
six
43
argparse
34.3 KB
Loading

tests/test_images.py

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
# -*- coding: utf-8 -*-
22
import pytest
3+
import os
34
import responses
45
import json as JSON
56

67
from opus.images import *
78

8-
99
@pytest.fixture
1010
def size():
1111
return 'med'
1212

13-
14-
@pytest.fixture
15-
def json(size):
16-
return JSON.loads(open('tests/api/images/{}.json'.format(size), 'r').read())
17-
18-
1913
@pytest.fixture
20-
def images(json, size):
14+
def images(size):
15+
json = JSON.loads(open('tests/api/images/{}.json'.format(size), 'r').read())
2116
return Images(json, size)
2217

18+
@pytest.fixture
19+
def image(size):
20+
ring_obs_id = 'S_IMG_CO_ISS_1459551972_N'
21+
json = JSON.loads(open('tests/api/image/'+size+'/'+ring_obs_id+'.json', 'r').read())
22+
return Image(ring_obs_id, json['path'], json['data'][0]['img'])
2323

2424
def test_images_meta(images, size):
2525
assert repr(images) == 'OPUS API Images object (with 10 images)'
@@ -42,3 +42,43 @@ def test_images_iter(images):
4242
images.next() == images[0]
4343
for ii, img in enumerate(images):
4444
img = images[ii]
45+
46+
47+
@responses.activate
48+
def test_image_download(image):
49+
fname = os.path.basename(image.url)
50+
51+
with open('tests/api/image/med/'+fname, 'rb') as img:
52+
responses.add(responses.GET, image.url,
53+
body=img.read(), status=200,
54+
content_type='image/jpeg',
55+
stream=True
56+
)
57+
58+
image.download(out='tests/')
59+
60+
assert len(responses.calls) == 1
61+
assert responses.calls[0].request.url == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
62+
63+
assert os.path.isfile('tests/'+fname)
64+
os.remove('tests/'+fname)
65+
66+
67+
@responses.activate
68+
def test_image_download_output(image):
69+
fname = os.path.basename(image.url)
70+
71+
with open('tests/api/image/med/'+fname, 'rb') as img:
72+
responses.add(responses.GET, image.url,
73+
body=img.read(), status=200,
74+
content_type='image/jpeg',
75+
stream=True
76+
)
77+
78+
image.download(out='tests/test.jpg')
79+
80+
assert len(responses.calls) == 1
81+
assert responses.calls[0].request.url == 'https://pds-rings.seti.org/holdings/previews/COISS_2xxx/COISS_2001/data/1459551663_1459568594/N1459551972_1_med.jpg'
82+
83+
assert os.path.isfile('tests/test.jpg')
84+
os.remove('tests/test.jpg')

0 commit comments

Comments
 (0)