Example #1
0
    def like_pic(self, pic=None, id=None, longurl_id=None, shorturl_id=None):

        if not self.is_authenticated:
            raise PicplzError(
                "like_pic requires an authenticated API instance")

        if (pic is None and id is None and longurl_id is None
                and shorturl_id is None):
            raise PicplzError(
                "like_pic method requires one of a pic id, longurl_id or shorturl_id"
            )

        parameters = {}
        if pic is not None:
            parameters['id'] = pic.id
        if id is not None:
            parameters['id'] = id
        if longurl_id is not None:
            parameters['longurl_id'] = longurl_id
        if shorturl_id is not None:
            parameters['shorturl_id'] = shorturl_id

        returned_json = self.__make_authenticated_post__(
            self.like_endpoint, parameters)
        returned_data = simplejson.loads(returned_json)
        like_data = returned_data['value']['like']
        like = PicplzLike.from_dict(self, like_data)

        return like
Example #2
0
    def get_access_token(self, code=None):
        if self.request_code is None and code is None:
            raise PicplzError(
                "Authenticator does not have a valid request code and no code was passed in. A valid code is required to obtain an access token"
            )

        if code is not None:
            self.request_code = code

        at_url = build_access_token_url(self.client_id, self.client_secret,
                                        self.redirect_uri, self.request_code)

        response = urllib2.urlopen(at_url)
        json = response.read()
        try:
            picplz_response = simplejson.loads(json)
            access_token_crazy_picplz_string = picplz_response['access_token']
            #why does this have '1|' in it? who knows... let's just deal with it
            #access_token_string = access_token_crazy_picplz_string.split('|')[1]
            access_token_string = access_token_crazy_picplz_string
            self.access_token = PicplzOauthToken.from_string(
                access_token_string)
            return self.access_token
        except:
            raise PicplzError(
                "failed to obtain access token, picplz response was: %s" %
                (json))
Example #3
0
    def comment(self,
                comment=None,
                comment_text=None,
                id=None,
                longurl_id=None,
                shorturl_id=None):

        if not self.is_authenticated:
            raise PicplzError("comment requires an authenticated API instance")

        parameters = {}
        comment_content = None
        pic_id = None

        if comment is None:
            if (id is None and longurl_id is None and shorturl_id is None):
                raise PicplzError(
                    "comment method requires one of a pic id, longurl_id or shorturl_id"
                )
            if comment_text is None:
                raise PicplzError(
                    "To make a comment you must supply either a PicplzComment object (with the content field set) or pass in a string to comment_text parameter, otherwise what are you commenting?"
                )
            comment_content = comment_text
            if id is not None:
                pic_id = id
            if longurl_id is not None:
                parameters['longurl_id'] = longurl_id
            if shorturl_id is not None:
                parameters['shorturl_id'] = shorturl_id
        else:
            if comment.content is None:
                raise PicplzError(
                    "To make a comment you must supply either a PicplzComment object (with the content field set) or pass in a string to comment_text parameter, otherwise what are you commenting?"
                )
            if comment.pic is None:
                if (id is None and longurl_id is None and shorturl_id is None):
                    raise PicplzError(
                        "comment method requires one of a pic id, longurl_id or shorturl_id or setting the pic property on a PizPlzComment object"
                    )
            comment_content = comment.content
            pic_id = comment.pic.id

        if pic_id is not None:
            parameters['id'] = pic_id

        parameters['comment'] = comment_content

        returned_json = self.__make_authenticated_post__(
            self.comment_endpoint, parameters)

        returned_data = simplejson.loads(returned_json)
        data = returned_data['value']['comment']
        comment = PicplzComment.from_dict(self, data)

        return comment
Example #4
0
    def unfollow_user(self, username=None, id=None):

        if not self.is_authenticated:
            raise PicplzError(
                "unfollow_user requires an authenticated API instance")

        return None
Example #5
0
    def upload_pic(self, upload_pic):

        if not self.is_authenticated:
            raise PicplzError(
                "uploading a new pic requires an authenticated API instance")

        parameters = upload_pic.get_parameters()

        form = MultiPartForm()
        form.add_field('oauth_token',
                       self.authenticator.access_token.to_string())
        for key in parameters.keys():
            form.add_field(key, parameters[key])

        # Add a fake file
        form.add_file('file', upload_pic.file.name, fileHandle=upload_pic.file)

        # Build the request
        request = urllib2.Request(self.upload_endpoint)
        request.add_header(
            'User-agent',
            'python-picplz: https://github.com/ejesse/python-picplz')
        body = str(form)
        request.add_header('Content-type', form.get_content_type())
        request.add_header('Content-length', len(body))
        request.add_data(body)

        response = urllib2.urlopen(request)
        response_text = response.read()
        if self.print_json:
            print response_text
        self.__check_for_picplz_error__(response_text)
        return response_text
Example #6
0
    def get_pic(self,
                id=None,
                longurl_id=None,
                shorturl_id=None,
                include_comments=False):
        """" get individual pic, requires one of id, longurl_id, or shorturl_id"""

        if (id is None and longurl_id is None and shorturl_id is None):
            raise PicplzError(
                "get_pic method requires one of a pic id, longurl_id or shorturl_id"
            )

        parameters = {}
        if id is not None:
            parameters['id'] = id
        if longurl_id is not None:
            parameters['longurl_id'] = longurl_id
        if shorturl_id is not None:
            parameters['shorturl_id'] = shorturl_id
        if include_comments is not None:
            parameters['include_items'] = 1

        ## for now, include geo data by default
        parameters['include_geo'] = 1

        returned_json = self.__make_unauthenticated_get__(
            self.pic_endpoint, parameters)
        returned_data = simplejson.loads(returned_json)
        pic_data = returned_data['value']['pics'][0]
        pic = Pic.from_dict(self, pic_data)

        return pic
Example #7
0
 def __fetch_pics__(self, go_back_for_more=False, last_pic_id=None):
     if not self.api:
         raise PicplzError(
             "Objects must have been instantiated with no API. Without an API the object cannot fetch anything :("
         )
     if go_back_for_more:
         # NomNomNomNomNomNomNomNomNom
         pics_user = self.api.get_user(username=self.username,
                                       include_pics=True,
                                       last_pic_id=last_pic_id,
                                       pic_page_size=100)
     else:
         pics_user = self.api.get_user(username=self.username,
                                       include_pics=True,
                                       last_pic_id=last_pic_id)
     for pic_key in pics_user.pics.keys():
         self.pics[pic_key] = pics_user.pics[pic_key]
         self.pics[pic_key].creator = self
     if go_back_for_more:
         if pics_user.__has_more_pics__:
             # recursion FTW
             # NomNomNomNomNomNomNomNomNom
             return self.__fetch_pics__(
                 go_back_for_more=True,
                 last_pic_id=pics_user.__last_pic_id__)
     return self.pics
Example #8
0
 def __check_for_picplz_error__(self, json):
     error_text = 'Unknown picplz error'
     result = simplejson.loads(json)
     if result.has_key('result'):
         if result['result'] == "error":
             if result.has_key('text'):
                 error_text = result['text']
             raise PicplzError('An error was returned from PicPlz API: %s' %
                               (error_text))
Example #9
0
    def is_authenticated_user_following(self, username=None, id=None):
        """ query whether or not the currently authenticated user is following another user
        requires either username or id of the followee user"""
        if not self.is_authenticated:
            raise PicplzError(
                "is_authenticated_user_following requires an authenticated API instance"
            )

        return None
Example #10
0
    def get_user(self,
                 username=None,
                 id=None,
                 include_detail=False,
                 include_pics=False,
                 pic_page_size=None,
                 last_pic_id=False):
        """ get user info, requires either username or the user's picplz id"""

        if (id is None and username is None):
            if self.authenticator.access_token is not None:
                id = 'self'
            else:
                raise PicplzError(
                    "get_user method requires one of a pic id, longurl_id or shorturl_id"
                )

        parameters = {}
        if id is not None:
            parameters['id'] = id
        if username is not None:
            parameters['username'] = username
        if include_detail:
            parameters['include_detail'] = 1
        if include_pics:
            parameters['include_pics'] = 1
        if last_pic_id:
            parameters['last_pic_id'] = last_pic_id
        if pic_page_size is not None:
            parameters['pic_page_size'] = pic_page_size

        if id == 'self':
            returned_json = self.__make_authenticated_get__(
                self.user_endpoint, parameters)
        returned_json = self.__make_unauthenticated_get__(
            self.user_endpoint, parameters)
        returned_data = simplejson.loads(returned_json)
        data = returned_data['value']['users'][0]
        user = PicplzUser.from_dict(self, data)
        try:
            has_more_pics = returned_data['value']['users'][0]['more_pics']
            if has_more_pics:
                user.__has_more_pics__ = True
            else:
                user.__has_more_pics__ = False
        except:
            user.__has_more_pics__ = False
        try:
            last_pic_id = returned_data['value']['users'][0]['last_pic_id']
            user.__last_pic_id__ = last_pic_id
        except:
            user.__last_pic_id__ = False

        return user
Example #11
0
    def delete_comment(self, comment_id=None, comment=None):

        if not self.is_authenticated:
            raise PicplzError(
                "deleting a comment requires an authenticated API instance")

        if (comment_id is None and comment is None):
            raise PicplzError(
                "In order to delete a comment you must pass in a comment_id or the comment to be deleted"
            )

        parameters = {}
        if comment is not None:
            parameters['comment_id'] = comment.id
        else:
            parameters['comment_id'] = comment_id

        returned_json = self.__make_authenticated_delete__(
            self.comment_endpoint, parameters)

        returned_data = simplejson.loads(returned_json)
        return returned_data
Example #12
0
    def unlike_pic(self, id=None, pic=None):
        if not self.is_authenticated:
            raise PicplzError(
                "unlike_pic requires an authenticated API instance")

        if id is None and pic is None:
            raise PicplzError(
                "Pass in a pic object or a pic id, otherwise there's nothing to unlike!"
            )

        parameters = {}
        if id is not None:
            parameters['id'] = id
        if pic is not None:
            parameters['id'] = pic.id

        returned_json = self.__make_authenticated_delete__(
            self.like_endpoint, parameters)
        returned_data = simplejson.loads(returned_json)
        #like_data = returned_data['value']['like']
        #like = PicplzLike.from_dict(self,like_data)

        return returned_data
Example #13
0
    def get_pics(self, ids=None, place=None, user=None, last_pic_id=False):

        if (id is None and place is None and user is None):
            if self.authenticated_user is None:
                raise PicplzError(
                    "get_pic method requires one of: a comma delimited list of pic ids, a PicplzPlace, or PicplzUser"
                )
            else:
                user = self.authenticated_user

        if user is not None:
            return user.fetch_all_pics()

        pics = []

        return pics