def search_tvshow(tvshow, season, episode, languages, filename):
    tvshow = string.strip(tvshow)

    search_string = "%s s%#02de%#02d" % (tvshow, int(season), int(episode))
    log(__name__, "Search tvshow = %s" % search_string)
    res = search_manual(search_string, languages, filename)
    if res:
        return res
    # try and be less exact
    search_string = "%s %i %i" % (tvshow, int(season), int(episode))
    log(__name__, "Search tvshow = %s" % search_string)
    res = search_manual(search_string, languages, filename)
    return res
def search_movie(title, year, languages, filename):
    title = string.strip(title)

    log(__name__, "Search movie = %s" % title)
    res = search_manual(title, languages, filename)
    for result in res:
        rtitle, ryear = xbmc.getCleanMovieTitle(result['filename'])
        rtitle, ryear = rtitle.strip().lower(), ryear.strip().lower()
        log(__name__, "Got cleaned movie result of %s (%s) '%s'" % (rtitle, ryear, result['filename']))
        if (rtitle, ryear) == (title, year):
            yield result
def search(item):
    res = []
    filename = os.path.splitext(os.path.basename(item['file_original_path']))[0]
    log(__name__, "Search_thaisubtitle='%s', filename='%s', addon_version=%s" % (item, filename, __version__))

    if item['mansearch']:
        res = search_manual(item['mansearchstr'], item['3let_language'], filename)
        return append_subtitles(res)
    elif item['tvshow']:
        res = search_tvshow(item['tvshow'], item['season'], item['episode'], item['3let_language'], filename)
        if res:
            return append_subtitles(res)
    elif item['title'] and item['year']:
        res = search_manual(item['title'], item['3let_language'], filename, )
        if res:
            return append_subtitles(res)

    title, year = xbmc.getCleanMovieTitle(filename)
    log(__name__, "clean title: \"%s\" (%s)" % (title, year))
    try:
        yearval = int(year)
    except ValueError:
        yearval = 0
    if title and yearval > 1900:
        res = search_movie(title, year, item['3let_language'], filename)
        if res:
            return append_subtitles(res)
    match = re.search(r'\WS(?P<season>\d\d)E(?P<episode>\d\d)', title, flags=re.IGNORECASE)
    if match is not None:
        tvshow = string.strip(title[:match.start('season')-1])
        season = string.lstrip(match.group('season'), '0')
        episode = string.lstrip(match.group('episode'), '0')
        res = search_tvshow(tvshow, season, episode, item['3let_language'], filename)
        if res:
            return append_subtitles(res)
    # last fall back
    if len(filename) < 30:
        res = search_manual(filename, item['3let_language'], filename)
    if res:
        return append_subtitles(res)
    return []