def cron_run(conn): ''' This function will be called when the script is executed by cron. This will read the jobs and try to find sub downloads for each of them ''' # get all eps all_eps = db.get_all_eps(conn) to_download = [] for ep in all_eps: if ep.sid and ep.season and ep.ep: sublinks = bierdopje.get_subs(ep.sid, SUB_LANG, ep.season, ep.ep) sub = nameparser.find_link(ep.job_name, sublinks) if sub and os.path.exists(ep.final_loc): ep.sub = sub to_download.append(ep) else: ep_name = os.path.splitext(os.path.expanduser(ep.final_loc))[0] if os.path.exists(ep_name + '.srt'): # Mabe user downloaded sub for this ep manually? db.remove_single(conn, ep) print(u'Cleaned up db because ' + ep_name + ' already has ' 'subs!') elif not os.path.exists(ep.final_loc): db.remove_single(conn, ep) print(u'Cleaned up db because ' + ep_name + ' is no ' 'longer available on disk!') if not to_download: if not quiet: print "No subs available for any of your eps yet!" return True for d in to_download: d.result = download(d) # remove successfully downloaded files from db successful = filter(lambda x: x.result, to_download) db.remove_downloaded(conn, successful) # check if all files are parsed successfully result = all([ep.result for ep in to_download]) # call post-processing for successfully downloaded files if not POST_CALL: return result for d in successful: for script in POST_CALL.split(','): to_call = shlex.split(script) to_call.append(d.final_loc) subprocess.call(to_call) # return result return result
def cron_run(conn): """ This function will be called when the script is executed by cron. This will read the jobs and try to find sub downloads for each of them """ # get all eps all_eps = db.get_all_eps(conn) to_download = {} subdl = periscope.Periscope("cache") for ep in all_eps: subs = subdl.listSubtitles(ep.final_loc, [SUB_LANG]) if subs and os.path.exists(ep.final_loc): to_download[ep] = subs else: ep_name = os.path.splitext(os.path.expanduser(ep.final_loc))[0] if os.path.exists(ep_name + ".srt"): # Mabe user downloaded sub for this ep manually? db.remove_single(conn, ep) print (u"Cleaned up db because " + ep_name + " already has subs!") elif not os.path.exists(ep.final_loc): db.remove_single(conn, ep) print (u"Cleaned up db because " + ep_name + " is no longer available on disk!") time.sleep(3) if not to_download: if not quiet: print "No subs available for any of your eps yet!" return True successful = [] for d in to_download: if subdl.attemptDownloadSubtitle(to_download[d], [SUB_LANG]) is not None: successful.append(d) # remove successfully downloaded files from db db.remove_downloaded(conn, successful) # call post-processing for successfully downloaded files if POST_CALL: for d in successful: for script in POST_CALL.split(","): to_call = shlex.split(script) to_call.append(d.final_loc) subprocess.call(to_call)
def find_subs(eps): """ This function searches for available subtitle files in each of the Ep instances in eps, using the periscope subtitle download library. """ to_download = {} try: import xdg.BaseDirectory as bd cache_folder = os.path.join(bd.xdg_config_home, "periscope") except ImportError: cache_folder = os.path.join(os.path.expanduser("~"), ".config", "periscope") subdl = periscope.Periscope(cache_folder) for ep in eps: ep_name = os.path.splitext(os.path.expanduser(ep.final_loc))[0] logging.info("Searching subs for {0}".format(ep_name)) if not os.path.exists(ep.final_loc): db.remove_single(conn, ep) logging.info(u"Cleaned up db because {0} is no longer available on disk!".format(ep_name)) continue if glob.glob(ep_name + "*.srt"): # Maybe user downloaded sub for this ep manually? db.remove_single(conn, ep) logging.info(u"Cleaned up db because {0} already has subs!".format(ep_name)) continue if (datetime.date.today() - ep.added_on).days > CLEAN_AFTER: # Clean this ep as it's too old db.remove_single(conn, ep) logging.info( u"Cleaned up db because {0} is older than {1} days, and there are still no subs!".format( ep_name, CLEAN_AFTER ) ) continue subs = subdl.listSubtitles(ep.final_loc, [SUB_LANG]) if subs: to_download[ep] = subs time.sleep(3) if not to_download: logging.info("No subs available for any of your eps yet!") return successful = [] for d in to_download: if subdl.attemptDownloadSubtitle(to_download[d], [SUB_LANG]) is not None: successful.append(d) # append languages if APPEND_LANG: for s in successful: base = os.path.splitext(s.final_loc)[0] old_sub = base + ".srt" if os.path.isfile(old_sub): os.rename(old_sub, ".".join((base, SUB_LANG, "srt"))) # remove successfully downloaded files from db db.remove_downloaded(conn, successful) # call post-processing for successfully downloaded files if POST_CALL: for d in successful: for script in POST_CALL.split(","): to_call = shlex.split(script) to_call.append(d.final_loc) subprocess.call(to_call)