def Search(item):  # standard input

    try:
        LTV = LegendasTV()
        LTV.Log = log
        subtitles = ""
        languages = []
        subtitles = LTV.Search(title=item['title'], 
                               tvshow=item['tvshow'], 
                               year=item['year'], 
                               season=item['season'], 
                               episode=item['episode'], 
                               lang=item['languages'])
    except:
        import traceback
        log("\n%s" % traceback.format_exc())
        return 1 

    if subtitles:
        for it in subtitles:
            listitem = xbmcgui.ListItem(label=it["language_name"],
                                        label2=it["filename"],
                                        iconImage=it["rating"],
                                        thumbnailImage=it["language_flag"]
                                        )
            if it["sync"]: listitem.setProperty( "sync", "true" )
            else: listitem.setProperty( "sync", "false" )
            if it.get("hearing_imp", False): listitem.setProperty( "hearing_imp", "true" )
            else: listitem.setProperty( "hearing_imp", "false" )
            url = "plugin://%s/?action=download&download_url=%s&filename=%s" % (__scriptid__, it["url"],os.path.basename(item["file_original_path"]))
            xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=url,listitem=listitem,isFolder=False)
def Search(item):  # standard input

    try:
        LTV = LegendasTV()
        LTV.Log = log
        subtitles = ""
        languages = []
        subtitles = LTV.Search(title=item['title'],
                               tvshow=item['tvshow'],
                               year=item['year'],
                               season=item['season'],
                               episode=item['episode'],
                               lang=item['languages'])
    except:
        import traceback
        log("\n%s" % traceback.format_exc())
        return 1

    if subtitles:
        for it in subtitles:
            listitem = xbmcgui.ListItem(label=it["language_name"],
                                        label2=it["filename"],
                                        iconImage=it["rating"],
                                        thumbnailImage=it["language_flag"])
            if it["sync"]: listitem.setProperty("sync", "true")
            else: listitem.setProperty("sync", "false")
            if it.get("hearing_imp", False):
                listitem.setProperty("hearing_imp", "true")
            else:
                listitem.setProperty("hearing_imp", "false")
            url = "plugin://%s/?action=download&download_url=%s&filename=%s" % (
                __scriptid__, it["url"],
                os.path.basename(item["file_original_path"]))
            xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),
                                        url=url,
                                        listitem=listitem,
                                        isFolder=False)
Beispiel #3
0
def Download(url, filename, pack, language):  #standard input
    #Create some variables
    subtitles = []
    extractPath = os.path.join(__temp__, "Extracted")
    cleanDirectory(extractPath)

    FileContent, FileExt = LTV.Download(url)

    fname = "%s.%s" % (os.path.join(__temp__, "subtitle"), FileExt)

    with open(fname, 'wb') as f:
        f.write(FileContent)

    # brunoga fixed solution for non unicode caracters
    # Ps. Windows allready parses Unicode filenames.
    fs_encoding = sys.getfilesystemencoding() if sys.getfilesystemencoding(
    ) else "utf-8"
    extractPath = extractPath.encode(fs_encoding)

    # Use XBMC.Extract to extract the downloaded file, extract it to the temp dir,
    # then removes all files from the temp dir that aren't subtitles.
    def extract_and_copy(extraction=0):
        for root, dirs, files in os.walk(extractPath, topdown=False):
            for file in files:
                dirfile = os.path.join(root, file)

                # Sanitize filenames - converting them to ASCII - and remove them from folders

                f = xbmcvfs.File(dirfile)
                temp = f.read()
                f.close()
                xbmcvfs.delete(dirfile)
                dirfile_with_path_name = normalizeString(
                    os.path.relpath(dirfile, extractPath))
                dirname, basename = os.path.split(dirfile_with_path_name)
                dirname = re.sub(r"[/\\]{1,10}", "-", dirname)
                dirfile_with_path_name = "(%s) %s" % (
                    dirname, basename) if len(dirname) else basename
                new_dirfile = os.path.join(extractPath, dirfile_with_path_name)
                with open(new_dirfile, "w") as f:
                    f.write(temp)

                # Get the file extension
                ext = os.path.splitext(new_dirfile)[1][1:].lower()
                if ext in sub_ext and xbmcvfs.exists(new_dirfile):
                    if not new_dirfile in subtitles:
                        #Append the matching file
                        subtitles.append(new_dirfile)
                elif ext in "rar zip" and not extraction:
                    # Extract compressed files, extracted priorly
                    xbmc.executebuiltin("XBMC.Extract(%s, %s)" %
                                        (new_dirfile, extractPath))
                    xbmc.sleep(1000)
                    extract_and_copy(1)
                elif ext not in "idx":
                    xbmcvfs.delete(new_dirfile)
            for dir in dirs:
                dirfolder = os.path.join(root, dir)
                xbmcvfs.rmdir(dirfolder)

    xbmc.executebuiltin("XBMC.Extract(%s, %s)" % (fname, extractPath))
    xbmc.sleep(1000)
    extract_and_copy()

    temp = []
    for sub in subtitles:
        ltv = LegendasTV()
        video_file = ltv.CleanLTVTitle(filename)
        sub_striped = ltv.CleanLTVTitle(os.path.basename(sub))
        Ratio = ltv.CalculateRatio(sub_striped, video_file)
        temp.append([Ratio, sub])
    subtitles = sorted(temp, reverse=True)
    outputSub = []
    if len(subtitles) > 1:
        if pack:
            subtitles.append(["pack", __language__(32010)])
        dialog = xbmcgui.Dialog()
        sel = dialog.select(
            "%s\n%s" % (__language__(32001).encode("utf-8"), filename),
            [os.path.basename(y) for x, y in subtitles])
        if sel >= 0:
            subSelected = subtitles[sel][1]

            # Handling package import
            if subtitles[sel][0] == "pack":
                dir = os.path.dirname(
                    urllib.unquote(
                        xbmc.Player().getPlayingFile().decode('utf-8')))
                for f in xbmc_walk(dir):
                    if os.path.splitext(f)[1] in [
                            ".mkv", ".avi", ".vob", ".mov", ".mp4"
                    ]:
                        for x, s in subtitles:
                            if re.search("s\d{2}e\d{2}", s.lower()):
                                se = re.findall("s\d{2}e\d{2}", s.lower())[0]
                            if re.search("s\d{2}e\d{2}", f.lower()):
                                fe = re.findall("s\d{2}e\d{2}", f.lower())[0]
                            if se == fe:
                                if os.path.basename(f) == os.path.basename(
                                        filename):
                                    outputSub.append(s)
                                name = os.path.splitext(f)[0]
                                ext = os.path.splitext(s)[1]
                                lang = "pt" if re.search(
                                    "Portuguese", language) else "en"
                                out = "%s.%s%s" % (name, lang, ext)
                                xbmcvfs.copy(s, out)

            outputSub.append(subSelected)
    elif len(subtitles) == 1:
        outputSub.append(subtitles[0][1])

    return outputSub
