Example #1
0
        def process(result):
            for size in result.getiterator('size'):
                #print size.get('label'), size.get('source')
                if size.get('label') == 'Original':
                    self.original_url = (size.get('source'),size.get('width')+'x'+size.get('height'))
                    self.url = self.original_url[0]
                elif size.get('label') == 'Large':
                    self.large_url = (size.get('source'),size.get('width')+'x'+size.get('height'))
                    self.url = self.large_url[0]
                elif size.get('label') == 'Medium':
                    self.medium_url = (size.get('source'),size.get('width')+'x'+size.get('height'))
                elif size.get('label') == 'Small':
                    self.small_url = (size.get('source'),size.get('width')+'x'+size.get('height'))
                elif size.get('label') == 'Thumbnail':
                    self.thumb_url = (size.get('source'),size.get('width')+'x'+size.get('height'))

            self.item = Photo(self.id,self.parent.get_id(),self.get_name())
            self.item.date = self.date
            dlna_tags = simple_dlna_tags[:]
            dlna_tags[3] = 'DLNA.ORG_FLAGS=00f00000000000000000000000000000'
            if hasattr(self,'original_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_LRG'
                res = Resource(self.original_url[0], 'http-get:*:%s:%s' % (self.mimetype,';'.join([dlna_pn]+dlna_tags)))
                res.resolution = self.original_url[1]
                self.item.res.append(res)
            elif hasattr(self,'large_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_LRG'
                res = Resource(self.large_url[0], 'http-get:*:%s:%s' % (self.mimetype,';'.join([dlna_pn]+dlna_tags)))
                res.resolution = self.large_url[1]
                self.item.res.append(res)
            if hasattr(self,'medium_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_MED'
                res = Resource(self.medium_url[0], 'http-get:*:%s:%s' % (self.mimetype,';'.join([dlna_pn]+dlna_tags)))
                res.resolution = self.medium_url[1]
                self.item.res.append(res)
            if hasattr(self,'small_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_SM'
                res = Resource(self.small_url[0], 'http-get:*:%s:%s' % (self.mimetype,';'.join([dlna_pn]+dlna_tags)))
                res.resolution = self.small_url[1]
                self.item.res.append(res)
            if hasattr(self,'thumb_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_TN'
                res = Resource(self.thumb_url[0], 'http-get:*:%s:%s' % (self.mimetype,';'.join([dlna_pn]+dlna_tags)))
                res.resolution = self.thumb_url[1]
                self.item.res.append(res)

            return self.item
Example #2
0
        def process(result):
            for size in result.getiterator('size'):
                #print size.get('label'), size.get('source')
                if size.get('label') == 'Original':
                    self.original_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))
                    if self.store.proxy == False:
                        self.url = self.original_url[0]
                    else:
                        self.location = ProxyImage(self.original_url[0])
                elif size.get('label') == 'Large':
                    self.large_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))
                    if self.store.proxy == False:
                        self.url = self.large_url[0]
                    else:
                        self.location = ProxyImage(self.large_url[0])
                elif size.get('label') == 'Medium':
                    self.medium_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))
                elif size.get('label') == 'Small':
                    self.small_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))
                elif size.get('label') == 'Thumbnail':
                    self.thumb_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))

            self.item = Photo(self.id, self.parent.get_id(), self.get_name())
            #print self.id, self.store.proxy, self.url
            self.item.date = self.date
            self.item.attachments = {}
            dlna_tags = simple_dlna_tags[:]
            dlna_tags[3] = 'DLNA.ORG_FLAGS=00f00000000000000000000000000000'
            if hasattr(self, 'original_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_LRG'
                if self.store.proxy == False:
                    res = Resource(self.original_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=original', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['original'] = ProxyImage(self.original_url[0])
                res.resolution = self.original_url[1]
                self.item.res.append(res)
            elif hasattr(self, 'large_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_LRG'
                if self.store.proxy == False:
                    res = Resource(self.large_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=large', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['large'] = ProxyImage(self.large_url[0])
                res.resolution = self.large_url[1]
                self.item.res.append(res)
            if hasattr(self, 'medium_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_MED'
                if self.store.proxy == False:
                    res = Resource(self.medium_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=medium', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['medium'] = ProxyImage(self.medium_url[0])
                res.resolution = self.medium_url[1]
                self.item.res.append(res)
            if hasattr(self, 'small_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_SM'
                if self.store.proxy == False:
                    res = Resource(self.small_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=small', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['small'] = ProxyImage(self.small_url[0])
                res.resolution = self.small_url[1]
                self.item.res.append(res)
            if hasattr(self, 'thumb_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_TN'
                if self.store.proxy == False:
                    res = Resource(self.thumb_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=thumb', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['thumb'] = ProxyImage(self.thumb_url[0])
                res.resolution = self.thumb_url[1]
                self.item.res.append(res)

            return self.item
Example #3
0
class FlickrItem(log.Loggable):
    logCategory = 'flickr_storage'

    def __init__(self, id, obj, parent, mimetype, urlbase, UPnPClass, store=None, update=False, proxy=False):
        log.Loggable.__init__(self)
        self.id = id
        self.real_url = None
        self.obj = obj
        self.upnp_class = UPnPClass
        self.store = store
        self.item = None
        self.date = None

        if isinstance(obj, str):
            self.name = obj
            if isinstance(self.id, basestring) and self.id.startswith('upload.'):
                self.mimetype = mimetype
            else:
                self.mimetype = 'directory'
        elif mimetype == 'directory':
            title = obj.find('title')
            self.name = title.text
            if len(self.name) == 0:
                self.name = obj.get('id')
            self.mimetype = 'directory'
        elif mimetype == 'contact':
            self.name = obj.get('realname')
            if self.name == '':
                self.name = obj.get('username')
            self.nsid = obj.get('nsid')
            self.mimetype = 'directory'
        else:
            self.name = obj.get('title')  # .encode('utf-8')
            if self.name == None:
                self.name = obj.find('title')
                if self.name != None:
                    self.name = self.name.text
            if self.name == None or len(self.name) == 0:
                self.name = 'untitled'
            self.mimetype = 'image/jpeg'

        self.parent = parent
        if not (isinstance(self.id, basestring) and self.id.startswith('upload.')):
            if parent:
                parent.add_child(self, update=update)

        if(len(urlbase) and urlbase[-1] != '/'):
            urlbase += '/'

        if self.mimetype == 'directory':
            try:
                self.flickr_id = obj.get('id')
            except:
                self.flickr_id = None
            self.url = urlbase + str(self.id)
        elif isinstance(self.id, basestring) and self.id.startswith('upload.'):
            self.url = urlbase + str(self.id)
            self.location = None
        else:
            self.flickr_id = obj.get('id')

            try:
                datetaken = obj.get('datetaken')
                date, time = datetaken.split(' ')
                year, month, day = date.split('-')
                hour, minute, second = time.split(':')
                self.date = datetime(int(year), int(month), int(day), int(hour), int(minute), int(second))
            except:
                import traceback
                self.debug(traceback.format_exc())

            self.real_url = "http://farm%s.static.flickr.com/%s/%s_%s.jpg" % (
                            obj.get('farm'),
                            obj.get('server'),
                            obj.get('id'),
                            obj.get('secret'))

            if proxy == True:
                self.url = urlbase + str(self.id)
                self.location = ProxyImage(self.real_url)
            else:
                self.url = u"http://farm%s.static.flickr.com/%s/%s_%s.jpg" % (
                        obj.get('farm').encode('utf-8'),
                        obj.get('server').encode('utf-8'),
                        obj.get('id').encode('utf-8'),
                        obj.get('secret').encode('utf-8'))

        if parent == None:
            self.parent_id = -1
        else:
            self.parent_id = parent.get_id()

        if self.mimetype == 'directory':
            self.children = []
            self.update_id = 0

    def set_item_size_and_date(self):

        def gotPhoto(result):
            self.debug("gotPhoto %s", result)
            _, headers = result
            length = headers.get('content-length', None)
            modified = headers.get('last-modified', None)
            if length != None:
                self.item.res[0].size = int(length[0])
            if modified != None:
                """ Tue, 06 Feb 2007 15:56:32 GMT """
                self.item.date = datetime(*parsedate_tz(modified[0])[0:6])

        def gotError(failure, url):
            self.warning("error requesting %s %s", failure, url)
            self.info(failure)

        getPage(self.real_url, method='HEAD', timeout=60).addCallbacks(gotPhoto, gotError, None, None, [self.real_url], None)

    def remove(self):
        #print "FSItem remove", self.id, self.get_name(), self.parent
        if self.parent:
            self.parent.remove_child(self)
        del self.item

    def add_child(self, child, update=False):
        self.children.append(child)
        if update == True:
            self.update_id += 1

    def remove_child(self, child):
        self.info("remove_from %d (%s) child %d (%s)", self.id, self.get_name(), child.id, child.get_name())
        if child in self.children:
            self.children.remove(child)
            self.update_id += 1

    def get_children(self, start=0, request_count=0):
        if request_count == 0:
            return self.children[start:]
        else:
            return self.children[start:request_count]

    def get_child_count(self):
        return len(self.children)

    def get_id(self):
        return self.id

    def get_location(self):
        return self.location

    def get_update_id(self):
        if hasattr(self, 'update_id'):
            return self.update_id
        else:
            return None

    def get_path(self):
        if isinstance(self.id, basestring) and self.id.startswith('upload.'):
            return '/tmp/' + self.id  # FIXME
        return self.url

    def get_name(self):
        return self.name

    def get_flickr_id(self):
        return self.flickr_id

    def get_child_by_flickr_id(self, flickr_id):
        for c in self.children:
            if flickr_id == c.flickr_id:
                return c
        return None

    def get_parent(self):
        return self.parent

    def get_item(self):
        if self.item == None:
            if self.mimetype == 'directory':
                self.item = self.upnp_class(self.id, self.parent_id, self.get_name())
                self.item.childCount = self.get_child_count()
                if self.get_child_count() > 0:
                    res = PlayContainerResource(self.store.server.uuid, cid=self.get_id(), fid=self.get_children()[0].get_id())
                    self.item.res.append(res)
            else:
                return self.create_item()
        return self.item

    def create_item(self):
        def process(result):
            for size in result.getiterator('size'):
                #print size.get('label'), size.get('source')
                if size.get('label') == 'Original':
                    self.original_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))
                    if self.store.proxy == False:
                        self.url = self.original_url[0]
                    else:
                        self.location = ProxyImage(self.original_url[0])
                elif size.get('label') == 'Large':
                    self.large_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))
                    if self.store.proxy == False:
                        self.url = self.large_url[0]
                    else:
                        self.location = ProxyImage(self.large_url[0])
                elif size.get('label') == 'Medium':
                    self.medium_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))
                elif size.get('label') == 'Small':
                    self.small_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))
                elif size.get('label') == 'Thumbnail':
                    self.thumb_url = (size.get('source'), size.get('width') + 'x' + size.get('height'))

            self.item = Photo(self.id, self.parent.get_id(), self.get_name())
            #print self.id, self.store.proxy, self.url
            self.item.date = self.date
            self.item.attachments = {}
            dlna_tags = simple_dlna_tags[:]
            dlna_tags[3] = 'DLNA.ORG_FLAGS=00f00000000000000000000000000000'
            if hasattr(self, 'original_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_LRG'
                if self.store.proxy == False:
                    res = Resource(self.original_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=original', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['original'] = ProxyImage(self.original_url[0])
                res.resolution = self.original_url[1]
                self.item.res.append(res)
            elif hasattr(self, 'large_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_LRG'
                if self.store.proxy == False:
                    res = Resource(self.large_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=large', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['large'] = ProxyImage(self.large_url[0])
                res.resolution = self.large_url[1]
                self.item.res.append(res)
            if hasattr(self, 'medium_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_MED'
                if self.store.proxy == False:
                    res = Resource(self.medium_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=medium', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['medium'] = ProxyImage(self.medium_url[0])
                res.resolution = self.medium_url[1]
                self.item.res.append(res)
            if hasattr(self, 'small_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_SM'
                if self.store.proxy == False:
                    res = Resource(self.small_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=small', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['small'] = ProxyImage(self.small_url[0])
                res.resolution = self.small_url[1]
                self.item.res.append(res)
            if hasattr(self, 'thumb_url'):
                dlna_pn = 'DLNA.ORG_PN=JPEG_TN'
                if self.store.proxy == False:
                    res = Resource(self.thumb_url[0], 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                else:
                    res = Resource(self.url + '?attachment=thumb', 'http-get:*:%s:%s' % (self.mimetype, ';'.join([dlna_pn] + dlna_tags)))
                    self.item.attachments['thumb'] = ProxyImage(self.thumb_url[0])
                res.resolution = self.thumb_url[1]
                self.item.res.append(res)

            return self.item

        d = self.store.flickr_photos_getSizes(photo_id=self.flickr_id)
        d.addCallback(process)
        return d

    def get_xml(self):
        return self.item.toString()

    def __repr__(self):
        return 'id: ' + str(self.id) + ' @ ' + str(self.url)
Example #4
0
    def __init__(self, id, obj, parent, mimetype, urlbase, UPnPClass,store=None,update=False,proxy=False):
        self.id = id
        self.real_url = None
        self.obj = obj
        self.store = store
        self.item = None
        self.date = None

        if isinstance(obj, str):
            self.name = obj
            if isinstance(self.id,basestring) and self.id.startswith('upload.'):
                self.mimetype = mimetype
            else:
                self.mimetype = 'directory'
        elif mimetype == 'directory':
            title = obj.find('title')
            self.name = title.text;
            if len(self.name) == 0:
                self.name = obj.get('id')
            self.mimetype = 'directory'
        elif mimetype == 'contact':
            self.name = obj.get('realname')
            if self.name == '':
                self.name = obj.get('username')
            self.nsid = obj.get('nsid')
            self.mimetype = 'directory'
        else:
            self.name = obj.get('title') #.encode('utf-8')
            if self.name == None:
                self.name = obj.find('title')
                if self.name != None:
                    self.name = self.name.text
            if self.name == None or len(self.name) == 0:
                self.name = 'untitled'
            self.mimetype = 'image/jpeg'

        self.parent = parent
        if not (isinstance(self.id,basestring) and self.id.startswith('upload.')):
            if parent:
                parent.add_child(self,update=update)

        if( len(urlbase) and urlbase[-1] != '/'):
            urlbase += '/'

        if self.mimetype == 'directory':
            try:
                self.flickr_id = obj.get('id')
            except:
                self.flickr_id = None
            self.url = urlbase + str(self.id)
        elif isinstance(self.id,basestring) and self.id.startswith('upload.'):
            self.url = urlbase + str(self.id)
            self.location = None
        else:
            self.flickr_id = obj.get('id')

            try:
                datetaken = obj.get('datetaken')
                date,time = datetaken.split(' ')
                year,month,day = date.split('-')
                hour,minute,second = time.split(':')
                self.date = datetime(int(year),int(month),int(day),int(hour),int(minute),int(second))
            except:
                import traceback
                self.debug(traceback.format_exc())

            self.real_url = "http://farm%s.static.flickr.com/%s/%s_%s.jpg" % (
                            obj.get('farm'),
                            obj.get('server'),
                            obj.get('id'),
                            obj.get('secret'))

            if proxy == True:
                self.url = urlbase + str(self.id)
                self.location = ProxyImage(self.real_url)
            else:
                self.url = u"http://farm%s.static.flickr.com/%s/%s_%s.jpg" % (
                        obj.get('farm').encode('utf-8'),
                        obj.get('server').encode('utf-8'),
                        obj.get('id').encode('utf-8'),
                        obj.get('secret').encode('utf-8'))

        if parent == None:
            parent_id = -1
        else:
            parent_id = parent.get_id()

        if self.mimetype == 'directory':
            self.item = UPnPClass(self.id, parent_id, self.get_name())
            if isinstance(self.item, Container):
                self.item.childCount = 0
            self.child_count = 0
            self.children = []
            self.update_id = 0