d68f0b31104205b06cec8a05db8a9f0d7b4f8d93
1 """This module provides access to user's settings on Diaspora*.
11 from diaspy
import errors
, streams
15 """Provides profile editing methods.
17 def __init__(self
, connection
):
18 self
._connection
= connection
19 self
.data
= {'utf-8': '✓',
21 'profile[first_name]': '',
22 'profile[last_name]': '',
23 'profile[tag_string]': '',
27 'profile[location]': '',
28 'profile[gender]': '',
29 'profile[date][year]': '',
30 'profile[date][month]': '',
31 'profile[date][day]': '',
34 def setName(self
, first
='', last
=''):
38 data
['profile[first_name]'] = first
39 data
['profile[last_name]'] = last
40 data
['authenticity_token'] = repr(self
._connection
)
42 request
= self
._connection
.post('profile', data
=data
, allow_redirects
=False)
43 return request
.status_code
47 """This object is used to get access to user's settings on
48 Diaspora* and provides interface for downloading user's stuff.
50 def __init__(self
, connection
):
51 self
._connection
= connection
53 def downloadxml(self
):
54 """Returns downloaded XML.
56 request
= self
._connection
.get('user/export')
59 def downloadPhotos(self
, size
='large', path
='.', mark_nsfw
=True, _critical
=False, _stream
=None):
60 """Downloads photos into the current working directory.
61 Sizes are: large, medium, small.
62 Filename is: {post_guid}_{photo_guid}.{extension}
64 Normally, this method will catch urllib-generated errors and
65 just issue warnings about photos that couldn't be downloaded.
66 However, with _critical param set to True errors will become
67 critical - the will be reraised in finally block.
69 :param size: size of the photos to download - large, medium or small
71 :param path: path to download (defaults to current working directory
73 :param mark_nsfw: will append '-nsfw' to images from posts marked as nsfw,
75 :param _stream: diaspy.streams.Generic-like object (only for testing)
76 :param _critical: if True urllib errors will be reraised after generating a warning (may be removed)
78 :returns: integer, number of photos downloaded
82 stream
= streams
.Activity(self
._connection
)
86 for i
, post
in enumerate(stream
):
87 if post
['nsfw'] is not False: nsfw
= '-nsfw'
90 for n
, photo
in enumerate(post
['photos']):
91 name
= '{0}_{1}{2}.{3}'.format(post
['guid'], photo
['guid'], nsfw
, photo
['sizes'][size
].split('.')[-1])
92 filename
= os
.path
.join(path
, name
)
94 urllib
.request
.urlretrieve(url
=photo
['sizes'][size
], filename
=filename
)
95 except (urllib
.error
.HTTPError
, urllib
.error
.URLError
) as e
:
96 warnings
.warn('downloading image {0} from post {1}: {2}'.format(photo
['guid'], post
['guid'], e
))
102 def setEmail(self
, email
):
103 """Changes user's email.
105 data
= {'_method': 'put', 'utf8': '✓', 'user[email]': email
, 'authenticity_token': repr(self
._connection
)}
106 request
= self
._connection
.post('user', data
=data
, allow_redirects
=False)
109 """Returns currently used email.
111 data
= self
._connection
.get('user/edit')
112 email
= re
.compile('<input id="user_email" name="user\[email\]" size="30" type="text" value=".+?"').search(data
.text
)
113 if email
is None: raise errors
.DiaspyError('cannot fetch email')
114 email
= email
.group(0)[:-1]
115 email
= email
[email
.rfind('"')+1:]
118 def setLanguage(self
, lang
):
119 """Changes user's email.
121 :param lang: language identifier from getLanguages()
123 data
= {'_method': 'put', 'utf8': '✓', 'user[language]': lang
, 'authenticity_token': repr(self
._connection
)}
124 request
= self
._connection
.post('user', data
=data
, allow_redirects
=False)
125 return request
.status_code
127 def getLanguages(self
):
128 """Returns a list of tuples containing ('Language name', 'identifier').
129 One of the Black Magic(tm) methods.
131 selection_start
= '<select id="user_language" name="user[language]">'
132 selection_end
= '</select>'
134 request
= self
._connection
.get('user/edit')
135 data
= request
.text
[request
.text
.find(selection_start
)+len(selection_start
):]
136 data
= data
[:data
.find(selection_end
)].split('\n')
138 name
= item
[item
.find('>')+1:item
.rfind('<')]
139 identifier
= item
[item
.find('"')+1:]
140 identifier
= identifier
[:identifier
.find('"')]
141 languages
.append((name
, identifier
))