Skip to content

Instantly share code, notes, and snippets.

@quasiyoke
Created July 31, 2014 23:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save quasiyoke/93b4f0931a7ab2c9992a to your computer and use it in GitHub Desktop.
Save quasiyoke/93b4f0931a7ab2c9992a to your computer and use it in GitHub Desktop.
FB2 library management helper
#!/usr/bin/env python
'''
Tool for renaming books. Unpacks zipped books and prompts to rename them.
'''
import os
import platform
import zipfile
IS_WINDOWS = 'Windows' == platform.system()
FILENAMES_ENCODING = IS_WINDOWS and 'cp1251' or 'utf8'
CONSOLE_ENCODING = IS_WINDOWS and 'cp866' or 'utf8'
files = os.listdir('.')
zips = [f for f in files if f.endswith('.zip')]
for f in zips:
z = zipfile.ZipFile(f)
z.extractall()
z.close()
os.remove(f)
files = os.listdir('.')
files = [unicode(f, FILENAMES_ENCODING) for f in files]
EXTENSION = '.fb2'
files = [f for f in files if f.endswith(EXTENSION)]
for f in sorted(files):
n = raw_input('\n\n' + f[:-len(EXTENSION)].encode(CONSOLE_ENCODING, 'ignore') + '\n')
if n:
n = unicode(n, CONSOLE_ENCODING)
os.rename(f, n + EXTENSION)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment