Skip to content

Commit

Permalink
Merge pull request #598 from philborman/master
Browse files Browse the repository at this point in the history
Various bug fixes and enhancements
  • Loading branch information
philborman authored Jan 12, 2017
2 parents 966122f + 2cb25e8 commit 7d5471a
Show file tree
Hide file tree
Showing 14 changed files with 225 additions and 155 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,7 @@ Open localhost:5299/home or click the LazyLibrarian icon in the top left. Type a

## Update
Auto update available via interface from master

## Docker packages
x64 version here : https://hub.docker.com/r/linuxserver/lazylibrarian/
armhf version here: https://hub.docker.com/r/lsioarmhf/lazylibrarian/
11 changes: 11 additions & 0 deletions data/interfaces/bookstrap/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,17 @@
<input type="checkbox" id="tor_convert_magnet" name="tor_convert_magnet" value="1" ${checked} />
Convert magnet links into torrent files</label>
</div>
<div class="checkbox">
<%
if lazylibrarian.PREFER_MAGNET == True:
checked = 'checked="checked"'
else:
checked = ''
%>
<label for="prefer_magnet" class="control-label">
<input type="checkbox" id="prefer_magnet" name="prefer_magnet" value="1" ${checked} />
Prefer magnet links to torrent files</label>
</div>
</fieldset>
<div class="form-group">
<label for="numberofseeders" class="control-label">Minimum seeders:</label>
Expand Down
4 changes: 2 additions & 2 deletions data/interfaces/bookstrap/searchresults.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ <h1>Search Results</h1>
value = "Have Author"
but_class = "btn-info"
else:
destination = "addAuthor?AuthorName="+result['safeauthorname']
destination = "addAuthorID?AuthorID="+result['authorid']+'&AuthorName='+result['safeauthorname']
value = "Add Author"
but_class = "btn-success"

else:
destination = "addAuthor?AuthorName="+result['safeauthorname']
destination = "addAuthorID?AuthorID="+result['authorid']+'&AuthorName='+result['safeauthorname']
value = "Add Author"
but_class = "btn-success"

Expand Down
10 changes: 10 additions & 0 deletions data/interfaces/default/config.html
Original file line number Diff line number Diff line change
Expand Up @@ -599,6 +599,16 @@ <h3>Torrents</h3>
<input type="checkbox" name="tor_convert_magnet" id="tor_convert_magnet" value="1" ${checked} />
<label>Convert magnet links into torrent files</label>
</div>
<div class="row checkbox">
<%
if lazylibrarian.PREFER_MAGNET == True:
checked = 'checked="checked"'
else:
checked = ''
%>
<input type="checkbox" name="prefer_magnet" id="prefer_magnet" value="1" ${checked} />
<label>Prefer magnet links to torrent files</label>
</div>
</fieldset>
<div>
<label>Minimum seeders:</label>
Expand Down
4 changes: 2 additions & 2 deletions data/interfaces/default/searchresults.html
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ <h1>Search Results<h1>
value = "Have Author"
but_class = "button green"
else:
destination = "addAuthor?AuthorName="+result['safeauthorname']
destination = "addAuthorID?AuthorID="+result['authorid']+'&AuthorName='+result['safeauthorname']
value = "Add Author"
but_class = "button"

else:
destination = "addAuthor?AuthorName="+result['safeauthorname']
destination = "addAuthorID?AuthorID="+result['authorid']+'&AuthorName='+result['safeauthorname']
value = "Add Author"
but_class = "button"

Expand Down
5 changes: 4 additions & 1 deletion lazylibrarian/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
NUMBEROFSEEDERS = 10
KEEP_SEEDING = 0
TORRENT_DIR = None
PREFER_MAGNET = 0

RTORRENT_HOST = None
RTORRENT_USER = None
Expand Down Expand Up @@ -499,7 +500,7 @@ def config_read(reloaded=False):
TOR_DOWNLOADER_SYNOLOGY, TOR_DOWNLOADER_DELUGE, DELUGE_HOST, DELUGE_USER, DELUGE_PASS, DELUGE_PORT, \
DELUGE_LABEL, FULL_SCAN, ADD_AUTHOR, NOTFOUND_STATUS, NEWBOOK_STATUS, NEWAUTHOR_STATUS, \
USE_NMA, NMA_APIKEY, NMA_PRIORITY, NMA_ONSNATCH, NMA_ONDOWNLOAD, \
GIT_USER, GIT_REPO, GIT_BRANCH, INSTALL_TYPE, CURRENT_VERSION, COMMIT_LIST, \
GIT_USER, GIT_REPO, GIT_BRANCH, INSTALL_TYPE, CURRENT_VERSION, COMMIT_LIST, PREFER_MAGNET, \
LATEST_VERSION, COMMITS_BEHIND, NUMBEROFSEEDERS, KEEP_SEEDING, SCHED, CACHE_HIT, CACHE_MISS, \
BOOKSTRAP_THEME, LOGFILES, LOGSIZE, HTTPS_ENABLED, HTTPS_CERT, HTTPS_KEY

