def search_subtitles( file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack ): #standard input ok = False msg = "" osdb_server = OSDBServer() osdb_server.create() subtitles_list = [] file_size = "" hashTry = "" language1 = languageTranslate(lang1,0,1) language2 = languageTranslate(lang2,0,1) language3 = languageTranslate(lang3,0,1) if set_temp : hash_search = False file_size = "000000000" SubHash = "000000000000" else: try: file_size, SubHash = hashFile(file_original_path, False) log( __name__ ,"xbmc module hash and size") hash_search = True except: file_size = "" SubHash = "" hash_search = False if file_size != "" and SubHash != "": log( __name__ ,"File Size [%s]" % file_size ) log( __name__ ,"File Hash [%s]" % SubHash) if hash_search : log( __name__ ,"Search for [%s] by hash" % (os.path.basename( file_original_path ),)) subtitles_list, session_id = osdb_server.searchsubtitles_pod( SubHash ,language1, language2, language3, stack) if not subtitles_list: log( __name__ ,"Search for [%s] by name" % (os.path.basename( file_original_path ),)) subtitles_list = osdb_server.searchsubtitlesbyname_pod( title, tvshow, season, episode, language1, language2, language3, year, stack ) return subtitles_list, "", "" #standard output
def search_subtitles(file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack): #standard input ok = False msg = "" hash_search = False subtitles_list = [] if len(tvshow) > 0: # TvShow OS_search_string = ("%s S%.2dE%.2d" % ( tvshow, int(season), int(episode), )).replace(" ", "+") else: # Movie or not in Library if str(year) == "": # Not in Library title, year = xbmc.getCleanMovieTitle(title) else: # Movie in Library year = year title = title OS_search_string = title.replace(" ", "+") log(__name__, "Search String [ %s ]" % (OS_search_string, )) if set_temp: hash_search = False file_size = "000000000" SubHash = "000000000000" else: try: file_size, SubHash = hashFile(file_original_path, rar) log(__name__, "xbmc module hash and size") hash_search = True except: file_size = "" SubHash = "" hash_search = False if file_size != "" and SubHash != "": log(__name__, "File Size [%s]" % file_size) log(__name__, "File Hash [%s]" % SubHash) log( __name__, "Search by hash and name %s" % (os.path.basename(file_original_path), )) subtitles_list, msg = OSDBServer().searchsubtitles(OS_search_string, lang1, lang2, lang3, hash_search, SubHash, file_size) return subtitles_list, "", msg #standard output
def setState(self): """ The sqlaclchemy object that will hold state. If the file already exists within the database we will not """ def getRecords(): return self.db.session.query(UnFiles).filter( UnFiles.file_name == self.fileName).all() records = getRecords() if records: if len(records) == 1: self.dbUnFileRecord = records[0] self.bookId = records[0].id self.downloadFile() else: raise "Data Error. More than 1 record returned for file %".format( self.fileName) else: try: self.downloadFile() record = UnFiles() record.file_url = self.fileUrl record.file_name = self.fileUrl.split('/')[-1] record.source_hash = hashFile(self.dlPath + self.fileUrl.split('/')[-1]) record.created_at = datetime.datetime.utcnow().strftime( '%Y-%m-%d %H:%M:%S') record.url_country_name = self.urlCountryName except UnicodeEncodeError as e: record.url_country_name = self.encodeLabel(self.urlCountryName) finally: self.db.session.add(record) self.db.session.commit() self.dbUnFileRecord = record self.bookId = record.id
def search_subtitles( file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack ): #standard input ok = False msg = "" hash_search = False subtitles_list = [] if len(tvshow) > 0: # TvShow OS_search_string = ("%s S%.2dE%.2d" % (tvshow, int(season), int(episode),) ).replace(" ","+") else: # Movie or not in Library if str(year) == "": # Not in Library title, year = xbmc.getCleanMovieTitle( title ) else: # Movie in Library year = year title = title OS_search_string = title.replace(" ","+") log( __name__ , "Search String [ %s ]" % (OS_search_string,)) if set_temp : hash_search = False file_size = "000000000" SubHash = "000000000000" else: try: file_size, SubHash = hashFile(file_original_path, rar) log( __name__ ,"xbmc module hash and size") hash_search = True except: file_size = "" SubHash = "" hash_search = False if file_size != "" and SubHash != "": log( __name__ ,"File Size [%s]" % file_size ) log( __name__ ,"File Hash [%s]" % SubHash) log( __name__ ,"Search by hash and name %s" % (os.path.basename( file_original_path ),)) subtitles_list, msg = OSDBServer().searchsubtitles( OS_search_string, lang1, lang2, lang3, hash_search, SubHash, file_size ) return subtitles_list, "", msg #standard output
def set_filehash(path,rar): if rar: path = os.path.dirname( path ) file_hash = hashFile(path) return file_hash
def getallsubs(content, title, subtitles_list, file_original_path, stack, lang1, lang2, lang3): soup = BeautifulSoup(content) subs = soup("tr") sub_str = str(subs[1:]) first_row = True languages_map = {'Polski': 'pl', 'Angielski': 'en', 'Niemiecki': 'de'} for row in subs[1:]: sub_number_re = 'a href=\"/download/(\d+)/\"><strong>' title_re = '<a href="/download/\d+?/"><strong>(.+?)</strong></a>' release_re = '<td>(.+?)<br />|<td.+?>(.+?)<br />' rating_re = 'rednia ocena: (\d\,\d\d)<br />' lang_re = 'zyk:.+?alt="(.+?)"' disc_amount_re = '<td.+?style="text-align: center;">[\r\n\t ]+?(\d)[\r\n\t ]+?</td>' video_file_size_re = 'Rozmiar pliku: <strong>(\d+?)</strong>' video_file_size_re_multi = 'Rozmiar pliku:<br />- CD1: <strong>(\d+?)</strong>' archive_re = '<a href="/download/archiwum/(\d+?)/">' row_str = str(row) archive = re.findall(archive_re, row_str) if len(archive) == 0: if first_row == True: sub_number = re.findall(sub_number_re, row_str) subtitle = re.findall(title_re, row_str) release = re.findall(release_re, row_str) disc_amount = re.findall(disc_amount_re, row_str) first_row = False else: file_size, SubHash = hashFile(file_original_path, False) if disc_amount[0] > '1': video_file_size = re.findall(video_file_size_re_multi, row_str) else: video_file_size = re.findall(video_file_size_re, row_str) if len(video_file_size) == 0: video_file_size.append('0') sync_value = False else: video_file_size = unicode(video_file_size[0], "UTF-8") video_file_size = video_file_size.replace(u"\u00A0", "") if file_size == video_file_size: sync_value = True else: sync_value = False rating = re.findall(rating_re, row_str) language = re.findall(lang_re, row_str) if language[0] in languages_map: language = [languages_map[language[0]]] else: language = [] if len(language) > 0: first_row = True link = "%s%s/" % (down_url, sub_number[0]) log( __name__, "Subtitles found: %s %s (link=%s)" % (subtitle[0], release, link)) flag_pic = "flags/%s.gif" % (language[0]) lang = languageTranslate(language[0], 2, 0) if lang == lang1 or lang == lang2 or lang == lang3: for rel in re.findall("\'(.*?)\'", str(release)): rel = rel.replace(",", ":").replace(" ", "") if len(rel) > 1: rel_semicolon = "%s;" % (rel) for rel_sync in re.findall( '(.+?);', rel_semicolon): if rel_sync.upper( ) in file_original_path.upper(): sync_value = True filename_release = "%s - %s" % (subtitle[0], rel_semicolon) rating_dot = rating[0].replace(",", ".") if rating_dot == '0.00': sub_rating = '0' else: sub_rating = int( round(float(rating_dot) * 1.666, 0)) if stack == False: if disc_amount[0] > '1': log(__name__, "Nonstacked video file - stacked subs") else: subtitles_list.append({ 'filename': filename_release, 'sync': sync_value, 'link': link, 'language_flag': flag_pic, 'language_name': lang, 'rating': '%s' % (sub_rating) }) else: if disc_amount[0] > '1': subtitles_list.append({ 'filename': filename_release, 'sync': sync_value, 'link': link, 'language_flag': flag_pic, 'language_name': lang, 'rating': '%s' % (sub_rating) }) else: log(__name__, "Stacked video file - nonstacked subs") else: continue else: continue
def getallsubs(content, title, subtitles_list, file_original_path, stack, lang1, lang2, lang3): soup = BeautifulSoup(content) subs = soup("tr") sub_str = str(subs[1:]) first_row = True languages_map = {'Polski': 'pl', 'Angielski': 'en', 'Niemiecki': 'de'} for row in subs[1:]: sub_number_re = 'a href=\"/download/(\d+)/\"><strong>' title_re = '<a href="/download/\d+?/"><strong>(.+?)</strong></a>' release_re = '<td>(.+?)<br />|<td.+?>(.+?)<br />' rating_re = 'rednia ocena: (\d\,\d\d)<br />' lang_re = 'zyk:.+?alt="(.+?)"' disc_amount_re = '<td.+?style="text-align: center;">[\r\n\t ]+?(\d)[\r\n\t ]+?</td>' video_file_size_re = 'Rozmiar pliku: <strong>(\d+?)</strong>' video_file_size_re_multi = 'Rozmiar pliku:<br />- CD1: <strong>(\d+?)</strong>' archive_re = '<a href="/download/archiwum/(\d+?)/">' row_str = str(row) archive = re.findall(archive_re, row_str) if len(archive) == 0: if first_row == True: sub_number = re.findall(sub_number_re, row_str) subtitle = re.findall(title_re, row_str) release = re.findall(release_re, row_str) disc_amount = re.findall(disc_amount_re, row_str) first_row = False else: file_size, SubHash = hashFile(file_original_path, False) if disc_amount[0] > '1': video_file_size = re.findall(video_file_size_re_multi, row_str) else: video_file_size = re.findall(video_file_size_re, row_str) if len(video_file_size) == 0: video_file_size.append('0') sync_value = False else: video_file_size = unicode(video_file_size[0], "UTF-8") video_file_size = video_file_size.replace(u"\u00A0", "") if file_size == video_file_size: sync_value = True else: sync_value = False rating = re.findall(rating_re, row_str) language = re.findall(lang_re, row_str) if language[0] in languages_map: language = [languages_map[language[0]]] else: language = [] if len(language) > 0: first_row = True link = "%s%s/" % (down_url, sub_number[0]) log( __name__ ,"Subtitles found: %s %s (link=%s)" % (subtitle[0], release, link)) flag_pic = "flags/%s.gif" % (language[0]) lang = languageTranslate(language[0],2,0) if lang == lang1 or lang == lang2 or lang == lang3: for rel in re.findall("\'(.*?)\'", str(release)): rel = rel.replace(",",":").replace(" ","") if len(rel) > 1: rel_semicolon = "%s;" % (rel) for rel_sync in re.findall('(.+?);', rel_semicolon): if rel_sync.upper() in file_original_path.upper(): sync_value = True filename_release = "%s - %s" % (subtitle[0], rel_semicolon) rating_dot = rating[0].replace(",",".") if rating_dot == '0.00': sub_rating = '0' else: sub_rating = int(round(float(rating_dot) * 1.666,0)) if stack == False: if disc_amount[0] > '1': log( __name__ ,"Nonstacked video file - stacked subs") else: subtitles_list.append({'filename': filename_release, 'sync': sync_value, 'link': link, 'language_flag': flag_pic, 'language_name': lang,'rating': '%s' % (sub_rating)}) else: if disc_amount[0] > '1': subtitles_list.append({'filename': filename_release, 'sync': sync_value, 'link': link, 'language_flag': flag_pic, 'language_name': lang,'rating': '%s' % (sub_rating)}) else: log( __name__ ,"Stacked video file - nonstacked subs") else: continue else: continue
def set_filehash(path, rar): if rar: path = os.path.dirname(path) file_hash = hashFile(path) return file_hash
def search_subtitles( file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack ): # standard input log(__name__, "Starting search by TV Show") if tvshow == None or tvshow == "": log(__name__, "No TVShow name, stop") return [], "", "" cli = SerialZoneClient() found_tv_shows = cli.search_show(tvshow) if found_tv_shows.__len__() == 0: log(__name__, "TVShow not found, stop") return [], "", "" elif found_tv_shows.__len__() == 1: log(__name__, "One TVShow found, auto select") tvshow_url = found_tv_shows[0]["url"] else: log(__name__, "More TVShows found, user dialog for select") menu_dialog = [] for found_tv_show in found_tv_shows: if found_tv_show["orig_title"] == found_tv_show["title"]: menu_dialog.append(found_tv_show["title"] + " - " + found_tv_show["years"]) else: menu_dialog.append( found_tv_show["title"] + " / " + found_tv_show["orig_title"] + " - " + found_tv_show["years"] ) dialog = xbmcgui.Dialog() found_tv_show_id = dialog.select(_(610), menu_dialog) if found_tv_show_id == -1: return [], "", "" tvshow_url = found_tv_shows[found_tv_show_id]["url"] log(__name__, "Selected show URL: " + tvshow_url) try: file_size, file_hash = hashFile(file_original_path, rar) except: file_size, file_hash = -1, None log(__name__, "File size: " + str(file_size)) found_season_subtitles = cli.list_show_subtitles(tvshow_url, season) episode_subtitle_list = None for found_season_subtitle in found_season_subtitles: if found_season_subtitle["episode"] == int(episode) and found_season_subtitle["season"] == int(season): episode_subtitle_list = found_season_subtitle break if episode_subtitle_list == None: return [], "", "" max_down_count = 0 for episode_subtitle in episode_subtitle_list["versions"]: if max_down_count < episode_subtitle["down_count"]: max_down_count = episode_subtitle["down_count"] log(__name__, "Max download count: " + str(max_down_count)) result_subtitles = [] for episode_subtitle in episode_subtitle_list["versions"]: print_out_filename = episode_subtitle["rip"] + " by " + episode_subtitle["author"] if not episode_subtitle["notes"] == None: print_out_filename = print_out_filename + "(" + episode_subtitle["notes"] + ")" result_subtitles.append( { "filename": print_out_filename, "link": episode_subtitle["link"], "lang": lng_short2long(episode_subtitle["lang"]), "rating": str(episode_subtitle["down_count"] * 10 / max_down_count), "sync": (episode_subtitle["file_size"] == file_size), "language_flag": "flags/" + lng_short2flag(episode_subtitle["lang"]) + ".gif", "language_name": lng_short2long(episode_subtitle["lang"]), } ) log(__name__, result_subtitles) # Standard output - # subtitles list # session id (e.g a cookie string, passed on to download_subtitles), # message to print back to the user # return subtitlesList, "", msg return result_subtitles, "", ""
def search_subtitles(file_original_path, title, tvshow, year, season, episode, set_temp, rar, lang1, lang2, lang3, stack): #standard input log(__name__, "Starting search by TV Show") if (tvshow == None or tvshow == ''): log(__name__, "No TVShow name, stop") return [], "", "" cli = SerialZoneClient() found_tv_shows = cli.search_show(tvshow) if (found_tv_shows.__len__() == 0): log(__name__, "TVShow not found, stop") return [], "", "" elif (found_tv_shows.__len__() == 1): log(__name__, "One TVShow found, auto select") tvshow_url = found_tv_shows[0]['url'] else: log(__name__, "More TVShows found, user dialog for select") menu_dialog = [] for found_tv_show in found_tv_shows: if (found_tv_show['orig_title'] == found_tv_show['title']): menu_dialog.append(found_tv_show['title'] + " - " + found_tv_show['years']) else: menu_dialog.append(found_tv_show['title'] + " / " + found_tv_show['orig_title'] + " - " + found_tv_show['years']) dialog = xbmcgui.Dialog() found_tv_show_id = dialog.select(_(610), menu_dialog) if (found_tv_show_id == -1): return [], "", "" tvshow_url = found_tv_shows[found_tv_show_id]['url'] log(__name__, "Selected show URL: " + tvshow_url) try: file_size, file_hash = hashFile(file_original_path, rar) except: file_size, file_hash = -1, None log(__name__, "File size: " + str(file_size)) found_season_subtitles = cli.list_show_subtitles(tvshow_url, season) episode_subtitle_list = None for found_season_subtitle in found_season_subtitles: if (found_season_subtitle['episode'] == int(episode) and found_season_subtitle['season'] == int(season)): episode_subtitle_list = found_season_subtitle break if episode_subtitle_list == None: return [], "", "" max_down_count = 0 for episode_subtitle in episode_subtitle_list['versions']: if max_down_count < episode_subtitle['down_count']: max_down_count = episode_subtitle['down_count'] log(__name__, "Max download count: " + str(max_down_count)) result_subtitles = [] for episode_subtitle in episode_subtitle_list['versions']: print_out_filename = episode_subtitle[ 'rip'] + " by " + episode_subtitle['author'] if not episode_subtitle['notes'] == None: print_out_filename = print_out_filename + "(" + episode_subtitle[ 'notes'] + ")" result_subtitles.append({ 'filename': print_out_filename, 'link': episode_subtitle['link'], 'lang': lng_short2long(episode_subtitle['lang']), 'rating': str(episode_subtitle['down_count'] * 10 / max_down_count), 'sync': (episode_subtitle['file_size'] == file_size), 'language_flag': 'flags/' + lng_short2flag(episode_subtitle['lang']) + '.gif', 'language_name': lng_short2long(episode_subtitle['lang']), }) log(__name__, result_subtitles) # Standard output - # subtitles list # session id (e.g a cookie string, passed on to download_subtitles), # message to print back to the user # return subtitlesList, "", msg return result_subtitles, "", ""