aboutsummaryrefslogtreecommitdiff
path: root/pydora
diff options
context:
space:
mode:
authorMike Crute <mcrute@gmail.com>2013-12-30 20:54:43 -0500
committerMike Crute <mcrute@gmail.com>2013-12-30 20:54:43 -0500
commitd96f8e4362ec61e2731bac1d3eac3aeeb5bc1e3e (patch)
tree24c2623c67e1e4d7f9c3a20d929d2eb91b96bd07 /pydora
parentd5f0f7d7848da7655a2ef829315f9e304e9fd5fc (diff)
downloadpydora-d96f8e4362ec61e2731bac1d3eac3aeeb5bc1e3e.tar.bz2
pydora-d96f8e4362ec61e2731bac1d3eac3aeeb5bc1e3e.tar.xz
pydora-d96f8e4362ec61e2731bac1d3eac3aeeb5bc1e3e.zip
Externalize config
Diffstat (limited to 'pydora')
-rw-r--r--pydora/__init__.py0
-rwxr-xr-xpydora/player.py150
2 files changed, 150 insertions, 0 deletions
diff --git a/pydora/__init__.py b/pydora/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/pydora/__init__.py
diff --git a/pydora/player.py b/pydora/player.py
new file mode 100755
index 0000000..126c5ae
--- /dev/null
+++ b/pydora/player.py
@@ -0,0 +1,150 @@
1#!/usr/bin/env python
2"""
3Sample Barebones Pandora Player
4
5This is a very simple Pandora player that streams music from Pandora. It
6requires mpg123 to function. No songs are downloaded, they are streamed
7directly from Pandora's servers.
8"""
9import os
10import sys
11from ConfigParser import SafeConfigParser
12
13from pandora import APIClient
14from pandora.player import Player
15from pandora.utils import Colors, Screen
16
17
18class PlayerApp:
19
20 CMD_MAP = {
21 'n': ('play next song', 'skip_song'),
22 'p': ('pause/resume song', 'pause_song'),
23 's': ('stop playing station', 'stop_station'),
24 'd': ('dislike song', 'dislike_song'),
25 'u': ('like song', 'like_song'),
26 'b': ('bookmark song', 'bookmark_song'),
27 'a': ('bookmark artist', 'bookmark_artist'),
28 'S': ('sleep song for 30 days', 'sleep_song'),
29 'Q': ('quit player', 'quit'),
30 '?': ('display this help', 'help'),
31 }
32
33 def __init__(self):
34 settings, self.credentials = self._load_settings()
35 self.client = APIClient.from_settings_dict(settings)
36 self.player = Player(self, sys.stdin)
37
38 def _load_settings(self):
39 """Load settings from config file
40
41 Config file exists in either ~/.pydora.cfg or is pointed to by an
42 environment variable PYDORA_CFG.
43 """
44 path = os.path.expanduser(
45 os.environ.get('PYDORA_CFG', '~/.pydora.cfg'))
46
47 if not os.path.exists(path):
48 Screen.print_error('No settings at {!r}'.format(path))
49 sys.exit(1)
50
51 cfg = SafeConfigParser()
52 cfg.read(path)
53
54 return (
55 dict((k.upper(), v) for k, v in cfg.items('api')),
56 [i[1] for i in cfg.items('user')])
57
58 def station_selection_menu(self):
59 """Format a station menu and make the user select a station
60 """
61 Screen.clear()
62
63 for i, s in enumerate(self.stations):
64 i = '{:>3}'.format(i)
65 print('{}: {}'.format(Colors.yellow(i), s.name))
66
67 return self.stations[Screen.get_integer('Station: ')]
68
69 def play(self, song):
70 """Play callback
71 """
72 print('{} by {}'.format(Colors.blue(song.song_name),
73 Colors.yellow(song.artist_name)))
74
75 def skip_song(self, song):
76 self.player.stop()
77
78 def pause_song(self, song):
79 self.player.pause()
80
81 def stop_station(self, song):
82 self.player.end_station()
83
84 def dislike_song(self, song):
85 song.thumbs_down()
86 Screen.print_success('Track disliked')
87 self.player.stop()
88
89 def like_song(self, song):
90 song.thumbs_up()
91 Screen.print_success('Track liked')
92
93 def bookmark_song(self, song):
94 song.bookmark_song()
95 Screen.print_success('Bookmarked song')
96
97 def bookmark_artist(self, song):
98 song.bookmark_artist()
99 Screen.print_success('Bookmarked artist')
100
101 def sleep_song(self, song):
102 song.sleep()
103 Screen.print_success('Song will not be played for 30 days')
104 self.player.stop()
105
106 def quit(self, song):
107 self.player.end_station()
108 sys.exit(0)
109
110 def help(self, song):
111 print("")
112 print("\n".join([
113 "\t{} - {}".format(k, v[0]) for k, v in self.CMD_MAP.items()
114 ]))
115 print("")
116
117 def input(self, input, song):
118 """Input callback, handles key presses
119 """
120 try:
121 cmd = getattr(self, self.CMD_MAP[input][1])
122 except (IndexError, KeyError):
123 return Screen.print_error('Invalid command!')
124
125 cmd(song)
126
127 def pre_poll(self):
128 Screen.set_echo(False)
129
130 def post_poll(self):
131 Screen.set_echo(True)
132
133 def run(self):
134 self.client.login(*self.credentials)
135 self.stations = self.client.get_station_list()
136
137 while True:
138 try:
139 station = self.station_selection_menu()
140 self.player.play_station(station)
141 except KeyboardInterrupt:
142 sys.exit(0)
143
144
145def main():
146 PlayerApp().run()
147
148
149if __name__ == '__main__':
150 main()