~andrewsomething/exaile/karmic

« back to all changes in this revision

Viewing changes to plugins/streamripper/__init__.py

  • Committer: Aren Olson
  • Date: 2009-09-12 00:36:59 UTC
  • Revision ID: reacocard@gmail.com-20090912003659-w373sg0n04uoa8op
remove useless files, add soem of the fixes from lp bug 420019

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2006 Adam Olsen
2
 
#
3
 
# This program is free software; you can redistribute it and/or modify
4
 
# it under the terms of the GNU General Public License as published by
5
 
# the Free Software Foundation; either version 1, or (at your option)
6
 
# any later version.
7
 
#
8
 
# This program is distributed in the hope that it will be useful,
9
 
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
 
# GNU General Public License for more details.
12
 
#
13
 
# You should have received a copy of the GNU General Public License
14
 
# along with this program; if not, write to the Free Software
15
 
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 
 
17
 
import subprocess, logging, os
18
 
from xl import event, xdg
19
 
from xl.nls import gettext as _
20
 
from xl import settings
21
 
from xlgui import guiutil
22
 
import time, sys
23
 
import srprefs
24
 
 
25
 
# trying to not rely on the gui parts of exaile
26
 
try:
27
 
    from xlgui import commondialogs
28
 
except ImportError:
29
 
    commondialogs = None
30
 
 
31
 
logger = logging.getLogger(__name__)
32
 
 
33
 
BUTTON = None
34
 
CURRENT_TRACK = None
35
 
STREAMRIPPER_PID = None
36
 
STREAMRIPPER_OUT = None
37
 
APP = None
38
 
 
39
 
def get_prefs_pane():
40
 
    return srprefs
41
 
 
42
 
def toggle_record(widget=None, event=None):
43
 
    global STREAMRIPPER_PID, CURRENT_TRACK, STREAMRIPPER_OUT
44
 
 
45
 
    import gst
46
 
 
47
 
    track = APP.player.current
48
 
 
49
 
    if not STREAMRIPPER_PID:
50
 
        if not track: return True
51
 
        if track.is_local():
52
 
            logger.warning('Streamripper can only record streams')
53
 
            if commondialogs:
54
 
                commondialogs.error(APP.gui.main.window, _('Streamripper '
55
 
                    'can only record streams.'))
56
 
            return True
57
 
 
58
 
        savedir = settings.get_option(
59
 
                    'plugin/streamripper/save_location', 
60
 
                    os.getenv('HOME'))
61
 
        
62
 
        try:
63
 
            port = int(settings.get_option(
64
 
                'plugin/streamripper/relay_port', 8888))
65
 
        except ValueError:
66
 
            port = 8888
67
 
 
68
 
        outfile = "%s/streamripper.log" % xdg.get_config_dir()
69
 
        STREAMRIPPER_OUT = open(outfile, "w+", 0)
70
 
        STREAMRIPPER_OUT.write("Streamripper log file started: %s\n" %
71
 
            time.strftime("%c", time.localtime()))
72
 
        STREAMRIPPER_OUT.write(
73
 
            "-------------------------------------------------\n\n\n")
74
 
       
75
 
 
76
 
        APP.player.playbin.set_state(gst.STATE_NULL)
77
 
        sub = subprocess.Popen(['streamripper',
78
 
            APP.player.playbin.get_property('uri'), '-r',
79
 
            str(port), '-d', savedir], stdout=STREAMRIPPER_OUT)
80
 
        ret = sub.poll()
81
 
 
82
 
        logger.info("Using streamripper to play location: %s" % track['__loc'])
83
 
 
84
 
        if ret != None:
85
 
            logger.warning('There was an error executing streamripper')
86
 
            if commondialogs:
87
 
                commondialogs.error(APP.gui.main.window, _("Error "
88
 
                    "executing streamripper"))
89
 
                return True
90
 
 
91
 
        STREAMRIPPER_PID = sub.pid
92
 
        logger.info("Proxy location: http://localhost:%d" % port)
93
 
        APP.player.playbin.set_property('uri', 'http://localhost:%d' % port)
94
 
        time.sleep(1)
95
 
 
96
 
        APP.player.playbin.set_state(gst.STATE_PLAYING)
97
 
        CURRENT_TRACK = track
98
 
 
99
 
        return False
100
 
    else:
101
 
        os.system('kill -9 %d' % STREAMRIPPER_PID)
102
 
        APP.player.playbin.set_state(gst.STATE_READY)
103
 
        APP.player.playbin.set_property('uri', track['__loc'])
104
 
        CURRENT_TRACK = None
105
 
        APP.player.playbin.set_state(gst.STATE_PLAYING)
106
 
        STREAMRIPPER_PID = None
107
 
        if STREAMRIPPER_OUT:
108
 
            try:
109
 
                STREAMRIPPER_OUT.close()
110
 
            except OSError:
111
 
                pass
112
 
 
113
 
    return False
114
 
 
115
 
def playback_stop(type, player, object):
116
 
    global STREAMRIPPER_OUT, STREAMRIPPER_PID
117
 
    if BUTTON:
118
 
        BUTTON.set_active(False)
119
 
 
120
 
    if STREAMRIPPER_OUT:
121
 
        try:
122
 
            STREAMRIPPER_OUT.close()
123
 
        except OSError:
124
 
            pass
125
 
        STREAMRIPPER_OUT = None
126
 
 
127
 
    if STREAMRIPPER_PID:
128
 
        os.system("kill -9 %d" % STREAMRIPPER_PID)
129
 
        STREAMRIPPER_PID = None
130
 
 
131
 
def initialize(type, exaile, stuff=None):
132
 
    global BUTTON, APP
133
 
 
134
 
    APP = exaile
135
 
 
136
 
    # if the gui is available, add the record button
137
 
    if exaile.gui:
138
 
        import gtk
139
 
 
140
 
        BUTTON = gtk.ToggleButton()
141
 
        BUTTON.connect('button-release-event', toggle_record)
142
 
        image = gtk.Image()
143
 
        image.set_from_stock('gtk-media-record', gtk.ICON_SIZE_SMALL_TOOLBAR)
144
 
        BUTTON.set_image(image)
145
 
        
146
 
        toolbar = exaile.gui.play_toolbar
147
 
        toolbar.pack_start(BUTTON, False, False)
148
 
        toolbar.reorder_child(BUTTON, 3)
149
 
 
150
 
        BUTTON.show()
151
 
 
152
 
    event.add_callback(playback_stop, 'playback_player_end', 
153
 
        exaile.player)
154
 
 
155
 
def enable(exaile):
156
 
    """
157
 
        Enables the streamripper plugin
158
 
    """
159
 
    try:
160
 
        subprocess.call(['streamripper'], stdout=-1, stderr=-1)
161
 
    except OSError:
162
 
        raise NotImplementedError('Streamripper is not available.')
163
 
        return False
164
 
 
165
 
    if exaile.loading:
166
 
        event.add_callback(initialize, 'exaile_loaded', exaile)
167
 
    else:
168
 
        initialize(None, exaile)
169
 
 
170
 
def disable(exaile):
171
 
    global BUTTON
172
 
 
173
 
    if BUTTON:
174
 
        exaile.gui.play_toolbar.remove(BUTTON)
175
 
        BUTTON.hide()
176
 
        BUTTON.destroy()
177
 
 
178
 
        BUTTON = None
179
 
 
180
 
    event.remove_callback(playback_stop, 'playback_player_end',
181
 
        exaile.player)