Advertisement
Guest User

Python Epubs to mobis

a guest
May 20th, 2011
1,292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.07 KB | None | 0 0
  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()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement