diff --git a/coursera/api.py b/coursera/api.py index 0c803ac..be7d9e6 100644 --- a/coursera/api.py +++ b/coursera/api.py @@ -14,7 +14,7 @@ from six.moves.urllib_parse import quote_plus from .utils import (BeautifulSoup, make_coursera_absolute_url, extend_supplement_links, clean_url, clean_filename) -from .network import get_page, get_page_json, post_page_and_reply, post_page_json +from .network import get_reply, get_page, get_page_json, post_page_and_reply, post_page_json from .define import (OPENCOURSE_SUPPLEMENT_URL, OPENCOURSE_PROGRAMMING_ASSIGNMENTS_URL, OPENCOURSE_ASSET_URL, @@ -279,7 +279,7 @@ class Asset(namedtuple('Asset', 'id name type_name url data')): """ __slots__ = () def __repr__(self): - return 'Asset(id=%s, name=%s, type_name=%s, url=%s, data="<...>")' % ( + return 'Asset(id="%s", name="%s", type_name="%s", url="%s", data="<...>")' % ( self.id, self.name, self.type_name, self.url) @@ -305,13 +305,13 @@ class AssetRetrievier(object): asset_dict = asset_map[asset_id] url = asset_dict['url']['url'].strip() - request = self._session.get(url) - if request.status_code == 200: + reply = get_reply(self._session, url) + if reply.status_code == 200: result.append(Asset(id=asset_dict['id'].strip(), name=asset_dict['name'].strip(), type_name=asset_dict['typeName'].strip(), url=url, - data=request.content)) + data=reply.content)) return result diff --git a/coursera/test/fixtures/json/asset-retriever/assets-reply.json b/coursera/test/fixtures/json/asset-retriever/assets-reply.json new file mode 100644 index 0000000..0015caf --- /dev/null +++ b/coursera/test/fixtures/json/asset-retriever/assets-reply.json @@ -0,0 +1,69 @@ +{ + "elements": [ + { + "id": "bYHvf8YwEeWFNA5XwZEiOw", + "name": "M114.mp3", + "typeName": "audio", + "url": { + "url": "url1", + "expires": 1469664000000 + } + }, + { + "id": "VcmGXShKEea4ehL5RXz3EQ", + "name": "09_graph_decomposition_starter_files_1.zip", + "typeName": "generic", + "url": { + "url": "url2", + "expires": 1469664000000 + } + }, + { + "id": "bX9X18YwEeW7AxLLCrgDQQ", + "name": "M113.mp3", + "typeName": "audio", + "url": { + "url": "url3", + "expires": 1469664000000 + } + }, + { + "id": "bWTK9sYwEeW7AxLLCrgDQQ", + "name": "M111.mp3", + "typeName": "audio", + "url": { + "url": "url4", + "expires": 1469664000000 + } + }, + { + "id": "tZmigMYxEeWFNA5XwZEiOw", + "name": "M115.mp3", + "typeName": "audio", + "url": { + "url": "url5", + "expires": 1469664000000 + } + }, + { + "id": "bXCx18YwEeWicwr5JH8fgw", + "name": "M112.mp3", + "typeName": "audio", + "url": { + "url": "url6", + "expires": 1469664000000 + } + }, + { + "id": "VceKeChKEeaOMw70NkE3iw", + "name": "09_graph_decomposition_problems_1.pdf", + "typeName": "pdf", + "url": { + "url": "url7", + "expires": 1469664000000 + } + } + ], + "paging": null, + "linked": null +} diff --git a/coursera/test/test_api.py b/coursera/test/test_api.py index b178b64..21e8ac1 100644 --- a/coursera/test/test_api.py +++ b/coursera/test/test_api.py @@ -5,7 +5,7 @@ from os.path import expanduser import json import pytest -from mock import patch +from mock import patch, Mock from coursera import api from coursera import define @@ -298,7 +298,39 @@ def create_session(): login(session, username, password) return session -def test_asset_retriever(): + +@patch('coursera.api.get_page_json') +@patch('coursera.api.get_reply') +def test_asset_retriever(get_reply, get_page_json): + reply = json.loads(slurp_fixture('json/asset-retriever/assets-reply.json')) + get_page_json.side_effect = [reply] + get_reply.side_effect = [Mock(status_code=200, content='<...>')] * 7 + + asset_ids = ['bWTK9sYwEeW7AxLLCrgDQQ', + 'bXCx18YwEeWicwr5JH8fgw', + 'bX9X18YwEeW7AxLLCrgDQQ', + 'bYHvf8YwEeWFNA5XwZEiOw', + 'tZmigMYxEeWFNA5XwZEiOw', + 'VceKeChKEeaOMw70NkE3iw', + 'VcmGXShKEea4ehL5RXz3EQ'] + + expected_output = [ + api.Asset(id="bWTK9sYwEeW7AxLLCrgDQQ", name="M111.mp3", type_name="audio", url="url4", data="<...>"), + api.Asset(id="bXCx18YwEeWicwr5JH8fgw", name="M112.mp3", type_name="audio", url="url6", data="<...>"), + api.Asset(id="bX9X18YwEeW7AxLLCrgDQQ", name="M113.mp3", type_name="audio", url="url3", data="<...>"), + api.Asset(id="bYHvf8YwEeWFNA5XwZEiOw", name="M114.mp3", type_name="audio", url="url1", data="<...>"), + api.Asset(id="tZmigMYxEeWFNA5XwZEiOw", name="M115.mp3", type_name="audio", url="url5", data="<...>"), + api.Asset(id="VceKeChKEeaOMw70NkE3iw", name="09_graph_decomposition_problems_1.pdf", type_name="pdf", url="url7", data="<...>"), + api.Asset(id="VcmGXShKEea4ehL5RXz3EQ", name="09_graph_decomposition_starter_files_1.zip", type_name="generic", url="url2", data="<...>") + ] + + retriever = api.AssetRetrievier(session=None) + actual_output = retriever(asset_ids) + + assert expected_output == actual_output + + +def old_test_asset_retriever(): asset_ids = ['bWTK9sYwEeW7AxLLCrgDQQ', 'bXCx18YwEeWicwr5JH8fgw', 'bX9X18YwEeW7AxLLCrgDQQ', @@ -316,5 +348,3 @@ def test_asset_retriever(): assets = retriever(more) print(assets) - from ipdb import set_trace; set_trace() - print(assets)