aboutsummaryrefslogtreecommitdiff
path: root/code/utils/source_helpers/parse.py
blob: 84895156da3ac81966cc7bf02050518e115d429d (plain)
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
71
72
73
74
75
76
77
78
79
80
import discord

from utils.source_helpers.apple import (
    album as apple_album,
    playlist as apple_playlist,
    song as apple_song,
)
from utils.source_helpers.spotify import (
    album as spotify_album,
    playlist as spotify_playlist,
    song as spotify_song,
)
from utils.custom_sources import AppleSource, SpotifySource


async def parse_custom_source(
    self, provider: str, query: str, user: discord.User
):
    """
    Parse the query and run the appropriate functions to get the results/info

    Return the results and an embed or None, None
    """
    load_funcs = {
        "apple": {
            "album": apple_album.load,
            "playlist": apple_playlist.load,
            "song": apple_song.load,
        },
        "spotify": {
            "album": spotify_album.load,
            "playlist": spotify_playlist.load,
            "song": spotify_song.load,
        },
    }

    headers = {
        "apple": self.bot.apple_headers,
        "spotify": self.bot.spotify_headers,
    }

    sources = {
        "apple": AppleSource,
        "spotify": SpotifySource,
    }

    # Catch all songs
    if "?i=" in query or "/track/" in query:
        song, embed = await load_funcs[provider]["song"](
            headers[provider], query, user
        )

        if song:
            results = await sources[provider].load_item(self, user, song)
        else:
            return None, embed
    # Catch all playlists
    elif "/playlist/" in query:
        playlist, embed = await load_funcs[provider]["playlist"](
            headers[provider], query, user
        )

        if playlist:
            results = await sources[provider].load_playlist(
                self, user, playlist
            )
        else:
            return None, embed
    # Catch all albums
    elif "/album/" in query:
        album, embed = await load_funcs[provider]["album"](
            headers[provider], query, user
        )

        if album:
            results = await sources[provider].load_album(self, user, album)
        else:
            return None, embed

    return results, embed