openmedialibrary/oml/item/scan.py

194 lines
6.8 KiB
Python
Raw Normal View History

2014-05-04 17:26:43 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
from __future__ import division
import os
import shutil
from datetime import datetime
import ox
from app import app
import settings
from settings import db
from item.models import File
2014-05-16 14:30:16 +00:00
from user.models import User, List
2014-05-04 17:26:43 +00:00
from changelog import Changelog
import media
from websocket import trigger_event
2014-05-17 00:14:15 +00:00
import state
2014-05-04 17:26:43 +00:00
2014-05-16 14:30:16 +00:00
extensions = ['epub', 'pdf', 'txt']
2014-05-04 17:26:43 +00:00
def remove_missing():
dirty = False
with app.app_context():
user = User.get_or_create(settings.USER_ID)
prefs = settings.preferences
prefix = os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books/')
for f in File.query:
if not os.path.exists(f.item.get_path()):
dirty = True
print 'file gone', f, f.item.get_path()
f.item.users.remove(user)
if not f.item.users:
print 'last user, remove'
db.session.delete(f.item)
else:
f.item.update_lists()
Changelog.record(user, 'removeitem', f.item.id)
db.session.delete(f)
if dirty:
db.session.commit()
def run_scan():
remove_missing()
with app.app_context():
prefs = settings.preferences
prefix = os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books/')
if not prefix[-1] == '/':
prefix += '/'
user = User.get_or_create(settings.USER_ID)
assert isinstance(prefix, unicode)
books = []
for root, folders, files in os.walk(prefix):
for f in files:
#if f.startswith('._') or f == '.DS_Store':
if f.startswith('.'):
continue
f = os.path.join(root, f)
ext = f.split('.')[-1]
if ext in extensions:
books.append(f)
position = 0
added = 0
for f in ox.sorted_strings(books):
position += 1
id = media.get_id(f)
file = File.get(id)
path = f[len(prefix):]
if not file:
data = media.metadata(f)
ext = f.split('.')[-1]
data['extension'] = ext
data['size'] = os.stat(f).st_size
file = File.get_or_create(id, data, path)
item = file.item
if 'mainid' in file.info:
del file.info['mainid']
db.session.add(file)
if 'mainid' in item.info:
item.meta['mainid'] = item.info.pop('mainid')
item.meta[item.meta['mainid']] = item.info[item.meta['mainid']]
db.session.add(item)
item.users.append(user)
Changelog.record(user, 'additem', item.id, item.info)
if item.meta.get('mainid'):
Changelog.record(user, 'edititem', item.id, {
item.meta['mainid']: item.meta[item.meta['mainid']]
})
item.added = datetime.now()
item.scrape()
added += 1
2014-05-17 00:14:15 +00:00
trigger_event('change', {})
2014-05-04 17:26:43 +00:00
2014-05-16 14:30:16 +00:00
def run_import(options=None):
options = options or {}
2014-05-04 17:26:43 +00:00
with app.app_context():
prefs = settings.preferences
2014-05-17 00:14:15 +00:00
prefix = os.path.expanduser(options.get('path', prefs['importPath']))
2014-05-04 17:26:43 +00:00
if not prefix[-1] == '/':
prefix += '/'
2014-05-17 00:14:15 +00:00
prefix_books = os.path.join(os.path.expanduser(prefs['libraryPath']), 'Books/')
prefix_imported = os.path.join(prefix_books, 'Imported/')
2014-05-16 14:30:16 +00:00
if not os.path.exists(prefix):
2014-05-17 00:14:15 +00:00
trigger_event('activity', {
'activity': 'import',
2014-05-16 14:30:16 +00:00
'progress': [0, 0],
'status': {'code': 404, 'text': 'path not found'}
})
2014-05-17 00:14:15 +00:00
state.activity = {}
2014-05-04 17:26:43 +00:00
user = User.get_or_create(settings.USER_ID)
2014-05-16 14:30:16 +00:00
listname = options.get('list')
if listname:
listitems = []
2014-05-04 17:26:43 +00:00
assert isinstance(prefix, unicode)
books = []
for root, folders, files in os.walk(prefix):
for f in files:
#if f.startswith('._') or f == '.DS_Store':
if f.startswith('.'):
continue
f = os.path.join(root, f)
ext = f.split('.')[-1]
if ext in extensions:
books.append(f)
2014-05-17 00:14:15 +00:00
state.activity = {
'activity': 'import',
2014-05-16 14:30:16 +00:00
'progress': [0, len(books)],
2014-05-17 00:14:15 +00:00
}
trigger_event('activity', state.activity)
2014-05-04 17:26:43 +00:00
position = 0
added = 0
for f in ox.sorted_strings(books):
position += 1
2014-05-17 00:14:15 +00:00
if not os.path.exists(f):
continue
2014-05-04 17:26:43 +00:00
id = media.get_id(f)
file = File.get(id)
path = f[len(prefix):]
if not file:
f_import = f
f = f.replace(prefix, prefix_imported)
ox.makedirs(os.path.dirname(f))
2014-05-16 14:30:16 +00:00
if options.get('mode') == 'move':
shutil.move(f_import, f)
else:
shutil.copy(f_import, f)
2014-05-04 17:26:43 +00:00
path = f[len(prefix_books):]
data = media.metadata(f)
ext = f.split('.')[-1]
data['extension'] = ext
data['size'] = os.stat(f).st_size
file = File.get_or_create(id, data, path)
item = file.item
if 'mainid' in file.info:
del file.info['mainid']
db.session.add(file)
if 'mainid' in item.info:
item.meta['mainid'] = item.info.pop('mainid')
item.meta[item.meta['mainid']] = item.info[item.meta['mainid']]
db.session.add(item)
item.users.append(user)
Changelog.record(user, 'additem', item.id, item.info)
if item.meta.get('mainid'):
Changelog.record(user, 'edititem', item.id, {
item.meta['mainid']: item.meta[item.meta['mainid']]
})
item.scrape()
2014-05-16 14:30:16 +00:00
if listname:
listitems.append(item.id)
2014-05-04 17:26:43 +00:00
added += 1
2014-05-17 00:14:15 +00:00
state.activity = {
'activity': 'import',
2014-05-16 14:30:16 +00:00
'progress': [position, len(books)],
2014-05-04 17:26:43 +00:00
'path': path,
'added': added,
2014-05-17 00:14:15 +00:00
}
trigger_event('activity', state.activity)
2014-05-16 14:30:16 +00:00
if listname:
l = List.get_or_create(settings.USER_ID, listname)
l.add_items(listitems)
2014-05-17 00:14:15 +00:00
trigger_event('activity', {
'activity': 'import',
2014-05-16 14:30:16 +00:00
'progress': [position, len(books)],
'status': {'code': 200, 'text': ''},
2014-05-04 17:26:43 +00:00
'added': added,
})
2014-05-17 00:14:15 +00:00
state.activity = {}