mylar/mylar/readinglist.py

240 lines
12 KiB
Python
Raw Normal View History

2015-03-27 19:07:19 +00:00
# This file is part of Mylar.
#
# Mylar is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mylar is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mylar. If not, see <http://www.gnu.org/licenses/>.
from __future__ import with_statement
import os
import re
import mylar
from mylar import logger, db, helpers
class Readinglist(object):
def __init__(self, filelist=None, IssueID=None, IssueArcID=None):
if IssueID:
self.IssueID = IssueID
else:
self.IssueID = None
if IssueArcID:
self.IssueArcID = IssueArcID
else:
self.IssueArcID = None
if filelist:
self.filelist = filelist
else:
self.filelist = None
self.module = '[READLIST]'
def addtoreadlist(self):
annualize = False
myDB = db.DBConnection()
readlist = myDB.selectone("SELECT * from issues where IssueID=?", [self.IssueID]).fetchone()
if readlist is None:
logger.fdebug(self.module + ' Checking against annuals..')
readlist = myDB.selectone("SELECT * from annuals where IssueID=?", [self.IssueID]).fetchone()
if readlist is None:
logger.error(self.module + ' Cannot locate IssueID - aborting..')
return
else:
annualize = True
comicinfo = myDB.selectone("SELECT * from comics where ComicID=?", [readlist['ComicID']]).fetchone()
logger.info(self.module + ' Attempting to add issueid ' + readlist['IssueID'])
if comicinfo is None:
logger.info(self.module + ' Issue not located on your current watchlist. I should probably check story-arcs but I do not have that capability just yet.')
else:
locpath = None
if mylar.CONFIG.MULTIPLE_DEST_DIRS is not None and mylar.CONFIG.MULTIPLE_DEST_DIRS != 'None' and os.path.join(mylar.CONFIG.MULTIPLE_DEST_DIRS, os.path.basename(comicinfo['ComicLocation'])) != comicinfo['ComicLocation']:
pathdir = os.path.join(mylar.CONFIG.MULTIPLE_DEST_DIRS, os.path.basename(comicinfo['ComicLocation']))
2015-03-27 19:07:19 +00:00
if os.path.exists(os.path.join(pathdir, readlist['Location'])):
locpath = os.path.join(pathdir, readlist['Location'])
else:
if os.path.exists(os.path.join(comicinfo['ComicLocation'], readlist['Location'])):
locpath = os.path.join(comicinfo['ComicLocation'], readlist['Location'])
else:
if os.path.exists(os.path.join(comicinfo['ComicLocation'], readlist['Location'])):
locpath = os.path.join(comicinfo['ComicLocation'], readlist['Location'])
if not locpath is None:
comicissue = readlist['Issue_Number']
comicname = comicinfo['ComicName']
dspinfo = comicname + ' #' + comicissue
if annualize:
if mylar.CONFIG.ANNUALS_ON:
2015-03-27 19:07:19 +00:00
comicissue = 'Annual ' + readlist['Issue_Number']
dspinfo = comicname + ' Annual #' + readlist['Issue_Number']
else:
comicname = comicinfo['ComicName'] + ' Annual'
dspinfo = comicname + ' #' + comicissue
ctrlval = {"IssueID": self.IssueID}
newval = {"DateAdded": helpers.today(),
"Status": "Added",
"ComicID": readlist['ComicID'],
"Issue_Number": comicissue,
"IssueDate": readlist['IssueDate'],
"SeriesYear": comicinfo['ComicYear'],
"ComicName": comicname,
"Location": locpath}
myDB.upsert("readlist", newval, ctrlval)
logger.info(self.module + ' Added ' + dspinfo + ' to the Reading list.')
return
def markasRead(self):
myDB = db.DBConnection()
if self.IssueID:
issue = myDB.selectone('SELECT * from readlist WHERE IssueID=?', [self.IssueID]).fetchone()
if issue['Status'] == 'Read':
NewVal = {"Status": "Added"}
else:
NewVal = {"Status": "Read"}
NewVal['StatusChange'] = helpers.today()
CtrlVal = {"IssueID": self.IssueID}
myDB.upsert("readlist", NewVal, CtrlVal)
logger.info(self.module + ' Marked ' + issue['ComicName'] + ' #' + str(issue['Issue_Number']) + ' as Read.')
elif self.IssueArcID:
issue = myDB.selectone('SELECT * from readinglist WHERE IssueArcID=?', [self.IssueArcID]).fetchone()
if issue['Status'] == 'Read':
NewVal = {"Status": "Added"}
else:
NewVal = {"Status": "Read"}
NewVal['StatusChange'] = helpers.today()
CtrlVal = {"IssueArcID": self.IssueArcID}
myDB.upsert("readinglist", NewVal, CtrlVal)
logger.info(self.module + ' Marked ' + issue['ComicName'] + ' #' + str(issue['IssueNumber']) + ' as Read.')
return
def syncreading(self):
#3 status' exist for the readlist.
# Added (Not Read) - Issue is added to the readlist and is awaiting to be 'sent' to your reading client.
# Read - Issue has been read
# Not Read - Issue has been downloaded to your reading client after the syncfiles has taken place.
module = '[READLIST-TRANSFER]'
myDB = db.DBConnection()
readlist = []
cidlist = []
sendlist = []
if self.filelist is None:
rl = myDB.select('SELECT issues.IssueID, comics.ComicID, comics.ComicLocation, issues.Location FROM readlist LEFT JOIN issues ON issues.IssueID = readlist.IssueID LEFT JOIN comics on comics.ComicID = issues.ComicID WHERE readlist.Status="Added"')
2015-03-27 19:07:19 +00:00
if rl is None:
logger.info(module + ' No issues have been marked to be synced. Aborting syncfiles')
return
for rlist in rl:
readlist.append({"filepath": os.path.join(rlist['ComicLocation'],rlist['Location']),
2015-03-27 19:07:19 +00:00
"issueid": rlist['IssueID'],
"comicid": rlist['ComicID']})
else:
readlist = self.filelist
if len(readlist) > 0:
for clist in readlist:
if clist['filepath'] == 'None' or clist['filepath'] is None:
logger.warn(module + ' There was a problem with ComicID/IssueID: [' + clist['comicid'] + '/' + clist['issueid'] + ']. I cannot locate the file in the given location (try re-adding to your readlist)[' + clist['filepath'] + ']')
2015-03-27 19:07:19 +00:00
continue
else:
# multiplecid = False
# for x in cidlist:
# if clist['comicid'] == x['comicid']:
# comicid = x['comicid']
# comiclocation = x['location']
# multiplecid = True
# if multiplecid == False:
# cid = myDB.selectone("SELECT * FROM comics WHERE ComicID=?", [clist['comicid']]).fetchone()
# if cid is None:
# continue
# else:
# comiclocation = cid['ComicLocation']
# comicid = cid['ComicID']
# if mylar.CONFIG.MULTIPLE_DEST_DIRS is not None and mylar.CONFIG.MULTIPLE_DEST_DIRS != 'None' and os.path.join(mylar.CONFIG.MULTIPLE_DEST_DIRS, os.path.basename(comiclocation)) != comiclocation:
# logger.fdebug(module + ' Multiple_dest_dirs:' + mylar.CONFIG.MULTIPLE_DEST_DIRS)
# logger.fdebug(module + ' Dir: ' + comiclocation)
# logger.fdebug(module + ' Os.path.basename: ' + os.path.basename(comiclocation))
# pathdir = os.path.join(mylar.CONFIG.MULTIPLE_DEST_DIRS, os.path.basename(comiclocation))
if os.path.exists(clist['filepath']):
2015-03-27 19:07:19 +00:00
sendlist.append({"issueid": clist['issueid'],
"filepath": clist['filepath'],
"filename": os.path.split(clist['filepath'])[1]})
# else:
# if os.path.exists(os.path.join(comiclocation, clist['filename'])):
# sendlist.append({"issueid": clist['issueid'],
# "filepath": comiclocation,
# "filename": clist['filename']})
# else:
# if os.path.exists(os.path.join(comiclocation, clist['filename'])):
# sendlist.append({"issueid": clist['issueid'],
# "filepath": comiclocation,
# "filename": clist['filename']})
else:
logger.warn(module + ' ' + clist['filepath'] + ' does not exist in the given location. Remove from the Reading List and Re-add and/or confirm the file exists in the specified location')
continue
# #cidlist is just for this reference loop to not make unnecessary db calls if the comicid has already been processed.
# cidlist.append({"comicid": clist['comicid'],
# "issueid": clist['issueid'],
# "location": comiclocation}) #store the comicid so we don't make multiple sql requests
2015-03-27 19:07:19 +00:00
if len(sendlist) == 0:
logger.info(module + ' Nothing to send from your readlist')
return
logger.info(module + ' ' + str(len(sendlist)) + ' issues will be sent to your reading device.')
2015-03-27 19:07:19 +00:00
# test if IP is up.
import shlex
import subprocess
#fhost = mylar.CONFIG.TAB_HOST.find(':')
host = mylar.CONFIG.TAB_HOST[:mylar.CONFIG.TAB_HOST.find(':')]
if 'windows' not in mylar.OS_DETECT.lower():
cmdstring = str('ping -c1 ' + str(host))
else:
cmdstring = str('ping -n 1 ' + str(host))
cmd = shlex.split(cmdstring)
try:
output = subprocess.check_output(cmd)
2015-05-22 08:32:51 +00:00
except subprocess.CalledProcessError, e:
logger.info(module + ' The host {0} is not Reachable at this time.'.format(cmd[-1]))
return
else:
if 'unreachable' in output:
logger.info(module + ' The host {0} is not Reachable at this time.'.format(cmd[-1]))
return
else:
logger.info(module + ' The host {0} is Reachable. Preparing to send files.'.format(cmd[-1]))
2015-03-27 19:07:19 +00:00
success = mylar.ftpsshup.sendfiles(sendlist)
FIX: included version of comictagger should now work with both Windows and *nix based OS' again, IMP: Global Copy/Move option available when performing post-processing, IMP: Added a verbose file-checking option (FOLDER_SCAN_LOG_VERBOSE) - when enabled will log as it currently does during manual post-processing/file-checking runs, when disabled it will not spam the log nearly as much resulting in more readable log files, IMP: Added Verbose debug logging both via startup option(-v), as well as toggle button in Log GUI (from headphones), as well as per-page loading of log file(s) in GUI, FIX: When doing manual post-processing on issues that were in story arcs, will now indicate X story-arc issues were post-processed for better visibility, FIX: Fixed an issue with deleting from the nzblog table when story arc issues were post-processed, IMP: Added WEEKFOLDER_LOC to the config.ini to allow for specification of where the weekly download directories will default to (as opposed to off of ComicLocation root), IMP: Better handling of some special character references in series titles when looking for series on the auto-wanted list, IMP: 32P will now auto-disable provider if logon returns invalid credentials, FIX: When using alt_pull on weekly pull list, xA0 unicode character caused error, FIX: If title had invalid character in filename that was replaced with a character that already existed in the title, would not scan in during file-checking, FIX: When searching for a series (weeklypull-list/add a series), if the title contained 'and' or '&' would return really mixed up results, FIX: When Post-Processing, if filename being processed had special characters (ie. comma) and was different than nzbname, in some cases would fail to find/move issues, IMP: Utilize internal comictagger to convert from cbr/cbz, IMP: Added more checks when post-processing to ensure files are handled correctly, IMP: Added meta-tag reading when importing series/issues - if previously tagged with CT, will reverse look-up the provided IssueID to reference the correct ComicID, IMP: If scanned directory during import contins cvinfo file, use that and force the ComicID to entire directory when importing a series, IMP: Manual meta-tagging issues will no longer create temporary directories and/or create files in the Comic Location root causing problems for some users, FIX: Annuals weren't properly sorted upon loading of comic details page for some series, IMP: Added some extra checks when validating/creating directories, FIX: Fixed a problem when displaying some covers of .cbz files on the comic details page
2016-01-26 07:49:56 +00:00
if success == 'fail':
return
2015-03-27 19:07:19 +00:00
if len(success) > 0:
for succ in success:
newCTRL = {"issueid": succ['issueid']}
newVAL = {"Status": 'Downloaded',
"StatusChange": helpers.today()}
2015-03-27 19:07:19 +00:00
myDB.upsert("readlist", newVAL, newCTRL)