Expand Down Expand Up @@ -711,6 +712,7 @@ def config_read(reloaded=False):
NUMBEROFSEEDERS = check_setting_int(CFG, 'TORRENT', 'numberofseeders', 10)
TOR_DOWNLOADER_DELUGE = check_setting_bool(CFG, 'TORRENT', 'tor_downloader_deluge', 0)
KEEP_SEEDING = check_setting_bool(CFG, 'TORRENT', 'keep_seeding', 1)
PREFER_MAGNET = check_setting_bool(CFG, 'TORRENT', 'prefer_magnet', 1)
TORRENT_DIR = check_setting_str(CFG, 'TORRENT', 'torrent_dir', '')

RTORRENT_HOST = check_setting_str(CFG, 'RTORRENT', 'rtorrent_host', '')
Expand Down Expand Up @@ -1067,6 +1069,7 @@ def config_write():
CFG.set('TORRENT', 'numberofseeders', NUMBEROFSEEDERS)
CFG.set('TORRENT', 'torrent_dir', TORRENT_DIR)
CFG.set('TORRENT', 'keep_seeding', KEEP_SEEDING)
CFG.set('TORRENT', 'prefer_magnet', PREFER_MAGNET)
#
check_section('RTORRENT')
CFG.set('RTORRENT', 'rtorrent_host', RTORRENT_HOST)
Expand Down
12 changes: 12 additions & 0 deletions lazylibrarian/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
'moveBooks': '&fromname= &toname= move all books from one author to another by AuthorName',
'moveBook': '&id= &toid= move one book to new author by BookID and AuthorID',
'addAuthor': '&name= add author to database by name',
'addAuthorID': '&id= add author to database by AuthorID',
'removeAuthor': '&id= remove author from database by AuthorID',
'addMagazine': '&name= add magazine to database by name',
'removeMagazine': '&name= remove magazine and all of its issues from database by name',
Expand Down Expand Up @@ -605,6 +606,17 @@ def _addAuthor(self, **kwargs):
except Exception as e:
self.data = str(e)

def _addAuthorID(self, **kwargs):
if 'id' not in kwargs:
self.data = 'Missing parameter: id'
return
else:
self.id = kwargs['id']
try:
addAuthorToDB('', refresh=False, self.id)
except Exception as e:
self.data = str(e)

def _searchBook(self, **kwargs):
if 'id' not in kwargs:
self.data = 'Missing parameter: id'
Expand Down
3 changes: 2 additions & 1 deletion lazylibrarian/gr.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ def find_author_id(self, refresh=False):
authorlist = self.get_author_info(authorid, authorname)
return authorlist


def get_author_info(self, authorid=None, authorname=None):

URL = 'http://www.goodreads.com/author/show/' + authorid + '.xml?' + urllib.urlencode(self.params)
Expand All @@ -220,8 +221,8 @@ def get_author_info(self, authorid=None, authorname=None):
logger.warn('No author found with ID: ' + authorid)
else:
logger.debug("[%s] Processing info for authorID: %s" % (authorname, authorid))

