3 import re
, tempfile
, mp3Handler
5 streamURLRegex
= re
.compile(
6 "(http://.*[.]grooveshark[.]com/.*[.]mp3.*" +
7 "|http://stream.*[.]grooveshark[.]com/stream.php)"
10 adCSSRegex
= re
.compile(
11 "http://.*[.]grooveshark[.]com/webincludes/css/gslite[.]css.*"
15 def __init__(self
, *args
, **kwargs
):
18 def process(self
, data
):
24 class SongDownloader(Filter
):
25 """A filter which will save MP3s which are transferred through the proxy"""
26 # A string which will store the song data
29 # Remember if the headers have been sent yet
30 headersFinished
= False
32 def process(self
, data
):
34 return Filter
.process(self
, data
)
38 header
, song
= self
.data
.split("\r\n\r\n", 2)
40 # Header issues of some sort...
41 return Filter
.done(self
)
43 # Check that a music file arrived
45 _
, mp3FileName
= tempfile
.mkstemp()
46 mp3File
= open(mp3FileName
, "wb")
50 mp3Handler
.organiseMP3(mp3FileName
)
52 return Filter
.done(self
)
54 class AdStripper(Filter
):
56 A filter to strip out ads from grooveshark.
58 This uses a prettey nasty hack by simply swapping out the actual response with
59 some static CSS and a rough invalid HTTP header. It works but it ain't prety.
61 def process(self
, data
):
67 # Makeshift header -- invalid really!
68 newCSS
= "HTTP/1.0 200 OK\r\n"
71 # Create a simple stylesheet to do the minimum to show just the flash
72 # component and no ads!
73 newCSS
+= "body { background-color : black; color : black; }"
74 newCSS
+= "#adPane { display : none }"
75 newCSS
+= "#sidebarFrameWrapper { display : none }"
76 newCSS
+= "#gsliteswf { position : absolute; display : block;"
77 newCSS
+= " top : 0; left : 0; right : 0; bottom : 0;}"
82 if streamURLRegex
.match(path
):
83 return SongDownloader(path
)
84 elif adCSSRegex
.match(path
):
85 return AdStripper(path
)