Exemplo n.º 1
0
def extend_playlist(playlist):
    playlist_id = encode_int_id(playlist["playlist_id"])
    owner_id = encode_int_id(playlist["playlist_owner_id"])
    playlist["id"] = playlist_id
    playlist["user_id"] = owner_id
    playlist = add_playlist_artwork(playlist)
    if "user" in playlist:
        playlist["user"] = extend_user(playlist["user"])
    if "followee_saves" in playlist:
        playlist["followee_favorites"] = list(
            map(extend_favorite, playlist["followee_saves"]))
    if "followee_reposts" in playlist:
        playlist["followee_reposts"] = list(
            map(extend_repost, playlist["followee_reposts"]))
    if "save_count" in playlist:
        playlist["favorite_count"] = playlist["save_count"]

    playlist["added_timestamps"] = add_playlist_added_timestamps(playlist)
    playlist["cover_art"] = playlist["playlist_image_multihash"]
    playlist["cover_art_sizes"] = playlist["playlist_image_sizes_multihash"]
    # If a trending playlist, we have 'track_count'
    # already to preserve the original, non-abbreviated track count
    playlist["track_count"] = (playlist["track_count"]
                               if "track_count" in playlist else len(
                                   playlist["playlist_contents"]["track_ids"]))
    return playlist
Exemplo n.º 2
0
def resolve_url(session, url):
    """
    Resolves an Audius URL into the cannonical API route.
    Accepts fully formed urls as well as just url paths.
    """
    parsed = urlparse(url)
    # Will strip out any preceding protocol & domain (e.g. https://audius.co)
    path = parsed.path

    match = track_url_regex.match(path)
    if match:
        slug = match.group("slug")
        handle = match.group("handle")
        return ns_url_for(tracks_ns, "bulk_tracks", slug=slug, handle=handle)

    match = playlist_url_regex.match(path)
    if match:
        playlist_id = match.group("id")
        hashed_id = encode_int_id(int(playlist_id))
        return ns_url_for(playlists_ns, "playlist", playlist_id=hashed_id)

    match = user_url_regex.match(path)
    if match:
        handle = match.group("handle")
        user = (
            session.query(User)
            .filter(User.handle_lc == handle.lower(), User.is_current == True)
            .one()
        )
        hashed_id = encode_int_id(user.user_id)
        return ns_url_for(users_ns, "user", id=hashed_id)

    return None
Exemplo n.º 3
0
def extend_track(track):
    track_id = encode_int_id(track["track_id"])
    owner_id = encode_int_id(track["owner_id"])
    if "user" in track:
        track["user"] = extend_user(track["user"])
    track["id"] = track_id
    track["user_id"] = owner_id
    if "followee_saves" in track:
        track["followee_favorites"] = list(
            map(extend_favorite, track["followee_saves"]))
    if "followee_reposts" in track:
        track["followee_reposts"] = list(
            map(extend_repost, track["followee_reposts"]))
    if "remix_of" in track:
        track["remix_of"] = extend_remix_of(track["remix_of"])

    track = add_track_artwork(track)

    if "save_count" in track:
        track["favorite_count"] = track["save_count"]

    duration = 0.0
    for segment in track["track_segments"]:
        # NOTE: Legacy track segments store the duration as a string
        duration += float(segment["duration"])
    track["duration"] = round(duration)

    downloadable = ("download" in track and track["download"]
                    and track["download"]["is_downloadable"])
    track["downloadable"] = bool(downloadable)

    return track
Exemplo n.º 4
0
def stem_from_track(track):
    track_id = encode_int_id(track["track_id"])
    parent_id = encode_int_id(track["stem_of"]["parent_track_id"])
    category = track["stem_of"]["category"]
    return {
        "id": track_id,
        "parent_id": parent_id,
        "category": category,
        "cid": track["download"]["cid"],
        "user_id": encode_int_id(track["owner_id"]),
        "blocknumber": track["blocknumber"],
    }
Exemplo n.º 5
0
def extend_challenge_response(challenge: ChallengeResponse):
    user_id = encode_int_id(challenge["user_id"])
    new_challenge = challenge.copy()
    new_challenge["user_id"] = user_id
    new_challenge["challenge_type"] = challenge_type_map[
        challenge["challenge_type"]]
    return new_challenge
Exemplo n.º 6
0
def extend_tip(tip):
    new_tip = tip.copy()
    new_tip["amount"] = to_wei_string(tip["amount"])
    new_tip["sender"] = extend_user(tip["sender"])
    new_tip["receiver"] = extend_user(tip["receiver"])
    new_tip["followee_supporters"] = [{
        "user_id": encode_int_id(id)
    } for id in new_tip["followee_supporters"]]
    return new_tip
Exemplo n.º 7
0
def add_playlist_added_timestamps(playlist):
    if "playlist_contents" not in playlist:
        return playlist
    added_timestamps = []
    for track in playlist["playlist_contents"]["track_ids"]:
        added_timestamps.append({
            "track_id": encode_int_id(track["track"]),
            "timestamp": track["time"]
        })
    return added_timestamps
Exemplo n.º 8
0
def extend_user(user, current_user_id=None):
    user_id = encode_int_id(user["user_id"])
    user["id"] = user_id
    user = add_user_artwork(user)
    # Do not surface playlist library in user response unless we are
    # that user specifically
    if "playlist_library" in user and (not current_user_id
                                       or current_user_id != user["user_id"]):
        del user["playlist_library"]
    # Marshal wallets into clear names
    user["erc_wallet"] = user["wallet"]

    return user
Exemplo n.º 9
0
def extend_reaction(reaction: ReactionResponse):
    new_reaction = reaction.copy()
    new_reaction["sender_user_id"] = encode_int_id(reaction["sender_user_id"])
    return new_reaction
Exemplo n.º 10
0
def extend_undisbursed_challenge(
        undisbursed_challenge: UndisbursedChallengeResponse):
    new_undisbursed_challenge = undisbursed_challenge.copy()
    new_undisbursed_challenge["user_id"] = encode_int_id(
        new_undisbursed_challenge["user_id"])
    return new_undisbursed_challenge
Exemplo n.º 11
0
def get_encoded_track_id(track):
    return {"id": encode_int_id(track["track_id"])}
Exemplo n.º 12
0
 def extend_track_element(track):
     track_id = track["parent_track_id"]
     track["parent_track_id"] = encode_int_id(track_id)
     if "user" in track:
         track["user"] = extend_user(track["user"])
     return track
Exemplo n.º 13
0
def extend_favorite(favorite):
    favorite["user_id"] = encode_int_id(favorite["user_id"])
    favorite["favorite_item_id"] = encode_int_id(favorite["save_item_id"])
    favorite["favorite_type"] = favorite["save_type"]
    return favorite
Exemplo n.º 14
0
def extend_repost(repost):
    repost["user_id"] = encode_int_id(repost["user_id"])
    repost["repost_item_id"] = encode_int_id(repost["repost_item_id"])
    return repost
Exemplo n.º 15
0
 def get(self):
     args = user_associated_wallet_route_parser.parse_args()
     user_id = get_associated_user_id({"wallet": args.get("associated_wallet")})
     return success_response(
         {"user_id": encode_int_id(user_id) if user_id else None}
     )