示例#1
0
def edit(path="/"):
    """Edit the given subtitle, or create a new one"""
    path = normalize_path(path)
    realpath = os.path.abspath(root_dir + path)
    data = {}
    title = ".".join(path.split("/")[-1].split(".")[:-2])
    data["title"] = title
    data["path"] = path
    data["root"] = "/".join(path.split("/")[:-1])
    data["breadCrumbs"] = listBreadCrumbs(path)

    if "shift" in request.values:
        # Shift subtitle
        try:
            s = float(request.values["shift"])
            shift.shift(realpath, s)
            data["message"] = "File shifted by {}s.".format(s)
        except ValueError:
            pass
    elif "upload" in request.files:
        # Upload custom subtitle
        f = request.files["upload"]
        name = os.path.splitext(path.split("/")[-1])[0]
        data["message"] = 'The file "' + name + '.srt" has been uploaded'
        ext = os.path.splitext(f.filename)[1]
        target = os.path.splitext(realpath)[0] + ".srt"
        if ext == ".sub":
            # Convert .sub to .srt
            t = tempfile.NamedTemporaryFile()
            f.save(t.name)
            sub2srt.convert(t.name, target)
            data["message"] += " and converted to SRT format"
        else:
            f.save(target)
        os.chmod(target, 0o666)
        normalize_encoding(target)
    elif "auto" in request.values:
        # Download subtitle from OpenSubtitle
        try:
            folderPath = "/".join((root_dir + normalize_path(path)).split("/")[:-1])
            opensubs.get_sub(root_dir + normalize_path(path))
        except:
            logging.warning(traceback.format_exc())
            data["message"] = "Could not download subtitles"

    # Subtitle content
    try:
        data["filecontent"] = open(realpath).read().replace("\n", "<br/>")
        data["sub_title"] = "Subtitle content"
    except:
        data["sub_title"] = "No subtitle"

    return flask.render_template("edit.html", **data)
示例#2
0
def getFiles(path):
    """Get the subtitle files for movies in the path folder"""
    files = []
    p = pathlib.Path(root_dir + normalize_path(path))
    folder = [x.name for x in p.iterdir() if x.is_dir()]
    movies = [x.name for x in p.iterdir() if x.is_file() and x.suffix in movie_ext]
    for m in movies:
        name = os.path.splitext(m)[0]
        for e in languages:
            files.append("{}.{}.srt".format(name, plex_lang[e]))
    return (sorted(folder), sorted(files))