diff --git a/plugin.video.alfa/channels/search.py b/plugin.video.alfa/channels/search.py index d0bbf80d..34f98475 100644 --- a/plugin.video.alfa/channels/search.py +++ b/plugin.video.alfa/channels/search.py @@ -11,12 +11,73 @@ from core import channeltools from core.item import Item from platformcode import config, logger from platformcode import platformtools +from core import tmdb + +link_list = [] +max_links = 30 def mainlist(item): logger.info() item.channel = "search" + itemlist = [] + context = [{"title": "Elegir canales incluidos", "action": "setting_channel", "channel": item.channel}] + itemlist.append(Item(channel=item.channel, action="sub_menu", title="Buscar en canales", context=context, + thumbnail=get_thumb("search.png"))) + + itemlist.append(Item(channel=item.channel, action='genres_menu', title='Películas por Generos', type='movie')) + + itemlist.append (Item(channel=item.channel, action='discover_list', title='Películas mas populares', + context=context, + search_type='list', list_type='movie/popular')) + + itemlist.append(Item(channel=item.channel, action='discover_list', title='Películas mejor valoradas', + context=context, + search_type='list', list_type='movie/top_rated')) + + itemlist.append( + Item(channel=item.channel, action='discover_list', title='Películas Ahora en cines', context=context, + search_type='list', + list_type='movie/now_playing')) + + itemlist.append(Item(channel=item.channel, action='genres_menu', title='Series por Generos', type='tv')) + + itemlist.append( + Item(channel=item.channel, action='discover_list', title='Series mas populares', context=context, + search_type='list',list_type='tv/popular')) + + itemlist.append(Item(channel=item.channel, action='discover_list', title='Series en emisión', context=context, + search_type='list', list_type='tv/on_the_air')) + + + itemlist.append(Item(channel=item.channel, action='discover_list', title='Series mejor valoradas', context=context, + search_type='list', list_type='tv/top_rated')) + + + + + return itemlist + + +def genres_menu(item): + + itemlist = [] + + genres = tmdb.get_genres(item.type) + + logger.debug(genres) + logger.debug(genres[item.type]) + + for key, value in genres[item.type].items(): + itemlist.append(item.clone(title=value, action='discover_list', search_type='discover', + list_type=key, page='1')) + return sorted(itemlist, key=lambda it: it.title) + +def sub_menu(item): + logger.info() + item.channel = "search" + itemlist = list() context = [{"title": "Elegir canales incluidos", "action": "setting_channel", @@ -433,7 +494,7 @@ def do_search(item, categories=None): title = channel # resultados agrupados por canales - if item.contextual == True: + if item.contextual == True or item.action == 'search_tmdb': result_mode = 1 if result_mode == 0: if len(search_results[channel]) > 1: @@ -514,3 +575,61 @@ def get_saved_searches(): saved_searches_list = list(current_saved_searches_list) return saved_searches_list + + +def discover_list(item): + itemlist = [] + + itemlist = tmdb.discovery(item) + tmdb.set_infoLabels_itemlist(itemlist, seekTmdb=True) + + if item.search_type=='discover': + next_page = str(int(item.page)+1) + itemlist.append(item.clone(title='Pagina Siguente', page=next_page)) + + return itemlist + +def search_tmdb(item): + logger.debug(item) + + itemlist = [] + threads = [] + logger.debug(item) + wanted = item.contentTitle + + search = do_search(item) + + if item.contentSerieName == '': + results = exact_results(search, wanted) + for result in results: + logger.debug(result) + t = Thread(target=get_links, args=[result]) + t.start() + threads.append(t) + + for thread in threads: + thread.join() + + # try: + # get_links(result) + # except: + # pass + + for link in link_list: + if link.action == 'play' and not 'trailer' in link.title.lower() and len(itemlist) < max_links: + itemlist.append(link) + + return sorted(itemlist, key=lambda it: it.server) + else: + for item in search: + if item.contentSerieName != '' and item.contentSerieName == wanted: + logger.debug(item) + itemlist.append(item) + return itemlist + +def get_links (item): + logger.info() + results =[] + channel = __import__('channels.%s' % item.from_channel, None, None, ["channels.%s" % item.from_channel]) + if len(link_list) <= max_links: + link_list.extend(getattr(channel, item.from_action)(item)) diff --git a/plugin.video.alfa/core/tmdb.py b/plugin.video.alfa/core/tmdb.py old mode 100755 new mode 100644 index 1ba25685..0412b42d --- a/plugin.video.alfa/core/tmdb.py +++ b/plugin.video.alfa/core/tmdb.py @@ -536,6 +536,54 @@ def completar_codigos(item): item.infoLabels['url_scraper'].append(url_scraper) +def discovery(item): + from core.item import Item + from platformcode import unify + + tvshow = False + itemlist = [] + logger.debug(item) + + if item.search_type == 'discover': + listado = Tmdb(discover={'url':'discover/%s' % item.type, 'with_genres':item.list_type, 'language':'es', + 'page':item.page}) + + elif item.search_type == 'list': + listado = Tmdb(list={'url': item.list_type, 'language':'es'}) + + logger.debug(listado.get_list_resultados()) + result = listado.get_list_resultados() + + for elem in result: + elem['tmdb_id']=elem['id'] + if 'title' in elem: + title = unify.normalize(elem['title']).capitalize() + contentTitle = title + elem['year'] = scrapertools.find_single_match(elem['release_date'], '(\d{4})-\d+-\d+') + else: + title = unify.normalize(elem['name']).capitalize() + tvshow = True + + logger.debug(elem) + new_item = Item(channel=item.channel, title=title, infoLabels=elem, action='search_tmdb', extra=title) + + if tvshow: + new_item.contentSerieName = title + else: + new_item.contentTitle = title + + itemlist.append(new_item) + + return itemlist + +def get_genres(type): + lang = 'es' + genres = Tmdb(tipo=type) + + return genres.dic_generos[lang] + + + # Clase auxiliar class ResultDictDefault(dict): # Python 2.4 @@ -762,6 +810,7 @@ class Tmdb(object): self.busqueda_year = kwargs.get('year', '') self.busqueda_filtro = kwargs.get('filtro', {}) self.discover = kwargs.get('discover', {}) + self.list = kwargs.get('list', {}) # Reellenar diccionario de generos si es necesario if (self.busqueda_tipo == 'movie' or self.busqueda_tipo == "tv") and \ @@ -793,6 +842,9 @@ class Tmdb(object): elif self.discover: self.__discover() + elif self.list: + self.__list() + else: logger.debug("Creado objeto vacio") @@ -947,6 +999,65 @@ class Tmdb(object): logger.error(msg) return 0 + def __list(self, index_results=0): + self.result = ResultDictDefault() + results = [] + total_results = 0 + total_pages = 0 + + # Ejemplo self.discover: {'url': 'movie/', 'with_cast': '1'} + # url: Método de la api a ejecutar + # resto de claves: Parámetros de la búsqueda concatenados a la url + type_search = self.list.get('url', '') + if type_search: + params = [] + for key, value in self.list.items(): + if key != "url": + params.append("&"+key + "=" + str(value)) + # http://api.themoviedb.org/3/movie/popolar?api_key=a1ab8b8669da03637a4b98fa39c39228&&language=es + url = ('http://api.themoviedb.org/3/%s?api_key=a1ab8b8669da03637a4b98fa39c39228%s' + % (type_search, ''.join(params))) + + logger.info("[Tmdb.py] Buscando %s:\n%s" % (type_search, url)) + resultado = self.get_json(url) + + total_results = resultado.get("total_results", -1) + total_pages = resultado.get("total_pages", 1) + + if total_results > 0: + results = resultado["results"] + if self.busqueda_filtro and results: + # TODO documentar esta parte + for key, value in dict(self.busqueda_filtro).items(): + for r in results[:]: + if key not in r or r[key] != value: + results.remove(r) + total_results -= 1 + elif total_results == -1: + results = resultado + + if index_results >= len(results): + logger.error( + "La busqueda de '%s' no dio %s resultados" % (type_search, index_results)) + return 0 + + # Retornamos el numero de resultados de esta pagina + if results: + self.results = results + self.total_results = total_results + self.total_pages = total_pages + if total_results > 0: + self.result = ResultDictDefault(self.results[index_results]) + else: + self.result = results + return len(self.results) + else: + # No hay resultados de la busqueda + logger.error("La busqueda de '%s' no dio resultados" % type_search) + return 0 + + + def __discover(self, index_results=0): self.result = ResultDictDefault() results = [] diff --git a/plugin.video.alfa/platformcode/unify.py b/plugin.video.alfa/platformcode/unify.py index 494999af..4b19a6ce 100644 --- a/plugin.video.alfa/platformcode/unify.py +++ b/plugin.video.alfa/platformcode/unify.py @@ -140,6 +140,11 @@ def remove_format(string): #logger.debug('sale de remove: %s' % string) return string +def normalize(string): + string = string.decode('utf-8') + normal = ''.join((c for c in unicodedata.normalize('NFD', unicode(string)) if unicodedata.category(c) != 'Mn')) + return normal + def simplify(string): #logger.info() @@ -148,9 +153,11 @@ def simplify(string): string = string.replace('-',' ').replace('_',' ') string = re.sub(r'\d+','', string) string = string.strip() - string = string.decode('utf-8') - notilde = ''.join((c for c in unicodedata.normalize('NFD', unicode(string)) if unicodedata.category(c) != 'Mn')) - string = notilde.decode() + notilde = normalize(string) + try: + string = notilde.decode() + except: + pass string = string.lower() #logger.debug('sale de simplify: %s' % string) @@ -410,6 +417,15 @@ def title_format(item): if lang: item.title = add_languages(item.title, simple_language) + + # Para las busquedas por canal + if item.from_channel != '': + from core import channeltools + channel_parameters = channeltools.get_channel_parameters(item.from_channel) + logger.debug(channel_parameters) + item.title = '%s [%s]' % (item.title, channel_parameters['title']) + + # Formato para actualizaciones de series en la videoteca sobreescribe los colores anteriores if item.channel=='videolibrary' and item.context!='':