forked from naim-prog/soundcloud-py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example.py
70 lines (49 loc) · 2.02 KB
/
example.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Some simple tests/example for the Soundcloud api client."""
import argparse
import asyncio
import logging
import sys
from contextlib import suppress
from aiohttp import ClientSession
from soundcloudpy import SoundcloudAsyncAPI
LOGGER = logging.getLogger()
def get_arguments() -> argparse.Namespace:
"""Get parsed passed in arguments."""
parser = argparse.ArgumentParser(description="Soundcloud simple client for Python")
parser.add_argument("--debug", action="store_true", help="Log with debug level")
parser.add_argument("--client_id", type=str, help="Soundcloud client_id")
parser.add_argument("--auth_token", type=str, help="Soundcloud auth_token")
return parser.parse_args()
async def start_cli() -> None:
"""Run main."""
args = get_arguments()
level = logging.DEBUG if args.debug else logging.INFO
logging.basicConfig(level=level)
async with ClientSession() as session:
await connect(args, session)
async def connect(args: argparse.Namespace, session: ClientSession) -> None:
"""Connect to the Soundcloud api."""
soundcloud = SoundcloudAsyncAPI(args.auth_token, args.client_id, session)
await soundcloud.login()
me = await soundcloud.get_account_details()
LOGGER.info("My user_id: %s", me["id"])
following = await soundcloud.get_following(me["id"])
artists = [artist["username"] for artist in following["collection"]]
LOGGER.info("Following: %s", artists)
playlists = []
async for item in soundcloud.get_account_playlists():
playlists.append(item["playlist"]["title"])
LOGGER.info("Playlists: %s", playlists)
tracks = []
async for item in soundcloud.get_tracks_liked():
tracks.append(item)
LOGGER.info("Tracks: %s", tracks)
stream_url = await soundcloud.get_stream_url(tracks[0])
LOGGER.info("Stream url for track %s: %r", tracks[0], stream_url)
def main() -> None:
"""Run main."""
with suppress(KeyboardInterrupt):
asyncio.run(start_cli())
sys.exit(0)
if __name__ == "__main__":
main()