diff --git a/mylar/ftpsshup.py b/mylar/ftpsshup.py index 52981619..f599dea3 100644 --- a/mylar/ftpsshup.py +++ b/mylar/ftpsshup.py @@ -68,6 +68,7 @@ def putfile(localpath,file): #localpath=full path to .torrent (including file return "pass" def sendfiles(filelist): + try: import paramiko except ImportError: @@ -97,10 +98,7 @@ def sendfiles(filelist): if len(filelist) > 0: logger.info('Initiating send for ' + str(len(filelist)) + ' files...') - logger.info(sftp) - logger.info(filelist) - logger.info(transport) - sendtohome(sftp, remotepath, filelist, transport) + return sendtohome(sftp, remotepath, filelist, transport) def sendtohome(sftp, remotepath, filelist, transport): @@ -109,6 +107,7 @@ def sendtohome(sftp, remotepath, filelist, transport): port = int(mylar.TAB_HOST[fhost+1:]) successlist = [] + filestotal = len(filelist) for files in filelist: tempfile = files['filename'] @@ -131,18 +130,26 @@ def sendtohome(sftp, remotepath, filelist, transport): remdir = remotepath - localsend = os.path.join(files['filepath'], files['filename']) + localsend = files['filepath'] logger.info('Sending : ' + localsend) remotesend = os.path.join(remdir,filename) logger.info('To : ' + remotesend) + try: + sftp.stat(remotesend) + except IOError, e: + if e[0] == 2: + filechk = False + else: + filechk = True + if not filechk: sendcheck = False count = 1 while sendcheck == False: try: - sftp.put(localsend, remotesend) + sftp.put(localsend, remotesend)#, callback=printTotals) sendcheck = True except Exception, e: logger.info('Attempt #' + str(count) + ': ERROR Sending issue to seedbox *** Caught exception: %s: %s' % (e.__class__,e)) @@ -164,7 +171,7 @@ def sendtohome(sftp, remotepath, filelist, transport): else: logger.info('file already exists - checking if complete or not.') filesize = sftp.stat(remotesend).st_size - if not filesize == files['filesize']: + if not filesize == os.path.getsize(files['filepath']): logger.info('file not complete - attempting to resend') sendcheck = False count = 1 @@ -198,7 +205,11 @@ def sendtohome(sftp, remotepath, filelist, transport): sftp.close() transport.close() logger.fdebug('Upload of readlist complete.') - return + return successlist + +#def printTotals(transferred, toBeTransferred): +# percent = transferred / toBeTransferred +# logger.info("Transferred: " + str(transferred) + " Out of " + str(toBeTransferred)) #if __name__ == '__main__': # putfile(sys.argv[1]) diff --git a/mylar/readinglist.py b/mylar/readinglist.py index 2cb2ae19..2babb95e 100644 --- a/mylar/readinglist.py +++ b/mylar/readinglist.py @@ -143,7 +143,7 @@ class Readinglist(object): return for rlist in rl: - readlist.append({"filename": rlist['Location'], + readlist.append({"filepath": rlist['Location'], "issueid": rlist['IssueID'], "comicid": rlist['ComicID']}) @@ -153,77 +153,82 @@ class Readinglist(object): if len(readlist) > 0: for clist in readlist: - if clist['filename'] == 'None' or clist['filename'] 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)') + 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'] + ']') continue else: - multiplecid = False - for x in cidlist: - if clist['comicid'] == x['comicid']: - comicid = x['comicid'] - comiclocation = x['location'] - multiplecid = True +# 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 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.MULTIPLE_DEST_DIRS is not None and mylar.MULTIPLE_DEST_DIRS != 'None' and os.path.join(mylar.MULTIPLE_DEST_DIRS, os.path.basename(comiclocation)) != comiclocation: - logger.fdebug(module + ' Multiple_dest_dirs:' + mylar.MULTIPLE_DEST_DIRS) - logger.fdebug(module + ' Dir: ' + comiclocation) - logger.fdebug(module + ' Os.path.basename: ' + os.path.basename(comiclocation)) - pathdir = os.path.join(mylar.MULTIPLE_DEST_DIRS, os.path.basename(comiclocation)) - if os.path.exists(os.path.join(pathdir, clist['filename'])): +# if mylar.MULTIPLE_DEST_DIRS is not None and mylar.MULTIPLE_DEST_DIRS != 'None' and os.path.join(mylar.MULTIPLE_DEST_DIRS, os.path.basename(comiclocation)) != comiclocation: +# logger.fdebug(module + ' Multiple_dest_dirs:' + mylar.MULTIPLE_DEST_DIRS) +# logger.fdebug(module + ' Dir: ' + comiclocation) +# logger.fdebug(module + ' Os.path.basename: ' + os.path.basename(comiclocation)) +# pathdir = os.path.join(mylar.MULTIPLE_DEST_DIRS, os.path.basename(comiclocation)) + if os.path.exists(clist['filepath']): sendlist.append({"issueid": clist['issueid'], - "filepath": pathdir, - "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: - 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['filename'] + ' does not exist in the location I expect [' + comiclocation + ']. Remove from the Reading List and Re-add and/or confirm the file exists in the specified location') - continue + "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 +# #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 if len(sendlist) == 0: logger.info(module + ' Nothing to send from your readlist') return - logger.info(module + ' Preparing to send ' + str(len(sendlist)) + ' issues to your reading device.') + logger.info(module + ' ' + str(len(sendlist)) + ' issues will be sent to your reading device.') # test if IP is up. -# import shlex -# import subprocess + import shlex + import subprocess -# cmdstring = str('ping -c1 ' + str(mylar.TAB_HOST)) -# cmd = shlex.split(cmdstring) -# try: -# output = subprocess.check_output(cmd) -# except subprocess.CalledProcessError,e: -# logger.info('The host {0} is not Reachable at this time.'.format(cmd[-1])) -# return -# else: -# logger.info('The host {0} is Reachable. Preparing to send files.'.format(cmd[-1])) + #fhost = mylar.TAB_HOST.find(':') + host = mylar.TAB_HOST[:mylar.TAB_HOST.find(':')] + + cmdstring = str('ping -c1 ' + str(host)) + cmd = shlex.split(cmdstring) + try: + output = subprocess.check_output(cmd) + except subprocess.CalledProcessError,e: + 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])) success = mylar.ftpsshup.sendfiles(sendlist) + if len(success) > 0: for succ in success: newCTRL = {"issueid": succ['issueid']} - newVAL = {"Status": 'Downloaded'} + newVAL = {"Status": 'Downloaded', + "StatusChange": helpers.today()} myDB.upsert("readlist", newVAL, newCTRL) diff --git a/mylar/webserve.py b/mylar/webserve.py index 77c654e1..ae30319e 100755 --- a/mylar/webserve.py +++ b/mylar/webserve.py @@ -2079,7 +2079,7 @@ class WebInterface(object): myDB.action('DELETE from readlist WHERE IssueID=?', [IssueID]) elif action == 'Send': logger.fdebug('Queuing ' + mi['Location'] + ' to send to tablet.') - sendtablet_queue.append({"filename": mi['Location'], + sendtablet_queue.append({"filepath": mi['Location'], "issueid": IssueID, "comicid": mi['ComicID']}) if len(sendtablet_queue) > 0: