Пример #1
0
def share_on_diaspora(configs: dict, post_text: str, image_filenames: [str],
                      reply_to_latest_post: bool = False):
    api = Connection(pod=configs['diaspora']['pod_url'],
                     username=configs['diaspora']['username'],
                     password=configs['diaspora']['password'])
    api.login()

    success_message = "shared on diaspora*"

    if reply_to_latest_post:
        handle = '%s@%s' % (configs['diaspora']['username'],
                            configs['diaspora']['pod_url'].split('://')[1])
        user = User(api, handle=handle)

        latest_post = Post(api, user.stream[0].id, fetch=False, comments=False)
        latest_post.comment(post_text)

        success_message = "replied to %s" % shorten_text(str(user.stream[0]))
    else:
        stream = Stream(api)

        post_media = []
        for filename in image_filenames:
            post_media.append(stream._photoupload(filename))

        stream.post(text=post_text,
                    photos=post_media,
                    provider_display_name='diadon')

    print(success_message)
Пример #2
0
    def post(self, text='', aspect_ids='public', photos=None, photo=''):
        """This function sends a post to an aspect.
        If both `photo` and `photos` are specified `photos` takes precedence.

        :param text: Text to post.
        :type text: str
        :param aspect_ids: Aspect ids to send post to.
        :type aspect_ids: str
        :param photo: filename of photo to post
        :type photo: str
        :param photos: id of photo to post (obtained from _photoupload())
        :type photos: int

        :returns: diaspy.models.Post -- the Post which has been created
        """
        data = {}
        data['aspect_ids'] = aspect_ids
        data['status_message'] = {'text': text}
        if photo: data['photos'] = self._photoupload(photo)
        if photos: data['photos'] = photos

        request = self._connection.post('status_messages',
                                        data=json.dumps(data),
                                        headers={
                                            'content-type': 'application/json',
                                            'accept': 'application/json',
                                            'x-csrf-token':
                                            repr(self._connection)
                                        })
        if request.status_code != 201:
            raise Exception('{0}: Post could not be posted.'.format(
                request.status_code))

        post = Post(self._connection, request.json()['id'])
        return post
Пример #3
0
 def _obtain(self, max_time=0):
     """Obtains stream from pod.
     """
     params = {}
     if max_time: params['max_time'] = max_time
     request = self._connection.get(self._location, params=params)
     if request.status_code != 200:
         raise errors.StreamError('wrong status code: {0}'.format(
             request.status_code))
     return [Post(self._connection, post['id']) for post in request.json()]
Пример #4
0
    def _obtain(self, max_time=0, suppress=True):
        """Obtains stream from pod.

        suppress:bool - suppress post-fetching errors (e.g. 404)
        """
        params = {}
        if max_time:
            params['max_time'] = max_time
            params['_'] = int(time.time() * 1000)
        request = self._connection.get(self._location, params=params)
        if request.status_code != 200:
            raise errors.StreamError('wrong status code: {0}'.format(
                request.status_code))
        posts = []
        for post in request.json():
            try:
                posts.append(Post(self._connection, guid=post['guid']))
            except errors.PostError:
                if not suppress:
                    raise
        return posts
Пример #5
0
    def _obtain(self, max_time=0, suppress=True):
        """Obtains stream from pod.

		suppress:bool - suppress post-fetching errors (e.g. 404)
		"""
        params = {}
        if max_time:
            if self.latest == None:
                self.latest = int(time.mktime(time.gmtime()) * 1000)
                self.latest -= max_time
            else:
                self.latest += 1
            params['max_time'] = max_time
            params['_'] = self.latest
        request = self._connection.get(self._location, params=params)
        if request.status_code != 200:
            raise errors.StreamError('wrong status code: {0}'.format(
                request.status_code))
        posts = []
        latest_time = None  # Used to get the created_at from the latest posts we received.
        for post in request.json():
            try:
                comments = False
                if post['interactions']['comments_count'] > 3: comments = True
                posts.append(
                    Post(self._connection,
                         id=post['id'],
                         guid=post['guid'],
                         fetch=False,
                         comments=comments,
                         post_data=post))
                if post['created_at']: latest_time = post['created_at']
            except errors.PostError:
                if not suppress:
                    raise
        if latest_time:
            self.max_time = parse_utc_timestamp(latest_time)
        return posts
Пример #6
0
    def post(self,
             text='',
             aspect_ids='public',
             photos=None,
             photo='',
             poll_question=None,
             poll_answers=None,
             location_coords=None,
             provider_display_name=''):
        """This function sends a post to an aspect.
		If both `photo` and `photos` are specified `photos` takes precedence.

		:param text: Text to post.
		:type text: str

		:param aspect_ids: Aspect ids to send post to.
		:type aspect_ids: str

		:param photo: filename of photo to post
		:type photo: str

		:param photos: id of photo to post (obtained from _photoupload())
		:type photos: int

		:param provider_display_name: name of provider displayed under the post
		:type provider_display_name: str

		:param poll_question: Question string
		:type poll_question: str

		:param poll_answers: Anwsers to the poll
		:type poll_answers: list with strings

		:param location_coords: TODO
		:type location_coords: TODO

		:returns: diaspy.models.Post -- the Post which has been created
		"""
        data = {}
        data['aspect_ids'] = aspect_ids
        data['status_message'] = {
            'text': text,
            'provider_display_name': provider_display_name
        }
        if photo: data['photos'] = self._photoupload(photo)
        if photos: data['photos'] = photos
        if poll_question and poll_answers:
            data['poll_question'] = poll_question
            data['poll_answers'] = poll_answers
        if location_coords: data['location_coords'] = location_coords

        request = self._connection.post('status_messages',
                                        data=json.dumps(data),
                                        headers={
                                            'content-type': 'application/json',
                                            'accept': 'application/json',
                                            'x-csrf-token':
                                            repr(self._connection)
                                        })
        if request.status_code != 201:
            raise Exception('{0}: Post could not be posted.'.format(
                request.status_code))
        post_json = request.json()
        post = Post(self._connection,
                    id=post_json['id'],
                    guid=post_json['guid'],
                    post_data=post_json)
        return post