示例#1
0
    def tvdb_list(self, tvshowtitle, year, imdb, tvdb, lang, limit=''):
        try:
            if imdb == '0':
                try:
                    trakt_ids = trakt.SearchTVShow(
                        urllib.quote_plus(tvshowtitle), year, full=False)[0]
                    trakt_ids = trakt_ids.get('show', '0')
                    imdb = trakt_ids.get('ids', {}).get('imdb', '0')
                    imdb = 'tt' + re.sub('[^0-9]', '', str(imdb))
                    if not imdb:
                        imdb = '0'
                except:
                    imdb = '0'
                # if tmdb == '0':
                # try:
                # tmdb = trakt_ids.get('ids', {}).get('tmdb', '0')
                # tmdb = re.sub('[^0-9]', '', str(tmdb))
                # if not tmdb:
                # tmdb = '0'
                # except:
                # tmdb = '0'

            if tvdb == '0' and not imdb == '0':
                url = self.tvdb_by_imdb % imdb
                result = client.request(url, timeout='10')

                try:
                    tvdb = client.parseDOM(result, 'seriesid')[0]
                except:
                    tvdb = '0'

                try:
                    name = client.parseDOM(result, 'SeriesName')[0]
                except:
                    name = '0'

                dupe = re.compile('[***]Duplicate (\d*)[***]').findall(name)
                if len(dupe) > 0:
                    tvdb = str(dupe[0])

                if tvdb == '':
                    tvdb = '0'

            if tvdb == '0':
                url = self.tvdb_by_query % (urllib.quote_plus(tvshowtitle))

                years = [str(year), str(int(year) + 1), str(int(year) - 1)]

                tvdb = client.request(url, timeout='10')
                tvdb = re.sub(r'[^\x00-\x7F]+', '', tvdb)
                tvdb = client.replaceHTMLCodes(tvdb)
                tvdb = client.parseDOM(tvdb, 'Series')
                tvdb = [(x, client.parseDOM(x, 'SeriesName'),
                         client.parseDOM(x, 'FirstAired')) for x in tvdb]
                tvdb = [(x, x[1][0], x[2][0]) for x in tvdb
                        if len(x[1]) > 0 and len(x[2]) > 0]
                tvdb = [
                    x for x in tvdb
                    if cleantitle.get(tvshowtitle) == cleantitle.get(x[1])
                ]
                tvdb = [
                    x[0][0] for x in tvdb if any(y in x[2] for y in years)
                ][0]
                tvdb = client.parseDOM(tvdb, 'seriesid')[0]

                if tvdb == '':
                    tvdb = '0'
        except:
            return

        try:
            if tvdb == '0':
                return

            url = self.tvdb_info_link % (tvdb, 'en')
            data = urllib2.urlopen(url, timeout=30).read()

            zip = zipfile.ZipFile(StringIO.StringIO(data))
            result = zip.read('%s.xml' % 'en')
            artwork = zip.read('banners.xml')
            zip.close()

            dupe = client.parseDOM(result, 'SeriesName')[0]
            dupe = re.compile('[***]Duplicate (\d*)[***]').findall(dupe)

            if len(dupe) > 0:
                tvdb = str(dupe[0]).encode('utf-8')

                url = self.tvdb_info_link % (tvdb, 'en')
                data = urllib2.urlopen(url, timeout=30).read()

                zip = zipfile.ZipFile(StringIO.StringIO(data))
                result = zip.read('%s.xml' % 'en')
                artwork = zip.read('banners.xml')
                zip.close()

            if not lang == 'en':
                url = self.tvdb_info_link % (tvdb, lang)
                data = urllib2.urlopen(url, timeout=30).read()

                zip = zipfile.ZipFile(StringIO.StringIO(data))
                result2 = zip.read('%s.xml' % lang)
                zip.close()
            else:
                result2 = result

            artwork = artwork.split('<Banner>')
            artwork = [
                i for i in artwork if '<Language>en</Language>' in i
                and '<BannerType>season</BannerType>' in i
            ]
            artwork = [
                i for i in artwork if not 'seasonswide' in re.findall(
                    '<BannerPath>(.+?)</BannerPath>', i)[0]
            ]

            result = result.split('<Episode>')
            result2 = result2.split('<Episode>')

            item = result[0]
            item2 = result2[0]

            episodes = [i for i in result if '<EpisodeNumber>' in i]

            if control.setting('tv.specials') == 'true':
                episodes = [i for i in episodes]
            else:
                episodes = [
                    i for i in episodes
                    if not '<SeasonNumber>0</SeasonNumber>' in i
                ]
                episodes = [
                    i for i in episodes
                    if not '<EpisodeNumber>0</EpisodeNumber>' in i
                ]

            seasons = [
                i for i in episodes if '<EpisodeNumber>1</EpisodeNumber>' in i
            ]
            counts = self.seasonCountParse(seasons=seasons, episodes=episodes)
            locals = [i for i in result2 if '<EpisodeNumber>' in i]

            result = ''
            result2 = ''

            if limit == '':
                episodes = []
            elif limit == '-1':
                seasons = []
            else:
                episodes = [
                    i for i in episodes
                    if '<SeasonNumber>%01d</SeasonNumber>' % int(limit) in i
                ]
                seasons = []
            try:
                poster = client.parseDOM(item, 'poster')[0]
            except:
                poster = ''
            if not poster == '':
                poster = self.tvdb_image + poster
            else:
                poster = '0'
            poster = client.replaceHTMLCodes(poster)
            poster = poster.encode('utf-8')

            try:
                banner = client.parseDOM(item, 'banner')[0]
            except:
                banner = ''
            if not banner == '': banner = self.tvdb_image + banner
            else: banner = '0'
            banner = client.replaceHTMLCodes(banner)
            banner = banner.encode('utf-8')

            try:
                fanart = client.parseDOM(item, 'fanart')[0]
            except:
                fanart = ''
            if not fanart == '': fanart = self.tvdb_image + fanart
            else: fanart = '0'
            fanart = client.replaceHTMLCodes(fanart)
            fanart = fanart.encode('utf-8')

            if not poster == '0': pass
            elif not fanart == '0': poster = fanart
            elif not banner == '0': poster = banner

            if not banner == '0': pass
            elif not fanart == '0': banner = fanart
            elif not poster == '0': banner = poster

            try:
                status = client.parseDOM(item, 'Status')[0]
            except:
                status = ''
            if status == '': status = 'Ended'
            status = client.replaceHTMLCodes(status)
            status = status.encode('utf-8')

            try:
                studio = client.parseDOM(item, 'Network')[0]
            except:
                studio = ''
            if studio == '':
                studio = '0'
            studio = client.replaceHTMLCodes(studio)
            studio = studio.encode('utf-8')

            try:
                genre = client.parseDOM(item, 'Genre')[0]
            except:
                genre = ''
            genre = [x for x in genre.split('|') if not x == '']
            genre = ' / '.join(genre)
            if genre == '':
                genre = '0'
            genre = client.replaceHTMLCodes(genre)
            genre = genre.encode('utf-8')

            try:
                duration = client.parseDOM(item, 'Runtime')[0]
            except:
                duration = ''
            if duration == '':
                duration = '0'
            duration = client.replaceHTMLCodes(duration)
            duration = duration.encode('utf-8')

            try:
                rating = client.parseDOM(item, 'Rating')[0]
                rating = client.replaceHTMLCodes(rating)
                rating = rating.encode('utf-8')
            except:
                rating = '0'

            try:
                votes = client.parseDOM(item, 'RatingCount')[0]
                votes = client.replaceHTMLCodes(votes)
                votes = votes.encode('utf-8')
            except:
                votes = '0'

            try:
                mpaa = client.parseDOM(item, 'ContentRating')[0]
                mpaa = client.replaceHTMLCodes(mpaa)
                mpaa = mpaa.encode('utf-8')
            except:
                mpaa = '0'

            try:
                cast = client.parseDOM(item, 'Actors')[0]
                cast = [x for x in cast.split('|') if not x == '']
                cast = [(x.encode('utf-8'), '') for x in cast]
            except:
                cast = []

            try:
                label = client.parseDOM(item2, 'SeriesName')[0]
            except:
                label = '0'
            label = client.replaceHTMLCodes(label)
            label = label.encode('utf-8')

            try:
                plot = client.parseDOM(item2, 'Overview')[0]
            except:
                plot = ''
            if plot == '':
                plot = '0'
            plot = client.replaceHTMLCodes(plot)
            plot = plot.encode('utf-8')

            unaired = ''

        except:
            pass

        for item in seasons:
            try:
                premiered = client.parseDOM(item, 'FirstAired')[0]
                if premiered == '' or '-00' in premiered: premiered = '0'
                premiered = client.replaceHTMLCodes(premiered)
                premiered = premiered.encode('utf-8')

                # Show Unaired items.
                if status == 'Ended':
                    pass
                elif premiered == '0':
                    raise Exception()
                elif int(re.sub('[^0-9]', '', str(premiered))) > int(
                        re.sub('[^0-9]', '', str(self.today_date))):
                    unaired = 'true'
                    if self.showunaired != 'true':
                        raise Exception()

                season = client.parseDOM(item, 'SeasonNumber')[0]
                season = '%01d' % int(season)
                season = season.encode('utf-8')

                thumb = [
                    i for i in artwork
                    if client.parseDOM(i, 'Season')[0] == season
                ]
                try:
                    thumb = client.parseDOM(thumb[0], 'BannerPath')[0]
                except:
                    thumb = ''
                if not thumb == '':
                    thumb = self.tvdb_image + thumb
                else:
                    thumb = '0'
                thumb = client.replaceHTMLCodes(thumb)
                thumb = thumb.encode('utf-8')

                if thumb == '0': thumb = poster

                try:
                    seasoncount = counts[season]
                except:
                    seasoncount = None

                self.list.append({
                    'season': season,
                    'seasoncount': seasoncount,
                    'tvshowtitle': tvshowtitle,
                    'label': label,
                    'year': year,
                    'premiered': premiered,
                    'status': status,
                    'studio': studio,
                    'genre': genre,
                    'duration': duration,
                    'rating': rating,
                    'votes': votes,
                    'mpaa': mpaa,
                    'cast': cast,
                    'plot': plot,
                    'imdb': imdb,
                    'tmdb': '0',
                    'tvdb': tvdb,
                    'tvshowid': imdb,
                    'poster': poster,
                    'banner': banner,
                    'fanart': fanart,
                    'thumb': thumb,
                    'unaired': unaired
                })

            except:
                pass

        for item in episodes:
            try:
                premiered = client.parseDOM(item, 'FirstAired')[0]
                if premiered == '' or '-00' in premiered:
                    premiered = '0'
                premiered = client.replaceHTMLCodes(premiered)
                premiered = premiered.encode('utf-8')

                # Show future items
                if status == 'Ended':
                    pass
                elif premiered == '0':
                    raise Exception()
                elif int(re.sub('[^0-9]', '', str(premiered))) > int(
                        re.sub('[^0-9]', '', str(self.today_date))):
                    unaired = 'true'
                    if self.showunaired != 'true':
                        raise Exception()

                season = client.parseDOM(item, 'SeasonNumber')[0]
                season = '%01d' % int(season)
                season = season.encode('utf-8')

                episode = client.parseDOM(item, 'EpisodeNumber')[0]
                episode = re.sub('[^0-9]', '', '%01d' % int(episode))
                episode = episode.encode('utf-8')

                ### episode IDS
                try:
                    episodeIDS = trakt.getEpisodeSummary(imdb,
                                                         season,
                                                         episode,
                                                         full=False)
                    episodeIDS = episodeIDS.get('ids', {})
                except:
                    episodeIDS = {}
##------------------

                title = client.parseDOM(item, 'EpisodeName')[0]
                if title == '': title = '0'
                title = client.replaceHTMLCodes(title)
                title = title.encode('utf-8')

                try:
                    thumb = client.parseDOM(item, 'filename')[0]
                except:
                    thumb = ''
                if not thumb == '': thumb = self.tvdb_image + thumb
                else: thumb = '0'
                thumb = client.replaceHTMLCodes(thumb)
                thumb = thumb.encode('utf-8')

                if not thumb == '0': pass
                elif not fanart == '0':
                    thumb = fanart.replace(self.tvdb_image, self.tvdb_poster)
                elif not poster == '0':
                    thumb = poster

                try:
                    rating = client.parseDOM(item, 'Rating')[0]
                except:
                    rating = ''
                if rating == '':
                    rating = '0'
                rating = client.replaceHTMLCodes(rating)
                rating = rating.encode('utf-8')

                try:
                    director = client.parseDOM(item, 'Director')[0]
                except:
                    director = ''
                director = [x for x in director.split('|') if not x == '']
                director = ' / '.join(director)
                if director == '':
                    director = '0'
                director = client.replaceHTMLCodes(director)
                director = director.encode('utf-8')

                try:
                    writer = client.parseDOM(item, 'Writer')[0]
                except:
                    writer = ''
                writer = [x for x in writer.split('|') if not x == '']
                writer = ' / '.join(writer)
                if writer == '':
                    writer = '0'
                writer = client.replaceHTMLCodes(writer)
                writer = writer.encode('utf-8')

                try:
                    local = client.parseDOM(item, 'id')[0]
                    local = [
                        x for x in locals if '<id>%s</id>' % str(local) in x
                    ][0]
                except:
                    local = item

                label = client.parseDOM(local, 'EpisodeName')[0]
                if label == '':
                    label = '0'
                label = client.replaceHTMLCodes(label)
                label = label.encode('utf-8')

                try:
                    episodeplot = client.parseDOM(local, 'Overview')[0]
                except:
                    episodeplot = ''
                if episodeplot == '':
                    episodeplot = '0'
                if episodeplot == '0':
                    episodeplot = plot
                episodeplot = client.replaceHTMLCodes(episodeplot)
                try:
                    episodeplot = episodeplot.encode('utf-8')
                except:
                    pass

                try:
                    seasoncount = counts[season]
                except:
                    seasoncount = None

                self.list.append({
                    'title': title,
                    'label': label,
                    'seasoncount': seasoncount,
                    'season': season,
                    'episode': episode,
                    'tvshowtitle': tvshowtitle,
                    'year': year,
                    'premiered': premiered,
                    'status': status,
                    'studio': studio,
                    'genre': genre,
                    'duration': duration,
                    'rating': rating,
                    'votes': votes,
                    'mpaa': mpaa,
                    'director': director,
                    'writer': writer,
                    'cast': cast,
                    'plot': episodeplot,
                    'imdb': imdb,
                    'tmdb': '0',
                    'tvdb': tvdb,
                    'poster': poster,
                    'banner': banner,
                    'fanart': fanart,
                    'thumb': thumb,
                    'unaired': unaired,
                    'episodeIDS': episodeIDS
                })
            except:
                pass
        return self.list
    def tvdb_list(self, tvshowtitle, year, imdb, tmdb, tvdb, lang, limit=''):
        if (tvdb == '0' or tmdb == '0') and imdb != '0':
            try:
                trakt_ids = trakt.IdLookup('imdb', imdb, 'show')
                if trakt_ids:
                    if tvdb == '0':
                        tvdb = str(trakt_ids.get('tvdb', '0'))
                        if not tvdb or tvdb == 'None': tvdb = '0'
                    if tmdb == '0':
                        tmdb = str(trakt_ids.get('tmdb', '0'))
                        if not tmdb or tmdb == 'None': tmdb = '0'
            except:
                log_utils.error()

        if imdb == '0' or tmdb == '0' or tvdb == '0':
            try:
                trakt_ids = trakt.SearchTVShow(quote_plus(tvshowtitle),
                                               year,
                                               full=False)
                if not trakt_ids: raise Exception()
                trakt_ids = trakt_ids[0].get('show', '0')
                if imdb == '0':
                    imdb = trakt_ids.get('ids', {}).get('imdb', '0')
                    if not imdb or imdb == 'None': imdb = '0'
                    if not imdb.startswith('tt'): imdb = '0'
                if tmdb == '0':
                    tmdb = str(trakt_ids.get('ids', {}).get('tmdb', '0'))
                    if not tmdb or tmdb == 'None': tmdb = '0'
                if tvdb == '0':
                    tvdb = str(trakt_ids.get('ids', {}).get('tvdb', '0'))
                    if not tvdb or tvdb == 'None': tvdb = '0'
            except:
                log_utils.error()

        if tvdb == '0' and imdb != '0':  # Check TVDb by IMDB_ID for missing tvdb_id
            try:
                tvdb = cache.get(tvdb_v1.getSeries_ByIMDB, 96, tvshowtitle,
                                 year, imdb)
            except:
                tvdb = '0'
        if tvdb == '0':  # Check TVDb by seriesname for missing tvdb_id
            try:
                ids = cache.get(tvdb_v1.getSeries_ByName, 96, tvshowtitle,
                                year)
                if ids: tvdb = ids.get(tvdb, '0') or '0'
            except:
                tvdb = '0'
                log_utils.error()

        if tvdb == '0': return None
        try:
            result, artwork, actors = cache.get(tvdb_v1.getZip, 96, tvdb, True,
                                                True)
            dupe = client.parseDOM(result, 'SeriesName')[0]
            dupe = re.compile(r'[***]Duplicate (\d*)[***]').findall(dupe)
            if len(dupe) > 0:
                tvdb = str(dupe[0])
                result, artwork, actors = cache.get(tvdb_v1.getZip, 96, tvdb,
                                                    True, True)

            artwork = artwork.split('<Banner>')
            artwork = [
                i for i in artwork if '<Language>en</Language>' in i
                and '<BannerType>season</BannerType>' in i
            ]
            artwork = [
                i for i in artwork if not 'seasonswide' in re.findall(
                    r'<BannerPath>(.+?)</BannerPath>', i)[0]
            ]

            result = result.split('<Episode>')
            item = result[0]

            episodes = [i for i in result if '<EpisodeNumber>' in i]
            if control.setting('tv.specials') == 'true':
                episodes = [i for i in episodes]
            else:
                episodes = [
                    i for i in episodes
                    if not '<SeasonNumber>0</SeasonNumber>' in i
                ]
                episodes = [
                    i for i in episodes
                    if not '<EpisodeNumber>0</EpisodeNumber>' in i
                ]

            # season still airing check for pack scraping
            premiered_eps = [
                i for i in episodes if not '<FirstAired></FirstAired>' in i
            ]
            unaired_eps = [
                i for i in premiered_eps if int(
                    re.sub(r'[^0-9]', '', str(client.parseDOM(
                        i, 'FirstAired')))) > int(
                            re.sub(r'[^0-9]', '', str(self.today_date)))
            ]
            if unaired_eps:
                still_airing = client.parseDOM(unaired_eps, 'SeasonNumber')[0]
            else:
                still_airing = None

            seasons = [
                i for i in episodes if '<EpisodeNumber>1</EpisodeNumber>' in i
            ]
            counts = self.seasonCountParse(seasons=seasons, episodes=episodes)
            # locals = [i for i in result if '<EpisodeNumber>' in i]

            if limit == '': episodes = []
            elif limit == '-1': seasons = []
            else:
                episodes = [
                    i for i in episodes
                    if '<SeasonNumber>%01d</SeasonNumber>' % int(limit) in i
                ]
                seasons = []

            poster = client.replaceHTMLCodes(
                client.parseDOM(item, 'poster')[0])
            if poster != '': poster = '%s%s' % (self.tvdb_image, poster)

            fanart = client.replaceHTMLCodes(
                client.parseDOM(item, 'fanart')[0])
            if fanart != '': fanart = '%s%s' % (self.tvdb_image, fanart)

            banner = client.replaceHTMLCodes(
                client.parseDOM(item, 'banner')[0])
            if banner != '': banner = '%s%s' % (self.tvdb_image, banner)

            if poster != '': pass
            elif fanart != '': poster = fanart
            elif banner != '': poster = banner

            if banner != '': pass
            elif fanart != '': banner = fanart
            elif poster != '': banner = poster

            status = client.replaceHTMLCodes(
                client.parseDOM(item, 'Status')[0]) or 'Ended'
            studio = client.replaceHTMLCodes(
                client.parseDOM(item, 'Network')[0]) or ''
            genre = client.replaceHTMLCodes(client.parseDOM(item, 'Genre')[0])
            genre = ' / '.join([x for x in genre.split('|') if x != ''])
            duration = client.replaceHTMLCodes(
                client.parseDOM(item, 'Runtime')[0])
            rating = client.replaceHTMLCodes(
                client.parseDOM(item, 'Rating')[0])
            votes = client.replaceHTMLCodes(
                client.parseDOM(item, 'RatingCount')[0])
            mpaa = client.replaceHTMLCodes(
                client.parseDOM(item, 'ContentRating')[0])
            castandart = tvdb_v1.parseActors(actors)
            label = client.replaceHTMLCodes(
                client.parseDOM(item, 'SeriesName')[0])
            plot = client.replaceHTMLCodes(
                client.parseDOM(item, 'Overview')[0])
            plot = py_tools.ensure_str(plot)
        except:
            log_utils.error()

        for item in seasons:
            try:
                premiered = client.replaceHTMLCodes(
                    client.parseDOM(item, 'FirstAired')[0]) or '0'
                # Show Unaired items.
                unaired = ''
                if status.lower() == 'ended': pass
                elif premiered == '0':
                    unaired = 'true'
                    if self.showunaired != 'true': continue
                    pass
                elif int(re.sub(r'[^0-9]', '', str(premiered))) > int(
                        re.sub(r'[^0-9]', '', str(self.today_date))):
                    unaired = 'true'
                    if self.showunaired != 'true': continue

                season = client.parseDOM(item, 'SeasonNumber')[0]
                season = '%01d' % int(season)

                thumb = [
                    i for i in artwork
                    if client.parseDOM(i, 'Season')[0] == season
                ]
                try:
                    thumb = client.replaceHTMLCodes(
                        client.parseDOM(thumb[0], 'BannerPath')[0])
                except:
                    thumb = ''
                if thumb != '': thumb = '%s%s' % (self.tvdb_image, thumb)
                else: thumb = poster

                try:
                    seasoncount = counts[season]
                except:
                    seasoncount = None
                try:
                    total_seasons = len([i for i in counts if i != '0'])
                except:
                    total_seasons = None

                self.list.append({
                    'season': season,
                    'tvshowtitle': tvshowtitle,
                    'label': label,
                    'year': year,
                    'premiered': premiered,
                    'status': status,
                    'studio': studio,
                    'genre': genre,
                    'duration': duration,
                    'rating': rating,
                    'votes': votes,
                    'mpaa': mpaa,
                    'castandart': castandart,
                    'plot': plot,
                    'imdb': imdb,
                    'tmdb': tmdb,
                    'tvdb': tvdb,
                    'tvshowid': imdb,
                    'poster': poster,
                    'banner': banner,
                    'fanart': fanart,
                    'thumb': thumb,
                    'unaired': unaired,
                    'seasoncount': seasoncount,
                    'total_seasons': total_seasons
                })
                self.list = sorted(self.list, key=lambda k: int(k['season'])
                                   )  # fix for TVDb new sort by ID
            except:
                log_utils.error()

        for item in episodes:
            try:
                title = client.replaceHTMLCodes(
                    client.parseDOM(item, 'EpisodeName')[0])
                title = py_tools.ensure_str(title)
                premiered = client.replaceHTMLCodes(
                    client.parseDOM(item, 'FirstAired')[0]) or '0'
                # Show Unaired items.
                unaired = ''
                if status.lower() == 'ended': pass
                elif premiered == '0':
                    unaired = 'true'
                    if self.showunaired != 'true': continue
                    pass
                elif int(re.sub(r'[^0-9]', '', str(premiered))) > int(
                        re.sub(r'[^0-9]', '', str(self.today_date))):
                    unaired = 'true'
                    if self.showunaired != 'true': continue

                season = client.parseDOM(item, 'SeasonNumber')[0]
                season = '%01d' % int(season)
                episode = client.parseDOM(item, 'EpisodeNumber')[0]
                episode = re.sub(r'[^0-9]', '', '%01d' % int(episode))

                if still_airing:
                    if int(still_airing) == int(season): is_airing = True
                    else: is_airing = False
                else: is_airing = False

                # ### episode IDS
                episodeIDS = {}
                if control.setting('enable.upnext') == 'true':
                    episodeIDS = trakt.getEpisodeSummary(
                        imdb, season, episode, full=False) or {}
                    if episodeIDS != {}:
                        episodeIDS = episodeIDS.get('ids', {})
