Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions soundcloud/resource/history.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from dataclasses import dataclass
from typing import List

# from dacite import from_dict

from soundcloud.resource.base import BaseData
from soundcloud.resource.track import BasicTrack


"""Hello copilot!

Write a dataclass that has the following attributes:

1. An int attribute called played_at
2. An int attribute called track_id
3. A basic track attribute called track

The dataclass should be called TaggedTrack

"""

# This was inaccurate so here's my version
@dataclass
class TaggedTrack(BaseData):
played_at: int
track_id: int
track: BasicTrack

"""Hello copilot!

Write a dataclass that has the following attributes:

1. A list of tagged tracks called tracks

The dataclass should be called History

"""

@dataclass
class History(BaseData):
tracks: List[TaggedTrack]


"""Hello copilot!
Based on my the entire soundcloud.py directory write a dataclass that represents Listening history of a user and stores the all tracks along with their nested data.
"""

# @dataclass
# class History(BaseData):
# tracks: list[AlbumPlaylistNoTracks]
3 changes: 1 addition & 2 deletions soundcloud/resource/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ class BaseTrack(BaseItem):
purchase_url: Optional[str]
state: str
streamable: bool
track_format: str
urn: str
visuals: Optional[Visuals]
waveform_url: str
Expand Down Expand Up @@ -96,4 +95,4 @@ class CommentTrack(BaseData):
track_authorization: str
monetization_model: str
policy: str
user: BasicUser
user: BasicUser
25 changes: 19 additions & 6 deletions soundcloud/soundcloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
import string
from dataclasses import dataclass
from typing import Dict, Generator, Generic, List, Optional, TypeVar, Union
#print("hard nops")

#exit()
try:
from typing import get_args, get_origin
except ImportError:
Expand All @@ -24,6 +26,7 @@ def get_origin(tp):
from .resource.download import OriginalDownload
from .resource.like import PlaylistLike, TrackLike
from .resource.message import Message
from .resource.history import History, TaggedTrack
from .resource.playlist import AlbumPlaylist, BasicAlbumPlaylist
from .resource.track import BasicTrack, Track
from .resource.user import User
Expand All @@ -43,7 +46,7 @@ class SoundCloud:
CLIENT_ID_REGEX = re.compile(r"client_id:\"([^\"]+)\"")

def __init__(self, client_id: str = None, auth_token: str = None, user_agent: str = DEFAULT_USER_AGENT) -> None:

if not client_id:
client_id = self.generate_client_id()

Expand All @@ -55,6 +58,8 @@ def __init__(self, client_id: str = None, auth_token: str = None, user_agent: st

self.requests: Dict[str, Request] = {
"me": Request[User](self, "/me", User),
# get listening history of user with the following key-value pair
"history": ListRequest[History](self, "/me/play-history/tracks", History),
"me_stream": CollectionRequest[StreamItem](self, "/stream", StreamItem),
"resolve": Request[SearchItem](self, "/resolve", SearchItem),
"search": CollectionRequest[SearchItem](self, "/search", SearchItem),
Expand Down Expand Up @@ -122,9 +127,7 @@ def is_client_id_valid(self) -> bool:
Checks if current client_id is valid
"""
try:
next(
self.requests["tag_recent_tracks"](tag="electronic", limit="1", use_auth=False)
)
self.get_track(1032303631)
return True
except HTTPError as err:
if err.response.status_code == 401:
Expand Down Expand Up @@ -157,6 +160,13 @@ def get_my_stream(self, **kwargs) -> Generator[StreamItem, None, None]:
for the client's auth token
"""
return self.requests["me_stream"](**kwargs)

def get_history(self, **kwargs) -> Generator[StreamItem, None, None]:
"""
Returns the stream of recent uploads/reposts
for the client's auth token
"""
return self.requests["history"](history=True, **kwargs)

def resolve(self, url: str) -> Optional[SearchItem]:
"""
Expand Down Expand Up @@ -504,6 +514,9 @@ def __call__(self, use_auth=True, **kwargs) -> List[T]:
if r.status_code in (400, 404, 500):
return []
r.raise_for_status()
for resource in r.json():
resources.append(self.convert_dict(resource))
if kwargs.get("history"):
return self.convert_dict(dict(tracks=r.json()["collection"]))
else:
for resource in r.json():
resources.append(self.convert_dict(resource))
return resources