Exemplo n.º 1
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
Exemplo n.º 2
0
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