##------------------

                thumb = client.replaceHTMLCodes(
                    client.parseDOM(item, 'filename')[0])
                if thumb != '': thumb = '%s%s' % (self.tvdb_image, thumb)

                season_poster = [
                    i for i in artwork
                    if client.parseDOM(i, 'Season')[0] == season
                ]
                try:
                    season_poster = client.replaceHTMLCodes(
                        client.parseDOM(season_poster[0], 'BannerPath')[0])
                except:
                    season_poster = ''
                if season_poster != '':
                    season_poster = '%s%s' % (self.tvdb_image, season_poster)
                else:
                    season_poster = poster

                if thumb != '': pass
                elif fanart != '':
                    thumb = fanart.replace(self.tvdb_image, self.tvdb_poster)
                elif season_poster != '':
                    thumb = season_poster

                rating = client.replaceHTMLCodes(
                    client.parseDOM(item, 'Rating')[0])
                director = client.replaceHTMLCodes(
                    client.parseDOM(item, 'Director')[0])
                director = ' / '.join([
                    x for x in director.split('|') if x != ''
                ])  # check if this needs ensure_str()
                writer = client.replaceHTMLCodes(
                    client.parseDOM(item, 'Writer')[0])
                writer = ' / '.join([x for x in writer.split('|') if x != ''
                                     ])  # check if this needs ensure_str()
                label = client.replaceHTMLCodes(
                    client.parseDOM(item, 'EpisodeName')[0])

                episodeplot = client.replaceHTMLCodes(
                    client.parseDOM(item, 'Overview')[0]) or plot
                episodeplot = py_tools.ensure_str(episodeplot)

                try:
                    seasoncount = counts[season]
                except:
                    seasoncount = None
                try:
                    total_seasons = len([i for i in counts if i != '0'])
                except:
                    total_seasons = None

                self.list.append({
                    'title': title,
                    'label': label,
                    'season': season,
                    'episode': episode,
                    'tvshowtitle': tvshowtitle,
                    'year': year,
                    'premiered': premiered,
                    'status': status,
                    'studio': studio,
                    'genre': genre,
                    'duration': duration,
                    'rating': rating,
                    'votes': votes,
                    'mpaa': mpaa,
                    'director': director,
                    'writer': writer,
                    'castandart': castandart,
                    'plot': episodeplot,
                    'imdb': imdb,
                    'tmdb': tmdb,
                    'tvdb': tvdb,
                    'poster': poster,
                    'banner': banner,
                    'fanart': fanart,
                    'thumb': thumb,
                    'season_poster': season_poster,
                    'unaired': unaired,
                    'seasoncount': seasoncount,
                    'counts': counts,
                    'total_seasons': total_seasons,
                    'is_airing': is_airing,
                    'episodeIDS': episodeIDS
                })
                self.list = sorted(self.list,
                                   key=lambda k:
                                   (int(k['season']), int(k['episode'])
                                    ))  # fix for TVDb new sort by ID
                # meta = {}
                # meta = {'imdb': imdb, 'tmdb': tmdb, 'tvdb': tvdb, 'lang': self.lang, 'user': self.tvdb_key, 'item': item}
                # self.list.append(item)
                # metacache.insert(self.meta)
            except:
                log_utils.error()
        return self.list
