Skip to content

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also .

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also .
  • 2 commits
  • 1 file changed
  • 0 commit comments
  • 1 contributor
Commits on Apr 06, 2012
@fvieira If script is stopped during a download, removes partially downloaded …
…file.

This idea was shamelessly stolen from jplehmann's script which does
pretty much the same as mine, so do me a favor and go check his script,
maybe you'll his better than mine: https://github.com/jplehmann/coursera
cc0551b
@fvieira Add option --all to allow downloading all resources f2ba428
Showing with 39 additions and 30 deletions.
  1. +39 −30 coursera
View
69 coursera
@@ -32,34 +32,42 @@ def make_valid_filename(filename):
# Based in PabloG answer at http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python
def download_to_file(open_url, file_name):
- with open(file_name, 'wb') as f:
- meta = open_url.info()
- length_headers = meta.getheaders('Content-Length')
- file_size = int(length_headers[0]) if length_headers else None
- print('Downloading: {0}'.format(file_name))
-
- file_size_dl = 0
- block_sz = 8192
- while True:
- buf = open_url.read(block_sz)
- if not buf:
- break
-
- file_size_dl += len(buf)
- f.write(buf)
- fsdmb = '{0:d}'.format(file_size_dl / 1000000)
- fsdkb = '{0:03d}'.format(file_size_dl / 1000 % 1000)
- fsd = '{0}.{1}'.format(fsdmb, fsdkb)
- if file_size:
- fsmb = '{0:d}'.format(file_size / 1000000)
- fskb = '{0:03d}'.format(file_size / 1000 % 1000)
- fs = '{0}.{1}'.format(fsmb, fskb)
- percentage = '{0:.2f}'.format(file_size_dl * 100. / file_size)
- status = r'{0:>8s}/{1:s} Mb [{2}%]'.format(fsd, fs, percentage)
- else:
- status = r'{0:>8s} Mb'.format(fsd)
- status = status + chr(8) * (len(status) + 1)
- print(status, end='')
+ try:
+ with open(file_name, 'wb') as f:
+ meta = open_url.info()
+ length_headers = meta.getheaders('Content-Length')
+ file_size = int(length_headers[0]) if length_headers else None
+ print('Downloading: {0}'.format(file_name))
+
+ file_size_dl = 0
+ block_sz = 8192
+ while True:
+ buf = open_url.read(block_sz)
+ if not buf:
+ break
+
+ file_size_dl += len(buf)
+ f.write(buf)
+ fsdmb = '{0:d}'.format(file_size_dl / 1000000)
+ fsdkb = '{0:03d}'.format(file_size_dl / 1000 % 1000)
+ fsd = '{0}.{1}'.format(fsdmb, fsdkb)
+ if file_size:
+ fsmb = '{0:d}'.format(file_size / 1000000)
+ fskb = '{0:03d}'.format(file_size / 1000 % 1000)
+ fs = '{0}.{1}'.format(fsmb, fskb)
+ percentage = '{0:.2f}'.format(file_size_dl * 100. / file_size)
+ status = r'{0:>8s}/{1:s} Mb [{2}%]'.format(fsd, fs, percentage)
+ else:
+ status = r'{0:>8s} Mb'.format(fsd)
+ status = status + chr(8) * (len(status) + 1)
+ print(status, end='')
+ except KeyboardInterrupt:
+ print('\nDownload was interrupted. Removed partially downloaded file.')
+ os.remove(file_name)
+ sys.exit()
+ except Exception:
+ os.remove(file_name)
+ raise
def clean_lecture_name(lecture_name):
@@ -122,7 +130,7 @@ def get_id_from_url(url):
def download_course_resources(args):
- if not any(getattr(args, res_dict['arg']) for res_dict in RESOURCE_DICTS_BY_I_CLASS.values()):
+ if not args.dl_all and not any(getattr(args, res_dict['arg']) for res_dict in RESOURCE_DICTS_BY_I_CLASS.values()):
print('ERROR: You disabled video download but didn\'t enable any other resource for download.')
sys.exit()
@@ -194,7 +202,7 @@ def download_course_resources(args):
url_list = lecture_div.xpath('./a/@href')
resource_icon_class_list = [classes.split(' ')[0] for classes in lecture_div.xpath('./a/i/@class')]
for resource_img, resource_dict in RESOURCE_DICTS_BY_I_CLASS.items():
- if getattr(args, resource_dict['arg']):
+ if args.dl_all or getattr(args, resource_dict['arg']):
filetype_urls = [url for url, img in zip(url_list, resource_icon_class_list) if img == resource_img]
add_ft_counter = len(filetype_urls) > 1
for j, filetype_url in enumerate(filetype_urls, 1):
@@ -221,6 +229,7 @@ def main():
dl_res_parser.add_argument('course_id', help='Course identifier. Run the script once with the subcommand list-course-ids to see the ones available.')
dl_res_parser.add_argument('email', help='Your coursera email.')
dl_res_parser.add_argument('password', nargs='?', default=None, help='Your coursera password. You can omit it in the command line and provide it interactively.')
+ dl_res_parser.add_argument('--all', dest='dl_all', action='store_true', help='Downloads all available resources (video, pdfs, pptx, etc.). Disabled by default.')
dl_res_parser.add_argument('--pdfs', action='store_true', help='Get the pdfs for each lecture. Disabled by default.')
dl_res_parser.add_argument('--pptx', action='store_true', help='Get the pptx\'s for each lecture. Disabled by default.')
dl_res_parser.add_argument('--txt', action='store_true', help='Get the text subtitles for each lecture. Disabled by default.')

No commit comments for this range

Something went wrong with that request. Please try again.