Пример #1
0
 def __init__(self):
     """Constructor."""
     self.bookmarks = Bookmarks(self.get_data_path())
Пример #2
0
class Actions(Context):
    """Non content provider actions."""
    
    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor."""
        self.bookmarks = Bookmarks(self.get_data_path())
        
    #----------------------------------------------------------------------
    def _download_photo(self, url):
        """Download picture from Picasa web."""
        # prepare the download directory path
        dlpath = self.get_option('dlpath')
        dlpath = os.path.expanduser(dlpath)
        if not os.path.exists(dlpath) or not os.path.isdir(dlpath):
            dlpath = os.path.expanduser('~')
        
        # get the destination filename
        dlname = os.path.basename(urllib2.unquote(url))
        if not dlname:
            dlname = 'xbmc_downloaded.jpg'
        
        # fetch the picture
        client = httpclient.HTTPClient(None)
        client.proxy = self.get_network_settings()
        
        try:
            response = client.request(url)
            try:
                f = open(os.path.join(dlpath, dlname), 'wb')
                try:
                    while True:
                        buf = response.read(4096)
                        if len(buf) == 0:
                            break
                        f.write(buf)
                except:
                    result = Response(Response.ERROR, self.get_str(30401))
                else:
                    result = Response(Response.SUCCESS, self.get_str(30402))
                f.close()
            except IOError:
                result = Response(Response.ERROR, self.get_str(30401))
        except urllib2.HTTPError:
            result = Response(Response.ERROR, self.get_str(30401))

        return result
    
    #----------------------------------------------------------------------
    def _get_album_by_invitation(self, u_url):
        """
        
        returns: Save() instance, or None is case of any error.
        """
        # sanity check of the given url
        if u_url.get_query('uname') is None or u_url.get_query('uname') == '':
            return None
        if u_url.get_query('id') is None or u_url.get_query('id') == '':
            return None
        if u_url.get_query('target') != 'ALBUM':
            return None
        
        # convert the email url to picasa feed url
        authkey = u_url.get_query('authkey')
        
        n_url = PhotosOfAlbum(u_url.get_query('uname'), u_url.get_query('id'))
        if  authkey is not None:
            n_url.set_query(authkey=authkey)
        n_url.set_query(max_results='0')
        
        # retrieve the album feed to get album parameters
        client = picasaclient.PicasaClient()
        client.set_proxy(self.get_network_settings())
        
        try:
            feed = client.get_feed(uri=n_url.get_url())
            if not isinstance(feed, picasaclient.AlbumFeed):
                return None
        except picasaclient.RequestError:
            return None
        
        # create node from feed parameters instead of entry
        album = Album(attrs=dict([['user', feed.user],
                                  ['nick', feed.nickname],
                                  ['thumbnail', feed.icon],
                                  ['title', feed.title],
                                  ['gid', feed.gphoto_id],
                                  ['numphotos', feed.numphotos],
                                  ['authkey', authkey]]))

        return Save(album)
    
    #----------------------------------------------------------------------
    def _save_favorite(self, n_url):
        """
        """
        # when 'kind' query empty or missing
        if not n_url.get_query('kind'):
            u = self.get_user_input('string', self.get_str(30301))
            
            # user input cancelled
            if u is None:
                return Response(Response.SUCCESS)

            n_url = self._get_album_by_invitation(Url(u))

            if n_url is None:
                return Response(Response.ERROR, message=self.get_str(30403))

        # when we're supposed to have complete input data
        favorite = Bookmark('favorite')
        favorite.add_attributes(n_url.query)

        self.log(self.__class__, "saving bookmark: %s[%s]" %
                 (favorite.name, repr(favorite.attr)))
    
        self.bookmarks.add_bookmark(favorite)
    
        return Response(Response.SUCCESS,
                        self.get_str(30404),
                        task=Response.REFRESH)

    #----------------------------------------------------------------------
    def _delete_favorite(self, n_url):
        """Delete the favorite from the bookmark file."""
        favorite = Bookmark('favorite')
        favorite.add_attributes(n_url.query)

        self.log(self.__class__, "deleting bookmark: %s[%s]" %
                 (favorite.name, repr(favorite.attr)))
    
        try:
            self.bookmarks.delete_bookmark(favorite)
        except:
            return Response(Response.ERROR, message=self.get_str(30405))

        return Response(Response.SUCCESS,
                        message=self.get_str(30406),
                        task=Response.REFRESH)
    
    #----------------------------------------------------------------------
    def _upload_picture(self):
        """Upload photos to Picasa."""
        tree = Tree()
        userid = tree.userid
        token = tree.token
        client = tree.client
        
        # here user already logged in because action can run only from within
        if token == NOLOGIN:
            return Response(Response.ERROR, self.get_str(30409))

        # select photo to upload
        photo = self.get_user_input('picture', self.get_str(30304), True)
        if photo is None:
            return Response(Response.SUCCESS)

        self.log(self.__class__, "selected path: " + repr(photo))

        # get list of own albums
        result = tree.list_directory(AlbumsOfUser(userid))
        if result.status == Response.ERROR:
            return result
        
        # select album to upload to, or create new
        albums = [n for n in result.nodes if n.get_type() == 'album']
        album_names = ["<%s>" % self.get_str(30500),] + [a.title for a in albums]

        index = self.get_user_input('select', self.get_str(30302), album_names)
        if index == -1:
            return Response(Response.SUCCESS)                
        elif index == 0:
            new = self.get_user_input('string', self.get_str(30303))
            if new is None:
                return Response(Response.SUCCESS)

            try:
                url = NodeUrl(userid=userid)
                album = client.create_album(url.get_url(), new, token=token)
                albumid = album.gphoto_id
            except picasaclient.RequestError, err:
                return Response(Response.ERROR, str(err))
        else: