home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / calibre-0.7.26.msi / file_882 (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-10-31  |  5.2 KB  |  100 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. __license__ = 'GPL v3'
  5. __copyright__ = '2010, Kovid Goyal <kovid@kovidgoyal.net>'
  6. __docformat__ = 'restructuredtext en'
  7. from uuid import uuid4
  8. from calibre.constants import __appname__, __version__
  9. from calibre import strftime, prepare_string_for_xml as xml
  10. SONY_METADATA = u'<?xml version="1.0" encoding="utf-8"?>\n<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\n\t\t xmlns:dcterms="http://purl.org/dc/terms/"\n\t\t xmlns:dc="http://purl.org/dc/elements/1.1/"\n\t\t xmlns:prs="http://xmlns.sony.net/e-book/prs/">\n\t<rdf:Description rdf:about="">\n\t\t<dc:title>{title}</dc:title>\n\t\t<dc:publisher>{publisher}</dc:publisher>\n\t\t<dcterms:alternative>{short_title}</dcterms:alternative>\n\t\t<dcterms:issued>{issue_date}</dcterms:issued>\n\t\t<dc:language>{language}</dc:language>\n\t\t<dcterms:conformsTo rdf:resource="http://xmlns.sony.net/e-book/prs/periodicals/1.0/newspaper/1.0"/>\n\t\t<dcterms:type rdf:resource="http://xmlns.sony.net/e-book/prs/datatype/newspaper"/>\n\t\t<dcterms:type rdf:resource="http://xmlns.sony.net/e-book/prs/datatype/periodical"/>\n\t</rdf:Description>\n</rdf:RDF>\n'
  11. SONY_ATOM = u'<?xml version="1.0" encoding="utf-8" ?>\n<feed xmlns="http://www.w3.org/2005/Atom"\n    xmlns:dc="http://purl.org/dc/elements/1.1/"\n    xmlns:dcterms="http://purl.org/dc/terms/"\n    xmlns:prs="http://xmlns.sony.net/e-book/prs/"\n    xmlns:media="http://video.search.yahoo.com/mrss"\n    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"\n    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">\n\n<title>{short_title}</title>\n<updated>{updated}</updated>\n<id>{id}</id>\n{entries}\n</feed>\n'
  12. SONY_ATOM_SECTION = u'<entry rdf:ID="{title}">\n  <title>{title}</title>\n  <link href="{href}"/>\n  <id>{id}</id>\n  <updated>{updated}</updated>\n  <summary>{desc}</summary>\n  <category term="{short_title}/{title}"\n      scheme="http://xmlns.sony.net/e-book/terms/" label="{title}"/>\n  <dc:type xsi:type="prs:datatype">newspaper/section</dc:type>\n  <dcterms:isReferencedBy rdf:resource=""/>\n</entry>\n'
  13. SONY_ATOM_ENTRY = u'<entry>\n  <title>{title}</title>\n  <author><name>{author}</name></author>\n  <link href="{href}"/>\n  <id>{id}</id>\n  <updated>{updated}</updated>\n  <summary>{desc}</summary>\n  <category term="{short_title}/{section_title}"\n      scheme="http://xmlns.sony.net/e-book/terms/" label="{section_title}"/>\n  <dcterms:extent xsi:type="prs:word-count">{word_count}</dcterms:extent>\n  <dc:type xsi:type="prs:datatype">newspaper/article</dc:type>\n  <dcterms:isReferencedBy rdf:resource="#{section_title}"/>\n</entry>\n'
  14.  
  15. def sony_metadata(oeb):
  16.     m = oeb.metadata
  17.     title = short_title = unicode(m.title[0])
  18.     publisher = __appname__ + ' ' + __version__
  19.     
  20.     try:
  21.         pt = unicode(oeb.metadata.publication_type[0])
  22.         short_title = u':'.join(pt.split(':')[2:])
  23.     except:
  24.         pass
  25.  
  26.     
  27.     try:
  28.         date = unicode(m.date[0]).split('T')[0]
  29.     except:
  30.         date = strftime('%Y-%m-%d')
  31.  
  32.     
  33.     try:
  34.         language = unicode(m.language[0]).replace('_', '-')
  35.     except:
  36.         language = 'en'
  37.  
  38.     short_title = xml(short_title, True)
  39.     metadata = SONY_METADATA.format(title = xml(title), short_title = short_title, publisher = xml(publisher), issue_date = xml(date), language = xml(language))
  40.     updated = strftime('%Y-%m-%dT%H:%M:%SZ')
  41.     
  42.     def cal_id(x):
  43.         for k, v in x.attrib.items():
  44.             if k.endswith('scheme') and v == 'uuid':
  45.                 return True
  46.         
  47.  
  48.     
  49.     try:
  50.         base_id = unicode(list(filter(cal_id, m.identifier))[0])
  51.     except:
  52.         base_id = str(uuid4())
  53.  
  54.     entries = []
  55.     seen_titles = set([])
  56.     for i, section in enumerate(oeb.toc):
  57.         if not section.href:
  58.             continue
  59.         
  60.         secid = 'section%d' % i
  61.         sectitle = section.title
  62.         if not sectitle:
  63.             sectitle = _('Unknown')
  64.         
  65.         d = 1
  66.         bsectitle = sectitle
  67.         while sectitle in seen_titles:
  68.             sectitle = bsectitle + ' ' + str(d)
  69.             d += 1
  70.         seen_titles.add(sectitle)
  71.         sectitle = xml(sectitle, True)
  72.         secdesc = section.description
  73.         if not secdesc:
  74.             secdesc = ''
  75.         
  76.         secdesc = xml(secdesc)
  77.         entries.append(SONY_ATOM_SECTION.format(title = sectitle, href = section.href, id = xml(base_id) + '/' + secid, short_title = short_title, desc = secdesc, updated = updated))
  78.         for j, article in enumerate(section):
  79.             if not article.href:
  80.                 continue
  81.             
  82.             atitle = article.title
  83.             btitle = atitle
  84.             d = 1
  85.             while atitle in seen_titles:
  86.                 atitle = btitle + ' ' + str(d)
  87.                 d += 1
  88.             auth = None if article.author else ''
  89.             desc = section.description
  90.             if not desc:
  91.                 desc = ''
  92.             
  93.             aid = 'article%d' % j
  94.             entries.append(SONY_ATOM_ENTRY.format(title = xml(atitle), author = xml(auth), updated = updated, desc = desc, short_title = short_title, section_title = sectitle, href = article.href, word_count = str(1), id = xml(base_id) + '/' + secid + '/' + aid))
  95.         
  96.     
  97.     atom = SONY_ATOM.format(short_title = short_title, entries = '\n\n'.join(entries), updated = updated, id = xml(base_id)).encode('utf-8')
  98.     return (metadata, atom)
  99.  
  100.