File: __init__.py

package info (click to toggle)
exaile 0.3.2.2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,564 kB
  • sloc: python: 35,424; makefile: 265; sh: 58
file content (158 lines) | stat: -rw-r--r-- 5,235 bytes parent folder | download
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
# Copyright (C) 2006 Adam Olsen
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 1, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

import subprocess, logging, os, shutil
import gtk
from xl import event
from xl.nls import gettext as _
from xl import settings
from xlgui.widgets import dialogs
import srprefs

logger = logging.getLogger(__name__)

STREAMRIPPER = None

def get_preferences_pane():
    return srprefs

class Streamripper(object):
    def __init__(self):
        self.savedir = None

    def toggle_record(self, add_call):
        current_track = self.exaile.player.current
        if not current_track:
            return True
        if current_track.is_local():
            logger.warning('Streamripper can only record streams')
            return True

        self.savedir = settings.get_option('plugin/streamripper/save_location',
                            os.getenv('HOME'))
        options = []
        options.append('streamripper')
        options.append(self.exaile.player.playbin.get_property('uri'))
        options.append('-D')
        options.append('%A/%a/%T')
        if settings.get_option('plugin/streamripper/single_file', False):
			options.append("-a")
			options.append("-A")
        options.append("-r")
        options.append(settings.get_option('plugin/streamripper/relay_port', '8888'))
        options.append("-d")
        options.append(self.savedir)

        try:
            self.process = subprocess.Popen(options, 0, None, subprocess.PIPE,
                        subprocess.PIPE, subprocess.PIPE)
        except OSError:
            logger.error('There was an error executing streamripper')
            dialogs.error(self.exaile.gui.main.window, _('Error '
                    'executing streamripper'))
            return True

        if add_call:
            event.add_callback(self.quit_application, 'quit_application')
            event.add_callback(self.start_track, 'playback_track_start')
            event.add_callback(self.stop_playback, 'playback_player_end')
        return False

    def stop_ripping(self):
        try:
            self.process.terminate()
        except Exception:
            pass
        if settings.get_option('plugin/streamripper/delete_incomplete', True):
            try:
                shutil.rmtree(self.savedir + "/incomplete")
            except OSError:
                pass
                
    def quit_application(self, type, player, track):
        self.stop_ripping()

    def stop_playback(self, type, player, track):
        self.stop_ripping()
        self.button.set_active(False)
        self.remove_callbacks()

    def start_track(self, type, player, track):
        self.stop_ripping()
        if self.toggle_record(False):
            self.button.set_active(False)
            self.remove_callbacks()

    def remove_callbacks(self):
        event.remove_callback(self.quit_application, 'quit_application')
        event.remove_callback(self.start_track, 'playback_track_start')
        event.remove_callback(self.stop_playback, 'playback_player_end')


class Button(Streamripper):
    def __init__(self, exaile):
        self.exaile = exaile
        self.button = gtk.ToggleButton()
        image = gtk.Image()
        image.set_from_stock(gtk.STOCK_MEDIA_RECORD, gtk.ICON_SIZE_SMALL_TOOLBAR)
        self.button.set_image(image)

        toolbar = self.exaile.gui.play_toolbar
        toolbar.pack_start(self.button, False, False)
        toolbar.reorder_child(self.button, 3)

        self.button.show()

        self.button.connect('button-release-event', self.toggle_button_callback)

    def toggle_button_callback(self, widget, data):
        if widget.get_active():
            self.stop_ripping()
            self.remove_callbacks()
        else:
            if self.toggle_record(True): #couldn't record stream
                self.button.set_active(True)
                self.remove_callbacks()

    def remove_button(self):
        self.exaile.gui.play_toolbar.remove(self.button)
        self.button.hide()
        self.button.destroy()
        self.remove_callbacks()


def enable(exaile):
    try: #just test if streamripper is installed
        subprocess.call(['streamripper'], stdout=-1, stderr=-1)
    except OSError:
        raise NotImplementedError('Streamripper is not available.')
        return False

    if (exaile.loading):
        event.add_callback(_enable, 'exaile_loaded')
    else:
        _enable(None, exaile, None)


def _enable(eventname, exaile, nothing):
    global STREAMRIPPER
    STREAMRIPPER = Button(exaile)


def disable(exaile):
    global STREAMRIPPER
    STREAMRIPPER.remove_button()
    STREAMRIPPER = None