- assert match
- license_url = match.group(1)
- self.cleaned_data['license_url'] = license_url
- re_license = re.compile(r'http://creativecommons.org/licenses/([^/]*)/([^/]*)/.*')
- m = re_license.match(license_url)
- assert m
- self.cleaned_data['license_name'] = 'CC %s %s' % (m.group(1).upper(), m.group(2))
- except AssertionError:
- raise forms.ValidationError('Error reading license name.')
-
- m = re.search(r'<strong class="username">By <a href="[^"]*">([^<]*)</a></strong>', html)
- if m:
- self.cleaned_data['author'] = "%s@Flickr" % m.group(1)
- else:
- raise forms.ValidationError('Error reading author name.')
-
- m = re.search(r'<h1[^>]*>(.*?)</h1>', html)
- if not m:
- raise forms.ValidationError('Error reading image title.')
- self.cleaned_data['title'] = m.group(1)
-
- url_size = base_url + "sizes/o/"
- html = urlopen(url_size).read().decode('utf-8')
- m = re.search(r'<div id="allsizes-photo">\s*<img src="([^"]*)"', html)
- if m:
- self.cleaned_data['download_url'] = m.group(1)
- else:
- raise forms.ValidationError('Error reading image URL.')
- return base_url
+ flickr_data = get_flickr_data(url)
+ except FlickrError as e:
+ raise forms.ValidationError(e)
+ for field_name in ('license_url', 'license_name', 'author', 'title', 'download_url'):
+ self.cleaned_data[field_name] = flickr_data[field_name]
+ return flickr_data['source_url']