示例#3
0
    def tvdb_list(self, tvshowtitle, year, imdb, tmdb, tvdb, lang, limit=''):
        if (tvdb == '0' or tmdb == '0') and imdb != '0':
            try:
                trakt_ids = trakt.IdLookup('imdb', imdb, 'show')
                if trakt_ids:
                    if tvdb == '0':
                        tvdb = str(trakt_ids.get('tvdb', '0'))
                        if not tvdb or tvdb == 'None': tvdb = '0'
                    if tmdb == '0':
                        tmdb = str(trakt_ids.get('tmdb', '0'))
                        if not tmdb or tmdb == 'None': tmdb = '0'
            except:
                log_utils.error()

        if imdb == '0' or tmdb == '0' or tvdb == '0':
            try:
                trakt_ids = trakt.SearchTVShow(quote_plus(tvshowtitle),
                                               year,
                                               full=False)
                if not trakt_ids: raise Exception()
                trakt_ids = trakt_ids[0].get('show', '0')
                if imdb == '0':
                    imdb = trakt_ids.get('ids', {}).get('imdb', '0')
                    if not imdb or imdb == 'None': imdb = '0'
                    if not imdb.startswith('tt'): imdb = '0'
                if tmdb == '0':
                    tmdb = str(trakt_ids.get('ids', {}).get('tmdb', '0'))
                    if not tmdb or tmdb == 'None': tmdb = '0'
                if tvdb == '0':
                    tvdb = str(trakt_ids.get('ids', {}).get('tvdb', '0'))
                    if not tvdb or tvdb == 'None': tvdb = '0'
            except:
                log_utils.error()

###--Check TVDb by IMDB_ID for missing ID's
        if tvdb == '0' and imdb != '0':
            try:
                url = self.tvdb_by_imdb % imdb
                # result = client.request(url, timeout='10')
                result = requests.get(url, timeout=10).content
                result = re.sub(r'[^\x00-\x7F]+', '', result)
                result = client.replaceHTMLCodes(result)
                result = client.parseDOM(result, 'Series')
                result = [(client.parseDOM(x, 'SeriesName'),
                           client.parseDOM(x, 'FirstAired'),
                           client.parseDOM(x, 'seriesid'),
                           client.parseDOM(x, 'AliasNames')) for x in result]

                years = [str(year), str(int(year) + 1), str(int(year) - 1)]

                item = [(x[0], x[1], x[2], x[3]) for x in result
                        if cleantitle.get(tvshowtitle) == cleantitle.get(
                            str(x[0][0])) and any(y in str(x[1][0])
                                                  for y in years)]
                if item == []:
                    item = [(x[0], x[1], x[2], x[3]) for x in result
                            if cleantitle.get(tvshowtitle) == cleantitle.get(
                                str(x[3][0]))]
                if item == []:
                    item = [(x[0], x[1], x[2], x[3]) for x in result
                            if cleantitle.get(tvshowtitle) == cleantitle.get(
                                str(x[0][0]))]
                if item == []:
                    raise Exception()

                tvdb = item[0][2]
                tvdb = tvdb[0] or '0'
            except:
                log_utils.error()
##########################

###--Check TVDb by seriesname
        if tvdb == '0':
            try:
                years = [str(year), str(int(year) + 1), str(int(year) - 1)]
                url = self.tvdb_by_query % (quote_plus(tvshowtitle))
                # tvdb = client.request(url, timeout='10')
                tvdb = requests.get(url, timeout=10).content
                tvdb = re.sub(r'[^\x00-\x7F]+', '', tvdb)
                tvdb = client.replaceHTMLCodes(tvdb)
                tvdb = client.parseDOM(tvdb, 'Series')
                tvdb = [(x, client.parseDOM(x, 'SeriesName'),
                         client.parseDOM(x, 'FirstAired')) for x in tvdb]
                tvdb = [(x, x[1][0], x[2][0]) for x in tvdb
                        if len(x[1]) > 0 and len(x[2]) > 0]
                tvdb = [
                    x for x in tvdb
                    if cleantitle.get(tvshowtitle) == cleantitle.get(x[1])
                ]
                tvdb = [
                    x[0][0] for x in tvdb if any(y in x[2] for y in years)
                ][0]
                tvdb = client.parseDOM(tvdb, 'seriesid')[0]
                if tvdb == '': tvdb = '0'
            except:
                log_utils.error()
#############################

        if tvdb == '0': return None
        try:
            url = self.tvdb_info_link % (tvdb, 'en')
            # data = urlopen(url, timeout=30).read()
            data = requests.get(
                url, timeout=30
            ).content  # sometimes one api key pulls empty xml while another does not
            zip = zipfile.ZipFile(StringIO(data))
            result = zip.read('en.xml')
            artwork = zip.read('banners.xml')
            actors = zip.read('actors.xml')
            zip.close()

            dupe = client.parseDOM(result, 'SeriesName')[0]
            dupe = re.compile(r'[***]Duplicate (\d*)[***]').findall(dupe)

            if len(dupe) > 0:
                tvdb = str(dupe[0]).encode('utf-8')
                url = self.tvdb_info_link % (tvdb, 'en')
                # data = urlopen(url, timeout=30).read()
                data = requests.get(url, timeout=30).content
                zip = zipfile.ZipFile(StringIO(data))
                result = zip.read('en.xml')
                artwork = zip.read('banners.xml')
                actors = zip.read('actors.xml')
                zip.close()

            if lang != 'en':
                url = self.tvdb_info_link % (tvdb, lang)
                # data = urlopen(url, timeout=30).read()
                data = requests.get(url, timeout=30).content
                zip = zipfile.ZipFile(StringIO(data))
                result2 = zip.read('%s.xml' % lang)
                zip.close()
            else:
                result2 = result

            artwork = artwork.split('<Banner>')
            artwork = [
                i for i in artwork if '<Language>en</Language>' in i
                and '<BannerType>season</BannerType>' in i
            ]
            artwork = [
                i for i in artwork if not 'seasonswide' in re.findall(
                    r'<BannerPath>(.+?)</BannerPath>', i)[0]
            ]

            result = result.split('<Episode>')
            result2 = result2.split('<Episode>')

            item = result[0]
            item2 = result2[0]

            episodes = [i for i in result if '<EpisodeNumber>' in i]
            if control.setting('tv.specials') == 'true':
                episodes = [i for i in episodes]
            else:
                episodes = [
                    i for i in episodes
                    if not '<SeasonNumber>0</SeasonNumber>' in i
                ]
                episodes = [
                    i for i in episodes
                    if not '<EpisodeNumber>0</EpisodeNumber>' in i
                ]

            # season still airing check for pack scraping
            premiered_eps = [
                i for i in episodes if not '<FirstAired></FirstAired>' in i
            ]
            unaired_eps = [
                i for i in premiered_eps if int(
                    re.sub(r'[^0-9]', '', str(client.parseDOM(
                        i, 'FirstAired')))) > int(
                            re.sub(r'[^0-9]', '', str(self.today_date)))
            ]
            if unaired_eps:
                still_airing = client.parseDOM(unaired_eps, 'SeasonNumber')[0]
            else:
                still_airing = None

            seasons = [
                i for i in episodes if '<EpisodeNumber>1</EpisodeNumber>' in i
            ]
            counts = self.seasonCountParse(seasons=seasons, episodes=episodes)
            locals = [i for i in result2 if '<EpisodeNumber>' in i]

            result = ''
            result2 = ''

            if limit == '': episodes = []
            elif limit == '-1': seasons = []
            else:
                episodes = [
                    i for i in episodes
                    if '<SeasonNumber>%01d</SeasonNumber>' % int(limit) in i
                ]
                seasons = []

            try:
                poster = client.parseDOM(item, 'poster')[0]
            except:
                poster = ''
            if poster != '': poster = '%s%s' % (self.tvdb_image, poster)
            else: poster = '0'
            poster = client.replaceHTMLCodes(poster)
            poster = poster.encode('utf-8')

            try:
                banner = client.parseDOM(item, 'banner')[0]
            except:
                banner = ''
            if banner != '': banner = '%s%s' % (self.tvdb_image, banner)
            else: banner = '0'
            banner = client.replaceHTMLCodes(banner)
            banner = banner.encode('utf-8')

            try:
                fanart = client.parseDOM(item, 'fanart')[0]
            except:
                fanart = ''
            if fanart != '': fanart = '%s%s' % (self.tvdb_image, fanart)
            else: fanart = '0'
            fanart = client.replaceHTMLCodes(fanart)
            fanart = fanart.encode('utf-8')

            if poster != '0': pass
            elif fanart != '0': poster = fanart
            elif banner != '0': poster = banner

            if banner != '0': pass
            elif fanart != '0': banner = fanart
            elif poster != '0': banner = poster

            try:
                status = client.parseDOM(item, 'Status')[0]
            except:
                status = ''
            if status == '': status = 'Ended'
            status = client.replaceHTMLCodes(status)
            status = status.encode('utf-8')

            try:
                studio = client.parseDOM(item, 'Network')[0]
            except:
                studio = ''
            if studio == '': studio = '0'
            studio = client.replaceHTMLCodes(studio)
            studio = studio.encode('utf-8')

            try:
                genre = client.parseDOM(item, 'Genre')[0]
            except:
                genre = ''
            genre = [x for x in genre.split('|') if x != '']
            genre = ' / '.join(genre)
            if genre == '': genre = '0'
            genre = client.replaceHTMLCodes(genre)
            genre = genre.encode('utf-8')

            try:
                duration = client.parseDOM(item, 'Runtime')[0]
            except:
                duration = ''
            if duration == '': duration = '0'
            duration = client.replaceHTMLCodes(duration)
            duration = duration.encode('utf-8')

            try:
                rating = client.parseDOM(item, 'Rating')[0]
                rating = client.replaceHTMLCodes(rating)
                rating = rating.encode('utf-8')
            except:
                rating = '0'

            try:
                votes = client.parseDOM(item, 'RatingCount')[0]
                votes = client.replaceHTMLCodes(votes)
                votes = votes.encode('utf-8')
            except:
                votes = '0'

            try:
                mpaa = client.parseDOM(item, 'ContentRating')[0]
                mpaa = client.replaceHTMLCodes(mpaa)
                mpaa = mpaa.encode('utf-8')
            except:
                mpaa = '0'

            import xml.etree.ElementTree as ET
            tree = ET.ElementTree(ET.fromstring(actors))
            root = tree.getroot()
            castandart = []
            for actor in root.iter('Actor'):
                person = [name.text for name in actor]
                image = person[1]
                name = person[2]
                try:
                    name = client.replaceHTMLCodes(person[2])
                except:
                    pass
                role = person[3]
                try:
                    role = client.replaceHTMLCodes(person[3])
                except:
                    pass
                try:
                    try:
                        castandart.append({
                            'name':
                            name.encode('utf-8'),
                            'role':
                            role.encode('utf-8'),
                            'thumbnail':
                            ((self.tvdb_image +
                              image) if image is not None else '0')
                        })
                    except:
                        castandart.append({
                            'name':
                            name,
                            'role':
                            role,
                            'thumbnail':
                            ((self.tvdb_image +
                              image) if image is not None else '0')
                        })
                except:
                    castandart = []
                if len(castandart) == 150: break

            try:
                label = client.parseDOM(item2, 'SeriesName')[0]
            except:
                label = '0'
            label = client.replaceHTMLCodes(label)
            label = label.encode('utf-8')

            try:
                # plot = client.parseDOM(item2, 'Overview')[0]
                plot = client.parseDOM(item2, 'Overview')[0].encode(
                    'ascii', errors='ignore').decode('ascii', errors='ignore')
            except:
                plot = ''
            if plot == '': plot = '0'
            plot = client.replaceHTMLCodes(plot)
            plot = plot.encode('utf-8')
        except:
            log_utils.error()

        for item in seasons:
            try:
                premiered = client.parseDOM(item, 'FirstAired')[0]
                if premiered == '' or '-00' in premiered: premiered = '0'
                premiered = client.replaceHTMLCodes(premiered)
                premiered = premiered.encode('utf-8')

                # Show Unaired items.
                unaired = ''
                if status.lower() == 'ended': pass
                elif premiered == '0':
                    unaired = 'true'
                    if self.showunaired != 'true': continue
                    pass
                elif int(re.sub(r'[^0-9]', '', str(premiered))) > int(
                        re.sub(r'[^0-9]', '', str(self.today_date))):
                    unaired = 'true'
                    if self.showunaired != 'true': continue

                season = client.parseDOM(item, 'SeasonNumber')[0]
                season = '%01d' % int(season)
                season = season.encode('utf-8')

                thumb = [
                    i for i in artwork
                    if client.parseDOM(i, 'Season')[0] == season
                ]
                try:
                    thumb = client.parseDOM(thumb[0], 'BannerPath')[0]
                except:
                    thumb = ''
                if thumb != '': thumb = '%s%s' % (self.tvdb_image, thumb)
                else: thumb = '0'
                thumb = client.replaceHTMLCodes(thumb)
                thumb = thumb.encode('utf-8')
                if thumb == '0': thumb = poster

                try:
                    seasoncount = counts[season]
                except:
                    seasoncount = None

                try:
                    total_seasons = len([i for i in counts if i != '0'])
                except:
                    total_seasons = None

                self.list.append({
                    'season': season,
                    'tvshowtitle': tvshowtitle,
                    'label': label,
                    'year': year,
                    'premiered': premiered,
                    'status': status,
                    'studio': studio,
                    'genre': genre,
                    'duration': duration,
                    'rating': rating,
                    'votes': votes,
                    'mpaa': mpaa,
                    'castandart': castandart,
                    'plot': plot,
                    'imdb': imdb,
                    'tmdb': tmdb,
                    'tvdb': tvdb,
                    'tvshowid': imdb,
                    'poster': poster,
                    'banner': banner,
                    'fanart': fanart,
                    'thumb': thumb,
                    'unaired': unaired,
                    'seasoncount': seasoncount,
                    'total_seasons': total_seasons
                })
                self.list = sorted(self.list, key=lambda k: int(k['season'])
                                   )  # fix for TVDb new sort by ID
            except:
                log_utils.error()

        for item in episodes:
            try:
                title = client.parseDOM(item, 'EpisodeName')[0]
                title = client.replaceHTMLCodes(title)
                try:
                    title = title.encode('utf-8')
                except:
                    pass

                premiered = client.parseDOM(item, 'FirstAired')[0]
                if premiered == '' or '-00' in premiered: premiered = '0'
                premiered = client.replaceHTMLCodes(premiered)
                premiered = premiered.encode('utf-8')

                # Show Unaired items.
                unaired = ''
                if status.lower() == 'ended': pass
                elif premiered == '0':
                    unaired = 'true'
                    if self.showunaired != 'true': continue
                    pass
                elif int(re.sub(r'[^0-9]', '', str(premiered))) > int(
                        re.sub(r'[^0-9]', '', str(self.today_date))):
                    unaired = 'true'
                    if self.showunaired != 'true': continue

                season = client.parseDOM(item, 'SeasonNumber')[0]
                season = '%01d' % int(season)
                season = season.encode('utf-8')

                episode = client.parseDOM(item, 'EpisodeNumber')[0]
                episode = re.sub(r'[^0-9]', '', '%01d' % int(episode))
                episode = episode.encode('utf-8')

                if still_airing:
                    if int(still_airing) == int(season): is_airing = True
                    else: is_airing = False
                else: is_airing = False

                # ### episode IDS
                episodeIDS = {}
                if control.setting('enable.upnext') == 'true':
                    episodeIDS = trakt.getEpisodeSummary(
                        imdb, season, episode, full=False) or {}
                    if episodeIDS != {}:
                        episodeIDS = episodeIDS.get('ids', {})
##------------------

                try:
                    thumb = client.parseDOM(item, 'filename')[0]
                except:
                    thumb = ''
                if thumb != '': thumb = '%s%s' % (self.tvdb_image, thumb)
                else: thumb = '0'
                thumb = client.replaceHTMLCodes(thumb)
                thumb = thumb.encode('utf-8')

                if thumb != '0': pass
                elif fanart != '0':
                    thumb = fanart.replace(self.tvdb_image, self.tvdb_poster)
                elif poster != '0':
                    thumb = poster

                season_poster = [
                    i for i in artwork
                    if client.parseDOM(i, 'Season')[0] == season
                ]
                try:
                    season_poster = client.parseDOM(season_poster[0],
                                                    'BannerPath')[0]
                except:
                    season_poster = ''
                if season_poster != '':
                    season_poster = '%s%s' % (self.tvdb_image, season_poster)
                else:
                    season_poster = '0'
                season_poster = client.replaceHTMLCodes(season_poster)
                season_poster = season_poster.encode('utf-8')
                if season_poster == '0': season_poster = poster

                try:
                    rating = client.parseDOM(item, 'Rating')[0]
                except:
                    rating = ''
                if rating == '': rating = '0'
                rating = client.replaceHTMLCodes(rating)
                rating = rating.encode('utf-8')

                try:
                    director = client.parseDOM(item, 'Director')[0]
                except:
                    director = ''
                director = [x for x in director.split('|') if x != '']
                director = ' / '.join(director)
                if director == '': director = '0'
                director = client.replaceHTMLCodes(director)
                director = director.encode('utf-8')

                try:
                    writer = client.parseDOM(item, 'Writer')[0]
                except:
                    writer = ''
                writer = [x for x in writer.split('|') if x != '']
                writer = ' / '.join(writer)
                if writer == '': writer = '0'
                writer = client.replaceHTMLCodes(writer)
                writer = writer.encode('utf-8')

                try:
                    local = client.parseDOM(item, 'id')[0]
                    local = [
                        x for x in locals if '<id>%s</id>' % str(local) in x
                    ][0]
                except:
                    local = item

                label = client.parseDOM(local, 'EpisodeName')[0]
                if label == '': label = '0'
                label = client.replaceHTMLCodes(label)
                label = label.encode('utf-8')

                try:
                    episodeplot = client.parseDOM(local, 'Overview')[0]
                except:
                    episodeplot = ''
                if episodeplot == '': episodeplot = '0'
                if episodeplot == '0': episodeplot = plot
                episodeplot = client.replaceHTMLCodes(episodeplot)
                try:
                    episodeplot = episodeplot.encode('utf-8')
                except:
                    pass

                try:
                    seasoncount = counts[season]
                except:
                    seasoncount = None

                try:
                    total_seasons = len([i for i in counts if i != '0'])
                except:
                    total_seasons = None

                self.list.append({
                    'title': title,
                    'label': label,
                    'season': season,
                    'episode': episode,
                    'tvshowtitle': tvshowtitle,
                    'year': year,
                    'premiered': premiered,
                    'status': status,
                    'studio': studio,
                    'genre': genre,
                    'duration': duration,
                    'rating': rating,
                    'votes': votes,
                    'mpaa': mpaa,
                    'director': director,
                    'writer': writer,
                    'castandart': castandart,
                    'plot': episodeplot,
                    'imdb': imdb,
                    'tmdb': tmdb,
                    'tvdb': tvdb,
                    'poster': poster,
                    'banner': banner,
                    'fanart': fanart,
                    'thumb': thumb,
                    'season_poster': season_poster,
                    'unaired': unaired,
                    'seasoncount': seasoncount,
                    'counts': counts,
                    'total_seasons': total_seasons,
                    'is_airing': is_airing,
                    'episodeIDS': episodeIDS
                })
                self.list = sorted(self.list,
                                   key=lambda k:
                                   (int(k['season']), int(k['episode'])
                                    ))  # fix for TVDb new sort by ID
                # meta = {}
                # meta = {'imdb': imdb, 'tmdb': tmdb, 'tvdb': tvdb, 'lang': self.lang, 'user': self.tvdb_key, 'item': item}

                # self.list.append(item)
                # metacache.insert(self.meta)

            except:
                log_utils.error()
        return self.list
示例#4
0
    def tvdb_list(self, tvshowtitle, year, imdb, tvdb, lang, limit=''):
        tmdb = '0'
        try:
            # if imdb == '0' or tmdb == '0' or tvdb == '0':
            if imdb == '0' or tvdb == '0':
                try:
                    # trakt_ids = trakt.SearchTVShow(urllib.quote_plus(tvshowtitle), year, full=False)[0]
                    trakt_ids = trakt.SearchTVShow(tvshowtitle,
                                                   year,
                                                   full=False)[0]

                    trakt_ids = trakt_ids.get('show', '0')

                    if imdb == '0':
                        imdb = trakt_ids.get('ids', {}).get('imdb', '0')
                        if imdb == '' or imdb is None or imdb == 'None':
                            imdb = '0'

                    if tmdb == '0':
                        tmdb = str(trakt_ids.get('ids', {}).get('tmdb', 0))
                        if tmdb == '' or tmdb is None or tmdb == 'None':
                            tmdb = '0'

                    if tvdb == '0':
                        tvdb = str(trakt_ids.get('ids', {}).get('tvdb', 0))
                        if tvdb == '' or tvdb is None or tvdb == 'None':
                            tvdb = '0'
                except:
                    pass

            if tvdb == '0' and imdb != '0':
                url = self.tvdb_by_imdb % imdb
                result = client.request(url, timeout='10')

                try:
                    tvdb = client.parseDOM(result, 'seriesid')[0]
                except:
                    tvdb = '0'

                try:
                    name = client.parseDOM(result, 'SeriesName')[0]
                except:
                    name = '0'

                dupe = re.compile('[***]Duplicate (\d*)[***]').findall(name)
                if len(dupe) > 0:
                    tvdb = str(dupe[0])

                if tvdb == '':
                    tvdb = '0'

            if tvdb == '0':
                url = self.tvdb_by_query % (urllib.quote_plus(tvshowtitle))

                years = [str(year), str(int(year) + 1), str(int(year) - 1)]

                tvdb = client.request(url, timeout='10')
                tvdb = re.sub(r'[^\x00-\x7F]+', '', tvdb)
                tvdb = client.replaceHTMLCodes(tvdb)
                tvdb = client.parseDOM(tvdb, 'Series')
                tvdb = [(x, client.parseDOM(x, 'SeriesName'),
                         client.parseDOM(x, 'FirstAired')) for x in tvdb]
                tvdb = [(x, x[1][0], x[2][0]) for x in tvdb
                        if len(x[1]) > 0 and len(x[2]) > 0]
                tvdb = [
                    x for x in tvdb
                    if cleantitle.get(tvshowtitle) == cleantitle.get(x[1])
                ]
                tvdb = [
                    x[0][0] for x in tvdb if any(y in x[2] for y in years)
                ][0]
                tvdb = client.parseDOM(tvdb, 'seriesid')[0]

                if tvdb == '':
                    tvdb = '0'
        except:
            return

        try:
            if tvdb == '0':
                return
            url = self.tvdb_info_link % (tvdb, 'en')
            data = urllib2.urlopen(url, timeout=30).read()
            zip = zipfile.ZipFile(StringIO.StringIO(data))

            result = zip.read('en.xml')
            artwork = zip.read('banners.xml')
            actors = zip.read('actors.xml')
            zip.close()

            dupe = client.parseDOM(result, 'SeriesName')[0]
            dupe = re.compile('[***]Duplicate (\d*)[***]').findall(dupe)

            if len(dupe) > 0:
                tvdb = str(dupe[0]).encode('utf-8')

                url = self.tvdb_info_link % (tvdb, 'en')
                data = urllib2.urlopen(url, timeout=30).read()
                zip = zipfile.ZipFile(StringIO.StringIO(data))

                result = zip.read('en.xml')
                artwork = zip.read('banners.xml')
                actors = zip.read('actors.xml')
                zip.close()

            if lang != 'en':
                url = self.tvdb_info_link % (tvdb, lang)
                data = urllib2.urlopen(url, timeout=30).read()
                zip = zipfile.ZipFile(StringIO.StringIO(data))
                result2 = zip.read('%s.xml' % lang)
                zip.close()
            else:
                result2 = result

            artwork = artwork.split('<Banner>')
            artwork = [
                i for i in artwork if '<Language>en</Language>' in i
                and '<BannerType>season</BannerType>' in i
            ]
            artwork = [
                i for i in artwork if not 'seasonswide' in re.findall(
                    '<BannerPath>(.+?)</BannerPath>', i)[0]
            ]

            result = result.split('<Episode>')
            result2 = result2.split('<Episode>')

            item = result[0]
            item2 = result2[0]

            episodes = [i for i in result if '<EpisodeNumber>' in i]

            if control.setting('tv.specials') == 'true':
                episodes = [i for i in episodes]
            else:
                episodes = [
                    i for i in episodes
                    if not '<SeasonNumber>0</SeasonNumber>' in i
                ]
                episodes = [
                    i for i in episodes
                    if not '<EpisodeNumber>0</EpisodeNumber>' in i
                ]

            seasons = [
                i for i in episodes if '<EpisodeNumber>1</EpisodeNumber>' in i
            ]
            counts = self.seasonCountParse(seasons=seasons, episodes=episodes)

            locals = [i for i in result2 if '<EpisodeNumber>' in i]

            result = ''
            result2 = ''

            if limit == '':
                episodes = []
            elif limit == '-1':
                seasons = []
            else:
                episodes = [
                    i for i in episodes
                    if '<SeasonNumber>%01d</SeasonNumber>' % int(limit) in i
                ]
                seasons = []
            try:
                poster = client.parseDOM(item, 'poster')[0]
            except:
                poster = ''
            if poster != '':
                poster = self.tvdb_image + poster
            else:
                poster = '0'
            poster = client.replaceHTMLCodes(poster)
            poster = poster.encode('utf-8')

            try:
                banner = client.parseDOM(item, 'banner')[0]
            except:
                banner = ''
            if banner != '':
                banner = self.tvdb_image + banner
            else:
                banner = '0'
            banner = client.replaceHTMLCodes(banner)
            banner = banner.encode('utf-8')

            try:
                fanart = client.parseDOM(item, 'fanart')[0]
            except:
                fanart = ''
            if fanart != '':
                fanart = self.tvdb_image + fanart
            else:
                fanart = '0'
            fanart = client.replaceHTMLCodes(fanart)
            fanart = fanart.encode('utf-8')

            if poster != '0':
                pass
            elif fanart != '0':
                poster = fanart
            elif banner != '0':
                poster = banner

            if banner != '0':
                pass
            elif fanart != '0':
                banner = fanart
            elif poster != '0':
                banner = poster

            try:
                status = client.parseDOM(item, 'Status')[0]
            except:
                status = ''
            if status == '':
                status = 'Ended'
            status = client.replaceHTMLCodes(status)
            status = status.encode('utf-8')

            try:
                studio = client.parseDOM(item, 'Network')[0]
            except:
                studio = ''
            if studio == '':
                studio = '0'
            studio = client.replaceHTMLCodes(studio)
            studio = studio.encode('utf-8')

            try:
                genre = client.parseDOM(item, 'Genre')[0]
            except:
                genre = ''
            genre = [x for x in genre.split('|') if x != '']
            genre = ' / '.join(genre)
            if genre == '':
                genre = '0'
            genre = client.replaceHTMLCodes(genre)
            genre = genre.encode('utf-8')

            try:
                duration = client.parseDOM(item, 'Runtime')[0]
            except:
                duration = ''
            if duration == '':
                duration = '0'
            duration = client.replaceHTMLCodes(duration)
            duration = duration.encode('utf-8')

            try:
                rating = client.parseDOM(item, 'Rating')[0]
                rating = client.replaceHTMLCodes(rating)
                rating = rating.encode('utf-8')
            except:
                rating = '0'

            try:
                votes = client.parseDOM(item, 'RatingCount')[0]
                votes = client.replaceHTMLCodes(votes)
                votes = votes.encode('utf-8')
            except:
                votes = '0'

            try:
                mpaa = client.parseDOM(item, 'ContentRating')[0]
                mpaa = client.replaceHTMLCodes(mpaa)
                mpaa = mpaa.encode('utf-8')
            except:
                mpaa = '0'

            import xml.etree.ElementTree as ET
            tree = ET.ElementTree(ET.fromstring(actors))
            root = tree.getroot()
            castandart = []
            for actor in root.iter('Actor'):
                person = [name.text for name in actor]
                image = person[1]
                name = person[2]
                try:
                    name = client.replaceHTMLCodes(person[2])
                except:
                    pass
                role = person[3]
                try:
                    role = client.replaceHTMLCodes(person[3])
                except:
                    pass
                try:
                    try:
                        castandart.append({
                            'name':
                            name.encode('utf-8'),
                            'role':
                            role.encode('utf-8'),
                            'thumbnail':
                            ((self.tvdb_image +
                              image) if image is not None else '0')
                        })
                    except:
                        castandart.append({
                            'name':
                            name,
                            'role':
                            role,
                            'thumbnail':
                            ((self.tvdb_image +
                              image) if image is not None else '0')
                        })
                except:
                    castandart = []
                if len(castandart) == 200: break

            try:
                label = client.parseDOM(item2, 'SeriesName')[0]
            except:
                label = '0'
            label = client.replaceHTMLCodes(label)
            label = label.encode('utf-8')

            try:
                plot = client.parseDOM(item2, 'Overview')[0]
            except:
                plot = ''
            if plot == '':
                plot = '0'
            plot = client.replaceHTMLCodes(plot)
            plot = plot.encode('utf-8')

            unaired = ''

        except:
            import traceback
            traceback.print_exc()
            pass

        for item in seasons:
            try:
                premiered = client.parseDOM(item, 'FirstAired')[0]
                if premiered == '' or '-00' in premiered: premiered = '0'
                premiered = client.replaceHTMLCodes(premiered)
                premiered = premiered.encode('utf-8')

                # Show Unaired items.
                if status.lower() == 'ended':
                    pass
                elif premiered == '0':
                    continue
                elif int(re.sub('[^0-9]', '', str(premiered))) > int(
                        re.sub('[^0-9]', '', str(self.today_date))):
                    unaired = 'true'
                    if self.showunaired != 'true':
                        continue

                # # Show Unaired items.
                # if status.lower() == 'ended':
                # pass
                # elif premiered == '0':
                # unaired = 'true'
                # pass
                # elif premiered != '0':
                # if int(re.sub('[^0-9]', '', str(premiered))) > int(re.sub('[^0-9]', '', str(self.today_date))):
                # unaired = 'true'
                # if self.showunaired != 'true':
                # continue

                season = client.parseDOM(item, 'SeasonNumber')[0]
                season = '%01d' % int(season)
                season = season.encode('utf-8')

                thumb = [
                    i for i in artwork
                    if client.parseDOM(i, 'Season')[0] == season
                ]
                try:
                    thumb = client.parseDOM(thumb[0], 'BannerPath')[0]
                except:
                    thumb = ''
                if thumb != '':
                    thumb = self.tvdb_image + thumb
                else:
                    thumb = '0'
                thumb = client.replaceHTMLCodes(thumb)
                thumb = thumb.encode('utf-8')
                if thumb == '0':
                    thumb = poster

                try:
                    seasoncount = counts[season]
                except:
                    seasoncount = None

                self.list.append({
                    'season': season,
                    'seasoncount': seasoncount,
                    'tvshowtitle': tvshowtitle,
                    'label': label,
                    'year': year,
                    'premiered': premiered,
                    'status': status,
                    'studio': studio,
                    'genre': genre,
                    'duration': duration,
                    'rating': rating,
                    'votes': votes,
                    'mpaa': mpaa,
                    'castandart': castandart,
                    'plot': plot,
                    'imdb': imdb,
                    'tmdb': tmdb,
                    'tvdb': tvdb,
                    'tvshowid': imdb,
                    'poster': poster,
                    'banner': banner,
                    'fanart': fanart,
                    'thumb': thumb,
                    'unaired': unaired
                })

            except:
                import traceback
                traceback.print_exc()
                pass

        for item in episodes:
            try:
                premiered = client.parseDOM(item, 'FirstAired')[0]
                if premiered == '' or '-00' in premiered:
                    premiered = '0'
                premiered = client.replaceHTMLCodes(premiered)
                premiered = premiered.encode('utf-8')

                # Show Unaired items.
                if status.lower() == 'ended':
                    pass
                elif premiered == '0':
                    continue

                elif int(re.sub('[^0-9]', '', str(premiered))) > int(
                        re.sub('[^0-9]', '', str(self.today_date))):
                    unaired = 'true'
                    if self.showunaired != 'true':
                        continue

                # # Show Unaired items.
                # if status.lower() == 'ended':
                # pass
                # elif premiered == '0':
                # unaired = 'true'
                # pass
                # elif premiered != '0':
                # if int(re.sub('[^0-9]', '', str(premiered))) > int(re.sub('[^0-9]', '', str(self.today_date))):
                # unaired = 'true'
                # if self.showunaired != 'true':
                # continue

                season = client.parseDOM(item, 'SeasonNumber')[0]
                season = '%01d' % int(season)
                season = season.encode('utf-8')

                episode = client.parseDOM(item, 'EpisodeNumber')[0]
                episode = re.sub('[^0-9]', '', '%01d' % int(episode))
                episode = episode.encode('utf-8')

                # ### episode IDS
                episodeIDS = {}
                if control.setting('enable.upnext') == 'true':
                    episodeIDS = trakt.getEpisodeSummary(
                        imdb, season, episode, full=False) or {}
                    if episodeIDS != {}:
                        episodeIDS = episodeIDS.get('ids', {})
##------------------

                title = client.parseDOM(item, 'EpisodeName')[0]
                title = client.replaceHTMLCodes(title)
                title = title.encode('utf-8')

                try:
                    thumb = client.parseDOM(item, 'filename')[0]
                except:
                    thumb = ''
                if thumb != '':
                    thumb = self.tvdb_image + thumb
                else:
                    thumb = '0'
                thumb = client.replaceHTMLCodes(thumb)
                thumb = thumb.encode('utf-8')

                if thumb != '0':
                    pass
                elif fanart != '0':
                    thumb = fanart.replace(self.tvdb_image, self.tvdb_poster)
                elif poster != '0':
                    thumb = poster

                season_poster = [
                    i for i in artwork
                    if client.parseDOM(i, 'Season')[0] == season
                ]
                try:
                    season_poster = client.parseDOM(season_poster[0],
                                                    'BannerPath')[0]
                except:
                    season_poster = ''
                if season_poster != '':
                    season_poster = self.tvdb_image + season_poster
                else:
                    season_poster = '0'
                season_poster = client.replaceHTMLCodes(season_poster)
                season_poster = season_poster.encode('utf-8')
                if season_poster == '0':
                    season_poster = poster
                # log_utils.log('season_poster = %s for tvshowtitle = %s' % (season_poster, tvshowtitle), __name__, log_utils.LOGDEBUG)

                try:
                    rating = client.parseDOM(item, 'Rating')[0]
                except:
                    rating = ''
                if rating == '':
                    rating = '0'
                rating = client.replaceHTMLCodes(rating)
                rating = rating.encode('utf-8')

                try:
                    director = client.parseDOM(item, 'Director')[0]
                except:
                    director = ''
                director = [x for x in director.split('|') if x != '']
                director = ' / '.join(director)
                if director == '':
                    director = '0'
                director = client.replaceHTMLCodes(director)
                director = director.encode('utf-8')

                try:
                    writer = client.parseDOM(item, 'Writer')[0]
                except:
                    writer = ''
                writer = [x for x in writer.split('|') if x != '']
                writer = ' / '.join(writer)
                if writer == '':
                    writer = '0'
                writer = client.replaceHTMLCodes(writer)
                writer = writer.encode('utf-8')

                try:
                    local = client.parseDOM(item, 'id')[0]
                    local = [
                        x for x in locals if '<id>%s</id>' % str(local) in x
                    ][0]
                except:
                    local = item

                label = client.parseDOM(local, 'EpisodeName')[0]
                if label == '':
                    label = '0'
                label = client.replaceHTMLCodes(label)
                label = label.encode('utf-8')

                try:
                    episodeplot = client.parseDOM(local, 'Overview')[0]
                except:
                    episodeplot = ''
                if episodeplot == '':
                    episodeplot = '0'
                if episodeplot == '0':
                    episodeplot = plot
                episodeplot = client.replaceHTMLCodes(episodeplot)
                try:
                    episodeplot = episodeplot.encode('utf-8')
                except:
                    pass

                try:
                    seasoncount = counts[season]
                except:
                    seasoncount = None

                self.list.append({
                    'title': title,
                    'label': label,
                    'seasoncount': seasoncount,
                    'season': season,
                    'episode': episode,
                    'tvshowtitle': tvshowtitle,
                    'year': year,
                    'premiered': premiered,
                    'status': status,
                    'studio': studio,
                    'genre': genre,
                    'duration': duration,
                    'rating': rating,
                    'votes': votes,
                    'mpaa': mpaa,
                    'director': director,
                    'writer': writer,
                    'castandart': castandart,
                    'plot': episodeplot,
                    'imdb': imdb,
                    'tmdb': tmdb,
                    'tvdb': tvdb,
                    'poster': poster,
                    'banner': banner,
                    'fanart': fanart,
                    'thumb': thumb,
                    'season_poster': season_poster,
                    'unaired': unaired,
                    'episodeIDS': episodeIDS
                })

                # meta = {}
                # meta = {'imdb': imdb, 'tmdb': tmdb, 'tvdb': tvdb, 'lang': self.lang, 'user': self.tvdb_key, 'item': item}

                # self.list.append(item)
                # metacache.insert(self.meta)

            except:
                import traceback
                traceback.print_exc()
                pass
        return self.list