Beispiel #4
0
__profile__ = xbmc.translatePath(
    __addon__.getAddonInfo('profile')).decode("utf-8")
__resource__ = xbmc.translatePath(os.path.join(__cwd__, 'resources',
                                               'lib')).decode("utf-8")
__temp__ = xbmc.translatePath(os.path.join(__profile__, 'temp',
                                           '')).decode("utf-8")

sys.path.append(__resource__)
__search__ = __addon__.getSetting('SEARCH')
__username__ = __addon__.getSetting('USERNAME')
__password__ = __addon__.getSetting('PASSWORD')

from LTVutilities import log, xbmcOriginalTitle, cleanDirectory, isStacked
from LegendasTV import *

LTV = LegendasTV()
LTV.Log = log


def Search(item):  # standard input

    try:
        languages = []
        subtitles = LTV.Search(title=item['title'],
                               tvshow=item['tvshow'],
                               year=item['year'],
                               season=item['season'],
                               episode=item['episode'],
                               lang=item['languages'])
    except:
        import traceback
Beispiel #5
0
__language__   = __addon__.getLocalizedString

__cwd__        = xbmc.translatePath( __addon__.getAddonInfo('path') ).decode("utf-8")
__profile__    = xbmc.translatePath( __addon__.getAddonInfo('profile') ).decode("utf-8")
__resource__   = xbmc.translatePath( os.path.join( __cwd__, 'resources', 'lib' ) ).decode("utf-8")
__temp__       = xbmc.translatePath( os.path.join( __profile__, 'temp', '') ).decode("utf-8")

sys.path.append (__resource__)
__search__   = __addon__.getSetting( 'SEARCH' )
__username__ = __addon__.getSetting( 'USERNAME' )
__password__ = __addon__.getSetting( 'PASSWORD' )

from LTVutilities import log, cleanDirectory, isStacked, getMovieIMDB, getShowIMDB, getShowId, getTVShowOrginalTitle, getMovieOriginalTitle, safeFilename
from LegendasTV import *

LTV = LegendasTV()
LTV.Log = log

def Search(item):  # standard input

    try:
        languages = []
        subtitles = LTV.Search(title=item['title'], 
                               tvshow=item['tvshow'], 
                               year=item['year'], 
                               season=item['season'], 
                               episode=item['episode'], 
                               lang=item['languages'],
                               imdb=item['imdb'])
    except:
        import traceback
def Download(url, filename, stack=False):  #standard input
    #Create some variables
    subtitles = []
    extractPath = os.path.join(__temp__, "Extracted")
    cleanDirectory(__temp__)
    if not xbmcvfs.exists(extractPath): os.makedirs(extractPath)

    # Download the subtitle using its ID.
    Response = urllib2.urlopen(url).read()

    downloadID = re.findall(regex_3, Response)[0] if re.search(
        regex_3, Response) else 0

    if not downloadID: return ""
    Response = urllib2.urlopen(
        urllib2.Request("http://minister.legendas.tv%s" % downloadID))
    ltv_sub = Response.read()

    # Set the path of file concatenating the temp dir, the subtitle ID and a zip or rar extension.
    # Write the subtitle in binary mode.
    fname = os.path.join(__temp__, "subtitle")
    #     fname += '.rar' if re.search("\x52\x61\x72\x21\x1a\x07\x00", ltv_sub) else '.zip'
    fname += '.rar' if Response.url.__contains__('.rar') else '.zip'
    with open(fname, 'wb') as f:
        f.write(ltv_sub)

    # brunoga fixed solution for non unicode caracters
    # Ps. Windows allready parses Unicode filenames.
    fs_encoding = sys.getfilesystemencoding() if sys.getfilesystemencoding(
    ) else "utf-8"
    extractPath = extractPath.encode(fs_encoding)

    # Use XBMC.Extract to extract the downloaded file, extract it to the temp dir,
    # then removes all files from the temp dir that aren't subtitles.
    def extract_and_copy(extraction=0):
        for root, dirs, files in os.walk(extractPath, topdown=False):
            for file in files:
                dirfile = os.path.join(root, file)

                # Sanitize filenames - converting them to ASCII - and remove them from folders
                f = xbmcvfs.File(dirfile)
                temp = f.read()
                f.close()
                xbmcvfs.delete(dirfile)
                dirfile_with_path_name = normalizeString(
                    os.path.relpath(dirfile, extractPath))
                dirname, basename = os.path.split(dirfile_with_path_name)
                dirname = re.sub(r"[/\\]{1,10}", "-", dirname)
                dirfile_with_path_name = "(%s)%s" % (dirname, basename) if len(
                    dirname) else basename
                new_dirfile = os.path.join(extractPath, dirfile_with_path_name)
                with open(new_dirfile, "w") as f:
                    f.write(temp)

                # Get the file extension
                ext = os.path.splitext(new_dirfile)[1][1:].lower()
                if ext in sub_ext and xbmcvfs.exists(new_dirfile):
                    if not new_dirfile in subtitles:
                        #Append the matching file
                        subtitles.append(new_dirfile)
                elif ext in "rar zip" and not extraction:
                    # Extract compressed files, extracted priorly
                    xbmc.executebuiltin("XBMC.Extract(%s, %s)" %
                                        (new_dirfile, extractPath))
                    xbmc.sleep(1000)
                    extract_and_copy(1)
                elif ext not in "idx":
                    xbmcvfs.delete(new_dirfile)
            for dir in dirs:
                dirfolder = os.path.join(root, dir)
                xbmcvfs.rmdir(dirfolder)

    xbmc.executebuiltin("XBMC.Extract(%s, %s)" % (fname, extractPath))
    xbmc.sleep(1000)
    extract_and_copy()

    temp = []
    for sub in subtitles:
        ltv = LegendasTV()
        video_file = ltv.CleanLTVTitle(filename)
        sub_striped = ltv.CleanLTVTitle(os.path.basename(sub))
        Ratio = ltv.CalculateRatio(sub_striped, video_file)
        temp.append([Ratio, sub])
    subtitles = sorted(temp, reverse=True)
    outputSub = []
    if len(subtitles) > 1:
        dialog = xbmcgui.Dialog()
        sel = dialog.select(
            "%s\n%s" % (__language__(32001).encode("utf-8"), filename),
            [os.path.basename(y) for x, y in subtitles])
        if sel >= 0:
            subSelected = subtitles[sel][1]
            outputSub.append(subSelected)
            for x, sub in subtitles:
                if isStacked(subSelected, sub):
                    outputSub.append(sub)
    elif len(subtitles) == 1:
        outputSub.append(subtitles[0][1])
    return outputSub
    for a, b in langtemp: item["languages"].append(b)

    if item['episode'].lower().find("s") > -1:                                                # Check if season is "Special"
        item['season'] = "0"                                    
        item['episode'] = item['episode'][-1:]
    
    if ( item['file_original_path'].find("http") > -1 ):
        item['temp'] = True

    elif ( item['file_original_path'].find("rar://") > -1 ):
        item['rar']    = True
        item['file_original_path'] = os.path.dirname(item['file_original_path'][6:])

    elif ( item['file_original_path'].find("stack://") > -1 ):
        stackPath = item['file_original_path'].split(" , ")
        item['file_original_path'] = stackPath[0][8:]
    
    Search(item)    

elif params['action'] == 'download':
    ltv = LegendasTV()
    try:
        ltv.login(__username__, __password__)
        subs = Download(params["download_url"],params["filename"])
    except: subs = Download(params["download_url"],'filename')
    for sub in subs:
        listitem = xbmcgui.ListItem(label2=os.path.basename(sub))
        xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=sub,listitem=listitem,isFolder=False)
xbmcplugin.endOfDirectory(int(sys.argv[1]))