public
Description: Some random scripts
Homepage: http://blog.paulbonser.com/
Clone URL: git://github.com/pib/scripts.git
pib (author)
Sun Nov 30 13:55:30 -0800 2008
commit  60b2a76e0e77a34a2d144d0e9682db353dac9c5e
tree    af883412b8465fc0baa4b0b5d42bca26228a9223
parent  3568c045182f25fe32b997365a3dc150779e2411
scripts / shuffle_range.py
100755 60 lines (46 sloc) 1.535 kb
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
#!/usr/bin/env python
 
## Config: change these to match your setup
mpd_host = 'localhost'
mpd_port = 6600
 
import mpd
import random
 
class Shuffler:
    def __init__(self, host, port):
        self.host = host
        self.port = port
        self.connected = False
        self.client = mpd.MPDClient()
 
    def connect(self):
        if self.connected: return
        self.client.connect(self.host, self.port)
        self.connected = True
 
    def disconnect(self):
        if not self.connected: return
        self.client.disconnect()
        self.connected = false
 
    def shuffle_rest(self):
        self.connect()
        status = self.client.status()
        self.shuffle_range(int(status['song'])+1 or 0,
                           int(status['playlistlength'])-1)
 
    def shuffle_range(self, start, end):
        start, end = int(start), int(end)
        self.connect()
 
        for fromi in range(start, end+1):
            toi = random.randint(start, end)
            self.client.swap(fromi, toi)
 
if __name__=="__main__":
    import sys
 
    usage = """Usage: %s (rest | start end)
rest - shuffle all the songs after the current one
start - start of shuffling range
end - end of shuffling range""" % (sys.argv[0])
 
    if len(sys.argv) > 1:
        shuffler = Shuffler(mpd_host, mpd_port)
        if sys.argv[1] == 'rest':
            shuffler.shuffle_rest()
        elif len(sys.argv) == 3:
            shuffler.shuffle_range(sys.argv[1], sys.argv[2])
 
        else: print usage
 
    else:
        print usage