import items
This commit is contained in:
parent
e41942ea99
commit
823ae2a676
11 changed files with 181 additions and 36 deletions
|
|
@ -56,6 +56,15 @@ class Changelog(db.Model):
|
|||
def timestamp(self):
|
||||
return self.created.strftime('%s')
|
||||
|
||||
@classmethod
|
||||
def apply_changes(cls, user, changes):
|
||||
for change in changes:
|
||||
if not Changelog.apply_change(user, change):
|
||||
print 'FAIL', change
|
||||
break
|
||||
return False
|
||||
return True
|
||||
|
||||
@classmethod
|
||||
def apply_change(cls, user, change, rebuild=False):
|
||||
revision, timestamp, sig, data = change
|
||||
|
|
|
|||
|
|
@ -183,6 +183,8 @@ actions.register(scan, cache=False)
|
|||
|
||||
@returns_json
|
||||
def _import(request):
|
||||
state.main.add_callback(state.websockets[0].put, json.dumps(['import', {}]))
|
||||
data = json.loads(request.form['data']) if 'data' in request.form else {}
|
||||
print 'api.import', data
|
||||
state.main.add_callback(state.websockets[0].put, json.dumps(['import', data]))
|
||||
return {}
|
||||
actions.register(_import, 'import', cache=False)
|
||||
|
|
|
|||
|
|
@ -12,13 +12,15 @@ from app import app
|
|||
import settings
|
||||
from settings import db
|
||||
from item.models import File
|
||||
from user.models import User
|
||||
from user.models import User, List
|
||||
|
||||
from changelog import Changelog
|
||||
|
||||
import media
|
||||
from websocket import trigger_event
|
||||
|
||||
extensions = ['epub', 'pdf', 'txt']
|
||||
|
||||
def remove_missing():
|
||||
dirty = False
|
||||
with app.app_context():
|
||||
|
|
@ -49,7 +51,6 @@ def run_scan():
|
|||
prefix += '/'
|
||||
user = User.get_or_create(settings.USER_ID)
|
||||
assert isinstance(prefix, unicode)
|
||||
extensions = ['pdf', 'epub', 'txt']
|
||||
books = []
|
||||
for root, folders, files in os.walk(prefix):
|
||||
for f in files:
|
||||
|
|
@ -62,8 +63,7 @@ def run_scan():
|
|||
books.append(f)
|
||||
|
||||
trigger_event('scan', {
|
||||
'path': prefix,
|
||||
'files': len(books)
|
||||
'progress': [0, len(books)],
|
||||
})
|
||||
position = 0
|
||||
added = 0
|
||||
|
|
@ -96,29 +96,37 @@ def run_scan():
|
|||
item.scrape()
|
||||
added += 1
|
||||
trigger_event('scan', {
|
||||
'position': position,
|
||||
'length': len(books),
|
||||
'path': path,
|
||||
'progress': position/len(books),
|
||||
'added': added,
|
||||
'progress': [position, len(books)],
|
||||
'path': path,
|
||||
})
|
||||
trigger_event('scan', {
|
||||
'progress': 1,
|
||||
'progress': [position, len(books)],
|
||||
'added': added,
|
||||
'done': True
|
||||
'status': {'code': 200, 'text': ''}
|
||||
})
|
||||
|
||||
def run_import():
|
||||
def run_import(options=None):
|
||||
options = options or {}
|
||||
|
||||
with app.app_context():
|
||||
prefs = settings.preferences
|
||||
prefix = os.path.expanduser(prefs['importPath'])
|
||||
prefix = options.get('path', os.path.expanduser(prefs['importPath']))
|
||||
prefix_books = os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books/')
|
||||
prefix_imported = os.path.join(prefix_books, 'Imported/')
|
||||
if not prefix[-1] == '/':
|
||||
prefix += '/'
|
||||
|
||||
if not os.path.exists(prefix):
|
||||
trigger_event('import', {
|
||||
'progress': [0, 0],
|
||||
'status': {'code': 404, 'text': 'path not found'}
|
||||
})
|
||||
user = User.get_or_create(settings.USER_ID)
|
||||
listname = options.get('list')
|
||||
if listname:
|
||||
listitems = []
|
||||
assert isinstance(prefix, unicode)
|
||||
extensions = ['pdf', 'epub', 'txt']
|
||||
books = []
|
||||
for root, folders, files in os.walk(prefix):
|
||||
for f in files:
|
||||
|
|
@ -131,8 +139,7 @@ def run_import():
|
|||
books.append(f)
|
||||
|
||||
trigger_event('import', {
|
||||
'path': prefix,
|
||||
'files': len(books)
|
||||
'progress': [0, len(books)],
|
||||
})
|
||||
position = 0
|
||||
added = 0
|
||||
|
|
@ -145,7 +152,10 @@ def run_import():
|
|||
f_import = f
|
||||
f = f.replace(prefix, prefix_imported)
|
||||
ox.makedirs(os.path.dirname(f))
|
||||
shutil.move(f_import, f)
|
||||
if options.get('mode') == 'move':
|
||||
shutil.move(f_import, f)
|
||||
else:
|
||||
shutil.copy(f_import, f)
|
||||
path = f[len(prefix_books):]
|
||||
data = media.metadata(f)
|
||||
ext = f.split('.')[-1]
|
||||
|
|
@ -167,16 +177,19 @@ def run_import():
|
|||
item.meta['mainid']: item.meta[item.meta['mainid']]
|
||||
})
|
||||
item.scrape()
|
||||
if listname:
|
||||
listitems.append(item.id)
|
||||
added += 1
|
||||
trigger_event('import', {
|
||||
'position': position,
|
||||
'length': len(books),
|
||||
'progress': [position, len(books)],
|
||||
'path': path,
|
||||
'progress': position/len(books),
|
||||
'added': added,
|
||||
})
|
||||
if listname:
|
||||
l = List.get_or_create(settings.USER_ID, listname)
|
||||
l.add_items(listitems)
|
||||
trigger_event('import', {
|
||||
'progress': 1,
|
||||
'progress': [position, len(books)],
|
||||
'status': {'code': 200, 'text': ''},
|
||||
'added': added,
|
||||
'done': True
|
||||
})
|
||||
|
|
|
|||
|
|
@ -46,6 +46,8 @@ def info(epub):
|
|||
isbn = extract_isbn(text)
|
||||
if isbn:
|
||||
data['isbn'] = isbn
|
||||
if 'date' in data and 'T' in data['date']:
|
||||
data['date'] = data['date'].split('T')[0]
|
||||
return data
|
||||
|
||||
def extract_text(path):
|
||||
|
|
|
|||
|
|
@ -29,11 +29,10 @@ def api_pullChanges(app, remote_id, user_id=None, from_=None, to=None):
|
|||
|
||||
def api_pushChanges(app, user_id, changes):
|
||||
user = User.get(user_id)
|
||||
for change in changes:
|
||||
if not Changelog.apply_change(user, change):
|
||||
print 'FAILED TO APPLY CHANGE', change
|
||||
state.nodes.queue(user_id, 'pullChanges')
|
||||
return False
|
||||
if not Changelog.apply_changes(user, changes):
|
||||
print 'FAILED TO APPLY CHANGE'
|
||||
state.nodes.queue(user_id, 'pullChanges')
|
||||
return False
|
||||
return True
|
||||
|
||||
def api_requestPeering(app, user_id, username, message):
|
||||
|
|
|
|||
|
|
@ -164,12 +164,7 @@ class Node(object):
|
|||
changes = self.request('pullChanges', from_revision)
|
||||
if not changes:
|
||||
return False
|
||||
for change in changes:
|
||||
if not Changelog.apply_change(self.user, change):
|
||||
print 'FAIL', change
|
||||
break
|
||||
return False
|
||||
return True
|
||||
return Changelog.apply_changes(self.user, changes)
|
||||
|
||||
def pushChanges(self, changes):
|
||||
print 'pushing changes to', self.user_id, changes
|
||||
|
|
|
|||
|
|
@ -33,7 +33,7 @@ class Background:
|
|||
if action == 'ping':
|
||||
self.post(['pong', data])
|
||||
elif action == 'import':
|
||||
item.scan.run_import()
|
||||
item.scan.run_import(data)
|
||||
elif action == 'scan':
|
||||
item.scan.run_scan()
|
||||
elif action == 'update':
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue