예제 #1
0
def item_siblings_index(path):
    with open(
            os.path.join(
                os.path.dirname(
                    os.path.join(app.config["DATA_DIR"], b"library",
                                 encode_path(path))), os.path.pardir,
                b"index.json")) as f:
        return json.load(f)
def directory_for_track(path):
    directory = (filter(lambda item: item["path"] == encode_path(os.path.dirname(path)),
                        item_siblings_index(path).values()) + [None])[0]
    if directory:
        if re.search(DISC_DIRECTORY_REGEXP, directory["path"]):
            return directory_for_track(directory["path"])
        else:
            return directory
예제 #3
0
def directory_for_track(path):
    directory = (
        filter(lambda item: item["path"] == encode_path(os.path.dirname(path)),
               item_siblings_index(path).values()) + [None])[0]
    if directory:
        if re.search(DISC_DIRECTORY_REGEXP, directory["path"]):
            return directory_for_track(directory["path"])
        else:
            return directory
예제 #4
0
def library_possessions():
    possessions = {}

    connection = db.session.get_bind(mapper=None).connect()
    try:
        result = connection.execution_options(stream_results=True).execute("""
            SELECT file.checksum, file.path
            FROM file
        """)
        for row in result:
            possessions[row["checksum"]] = encode_path(bytes(row["path"]))
    finally:
        connection.close()

    return jsonify(possessions)
예제 #5
0
def library_possessions():
    possessions = {}

    connection = db.session.get_bind(mapper=None).connect()
    try:
        result = connection.execution_options(stream_results=True).execute("""
            SELECT file.checksum, file.path
            FROM file
        """)
        for row in result:
            possessions[row["checksum"]] = encode_path(bytes(row["path"]))
    finally:
        connection.close()

    return jsonify(possessions)
예제 #6
0
def recommendations_for_from_streamer(for_username, from_username):
    library_path = os.path.join(app.config["DATA_DIR"], b"library")

    artists_tracks = defaultdict(lambda: defaultdict(set))
    connection = db.session.get_bind(mapper=None).connect()
    try:
        result = connection.execution_options(stream_results=True).execute("""
            SELECT track.artist, track.title, file.path
            FROM file
            JOIN track ON (track.id = file.track_id)
        """)
        for row in result:
            artists_tracks[row["artist"]][row["title"]].add(bytes(row["path"]))
    finally:
        connection.close()

    params = {
        "include-user": from_username,
        "min-scrobbles-count": request.args["min-scrobbles-count"],
        "sort": request.args["sort"],
        "limit": 10000
    }
    if request.args.get("datetime-start"):
        params["datetime-start"] = request.args.get("datetime-start")
    if request.args.get("datetime-end"):
        params["datetime-end"] = request.args.get("datetime-end")
    if for_username != from_username:
        params["exclude-user"] = for_username
    limit = request.args.get("limit", 100, type=int)

    include_dirs = request.args.getlist("include-dir")
    exclude_dirs = request.args.getlist("exclude-dir")

    yielded = set()
    for line in requests.get(app.config["LAST_FM_THELOGIN_RU_URL"] +
                             "/api/recommendations/",
                             params=params,
                             stream=True).iter_lines():
        recommendation = json.loads(line)
        track_paths = set().union(*[
            set().union(*[
                artists_tracks[artist][track] for track in find_variants(
                    recommendation["track"], artists_tracks[artist].keys())
            ]) for artist in find_variants(recommendation["artist"],
                                           artists_tracks.keys())
        ])
        if track_paths:
            track_path = choose_best_track_path(track_paths)

            if include_dirs and not any(
                    track_path.startswith(prefix.encode("utf-8"))
                    for prefix in include_dirs):
                continue
            if exclude_dirs and any(
                    track_path.startswith(prefix.encode("utf-8"))
                    for prefix in exclude_dirs):
                continue

            if request.args.get("type") == "file":
                with open(
                        os.path.join(
                            os.path.dirname(
                                os.path.join(library_path,
                                             encode_path(track_path))),
                            b"index.json")) as f:
                    track = (filter(
                        lambda item: item["path"] == encode_path(track_path),
                        json.load(f).values()) + [None])[0]
                    if track:
                        yield json.dumps(track) + "\n"
                        limit -= 1
                        if limit == 0:
                            break
            elif request.args.get("type") == "directory":
                directory = directory_for_track(track_path)
                if directory:
                    if directory["path"] not in yielded:
                        yielded.add(directory["path"])
                        yield json.dumps(directory) + "\n"
                        limit -= 1
                        if limit == 0:
                            break
def recommendations_for_from_streamer(for_username, from_username):
    library_path = os.path.join(app.config["DATA_DIR"], b"library")

    artists_tracks = defaultdict(lambda: defaultdict(set))
    connection = db.session.get_bind(mapper=None).connect()
    try:
        result = connection.execution_options(stream_results=True).execute("""
            SELECT track.artist, track.title, file.path
            FROM file
            JOIN track ON (track.id = file.track_id)
        """)
        for row in result:
            artists_tracks[row["artist"]][row["title"]].add(bytes(row["path"]))
    finally:
        connection.close()

    params = {"include-user": from_username,
              "min-scrobbles-count": request.args["min-scrobbles-count"],
              "sort": request.args["sort"],
              "limit": 10000}
    if request.args.get("datetime-start"):
        params["datetime-start"] = request.args.get("datetime-start")
    if request.args.get("datetime-end"):
        params["datetime-end"] = request.args.get("datetime-end")
    if for_username != from_username:
        params["exclude-user"] = for_username
    limit = request.args.get("limit", 100, type=int)

    include_dirs = request.args.getlist("include-dir")
    exclude_dirs = request.args.getlist("exclude-dir")

    yielded = set()
    for line in requests.get(
        app.config["LAST_FM_THELOGIN_RU_URL"] + "/api/recommendations/",
        params=params,
        stream=True
    ).iter_lines():
        recommendation = json.loads(line)
        track_paths = set().union(*[
            set().union(*[
                artists_tracks[artist][track]
                for track in find_variants(recommendation["track"], artists_tracks[artist].keys())
            ])
            for artist in find_variants(recommendation["artist"], artists_tracks.keys())
        ])
        if track_paths:
            track_path = choose_best_track_path(track_paths)

            if include_dirs and not any(track_path.startswith(prefix.encode("utf-8"))
                                        for prefix in include_dirs):
                continue
            if exclude_dirs and any(track_path.startswith(prefix.encode("utf-8"))
                                    for prefix in exclude_dirs):
                continue

            if request.args.get("type") == "file":
                with open(os.path.join(os.path.dirname(os.path.join(library_path, encode_path(track_path))),
                                       b"index.json")) as f:
                    track = (filter(lambda item: item["path"] == encode_path(track_path),
                                    json.load(f).values()) + [None])[0]
                    if track:
                        yield json.dumps(track) + "\n"
                        limit -= 1
                        if limit == 0:
                            break
            elif request.args.get("type") == "directory":
                directory = directory_for_track(track_path)
                if directory:
                    if directory["path"] not in yielded:
                        yielded.add(directory["path"])
                        yield json.dumps(directory) + "\n"
                        limit -= 1
                        if limit == 0:
                            break
def item_siblings_index(path):
    with open(os.path.join(os.path.dirname(os.path.join(app.config["DATA_DIR"], b"library", encode_path(path))),
                           os.path.pardir,
                           b"index.json")) as f:
        return json.load(f)