This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Python Epubs to mobis

By: a guest on May 20th, 2011  |  syntax: Python  |  size: 1.07 KB  |  views: 575  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/python
  2. #
  3. #jmeb @ May 2011
  4. #A very first attempt at python scripting
  5. #
  6. #Create mobi's from epubs if not already present
  7. #
  8.  
  9. import os
  10. import subprocess
  11. import sys
  12.  
  13. def main():
  14.   os.chdir(sys.argv[1])
  15.  
  16. #Generate two lists: one .epub, one .mobis
  17.   epubs = []
  18.   mobis = []
  19.  
  20.   for filename in os.listdir(os.getcwd()):
  21.     if filename.endswith('.epub'):
  22.       epub = filename[:-len('.epub')]
  23.       epubs.append(epub)
  24.     elif filename.endswith('.mobi'):
  25.       mobi = filename[:-len('.mobi')]
  26.       mobis.append(mobi)
  27.  
  28. #Make a list of epub not in mobis
  29.   converts = []
  30.   for convert in epubs:
  31.     if convert not in mobis:
  32.       #Some hackish processing to write shell commands into list
  33.       inepub = '"' + convert + '.epub' + '"'
  34.       outmobi = '"' + convert + '.mobi'  + '"'
  35.       ebook = 'ebook-convert' + ' ' + inepub + ' ' + outmobi
  36.       #Generate list of shell commands to run
  37.       converts.append(ebook)
  38.  
  39. #Run the conversions
  40.   for ebook in converts:
  41.     subprocess.call(ebook, shell=True)
  42.  
  43. if __name__ == '__main__':
  44.     main()
clone this paste RAW Paste Data