Advertisement
Guest User

Untitled

a guest
Feb 21st, 2016
87
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import json
  2. from ebooklib import epub
  3. import urllib2
  4.  
  5.  
  6. book = epub.EpubBook()
  7.  
  8. book.set_identifier('idMissIBook')
  9. book.set_title('Mr P and Miss I\n The journal')
  10. book.set_language('pt')
  11.  
  12. book.add_author('Mr P')
  13. book.add_author('Miss I')
  14.  
  15. BLOGG = "mrp-and-missi"
  16. BLOGGER_URL='http://{}.blogspot.com/feeds/posts/default?alt=json&callback=mycallbackfunc&start-index=1&max-results=500'.format(BLOGG)
  17.  
  18. httpdata = urllib2.urlopen(BLOGGER_URL)
  19. jsonCallback = httpdata.read()
  20.  
  21. #Extract the callBack declaration
  22. jsonData=jsonCallback[31:]
  23. #Extract the final 2 chars that represent ");" for the enclosing of the callback
  24. jsonData=jsonData[:-2]
  25.  
  26. #Now parse the data into json object
  27. blogData=json.loads(jsonData)
  28. entries= blogData['feed']['entry']
  29.  
  30. cc=['nav']
  31.  
  32. #Iterate over the posts
  33. for entry in reversed(entries):
  34.     name=entry['author'][0]['name']['$t']
  35.     dataPublished = entry['published']['$t']
  36.     title=entry['title']['$t']
  37.     header='<h1>'+title+'</h1>'+'<h4>'+dataPublished.split("T")[0]+'</h4>'
  38.     content=header+entry['content']['$t']
  39.     c1 = epub.EpubHtml(title=title, file_name=title+'.xhtml', lang='pt')
  40.     c1.content=content
  41.     book.add_item(c1)
  42.     book.toc = book.toc + [epub.Link(title+'.xhtml', title, title)]
  43.     cc.append(c1)
  44.  
  45. book.add_item(epub.EpubNcx())
  46. book.add_item(epub.EpubNav())
  47. style = 'BODY {color: white;}'
  48. nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)
  49. # add CSS file
  50. book.add_item(nav_css)
  51. # basic spine
  52. book.spine = cc
  53. # write to the file
  54. epub.write_epub('mrp-and-missi.epub', book, {})
Advertisement
RAW Paste Data Copied
Advertisement