updated on Thu Jan 19 20:01:47 UTC 2012
[aur-mirror.git] / groovepwn / grooveMod.py
blobf2dfe81b8f5c236933ff13865e7a14a3a9ebbb55
1 #!/usr/bin/python2
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.*"
14 class Filter(object):
15 def __init__(self, *args, **kwargs):
16 pass
18 def process(self, data):
19 return data
21 def done(self):
22 pass
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
27 data = ""
29 # Remember if the headers have been sent yet
30 headersFinished = False
32 def process(self, data):
33 self.data += data
34 return Filter.process(self, data)
36 def done(self):
37 try:
38 header, song = self.data.split("\r\n\r\n", 2)
39 except ValueError:
40 # Header issues of some sort...
41 return Filter.done(self)
43 # Check that a music file arrived
44 if len(song) > 10000:
45 _ , mp3FileName = tempfile.mkstemp()
46 mp3File = open(mp3FileName, "wb")
47 mp3File.write(song)
48 mp3File.close()
50 mp3Handler.organiseMP3(mp3FileName)
52 return Filter.done(self)
54 class AdStripper(Filter):
55 """
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.
60 """
61 def process(self, data):
62 return ""
64 def done(self):
65 print "Strip ads..."
67 # Makeshift header -- invalid really!
68 newCSS = "HTTP/1.0 200 OK\r\n"
69 newCSS += "\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;}"
79 return newCSS
81 def getFilter(path):
82 if streamURLRegex.match(path):
83 return SongDownloader(path)
84 elif adCSSRegex.match(path):
85 return AdStripper(path)
86 else:
87 return Filter(path)