aboutsummaryrefslogtreecommitdiff
path: root/pydora/player.py
blob: c4ce2e06f096d499143496445cab09aca7498081 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python
"""
Sample Barebones Pandora Player

This is a very simple Pandora player that streams music from Pandora. It
requires mpg123 or VLC to function. No songs are downloaded, they are streamed
directly from Pandora's servers.
"""
import os
import sys
import logging
import argparse
from pandora import clientbuilder

from .utils import Colors, Screen
from .audio_backend import RemoteVLC
from .audio_backend import MPG123Player, VLCPlayer
from .audio_backend import UnsupportedEncoding, PlayerUnusable


class PlayerCallbacks:
    """Interface for Player Callbacks

    This class simply exists to document the interface for callback
    implementers implementers need not extend this class.
    """

    def play(self, song):
        """Called once when a song starts playing
        """
        pass

    def pre_poll(self):
        """Called before polling for process status
        """
        pass

    def post_poll(self):
        """Called after polling for process status
        """
        pass

    def input(self, value, song):
        """Called after user input during song playback
        """
        pass


class PlayerApp:

    CMD_MAP = {
        "n": ("play next song", "skip_song"),
        "p": ("pause/resume song", "pause_song"),
        "s": ("stop playing station", "stop_station"),
        "d": ("dislike song", "dislike_song"),
        "u": ("like song", "like_song"),
        "b": ("bookmark song", "bookmark_song"),
        "a": ("bookmark artist", "bookmark_artist"),
        "S": ("sleep song for 30 days", "sleep_song"),
        "Q": ("quit player", "quit"),
        "vu": ("raise volume", "raise_volume"),
        "vd": ("lower volume", "lower_volume"),
        "?": ("display this help", "help"),
    }

    def __init__(self):
        self.client = None
        self.screen = Screen()

    def get_player(self, vlc_net=None):
        # The user must explicitly request network VLC so we should always
        # honor that request, to this end we try network first and fail hard
        # if that isn't available.
        if vlc_net:
            try:
                host, port = vlc_net.split(":")
                player = RemoteVLC(host, port, self, sys.stdin)
                Screen.print_success("Using Remote VLC")
                return player
            except PlayerUnusable:
                Screen.print_error("Unable to connect to vlc")
                raise

        try:
            player = VLCPlayer(self, sys.stdin)
            self.screen.print_success("Using VLC")
            return player
        except PlayerUnusable:
            pass

        try:
            player = MPG123Player(self, sys.stdin)
            self.screen.print_success("Using mpg123")
            return player
        except PlayerUnusable:
            pass

        self.screen.print_error("Unable to find a player")
        sys.exit(1)

    def get_client(self):
        cfg_file = os.environ.get("PYDORA_CFG", "")
        builder = clientbuilder.PydoraConfigFileBuilder(cfg_file)
        if builder.file_exists:
            return builder.build()

        builder = clientbuilder.PianobarConfigFileBuilder()
        if builder.file_exists:
            return builder.build()

        if not self.client:
            self.screen.print_error("No valid config found")
            sys.exit(1)

    def station_selection_menu(self, error=None):
        """Format a station menu and make the user select a station
        """
        self.screen.clear()

        if error:
            self.screen.print_error("{}\n".format(error))

        for i, station in enumerate(self.stations):
            i = "{:>3}".format(i)
            print("{}: {}".format(Colors.yellow(i), station.name))

        return self.stations[self.screen.get_integer("Station: ")]

    def play(self, song):
        """Play callback
        """
        if song.is_ad:
            print("{} ".format(Colors.cyan("Advertisement")))
        else:
            print(
                "{} by {}".format(
                    Colors.cyan(song.song_name),
                    Colors.yellow(song.artist_name),
                )
            )

    def skip_song(self, song):
        if song.is_ad:
            self.screen.print_error("Cannot skip advertisements")
        else:
            self.player.stop()

    def pause_song(self, song):
        self.player.pause()

    def stop_station(self, song):
        self.player.end_station()

    def dislike_song(self, song):
        try:
            if song.thumbs_down():
                self.screen.print_success("Track disliked")
                self.player.stop()
            else:
                self.screen.print_error("Failed to dislike track")
        except NotImplementedError:
            self.screen.print_error("Cannot dislike this type of track")

    def like_song(self, song):
        try:
            if song.thumbs_up():
                self.screen.print_success("Track liked")
            else:
                self.screen.print_error("Failed to like track")
        except NotImplementedError:
            self.screen.print_error("Cannot like this type of track")

    def bookmark_song(self, song):
        try:
            if song.bookmark_song():
                self.screen.print_success("Bookmarked song")
            else:
                self.screen.print_error("Failed to bookmark song")
        except NotImplementedError:
            self.screen.print_error("Cannot bookmark this type of track")

    def bookmark_artist(self, song):
        try:
            if song.bookmark_artist():
                self.screen.print_success("Bookmarked artist")
            else:
                self.screen.print_error("Failed to bookmark artis")
        except NotImplementedError:
            self.screen.print_error(
                "Cannot bookmark artist for this type of track"
            )

    def sleep_song(self, song):
        try:
            if song.sleep():
                self.screen.print_success(
                    "Song will not be played for 30 days"
                )
                self.player.stop()
            else:
                self.screen.print_error("Failed to sleep song")
        except NotImplementedError:
            self.screen.print_error("Cannot sleep this type of track")

    def raise_volume(self, song):
        try:
            self.player.raise_volume()
        except NotImplementedError:
            self.screen.print_error("Cannot sleep this type of track")

    def lower_volume(self, song):
        try:
            self.player.lower_volume()
        except NotImplementedError:
            self.screen.print_error("Cannot sleep this type of track")

    def quit(self, song):
        self.player.end_station()
        sys.exit(0)

    def help(self, song):
        print("")
        print(
            "\n".join(
                [
                    "\t{:>2} - {}".format(k, v[0])
                    for k, v in sorted(self.CMD_MAP.items())
                ]
            )
        )
        print("")

    def input(self, input, song):
        """Input callback, handles key presses
        """
        try:
            cmd = getattr(self, self.CMD_MAP[input][1])
        except (IndexError, KeyError):
            return self.screen.print_error(
                "Invalid command {!r}!".format(input)
            )

        cmd(song)

    def pre_poll(self):
        self.screen.set_echo(False)

    def post_poll(self):
        self.screen.set_echo(True)

    def pre_flight_checks(self):
        # See #52, this key no longer passes some server-side check
        if self.client.partner_user == "iphone":
            self.screen.print_error(
                (
                    "The `iphone` partner key set is no longer compatible "
                    "with pydora. Please re-run pydora-configure to "
                    "re-generate your config file before continuing."
                )
            )
            sys.exit(1)

    def _parse_args(self):
        parser = argparse.ArgumentParser(
            description="command line Pandora player"
        )
        parser.add_argument(
            "--vlc-net",
            dest="vlc_net",
            help="connect to VLC over the network (host:port)",
        )
        parser.add_argument(
            "-v",
            dest="verbose",
            action="store_true",
            help="enable verbose logging",
        )
        return parser.parse_args()

    def run(self):
        args = self._parse_args()

        if args.verbose:
            logging.basicConfig(level=logging.DEBUG)
        else:
            logging.basicConfig(level=logging.ERROR)

        self.player = self.get_player(args.vlc_net)
        self.player.start()

        self.client = self.get_client()
        self.stations = self.client.get_station_list()

        self.pre_flight_checks()

        error = None

        while True:
            try:
                station = self.station_selection_menu(error)
                error = None
            except IndexError:
                error = "Invalid station selection."
                continue
            except KeyboardInterrupt:
                sys.exit(0)

            try:
                self.player.play_station(station)
            except UnsupportedEncoding as ex:
                error = str(ex)
            except KeyboardInterrupt:
                self.player.stop()
                sys.exit(0)


def main():
    PlayerApp().run()