Advertisement
Guest User

XBMC python handle images without content-length

a guest
Nov 12th, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.71 KB | None | 0 0
  1. import urlparse                 # http://docs.python.org/2/library/urlparse.html
  2. import httplib                  # http://docs.python.org/2/library/httplib.html
  3. import pickle                   # http://docs.python.org/2/library/pickle.html
  4. import os                       # http://docs.python.org/2/library/os.html
  5.  
  6.        
  7. url   = "http://forum.xbmc.org/uploads/avatars/avatar_e0dfbb69981bffe2a1eea03037a30b8d.gif?dateline=1331460338"
  8. state = str(hash("asnormal")) # this could be a hash of sys.argv to avoid loading huge cache files from disk  
  9. img_ext = ('png', 'gif', 'jpg')          
  10. cache_dir = os.getcwd()
  11. fpath = os.path.join( cache_dir, state )
  12.  
  13.  
  14.  
  15. def get_image_url(url):      
  16.     # RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax
  17.     # http://tools.ietf.org/html/rfc3986.html      
  18.     netloc = urlparse.urlparse( url ).netloc
  19.     print "Create a HTTP connection to", netloc
  20.     # HTTP1.1 request - http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html
  21.     conn  = httplib.HTTPConnection( netloc, timeout=10 )
  22.                             # nasty way of getting the selector
  23.     conn.putrequest( "GET", url.lstrip("http://").lstrip(netloc) )
  24.     conn.endheaders()
  25.     result = conn.getresponse()
  26.    
  27.     # Manage the result based on http status. Redirects could also be cached
  28.     if result.status != 200: raise httplib.InvalidURL( "status %i" % result.status )
  29.    
  30.     # Check for the content-length header field, could also check that content-type
  31.     # field indicates a image
  32.     if "content-lengthd"  in (name.lower() for name, value in result.getheaders()):
  33.         print "Content-Length header field found"
  34.         conn.close()
  35.         return url
  36.    
  37.     # Need to write the image to disk before handing the image location to XBMC
  38.     new_url = os.path.join( cache_dir, str(hash(url)) )
  39.     print "write image data to", new_url
  40.     fh = open( new_url, "wb" )
  41.     fh.write( result.read() )
  42.     fh.close()
  43.     conn.close()
  44.    
  45.     return new_url
  46.    
  47.  
  48.  
  49. if not os.path.isfile( fpath ):
  50.     cache = dict()
  51. else:            
  52.     fhndl = open( fpath, 'rb' )
  53.     cache = pickle.load( fhndl )
  54.     fhndl.close()
  55.    
  56. print url    
  57.  
  58. if not hash(url) in cache:
  59.     # is cached version expired?
  60.     # if ( time() - (cache_time * 3600) >= expiration_time )
  61.     print "Not found in cache"
  62.     if ( True not in ( url[-len(x):] == x for x in img_ext ) ):
  63.         print "None of extensions in tuple at the end of the url"
  64.         try:
  65.             url = get_image_url(url)
  66.         except httplib.InvalidURL as e:
  67.             print e
  68.         else:
  69.             cache[hash(url)] = url
  70.  
  71. print url
  72.    
  73. fhndl = open(fpath, 'wb')
  74. pickle.dump(cache, fhndl)
  75. fhndl.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement