From: Marek Marecki Date: Thu, 22 Aug 2013 20:50:21 +0000 (+0200) Subject: New stuff for version 0.4.1 detailed decription in Changelog X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=f61c14c1b63b90fa6a2afcdc1625436aa9f5b577;p=diaspy.git New stuff for version 0.4.1 detailed decription in Changelog --- diff --git a/Changelog.markdown b/Changelog.markdown index 92eb643..790cc0c 100644 --- a/Changelog.markdown +++ b/Changelog.markdown @@ -19,6 +19,21 @@ up-to-date than manual and if conflicts appear they should follow the order: *docstrings* -> *docs/* -> *manual/* +---- + +#### Version `0.4.1` (2013-08-): + +* __new__: `__getitem__()` in `diaspy.models.Post`, +* __new__: `json()` method in `diaspy.streams.Generic` adds the possibility to export streams to JSON, +* __new__: `full()` method in `diaspy.streams.Generic` will try to fetch full stream (containing all posts), +* __new__: `setEmail()` method in `diaspy.settings.Settings`, +* __new__: `setLanguage()` method in `diaspy.settings.Settings`, +* __new__: `downloadPhotos()` method in `diaspy.settings.Settings`, + +* __fix__: fixed some bugs in regular expressions used by `diaspy` internals + (html tag removal, so you get nicer notifications), + + ---- #### Version `0.4.0` (2013-08-20): diff --git a/diaspy/models.py b/diaspy/models.py index fb592cc..a832b0d 100644 --- a/diaspy/models.py +++ b/diaspy/models.py @@ -343,6 +343,9 @@ class Post(): """ return self.data['text'] + def __getitem__(self, key): + return self.data[key] + def _fetchdata(self): """This function retrieves data of the post. """ diff --git a/diaspy/settings.py b/diaspy/settings.py index bbb6ffe..245d3b4 100644 --- a/diaspy/settings.py +++ b/diaspy/settings.py @@ -3,10 +3,12 @@ import json +import os import re import urllib +import warnings -from diaspy import errors +from diaspy import errors, streams class Settings(): @@ -17,9 +19,49 @@ class Settings(): self._connection = connection def downloadxml(self): + """Returns downloaded XML. + """ request = self._connection.get('user/export') return request.text + def downloadPhotos(self, size='large', path='.', _critical=False, _stream=None): + """Downloads photos into the current working directory. + Sizes are: large, medium, small. + Filename is: {photo_guid}.{extension} + + Normally, this method will catch urllib-generated errors and + just issue warnings about photos that couldn't be downloaded. + However, with _critical param set to True errors will become + critical - the will be reraised in finally block. + + :param size: size of the photos to download - large, medium or small + :type size: str + :param path: path to download (defaults to current working directory + :type path: str + :param _stream: diaspy.streams.Generic-like object (only for testing) + :param _critical: if True urllib errors will be reraised after generating a warning (may be removed) + + :returns: integer, number of photos downloaded + """ + photos = 0 + if _stream is not None: stream = _stream + else: stream = streams.Activity + stream = stream(self._connection) + stream.full() + for i, post in enumerate(stream): + if post['photos']: + for n, photo in enumerate(post['photos']): + name = '{0}.{1}'.format(photo['guid'], photo['sizes'][size].split('.')[-1]) + filename = os.path.join(path, name) + try: + urllib.request.urlretrieve(url=photo['sizes'][size], filename=filename) + except (urllib.error.HTTPError, urllib.error.URLError) as e: + warnings.warn('downloading image {0} from post {1}: {2}'.format(photo['guid'], post['guid'], e)) + finally: + if _critical: raise + photos += 1 + return photos + def setEmail(self, email): """Changes user's email. """ diff --git a/diaspy/streams.py b/diaspy/streams.py index 36ff236..1d06a50 100644 --- a/diaspy/streams.py +++ b/diaspy/streams.py @@ -127,11 +127,40 @@ class Generic(): new_stream = self._obtain(max_time=max_time) self._expand(new_stream) + def full(self): + """Fetches full stream - containing all posts. + WARNING: this can be a **VERY** time consuming function on slow connections of massive streams. + + :returns: integer, lenght of the stream + """ + oldstream = self.copy() + self.more() + while len(oldstream) != len(self): + oldstream = self.copy() + self.more() + return len(self) + def copy(self): """Returns copy (list of posts) of current stream. """ return [p for p in self._stream] + def json(self, comments=False): + """Returns JSON encoded string containing stream's data. + + :param comments: to include comments or not to include 'em, that is the question this param holds answer to + :type comments: bool + """ + stream = [post for post in self._stream] + if comments: + for i, post in enumerate(stream): + post._fetchcomments() + comments = [c.data for c in post.comments] + post['interactions']['comments'] = comments + stream[i] = post + stream = [post.data for post in stream] + return json.dumps(stream) + class Outer(Generic): """Object used by diaspy.models.User to represent