# PAB added authorname to author_dict - this holds the intact name preferred by GR
authorname = resultxml[1].text
author_dict = {
'authorid': resultxml[0].text,
'authorlink': resultxml.find('link').text,
Expand Down
111 changes: 77 additions & 34 deletions lazylibrarian/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,50 +24,93 @@
from lazylibrarian.gr import GoodReads


def addAuthorToDB(authorname=None, refresh=False):
def addAuthorToDB(authorname=None, refresh=False, authorid=None):
"""
Add an author to the database, and get list of all their books
Add an author to the database by name or id, and get a list of all their books
If author already exists in database, refresh their details and booklist
"""
try:
myDB = database.DBConnection()
match = False
if authorid:
GR = GoodReads('unknown author')

GR = GoodReads(authorname)
query = "SELECT * from authors WHERE AuthorID='%s'" % authorid
dbauthor = myDB.match(query)
controlValueDict = {"AuthorID": authorid}
newValueDict = {"Status": "Loading"}

query = "SELECT * from authors WHERE AuthorName='%s'" % authorname.replace("'", "''")
dbauthor = myDB.match(query)
controlValueDict = {"AuthorName": authorname}
if not dbauthor:
logger.debug("Now adding new author id: %s to database" % authorid)
else:
logger.debug("Now updating author id: %s" % authorid)
myDB.upsert("authors", newValueDict, controlValueDict)

if not dbauthor:
newValueDict = {
"AuthorID": "0: %s" % authorname,
"Status": "Loading"
}
logger.debug("Now adding new author: %s to database" % authorname)
else:
newValueDict = {"Status": "Loading"}
logger.debug("Now updating author: %s" % authorname)
myDB.upsert("authors", newValueDict, controlValueDict)

author = GR.find_author_id(refresh=refresh)
if author:
authorid = author['authorid']
authorlink = author['authorlink']
authorimg = author['authorimg']
author = GR.get_author_info(authorid=authorid, authorname='unknown author')
if author:
authorname = author['authorname']
authorlink = author['authorlink']
authorimg = author['authorimg']
controlValueDict = {"AuthorID": authorid}
newValueDict = {
"AuthorName": authorname,
"AuthorLink": authorlink,
"AuthorImg": authorimg,
"AuthorBorn": author['authorborn'],
"AuthorDeath": author['authordeath'],
"DateAdded": today(),
}
myDB.upsert("authors", newValueDict, controlValueDict)
GR = GoodReads(authorname)
match = True
else:
logger.warn(u"Nothing found for %s" % authorid)
if not dbauthor:
myDB.action('DELETE from authors WHERE AuthorID="%s"' % authorid)

if authorname and not match:
authorname = ' '.join(authorname.split()) # ensure no extra whitespace
GR = GoodReads(authorname)

query = "SELECT * from authors WHERE AuthorName='%s'" % authorname.replace("'", "''")
dbauthor = myDB.match(query)
controlValueDict = {"AuthorName": authorname}
newValueDict = {
"AuthorID": authorid,
"AuthorLink": authorlink,
"AuthorImg": authorimg,
"AuthorBorn": author['authorborn'],
"AuthorDeath": author['authordeath'],
"DateAdded": today(),
"Status": "Loading"
}

if not dbauthor:
newValueDict = {
"AuthorID": "0: %s" % authorname,
"Status": "Loading"
}
logger.debug("Now adding new author: %s to database" % authorname)
else:
newValueDict = {"Status": "Loading"}
logger.debug("Now updating author: %s" % authorname)
myDB.upsert("authors", newValueDict, controlValueDict)
else:
logger.warn(u"Nothing found for %s" % authorname)
myDB.action('DELETE from authors WHERE AuthorName="%s"' % authorname)

author = GR.find_author_id(refresh=refresh)
if author:
authorid = author['authorid']
authorlink = author['authorlink']
authorimg = author['authorimg']
controlValueDict = {"AuthorName": authorname}
newValueDict = {
"AuthorID": authorid,
"AuthorLink": authorlink,
"AuthorImg": authorimg,
"AuthorBorn": author['authorborn'],
"AuthorDeath": author['authordeath'],
"DateAdded": today(),
"Status": "Loading"
}
myDB.upsert("authors", newValueDict, controlValueDict)
match = True
else:
logger.warn(u"Nothing found for %s" % authorname)
if not dbauthor:
myDB.action('DELETE from authors WHERE AuthorName="%s"' % authorname)
return
if not match:
logger.error("AddAuthorToDB: No matching result for authorname or authorid")
return

new_img = False
Expand Down
28 changes: 16 additions & 12 deletions lazylibrarian/postprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,20 +129,22 @@ def try_rename(directory, filename):
# or 8bit ascii str if it can't convert the filename to unicode
# eg 'Stephen Hawking - A Brief History of Time (PDF&EPUB&MOB\xc4\xb0)\xb0\x06'
# Return the new filename or empty string if failed
try:
# try decode first in case we called listdir with str instead of unicode
filename = filename.decode(lazylibrarian.SYS_ENCODING)
return filename
except Exception:
logger.error("Unable to convert %s to sys encoding" % repr(filename))
# strip out any non-ascii characters and try to rename
newfname = ''.join([c for c in filename if 128 > ord(c) > 31])
if isinstance(filename, str):
try:
os.rename(os.path.join(directory, filename), os.path.join(directory, newfname))
return newfname
# try decode first in case we called listdir with str instead of unicode
filename = filename.decode(lazylibrarian.SYS_ENCODING)
return filename
except Exception:
logger.error("Unable to rename %s" % repr(filename))
return ""
logger.error("Unable to convert %s to sys encoding" % repr(filename))

# strip out any non-ascii characters and try to rename
newfname = ''.join([c for c in filename if 128 > ord(c) > 31])
try:
os.rename(os.path.join(directory, filename), os.path.join(directory, newfname))
return newfname
except Exception:
logger.error("Unable to rename %s" % repr(filename))
return ""


def move_into_subdir(processpath, targetdir, fname):
Expand Down Expand Up @@ -344,6 +346,7 @@ def processDir(reset=False):
if data: # it's a book
logger.debug(u'Processing book %s' % book['BookID'])
authorname = data['AuthorName']
authorname = ' '.join(authorname.split()) # ensure no extra whitespace
bookname = data['BookName']
if 'windows' in platform.system().lower() and '/' in lazylibrarian.EBOOK_DEST_FOLDER:
logger.warn('Please check your EBOOK_DEST_FOLDER setting')
Expand Down Expand Up @@ -621,6 +624,7 @@ def import_book(pp_path=None, bookID=None):
data = myDB.match('SELECT * from books WHERE BookID="%s"' % bookID)
if data:
authorname = data['AuthorName']
authorname = ' '.join(authorname.split()) # ensure no extra whitespace
bookname = data['BookName']
processpath = lazylibrarian.DIRECTORY('Destination')

Expand Down
Loading

0 comments on commit 7d5471a

Please sign in to comment.