Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import urlparse # http://docs.python.org/2/library/urlparse.html
- import httplib # http://docs.python.org/2/library/httplib.html
- import pickle # http://docs.python.org/2/library/pickle.html
- import os # http://docs.python.org/2/library/os.html
- url = "http://forum.xbmc.org/uploads/avatars/avatar_e0dfbb69981bffe2a1eea03037a30b8d.gif?dateline=1331460338"
- state = str(hash("asnormal")) # this could be a hash of sys.argv to avoid loading huge cache files from disk
- img_ext = ('png', 'gif', 'jpg')
- cache_dir = os.getcwd()
- fpath = os.path.join( cache_dir, state )
- def get_image_url(url):
- # RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax
- # http://tools.ietf.org/html/rfc3986.html
- netloc = urlparse.urlparse( url ).netloc
- print "Create a HTTP connection to", netloc
- # HTTP1.1 request - http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
- conn = httplib.HTTPConnection( netloc, timeout=10 )
- # nasty way of getting the selector
- conn.putrequest( "GET", url.lstrip("http://").lstrip(netloc) )
- conn.endheaders()
- result = conn.getresponse()
- # Manage the result based on http status. Redirects could also be cached
- if result.status != 200: raise httplib.InvalidURL( "status %i" % result.status )
- # Check for the content-length header field, could also check that content-type
- # field indicates a image
- if "content-lengthd" in (name.lower() for name, value in result.getheaders()):
- print "Content-Length header field found"
- conn.close()
- return url
- # Need to write the image to disk before handing the image location to XBMC
- new_url = os.path.join( cache_dir, str(hash(url)) )
- print "write image data to", new_url
- fh = open( new_url, "wb" )
- fh.write( result.read() )
- fh.close()
- conn.close()
- return new_url
- if not os.path.isfile( fpath ):
- cache = dict()
- else:
- fhndl = open( fpath, 'rb' )
- cache = pickle.load( fhndl )
- fhndl.close()
- print url
- if not hash(url) in cache:
- # is cached version expired?
- # if ( time() - (cache_time * 3600) >= expiration_time )
- print "Not found in cache"
- if ( True not in ( url[-len(x):] == x for x in img_ext ) ):
- print "None of extensions in tuple at the end of the url"
- try:
- url = get_image_url(url)
- except httplib.InvalidURL as e:
- print e
- else:
- cache[hash(url)] = url
- print url
- fhndl = open(fpath, 'wb')
- pickle.dump(cache, fhndl)
- fhndl.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement