From 7a31b4aa26c5f0fc90dd97632eb48da859677f9e Mon Sep 17 00:00:00 2001 From: Marek Marecki Date: Tue, 30 Apr 2013 19:54:21 +0200 Subject: [PATCH] Stream() object with some basic functionality implemented (plus manual and tests) --- diaspy/client.py | 15 ++---- diaspy/connection.py | 19 +++++-- diaspy/models.py | 117 +++++++++++++++++++++++++++++++++++++++++++ manual/stream.mdown | 32 ++++++++++++ test-image.png | Bin 0 -> 10738 bytes tests.py | 47 +++++++++++++++-- 6 files changed, 210 insertions(+), 20 deletions(-) create mode 100644 manual/stream.mdown create mode 100644 test-image.png diff --git a/diaspy/client.py b/diaspy/client.py index 9adf538..041e495 100644 --- a/diaspy/client.py +++ b/diaspy/client.py @@ -71,10 +71,7 @@ class Client: :returns: dict -- json formatted user info. """ - r = self.connection.get('bookmarklet') - regex = re.compile(r'window.current_user_attributes = ({.*})') - userdata = json.loads(regex.search(r.text).group(1)) - return userdata + return self.connection.getUserInfo() def post_picture(self, filename): """This method posts a picture to D*. @@ -104,13 +101,9 @@ class Client: :returns: list -- list of Post objects. """ - request = self.connection.get('stream.json') - - if request.status_code != 200: - raise Exception('wrong status code: {0}'.format(request.status_code)) - - stream = request.json() - return [diaspy.models.Post(str(post['id']), self.connection) for post in stream] + stream = diaspy.models.Stream(self.connection) + stream.update() + return stream def get_notifications(self): """This functions returns a list of notifications. diff --git a/diaspy/connection.py b/diaspy/connection.py index 585089c..2f0329e 100644 --- a/diaspy/connection.py +++ b/diaspy/connection.py @@ -2,6 +2,7 @@ import re import requests +import json class LoginError(Exception): @@ -24,6 +25,7 @@ class Connection(): self.pod = pod self.session = requests.Session() self._token_regex = re.compile(r'content="(.*?)"\s+name="csrf-token') + self._userinfo_regex = re.compile(r'window.current_user_attributes = ({.*})') self._setlogin(username, password) def get(self, string): @@ -31,7 +33,7 @@ class Connection(): Performs additional checks if needed. Example: - To obtain 'foo' from pod one should call `_sessionget('foo')`. + To obtain 'foo' from pod one should call `get('foo')`. :param string: URL to get without the pod's URL and slash eg. 'stream'. :type string: str @@ -43,7 +45,7 @@ class Connection(): Performs additional checks if needed. Example: - To post to 'foo' one should call `_sessionpost('foo', data={})`. + To post to 'foo' one should call `post('foo', data={})`. :param string: URL to post without the pod's URL and slash eg. 'status_messages'. :type string: str @@ -99,7 +101,7 @@ class Connection(): data=self.login_data, headers={'accept': 'application/json'}) if request.status_code != 201: - raise Exception('{0}: Login failed.'.format(request.status_code)) + raise LoginError('{0}: Login failed.'.format(request.status_code)) def login(self, username='', password=''): """This function is used to log in to a pod. @@ -110,11 +112,20 @@ class Connection(): self._login() def podswitch(self, pod): - """Switches pod. + """Switches pod from current to another one. """ self.pod = pod self._login() + def getUserInfo(self): + """This function returns the current user's attributes. + + :returns: dict -- json formatted user info. + """ + request = self.get('bookmarklet') + userdata = json.loads(self._userinfo_regex.search(request.text).group(1)) + return userdata + def getToken(self): """This function returns a token needed for authentication in most cases. diff --git a/diaspy/models.py b/diaspy/models.py index 27b762d..04d8ed3 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -1,6 +1,9 @@ #!/usr/bin/env python3 +import json + + class Post: """This class represents a post. @@ -129,3 +132,117 @@ class Post: headers={'accept': 'application/json'}) if r.status_code != 204: raise Exception('{0}: Post could not be deleted'.format(r.status_code)) + + +class Stream: + """Object representing user's stream. + """ + def __init__(self, connection): + """ + :param connection: Connection() object + :param type: diaspy.connection.Connection + """ + self._connection = connection + self._stream = [] + self.fill() + + def __contains__(self, post): + """Returns True if stream contains given post. + """ + if type(post) is not Post: + raise TypeError('stream can contain only posts: checked for {0}'.format(type(post))) + return post in self._stream + + def __iter__(self): + """Provides iterable interface for stream. + """ + return iter(self._stream) + + def __getitem__(self, n): + """Returns n-th item in Stream. + """ + return self._stream[n] + + def __len__(self): + """Returns length of the Stream. + """ + return len(self._stream) + + def _obtain(self): + """Obtains stream from pod. + """ + request = self._connection.get('stream.json') + if request.status_code != 200: + raise Exception('wrong status code: {0}'.format(request.status_code)) + return [Post(str(post['id']), self._connection) for post in request.json()] + + def clear(self): + """Removes all posts from stream. + """ + self._stream = [] + + def update(self): + """Updates stream. + """ + stream = self._obtain() + _stream = self._stream + for i in range(len(stream)): + if stream[-i] not in _stream: + _stream = [stream[-i]] + _stream + self._stream = _stream + + def fill(self): + """Fills the stream with posts. + """ + self._stream = self._obtain() + + def post(self, text, aspect_ids='public', photos=None): + """This function sends a post to an aspect + + :param text: Text to post. + :type text: str + :param aspect_ids: Aspect ids to send post to. + :type aspect_ids: str + + :returns: diaspy.models.Post -- the Post which has been created + """ + data = {} + data['aspect_ids'] = aspect_ids + data['status_message'] = {'text': text} + 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': self._connection.getToken()}) + if request.status_code != 201: + raise Exception('{0}: Post could not be posted.'.format( + request.status_code)) + + post = Post(str(request.json()['id']), self._connection) + self.update() + return post + + def post_picture(self, filename): + """This method posts a picture to D*. + + :param filename: Path to picture file. + :type filename: str + """ + aspects = self._connection.getUserInfo()['aspects'] + params = {} + params['photo[pending]'] = 'true' + params['set_profile_image'] = '' + params['qqfile'] = filename + for i, aspect in enumerate(aspects): + params['photo[aspect_ids][%d]' % (i)] = aspect['id'] + + data = open(filename, 'rb') + + headers = {'content-type': 'application/octet-stream', + 'x-csrf-token': self._connection.getToken(), + 'x-file-name': filename} + request = self._connection.post('photos', params=params, data=data, headers=headers) + data.close() + self.update() + return request diff --git a/manual/stream.mdown b/manual/stream.mdown new file mode 100644 index 0000000..7be4c36 --- /dev/null +++ b/manual/stream.mdown @@ -0,0 +1,32 @@ +#### `Stream()` object + +This object is used to represent user's stream on D\*. +It is returned by `Client()`'s method `get_stream()` and +is basically the list of posts. + +It can get more functionality in future (probably it would be +moved from `Client()`). + +---- + +##### Getting stream + +To get basic stream you have to have working `Connection()` as +this is required by `Stream()`'s constructor. + + c = diaspy.connection.Connection(pod='https://pod.example.com', + username='foo', + password='bar') + c.login() + stream = diaspy.models.Stream(c) + +Now you have a stream filled with posts (if any can be found on user's stream). + +---- + +##### Length of a stream + +Stream's length can be checked by calling `len()` on it. + + len(stream) + 10 diff --git a/test-image.png b/test-image.png new file mode 100644 index 0000000000000000000000000000000000000000..2f7c51a739b7563602322f4caf57157e3bdb2cd9 GIT binary patch literal 10738 zcmZXabyQSew8t+q44u-Q(k-0>3?U#P-8s@FLrTYw4yC(Ox&*$?0LZhhK;%k z0z86$*NWXC-~ro1)z}LFa7i9sXh2RLC3q4O_4Jt%<`OzRF(tVM)zC}u6rH1<5lYeB z)fMT60{;a7#g|BH6w;2-*AeBwsPgogwjCc5695>2rwDmHzaIzY&Mu^L7jk<+{uBvK zO(#$}0Qwn@w?@U7f}y<#f6GDaR3i*y&5P6Jm)Hvm~tNkR@PdBKp`X>66 zo8Iv)8_W==Sb9X46XQ&{y(tkF!`}F4^wmFx8@bXa_DA{eTBMvcvbYm{e0>UhgU_Ur zUmZ%wGDQLn{s;7Jj~Z~_Z=p>((*qy2LT^Z+|4$B`I(i$#ZRR|ew{SxIUtz30<4^yaeu8ixvd3YJWg^4rA}OZ;@< z(jwfG`F#-~9v}{na9{zM5(Ew;l`nc{f-fO{20!02+Rk7pMsLNFalhX#spG=v zgrObO=FJKz`$`Uw%RJT{J9Q9yY2(B%-0Zob0n2+MT; z_v`FYq8Je`T9wxZt)9WZ(e`0Ogw%mu_tEvCC67#F7Js!V&k+VuI?&o}Ej62prrFMy zJ1$8++U4&4^l2>lj$UE2ZTH1-o?3=Y6{p*xdNE@fM4KW|vab{u@->#D0@y5i$(3|M zlFS4xprg!hwCTaC;wD?v)5Sqv1s<&k0^WT?6jVDNGB9H%Jk>d!l$VXe@TVP9uu@$% zPbf;$!tCuWZ^iN+X^aFgh8gJ7w^3BOCvkOk?MRJ8bQISK2;zcRc15?$M253odxvmGH-zank_chPWJK$C5MKr7GmESRN z*M0v!`z&4(-WI=F%BVQ2&XzjZ1&$v2I(Ve7@-+E2P?lF`O#Tbn5E>O0x`x%mI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JP0Uq+V2)7f-4p+eE7^R7riDnT{^7#A;if;&wlpa?7oEnpHj7C z9ZmlIJ)6J_y;qDfoP5efWN<5 zk6#CgFEf2|Jc5}C6n38)f-kt60xq0>UxV8D)2pKo?8?f@pTeM+h@hYk0C2Lw@Nd5L zF67^>B{5D^ZEbmeXvoUqtJ4n&CnN9=>G7MMLj>mot#o22L_IKm_)^D=xr% zF?a#?9n8DEEJE(Qzu%a5`+(P+XKRNS?DuBtK#N`WT*@#-5{2A+F9Z#N9L>NM%hg230zzqPh6p^Mp>%X~>(!M7kJ{SWn2E5m zc0k0C+kSU5*Xwo@GLpt=GyLZ9`l#i7v$+3h+3DY!y+TQEr?0XhcYBsFAO>XSI82dO z`}T6+^Z)Ls$Wi+Onz^FmJD9yI{2;@Q1pAJE@X;Z#i-}0gBG6g@mXRNL80`pm=HYVElXm#<8fdM z{3KfLuY9d}w*eR10VQ~gS-`$~(4Hv-z+AaQaG$ zi{*jPt2M%qs`u^Qd$UxxzP`TQ1bxOWFR1GC!P5J;r~{yHU=U|{f0|iRSvhn}`u@)k zX9@n4@AsYoG8+Vae$sp$w418X6M0f7cUA&dzT+czMZM>?cHPpDmmuGsyAvoFux( zJXX($aS-N$goT2Vi(*3hECIBY_VS7fLlCPhcw;=+xYW_nVJZl@_n%r>ara*V@i!^x zSZDc1q_EIfgu?stZ|XK%W7*74lTW9(Z{|EzfPC^L-g*60AoO*6%8UpB^kNLrqPk!-ICw>I`7i-Rzw>fiEd{&i3 z5=<6nKV%Xy&vWLafq-#n(cJ&vo9GXm=U~Dc{2#79eCU67E`l2vEjAAR0a^O%iTFk4 RCl6aMpDJl0>J_ZQ{|Ce;(CGjG literal 0 HcmV?d00001 diff --git a/tests.py b/tests.py index f81538f..4a2ec47 100644 --- a/tests.py +++ b/tests.py @@ -17,6 +17,42 @@ __username__ = testconf.__username__ __passwd__ = testconf.__passwd__ +class StreamTest(unittest.TestCase): + def testGetting(self): + c = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__) + c.login() + stream = diaspy.models.Stream(c) + stream.update() + + def testGettingLength(self): + c = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__) + c.login() + stream = diaspy.models.Stream(c) + stream.update() + len(stream) + + def testClearing(self): + c = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__) + c.login() + stream = diaspy.models.Stream(c) + stream.update() + stream.clear() + self.assertEqual(0, len(stream)) + + def testPostingText(self): + c = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__) + c.login() + stream = diaspy.models.Stream(c) + post = stream.post('`diaspy` test \n#diaspy') + self.assertEqual(diaspy.models.Post, type(post)) + + def testPostingImage(self): + c = diaspy.connection.Connection(pod=__pod__, username=__username__, password=__passwd__) + c.login() + stream = diaspy.models.Stream(c) + stream.post_picture('./test-image.png') + + class ConnectionTest(unittest.TestCase): def testLoginWithoutUsername(self): connection = diaspy.connection.Connection(pod=__pod__) @@ -26,17 +62,18 @@ class ConnectionTest(unittest.TestCase): connection = diaspy.connection.Connection(pod=__pod__) self.assertRaises(diaspy.connection.LoginError, connection.login, username='user') -class ClientTests(unittest.TestCase): def testGettingUserInfo(self): - client = diaspy.client.Client(__pod__, __username__, __passwd__) - info = client.get_user_info() + connection = diaspy.connection.Connection(__pod__, __username__, __passwd__) + connection.login() + info = connection.getUserInfo() self.assertEqual(dict, type(info)) + +class ClientTests(unittest.TestCase): def testGettingStream(self): client = diaspy.client.Client(__pod__, __username__, __passwd__) stream = client.get_stream() - self.assertEqual(list, type(stream)) - if stream: self.assertEqual(diaspy.models.Post, type(stream[0])) + if len(stream): self.assertEqual(diaspy.models.Post, type(stream[0])) def testGettingNotifications(self): client = diaspy.client.Client(__pod__, __username__, __passwd__) -- 2.25.1