-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.py
89 lines (66 loc) · 2.37 KB
/
reader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
from genericpath import exists
import glob
import os
import string
import zipfile
import subprocess
import win32api
import win32con
import win32file
import shutil
from pathlib import Path
def latest_file(path):
list_of_files = glob.glob(f'{path}/*')
latest_file = max(list_of_files, key=os.path.getctime)
return latest_file
def downloads_path():
return str(Path.home() / "Downloads")
def unzip_file(path, file):
with zipfile.ZipFile(file, 'r') as zip_ref:
zip_ref.extractall(path)
def create_mobi_file_path(file):
file_list = file.split(".")
file_list[-1] = "mobi"
path = ".".join(file_list)
return path
def convert_to_mobi(source, destination):
program_files = os.environ["ProgramFiles"]
program_files_86 = os.environ["ProgramFiles(x86)"]
calibre = f'{program_files}\\Calibre2\\ebook-convert.exe'
calibre_86 = f'{program_files_86}\\Calibre2\\ebook-convert.exe'
if os.path.exists(calibre):
execution = calibre
elif os.path.exists(calibre_86):
execution = calibre_86
else:
raise Exception("Calibre doesn't exist")
subprocess.call(f'"{execution}" "{source}" "{destination}"')
return destination
def find_kindle_path():
drives = [i for i in win32api.GetLogicalDriveStrings().split('\x00') if i]
for i in drives:
drivename = win32api.GetVolumeInformation(i)[0]+'('+i+')'
if drivename != None and "kindle" in drivename.lower():
return f'{i}documents'
raise Exception("No Kindle drive was found")
def copy_to_kindle(file, folder):
dst = os.path.join(folder, os.path.basename(file))
shutil.copyfile(file, dst)
def get_file_extension(file):
return file.split(".")[-1]
def check_supported_file(extension):
if extension.lower() not in ['zip', 'fb2', 'epub']:
raise Exception('Not supported file type')
def main():
latest_download = latest_file(downloads_path())
extension = get_file_extension(latest_download)
check_supported_file(extension)
if extension.lower() == "zip":
unzip_file(downloads_path(), latest_download)
latest_download = latest_file(downloads_path())
if len(latest_download) == 0:
return
mobi_path = convert_to_mobi(latest_download, create_mobi_file_path(latest_download))
copy_to_kindle(mobi_path, find_kindle_path())
if __name__ == "__main__":
main()