import books from import folder, fixes #136
This commit is contained in:
parent
1cd6f35b86
commit
75fd720b19
4 changed files with 32 additions and 11 deletions
|
@ -10,7 +10,7 @@ import time
|
||||||
import ox
|
import ox
|
||||||
|
|
||||||
from changelog import Changelog
|
from changelog import Changelog
|
||||||
from item.models import File, Scrape
|
from item.models import File
|
||||||
from user.models import List
|
from user.models import List
|
||||||
from utils import remove_empty_folders
|
from utils import remove_empty_folders
|
||||||
from websocket import trigger_event
|
from websocket import trigger_event
|
||||||
|
@ -47,7 +47,7 @@ def remove_missing():
|
||||||
if not state.tasks.connected:
|
if not state.tasks.connected:
|
||||||
return
|
return
|
||||||
f.move()
|
f.move()
|
||||||
remove_empty_folders(prefix)
|
remove_empty_folders(prefix, True)
|
||||||
|
|
||||||
def add_file(id, f, prefix, from_=None):
|
def add_file(id, f, prefix, from_=None):
|
||||||
user = state.user()
|
user = state.user()
|
||||||
|
@ -173,19 +173,24 @@ def run_import(options=None):
|
||||||
with db.session():
|
with db.session():
|
||||||
id = media.get_id(f)
|
id = media.get_id(f)
|
||||||
file = File.get(id)
|
file = File.get(id)
|
||||||
|
f_import = f
|
||||||
if not file:
|
if not file:
|
||||||
f_import = f
|
|
||||||
f = f.replace(prefix, prefix_imported)
|
f = f.replace(prefix, prefix_imported)
|
||||||
ox.makedirs(os.path.dirname(f))
|
ox.makedirs(os.path.dirname(f))
|
||||||
if options.get('mode') == 'move':
|
if options.get('mode') == 'move':
|
||||||
shutil.move(f_import, f)
|
try:
|
||||||
|
shutil.move(f_import, f)
|
||||||
|
except:
|
||||||
|
shutil.copy2(f_import, f)
|
||||||
else:
|
else:
|
||||||
shutil.copy(f_import, f)
|
shutil.copy2(f_import, f)
|
||||||
file = add_file(id, f, prefix_books, f_import)
|
file = add_file(id, f, prefix_books, f_import)
|
||||||
file.move()
|
file.move()
|
||||||
added += 1
|
added += 1
|
||||||
if listname:
|
elif options.get('mode') == 'move':
|
||||||
listitems.append(file.item.id)
|
os.unlink(f_import)
|
||||||
|
if listname:
|
||||||
|
listitems.append(file.item.id)
|
||||||
if time.time() - last > 5:
|
if time.time() - last > 5:
|
||||||
last = time.time()
|
last = time.time()
|
||||||
state.activity = {
|
state.activity = {
|
||||||
|
@ -214,4 +219,16 @@ def run_import(options=None):
|
||||||
state.activity = {}
|
state.activity = {}
|
||||||
remove_empty_folders(prefix_books)
|
remove_empty_folders(prefix_books)
|
||||||
if options.get('mode') == 'move':
|
if options.get('mode') == 'move':
|
||||||
remove_empty_folders(prefix)
|
remove_empty_folders(prefix, True)
|
||||||
|
|
||||||
|
def import_folder():
|
||||||
|
import_path = settings.preferences['importPath']
|
||||||
|
logger.debug('scan importPath %s', import_path)
|
||||||
|
if os.path.exists(import_path):
|
||||||
|
run_import({
|
||||||
|
'path': import_path,
|
||||||
|
'mode': 'move'
|
||||||
|
})
|
||||||
|
remove_empty_folders(import_path, True)
|
||||||
|
if state.main:
|
||||||
|
state.main.call_later(10*60, lambda: state.tasks.queue('scanimport'))
|
||||||
|
|
|
@ -159,6 +159,7 @@ def run():
|
||||||
state.main.call_later(10, publish)
|
state.main.call_later(10, publish)
|
||||||
else:
|
else:
|
||||||
nodes.publish_node()
|
nodes.publish_node()
|
||||||
|
state.main.call_later(10, lambda: state.tasks.queue('scanimport'))
|
||||||
state.main.add_callback(publish)
|
state.main.add_callback(publish)
|
||||||
|
|
||||||
if not state.update:
|
if not state.update:
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
# vi:si:et:sw=4:sts=4:ts=4
|
# vi:si:et:sw=4:sts=4:ts=4
|
||||||
|
|
||||||
|
import os
|
||||||
from queue import Queue
|
from queue import Queue
|
||||||
from threading import Thread
|
from threading import Thread
|
||||||
|
|
||||||
|
@ -36,6 +36,8 @@ class Tasks(Thread):
|
||||||
export_list(data)
|
export_list(data)
|
||||||
elif action == 'scan':
|
elif action == 'scan':
|
||||||
item.scan.run_scan()
|
item.scan.run_scan()
|
||||||
|
elif action == 'scanimport':
|
||||||
|
item.scan.import_folder()
|
||||||
elif action == 'peering':
|
elif action == 'peering':
|
||||||
update_user_peering(*data)
|
update_user_peering(*data)
|
||||||
else:
|
else:
|
||||||
|
|
|
@ -310,14 +310,15 @@ def update_dict(root, data):
|
||||||
if hasattr(root, '_save'):
|
if hasattr(root, '_save'):
|
||||||
root._save()
|
root._save()
|
||||||
|
|
||||||
def remove_empty_folders(prefix):
|
def remove_empty_folders(prefix, keep_root=False):
|
||||||
empty = []
|
empty = []
|
||||||
for root, folders, files in os.walk(prefix):
|
for root, folders, files in os.walk(prefix):
|
||||||
if len(files) == 1 and files[0] == '.DS_Store':
|
if len(files) == 1 and files[0] == '.DS_Store':
|
||||||
os.unlink(os.path.join(root, files[0]))
|
os.unlink(os.path.join(root, files[0]))
|
||||||
files = []
|
files = []
|
||||||
if not folders and not files:
|
if not folders and not files:
|
||||||
empty.append(root)
|
if root != prefix or not keep_root:
|
||||||
|
empty.append(root)
|
||||||
for folder in empty:
|
for folder in empty:
|
||||||
remove_empty_tree(folder)
|
remove_empty_tree(folder)
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue