2008-09-17 01:16:48 +05:30
|
|
|
from urlparse import urlunparse
|
|
|
|
import os
|
2009-01-03 22:35:46 +05:30
|
|
|
import sys
|
|
|
|
if sys.version < '2.6':
|
|
|
|
import md5
|
|
|
|
def md5hash(string):
|
|
|
|
return md5.new(string).hexdigest()
|
|
|
|
else:
|
|
|
|
from hashlib import md5
|
|
|
|
def md5hash(string):
|
|
|
|
return md5(string).hexdigest()
|
2008-09-17 01:16:48 +05:30
|
|
|
"""
|
|
|
|
A simple WSGI application for testing.
|
|
|
|
"""
|
|
|
|
|
|
|
|
_app_was_hit = False
|
|
|
|
|
|
|
|
def success():
|
|
|
|
return _app_was_hit
|
|
|
|
|
|
|
|
def test_app(environ, start_response):
|
|
|
|
"""Simplest possible application object"""
|
|
|
|
url = urlunparse((
|
|
|
|
environ['wsgi.url_scheme'],
|
|
|
|
environ['HTTP_HOST'],
|
|
|
|
environ['PATH_INFO'],
|
|
|
|
'',
|
|
|
|
environ['QUERY_STRING'],
|
|
|
|
''
|
|
|
|
))
|
2009-01-03 22:35:46 +05:30
|
|
|
key = md5hash(url)
|
2008-09-17 01:16:48 +05:30
|
|
|
status = '200 OK'
|
|
|
|
response_headers = [('Content-type','text/xml')]
|
|
|
|
start_response(status, response_headers)
|
|
|
|
|
|
|
|
global _app_was_hit
|
|
|
|
_app_was_hit = True
|
|
|
|
|
2009-03-04 13:37:34 +05:30
|
|
|
data_file = os.path.join(os.path.dirname(__file__), 'data', "%s.xml" % key)
|
|
|
|
|
2009-01-03 16:48:03 +05:30
|
|
|
try:
|
2009-03-04 13:37:34 +05:30
|
|
|
filedata = open(data_file).read()
|
2009-01-03 16:48:03 +05:30
|
|
|
except IOError:
|
2009-03-04 13:37:34 +05:30
|
|
|
#print "\nintercepted: %s" % url
|
|
|
|
#print "key:", key
|
|
|
|
import wsgi_intercept
|
|
|
|
wsgi_intercept.remove_wsgi_intercept('ws.audioscrobbler.com', 80)
|
|
|
|
import urllib2
|
|
|
|
filedata = urllib2.urlopen(url).read()
|
2009-03-04 17:19:10 +05:30
|
|
|
wsgi_intercept.add_wsgi_intercept('ws.audioscrobbler.com', 80, create_wsgi_app)
|
2009-03-04 13:37:34 +05:30
|
|
|
open(data_file, "w").write(filedata)
|
2008-09-17 01:16:48 +05:30
|
|
|
return [filedata]
|
|
|
|
|
|
|
|
def create_wsgi_app():
|
|
|
|
global _app_was_hit
|
|
|
|
_app_was_hit = False
|
|
|
|
return test_app
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
from wsgi_intercept.urllib2_intercept import install_opener
|
|
|
|
install_opener()
|
|
|
|
import wsgi_intercept
|
|
|
|
wsgi_intercept.add_wsgi_intercept('ws.audioscrobbler.com', 80, create_wsgi_app)
|
|
|
|
import urllib2
|
|
|
|
print urllib2.urlopen('http://ws.audioscrobbler.com/2.0/?album=Supersonic&api_key=152a230561e72192b8b0f3e42362c6ff&artist=Oasis&method=album.getInfo').read()
|