Changed fetching languages in diaspy/settings.py, setLanguage() now
[diaspy.git] / diaspy / settings.py
index 98aa0ceed29da2dc35023297097158f1ad805104..ded22589228a3ebdb5a6beb3a9b53e370cd5e402 100644 (file)
@@ -14,6 +14,9 @@ from diaspy import errors, streams
 class Account():
     """Provides interface to account settings.
     """
+    email_regexp = re.compile('<input id="user_email" name="user\[email\]" size="30" type="text" value="(.+?)"')
+    language_option_regexp = re.compile('<option value="([-_a-z]+)">(.*?)</option>')
+
     def __init__(self, connection):
         self._connection = connection
 
@@ -55,7 +58,9 @@ class Account():
             else: nsfw = ''
             if post['photos']:
                 for n, photo in enumerate(post['photos']):
-                    name = '{0}_{1}{2}.{3}'.format(post['guid'], photo['guid'], nsfw, photo['sizes'][size].split('.')[-1])
+                    # photo format -- .jpg, .png etc.
+                    ext = photo['sizes'][size].split('.')[-1]
+                    name = '{0}_{1}{2}.{3}'.format(post['guid'], photo['guid'], nsfw, ext)
                     filename = os.path.join(path, name)
                     try:
                         urllib.request.urlretrieve(url=photo['sizes'][size], filename=filename)
@@ -71,15 +76,16 @@ class Account():
         """
         data = {'_method': 'put', 'utf8': '✓', 'user[email]': email, 'authenticity_token': repr(self._connection)}
         request = self._connection.post('user', data=data, allow_redirects=False)
+        if request.status_code != 302:
+            raise errors.SettingsError('setting email failed: {0}'.format(request.status_code))
 
     def getEmail(self):
         """Returns currently used email.
         """
         data = self._connection.get('user/edit')
-        email = re.compile('<input id="user_email" name="user\[email\]" size="30" type="text" value=".+?"').search(data.text)
-        if email is None: raise errors.DiaspyError('cannot fetch email')
-        email = email.group(0)[:-1]
-        email = email[email.rfind('"')+1:]
+        email = self.email_regexp.search(data.text)
+        if email is None: email = ''
+        else: email = email.group(1)
         return email
 
     def setLanguage(self, lang):
@@ -89,24 +95,15 @@ class Account():
         """
         data = {'_method': 'put', 'utf8': '✓', 'user[language]': lang, 'authenticity_token': repr(self._connection)}
         request = self._connection.post('user', data=data, allow_redirects=False)
-        return request.status_code
+        if request.status_code != 302:
+            raise errors.SettingsError('setting language failed: {0}'.format(request.status_code))
 
     def getLanguages(self):
         """Returns a list of tuples containing ('Language name', 'identifier').
         One of the Black Magic(tm) methods.
         """
-        selection_start = '<select id="user_language" name="user[language]">'
-        selection_end = '</select>'
-        languages = []
         request = self._connection.get('user/edit')
-        data = request.text[request.text.find(selection_start)+len(selection_start):]
-        data = data[:data.find(selection_end)].split('\n')
-        for item in data:
-            name = item[item.find('>')+1:item.rfind('<')]
-            identifier = item[item.find('"')+1:]
-            identifier = identifier[:identifier.find('"')]
-            languages.append((name, identifier))
-        return languages
+        return self.language_option_regexp.findall(request.text)
 
 
 class Privacy():
@@ -126,16 +123,16 @@ class Profile():
         Setters can then be used to adjust the data.
         Finally, `update()` can be called to send data back to pod.
     """
-    firstname_regexp = re.compile('<input id="profile_first_name" name="profile\[first_name\]" type="text" value="(.*?)" />')
-    lastname_regexp = re.compile('<input id="profile_last_name" name="profile\[last_name\]" type="text" value="(.*?)" />')
+    firstname_regexp = re.compile('id="profile_first_name" name="profile\[first_name\]" type="text" value="(.*?)" />')
+    lastname_regexp = re.compile('id="profile_last_name" name="profile\[last_name\]" type="text" value="(.*?)" />')
     bio_regexp = re.compile('<textarea id="profile_bio" name="profile\[bio\]" placeholder="Fill me out" rows="5">\n(.*?)</textarea>')
-    location_regexp = re.compile('<input id="profile_location" name="profile\[location\]" placeholder="Fill me out" type="text" value="(.*?)" />')
-    gender_regexp = re.compile('<input id="profile_gender" name="profile\[gender\]" placeholder="Fill me out" type="text" value="(.*?)" />')
-    birth_year_regexp = re.compile('<option selected="selected" value="([0-9]{4,4})">[0-9]{4,4}</option>')
-    birth_month_regexp = re.compile('<option selected="selected" value="([0-9]{1,2})">(.*?)</option>')
-    birth_day_regexp = re.compile('<option selected="selected" value="([0-9]{1,2})">[0-9]{1,2}</option>')
-    is_searchable_regexp = re.compile('<input checked="checked" id="profile_searchable" name="profile\[searchable\]" type="checkbox" value="(.*?)" />')
-    is_nsfw_regexp = re.compile('<input checked="checked" id="profile_nsfw" name="profile\[nsfw\]" type="checkbox" value="(.*?)" />')
+    location_regexp = re.compile('id="profile_location" name="profile\[location\]" placeholder="Fill me out" type="text" value="(.*?)" />')
+    gender_regexp = re.compile('id="profile_gender" name="profile\[gender\]" placeholder="Fill me out" type="text" value="(.*?)" />')
+    birth_year_regexp = re.compile('selected="selected" value="([0-9]{4,4})">[0-9]{4,4}</option>')
+    birth_month_regexp = re.compile('selected="selected" value="([0-9]{1,2})">(.*?)</option>')
+    birth_day_regexp = re.compile('selected="selected" value="([0-9]{1,2})">[0-9]{1,2}</option>')
+    is_searchable_regexp = re.compile('checked="checked" id="profile_searchable" name="profile\[searchable\]" type="checkbox" value="(.*?)" />')
+    is_nsfw_regexp = re.compile('checked="checked" id="profile_nsfw" name="profile\[nsfw\]" type="checkbox" value="(.*?)" />')
 
     def __init__(self, connection):
         self._connection = connection