From de2ba5bdce577823dc7ccb3bccdcc76d266e7f39 Mon Sep 17 00:00:00 2001 From: Yuri Bochkarev Date: Sat, 9 Jun 2018 23:06:23 +0300 Subject: [PATCH] Add comment about split('/') in _get_notebook_folder --- coursera/api.py | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) diff --git a/coursera/api.py b/coursera/api.py index e9c5000..330b560 100644 --- a/coursera/api.py +++ b/coursera/api.py @@ -482,22 +482,33 @@ class CourseraOnDemand(object): for content in reply['content']: if content['type'] == 'directory': - a = self._get_notebook_folder(OPENCOURSE_NOTEBOOK_TREE, jupyterId, jupId=jupyterId, path=content['path'], timestamp=int(time.time())) + a = self._get_notebook_folder( + OPENCOURSE_NOTEBOOK_TREE, jupyterId, jupId=jupyterId, path=content['path'], timestamp=int(time.time())) supplement_links.update(a) elif content['type'] == 'file': - tmpUrl = OPENCOURSE_NOTEBOOK_DOWNLOAD.format(path=content['path'], jupId=jupyterId, timestamp=int(time.time())) + tmpUrl = OPENCOURSE_NOTEBOOK_DOWNLOAD.format( + path=content['path'], jupId=jupyterId, timestamp=int(time.time())) filename, extension = os.path.splitext(clean_url(tmpUrl)) head, tail = os.path.split(content['path']) - head = '/'.join([clean_filename(dir, minimal_change=True) for dir in head.split('/')]) + # '/' in the following line is for a reason: + # @noureddin says: "I split head using split('/') not + # os.path.split() because it's seems to me that it comes from a + # web page, so the separator will always be /, so using the + # native path splitting function is not the most portable way to + # do it." + # Original pull request: https://github.com/coursera-dl/coursera-dl/pull/654 + head = '/'.join([clean_filename(dir, minimal_change=True) + for dir in head.split('/')]) tail = clean_filename(tail, minimal_change=True) if os.path.isdir(self._course_name + "/notebook/" + head + "/") == False: logging.info('Creating [{}] directories...'.format(head)) os.makedirs(self._course_name + "/notebook/" + head + "/") - r = requests.get(tmpUrl.replace(" ", "%20"), cookies=self._session.cookies) + r = requests.get(tmpUrl.replace(" ", "%20"), + cookies=self._session.cookies) if os.path.exists(self._course_name + "/notebook/" + head + "/" + tail) == False: logging.info('Downloading {} into {}'.format(tail, head)) with open(self._course_name + "/notebook/" + head + "/" + tail, 'wb+') as f: @@ -505,14 +516,15 @@ class CourseraOnDemand(object): else: logging.info('Skipping {}... (file exists)'.format(tail)) - if not str(extension[1:]) in supplement_links: supplement_links[str(extension[1:])] = [] - supplement_links[str(extension[1:])].append((tmpUrl.replace(" ", "%20"), filename)) + supplement_links[str(extension[1:])].append( + (tmpUrl.replace(" ", "%20"), filename)) elif content['type'] == 'notebook': - tmpUrl = OPENCOURSE_NOTEBOOK_DOWNLOAD.format(path=content['path'], jupId=jupyterId, timestamp=int(time.time())) + tmpUrl = OPENCOURSE_NOTEBOOK_DOWNLOAD.format( + path=content['path'], jupId=jupyterId, timestamp=int(time.time())) filename, extension = os.path.splitext(clean_url(tmpUrl)) head, tail = os.path.split(content['path']) @@ -521,9 +533,11 @@ class CourseraOnDemand(object): logging.info('Creating [{}] directories...'.format(head)) os.makedirs(self._course_name + "/notebook/" + head + "/") - r = requests.get(tmpUrl.replace(" ", "%20"), cookies=self._session.cookies) + r = requests.get(tmpUrl.replace(" ", "%20"), + cookies=self._session.cookies) if os.path.exists(self._course_name + "/notebook/" + head + "/" + tail) == False: - logging.info('Downloading Jupyter {} into {}'.format(tail, head)) + logging.info( + 'Downloading Jupyter {} into {}'.format(tail, head)) with open(self._course_name + "/notebook/" + head + "/" + tail, 'wb+') as f: f.write(r.content) else: @@ -532,10 +546,12 @@ class CourseraOnDemand(object): if not "ipynb" in supplement_links: supplement_links["ipynb"] = [] - supplement_links["ipynb"].append((tmpUrl.replace(" ", "%20"), filename)) + supplement_links["ipynb"].append( + (tmpUrl.replace(" ", "%20"), filename)) else: - logging.info('Unsupported typename {} in notebook'.format(content['type'])) + logging.info( + 'Unsupported typename {} in notebook'.format(content['type'])) return supplement_links