| | 494 | |
|---|
| | 495 | def _buildZIP(self): |
|---|
| | 496 | print '-------------- _buildZIP -------------' |
|---|
| | 497 | # print self.Title() |
|---|
| | 498 | # print self.getLearningStoryText() |
|---|
| | 499 | |
|---|
| | 500 | def do_src_suff(html, _dir): # change src urls, download and put them in zip |
|---|
| | 501 | l = sre.split('<img\s+src="(.*?)"', html) |
|---|
| | 502 | |
|---|
| | 503 | from pprint import pprint |
|---|
| | 504 | imgs = {} |
|---|
| | 505 | newnames = {} |
|---|
| | 506 | img_urls = l[1::2] |
|---|
| | 507 | for url in img_urls: |
|---|
| | 508 | if not imgs.has_key(url): |
|---|
| | 509 | xurl = url |
|---|
| | 510 | if not url.startswith('http://'): |
|---|
| | 511 | xurl = self.absolute_url() + '/' + url |
|---|
| | 512 | print xurl |
|---|
| | 513 | try: |
|---|
| | 514 | img = urlopen(xurl) |
|---|
| | 515 | media_type, subtype = img.headers.getheader('Content-Type').split('/') |
|---|
| | 516 | if media_type == 'image': |
|---|
| | 517 | tmp_url = img.url |
|---|
| | 518 | if img.url.startswith(self.portal_url()): |
|---|
| | 519 | for s in ('/coverImage', '/image_large'): |
|---|
| | 520 | i = img.url.find(s) |
|---|
| | 521 | if i != -1: |
|---|
| | 522 | tmp_url = list(img.url) |
|---|
| | 523 | tmp_url[i] = '_' |
|---|
| | 524 | tmp_url = ''.join(tmp_url[:i + len(s)]) |
|---|
| | 525 | break |
|---|
| | 526 | basename = tmp_url.split('/')[-1].rsplit('.', 1)[0] |
|---|
| | 527 | filename = '%s.%s' % (basename, subtype) |
|---|
| | 528 | newnames[filename] = newnames.get(filename, 0) + 1 |
|---|
| | 529 | if newnames[filename] > 1: |
|---|
| | 530 | filename = '%s-%d.%s' % (basename, newnames[filename] - 1, subtype) |
|---|
| | 531 | imgs[url] = (filename, img.read()) |
|---|
| | 532 | except HTTPError, e: |
|---|
| | 533 | print "Failed to download %s: %s" % (xurl, e) |
|---|
| | 534 | |
|---|
| | 535 | pprint([(x, y) for x, (y, z) in imgs.iteritems()]) |
|---|
| | 536 | |
|---|
| | 537 | if _dir[-1] == '/': |
|---|
| | 538 | _dir = _dir[:-1] |
|---|
| | 539 | for oldname, (newname, content) in imgs.iteritems(): |
|---|
| | 540 | zf.writestr('%s/%s' % (_dir, newname), content) |
|---|
| | 541 | |
|---|
| | 542 | s = ['%s<img src="%s"' % (stuff, imgs.get(link, (link,))[0]) for stuff, link in zip(l[::2], l[1::2])] |
|---|
| | 543 | s.append(l[-1]) |
|---|
| | 544 | return ''.join(s) |
|---|
| | 545 | |
|---|
| | 546 | s = StringIO() |
|---|
| | 547 | zf = zipfile.ZipFile(s, 'w', compression=zipfile.ZIP_DEFLATED) |
|---|
| | 548 | |
|---|
| | 549 | main_dir = self.getId() |
|---|
| | 550 | print '%s/index.html' % main_dir |
|---|
| | 551 | zf.writestr('%s/index.html' % main_dir, do_img_suff(self.standalone_view(), main_dir)) |
|---|
| | 552 | for content in self.getResources(reftype='relatedContent'): |
|---|
| | 553 | print '%s/content/%s/index.html' % (main_dir, content.getId()) |
|---|
| | 554 | zf.writestr('%s/content/%s/index.html' % (main_dir, content.getId()), do_img_suff(content.standalone_view(), '%s/content/%s' % (main_dir, content.getId()))) |
|---|
| | 555 | |
|---|
| | 556 | for method in self.getResources(reftype='relatedMethods'): |
|---|
| | 557 | print '%s/methods/%s/index.html' % (main_dir, method.getId()) |
|---|
| | 558 | zf.writestr('%s/methods/%s/index.html' % (main_dir, method.getId()), do_img_suff(method.standalone_view(), '%s/methods/%s' % (main_dir, method.getId()))) |
|---|
| | 559 | |
|---|
| | 560 | for tool in self.getResources(reftype='relatedTools'): |
|---|
| | 561 | print '%s/tools/%s/index.html' % (main_dir, tool.getId()) |
|---|
| | 562 | zf.writestr('%s/tools/%s/index.html' % (main_dir, tool.getId()), do_img_suff(tool.standalone_view(), '%s/tools/%s' % (main_dir, tool.getId()))) |
|---|
| | 563 | |
|---|
| | 564 | print '-------------- Done ------------' |
|---|
| | 565 | return s, zf |
|---|
| | 566 | |
|---|
| | 567 | def getZIP(self): |
|---|
| | 568 | s, zf = self._buildZIP() |
|---|
| | 569 | zf.close() |
|---|
| | 570 | response = self.REQUEST.RESPONSE |
|---|
| | 571 | response.setHeader('Content-Type', 'application/zip') |
|---|
| | 572 | response.setHeader('Content-Disposition', 'attachment; filename="%s.zip"' % self.getId()) |
|---|
| | 573 | return s.getvalue() |
|---|
| | 574 | |
|---|
| | 575 | def getSCORM(self): |
|---|
| | 576 | """ hguriehguirehgre """ |
|---|
| | 577 | s, zf = self._buildZIP() |
|---|
| | 578 | zf.close() |
|---|
| | 579 | response = self.REQUEST.RESPONSE |
|---|
| | 580 | response.setHeader('Content-Type', 'application/zip') |
|---|
| | 581 | response.setHeader('Content-Disposition', 'attachment; filename="%s.zip"' % self.getId()) |
|---|
| | 582 | return s.getvalue() |
|---|