Stream() moved to `diaspy/streams.py`, Generic() stream implemented
[diaspy.git] / diaspy / streams.py
1 import json
2 from diaspy.models import Post
3
4
5 class Generic:
6 """Object representing generic stream. Used in Tag(),
7 Stream(), Activity() etc.
8 """
9 def __init__(self, connection, location):
10 """
11 :param connection: Connection() object
12 :param type: diaspy.connection.Connection
13 """
14 self._connection = connection
15 self._location = location
16 self._stream = []
17 self.fill()
18
19 def __contains__(self, post):
20 """Returns True if stream contains given post.
21 """
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
25
26 def __iter__(self):
27 """Provides iterable interface for stream.
28 """
29 return iter(self._stream)
30
31 def __getitem__(self, n):
32 """Returns n-th item in Stream.
33 """
34 return self._stream[n]
35
36 def __len__(self):
37 """Returns length of the Stream.
38 """
39 return len(self._stream)
40
41 def _obtain(self):
42 """Obtains stream from pod.
43 """
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()]
48
49 def clear(self):
50 """Removes all posts from stream.
51 """
52 self._stream = []
53
54 def update(self):
55 """Updates stream.
56 """
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
63
64 def fill(self):
65 """Fills the stream with posts.
66 """
67 self._stream = self._obtain()
68
69
70 class Stream(Generic):
71 """Object representing user's stream.
72 """
73 def post(self, text, aspect_ids='public', photos=None):
74 """This function sends a post to an aspect
75
76 :param text: Text to post.
77 :type text: str
78 :param aspect_ids: Aspect ids to send post to.
79 :type aspect_ids: str
80
81 :returns: diaspy.models.Post -- the Post which has been created
82 """
83 data = {}
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(
94 request.status_code))
95
96 post = Post(str(request.json()['id']), self._connection)
97 return post
98
99 def post_picture(self, filename):
100 """This method posts a picture to D*.
101
102 :param filename: Path to picture file.
103 :type filename: str
104 """
105 aspects = self._connection.getUserInfo()['aspects']
106 params = {}
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']
112
113 data = open(filename, 'rb')
114
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)
119 data.close()
120 return request
121
122
123 class Activity(Generic):
124 """Stream representing user's activity.
125 """
126 pass