2 from diaspy
.models
import Post
6 """Object representing generic stream. Used in Tag(),
7 Stream(), Activity() etc.
9 def __init__(self
, connection
, location
):
11 :param connection: Connection() object
12 :param type: diaspy.connection.Connection
14 self
._connection
= connection
15 self
._location
= location
19 def __contains__(self
, post
):
20 """Returns True if stream contains given post.
22 if type(post
) is not Post
:
23 raise TypeError('stream can contain only posts: checked for {0}'.format(type(post
)))
24 return post
in self
._stream
27 """Provides iterable interface for stream.
29 return iter(self
._stream
)
31 def __getitem__(self
, n
):
32 """Returns n-th item in Stream.
34 return self
._stream
[n
]
37 """Returns length of the Stream.
39 return len(self
._stream
)
42 """Obtains stream from pod.
44 request
= self
._connection
.get(self
._location
)
45 if request
.status_code
!= 200:
46 raise Exception('wrong status code: {0}'.format(request
.status_code
))
47 return [Post(str(post
['id']), self
._connection
) for post
in request
.json()]
50 """Removes all posts from stream.
57 stream
= self
._obtain
()
58 _stream
= self
._stream
59 for i
in range(len(stream
)):
60 if stream
[-i
] not in _stream
:
61 _stream
= [stream
[-i
]] + _stream
62 self
._stream
= _stream
65 """Fills the stream with posts.
67 self
._stream
= self
._obtain
()
70 class Stream(Generic
):
71 """Object representing user's stream.
73 def post(self
, text
, aspect_ids
='public', photos
=None):
74 """This function sends a post to an aspect
76 :param text: Text to post.
78 :param aspect_ids: Aspect ids to send post to.
81 :returns: diaspy.models.Post -- the Post which has been created
84 data
['aspect_ids'] = aspect_ids
85 data
['status_message'] = {'text': text
}
86 if photos
: data
['photos'] = photos
87 request
= self
._connection
.post('status_messages',
88 data
=json
.dumps(data
),
89 headers
={'content-type': 'application/json',
90 'accept': 'application/json',
91 'x-csrf-token': self
._connection
.getToken()})
92 if request
.status_code
!= 201:
93 raise Exception('{0}: Post could not be posted.'.format(
96 post
= Post(str(request
.json()['id']), self
._connection
)
99 def post_picture(self
, filename
):
100 """This method posts a picture to D*.
102 :param filename: Path to picture file.
105 aspects
= self
._connection
.getUserInfo()['aspects']
107 params
['photo[pending]'] = 'true'
108 params
['set_profile_image'] = ''
109 params
['qqfile'] = filename
110 for i
, aspect
in enumerate(aspects
):
111 params
['photo[aspect_ids][%d]' % (i
)] = aspect
['id']
113 data
= open(filename
, 'rb')
115 headers
= {'content-type': 'application/octet-stream',
116 'x-csrf-token': self
._connection
.getToken(),
117 'x-file-name': filename
}
118 request
= self
._connection
.post('photos', params
=params
, data
=data
, headers
=headers
)
123 class Activity(Generic
):
124 """Stream representing user's activity.