first commit
This commit is contained in:
31
functions/backupLogic.py
Normal file
31
functions/backupLogic.py
Normal file
@@ -0,0 +1,31 @@
|
||||
import os
|
||||
import requests
|
||||
|
||||
|
||||
def backup_book(api, env, selected_book, backup_extension, backup_extension_fileType):
|
||||
base_url = env['base_url']
|
||||
backup_dir = env['backup_dir']
|
||||
headers = {
|
||||
"Authorization": f"Token {env['token_id']}:{env['token_secret']}"
|
||||
}
|
||||
|
||||
pages_list = api.get_pages_list()['data']
|
||||
chapter_list = api.get_chapters_list()['data']
|
||||
|
||||
print(f"Backup type: {backup_extension}")
|
||||
|
||||
for chapter in chapter_list:
|
||||
for page in pages_list:
|
||||
if page['book_id'] == selected_book['id']:
|
||||
if chapter['id'] == page['chapter_id']:
|
||||
folder = os.path.join(backup_dir, chapter['name'])
|
||||
filename = os.path.join(folder, page['name'] + backup_extension_fileType)
|
||||
print(f"Backing up: {filename}")
|
||||
if not os.path.exists(folder):
|
||||
os.makedirs(folder, exist_ok=True)
|
||||
response = requests.get(
|
||||
f'{base_url}/api/pages/{page["id"]}/export/{backup_extension}',
|
||||
headers=headers
|
||||
)
|
||||
with open(filename, 'wb') as file:
|
||||
file.write(response.content)
|
||||
Reference in New Issue
Block a user