Added redireciton (301 and 302) when fetching media headers
---
  tp/client/media.py |   41 ++++++++++++++++++++++++++++++++++-------
  1 files changed, 34 insertions(+), 7 deletions(-)diff --git a/tp/ 
client/media.py b/tp/client/media.py
index 73f10a3..a9e9089 100644
--- a/tp/client/media.py
+++ b/tp/client/media.py
@@ -62,15 +62,36 @@ class Media:
                return self._getter
        getter = property(getter)

+       def getresponse(self, media_remote):
+               try:
+                       while True:
+                               self.connection.request("HEAD", media_remote)
+                               response = self.connection.getresponse()
+                               self.connection.close()
+                               if response.status == 301 or response.status == 
302:
+                                       media_remote = 
response.getheader('location')
+                                       print 'Redirected to', media_remote
+                                       self.connection.connect()
+                               else:
+                                       return response
+               except socket.error, e:
+                       print e
+                       return None
+
        def updated(self, media_remote, media_local):
                """\
                Checks the file's timestamp against the remote's version.
-               """
+               """     
                try:
-                       self.connection.request("HEAD", media_remote)
+                       response = self.getresponse(media_remote)
+                       if response == None:
+                               return False
+                       if response.status == 404:
+                               print 'File not found on server (404)'
+                               return False
                        
                        headers = {}
-                       headers['last-modified'] = 
self.connection.getresponse().getheader 
('last-modified')
+                       headers['last-modified'] = 
response.getheader('last-modified')
  #                     for key, value in 
self.connection.getresponse().getheaders():
  #                             headers[key] = value

@@ -85,9 +106,6 @@ class Media:
                        return True
                except IOError, e:
                        return True
-               except socket.error, e:
-                       print e
-                       return False

        def ready(self, file, timestamp):
                """\
@@ -122,6 +140,11 @@ class Media:
                                if not self.ready(file, timestamp) is None:
                                        return media_local

+               response = self.getresponse(media_remote)
+               if response == None or response.status == 404:
+                       print "No media on remote server"
+                       return media_local
+
                if self.noconnect:
                        raise IOError("Could not get the file as in no 
connection mode!")       
        
@@ -131,7 +154,11 @@ class Media:

                try:
                        (trash, message) = self.getter.retrieve(media_url, 
media_local,  
callback)
-
+                       
+                       #print "Number of headers:", len(message.keys())
+                       #for key in message.keys():
+                       #       print key, ":", message.getheader(key)
+                       
                        remotedate = 
strptime(message.getheader('last-modified'), "%a, % 
d %b %Y %H:%M:%S %Z")[0:5]

                        open(media_local + ".timestamp", 
'w').write(repr(remotedate))
_______________________________________________
tp-devel mailing list
[email protected]
http://www.thousandparsec.net/tp/mailman.php/listinfo/tp-devel

Reply via email to