def _create_photos_from_xml_response(self, xml_response):
     problem_children = []
     counter = 1
     for elem in xml_response:
         print counter
         counter += 1
         if elem.tag == "docs":
             if not self._resource_already_exists(elem):
                 if elem.find("identifier") is not None:
                     new_photo = CatPhoto(
                         title=elem.find("title").text,
                         description=elem.find("title_sort").text,
                         source=Source.objects.get(description=elem.find('institution').text),
                         source_key=elem.find("identifier").text,
                         author=elem.find("author").text,
                         source_url=elem.find("record_link").text,
                     )
                     if elem.find("era_facet") is not None:
                         new_photo.date_text = elem.find("era_facet").text
                     opener = urllib2.build_opener()
                     opener.addheaders = [("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36")]
                     img_response = opener.open(elem.find("image_links").text)
                     new_photo.image.save("finna.jpg", ContentFile(img_response.read()))
                     new_photo.save()
                     self.album.photos.add(new_photo)
                 else:
                     problem_children.append(elem.find("title").text)
     print problem_children
 def handle(self, *args, **options):
     translation.activate('en')
     set_id = '72157629276386494'
     page = 1
     set_url = 'https://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=' + FLICKR_API_KEY + '&photoset_id=' + set_id + '&extras=license,owner_name&format=json&nojsoncallback=1&page=' + str(page)
     # https://farm{farm-id}.staticflickr.com/{server-id}/{id}_{o-secret}_o.(jpg|gif|png)
     image_url_template = 'https://farm%s.staticflickr.com/%s/%s_%s_b.jpg'
     # https://www.flickr.com/photos/{user-id}/{photo-id}
     reference_url_template = 'https://www.flickr.com/photos/%s/%s'
     request = urllib2.Request(set_url)
     response = urllib2.urlopen(request)
     data = response.read()
     data = json.loads(data)
     source = Source.objects.filter(description='Flickr').first()
     if not source:
         source = Source(
             name='Flickr',
             description='Flickr'
         )
         source.save()
     album = CatAlbum.objects.get(pk=15)
     for photo in data['photoset']['photo']:
         if not self._resource_already_exists(photo['id']):
             new_photo = CatPhoto(
                 source=source,
                 source_url=(reference_url_template % ('swedish_heritage_board', photo['id'])),
                 source_key=photo['id'],
                 title=photo['title'],
                 author='Berit Wallenberg'
             )
             try:
                 image_url = image_url_template % (photo['farm'], photo['server'], photo['id'], photo['secret'])
                 opener = urllib2.build_opener()
                 opener.addheaders = [("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36")]
                 img_response = opener.open(image_url)
                 new_photo.image.save("ceric.jpg", ContentFile(img_response.read()))
                 new_photo.save()
                 album.photos.add(new_photo)
             except:
                 print "Problem loading image %s" % photo['id']
                 continue
 def handle(self, *args, **options):
     activate("en")
     parser = etree.XMLParser(encoding="utf-8", recover=True)
     tree = etree.parse(
         ABSOLUTE_PROJECT_ROOT + "/project/sift/management/commands/early-photographically-illustrated-books.xml",
         parser,
     )
     album = CatAlbum.objects.get(pk=11)
     source = Source.objects.get(pk=59)
     opener = urllib2.build_opener()
     opener.addheaders = [
         (
             "User-Agent",
             "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36",
         )
     ]
     count = 0
     for item in tree.getroot().iterchildren("item"):
         count += 1
         if count > 144:
             cp = CatPhoto(
                 title=item.find("title").text.strip(),
                 author=item.find("item_creator").text.strip(),
                 source=source,
                 source_url=item.find("url").text.strip(),
                 source_key=item.find("uid").text.strip(),
             )
             try:
                 img_response = opener.open(item.find("full_image_url").text)
                 cp.image.save("bl.jpg", ContentFile(img_response.read()))
                 cp.save()
                 album.photos.add(cp)
             except urllib2.HTTPError:
                 print "Not found"
                 print item.find("full_image_url").text
         print count
     album.save()
Beispiel #4
0
def cat_curator_upload_handler(request):
    profile = request.get_user().catprofile

    curator_album_select_form = CatTaggerAlbumSelectionForm(request.POST)
    curator_album_create_form = CatCuratorAlbumAddForm(request.POST)

    selection_json = request.POST.get('selection') or None
    selection = None
    if selection_json is not None:
        # Query again to block p**n
        parsed_selection = loads(selection_json)
        ids = [k for k, v in parsed_selection.iteritems()]
        response = _curator_get_records_by_ids(ids)
        parsed_response = loads(response.text)['result']
        parsed_kv = {}
        for each in parsed_response:
            parsed_kv[each['id']] = each
        for k, v in parsed_selection.iteritems():
            for sk, sv in parsed_kv[k].iteritems():
                parsed_selection[k][sk] = sv
        selection = parsed_selection

    ret = {
        'photos': {}
    }

    if selection and len(selection) > 0 and profile is not None \
            and (curator_album_select_form.is_valid() or curator_album_create_form.is_valid()):
        if curator_album_select_form.is_valid():
            album = curator_album_select_form.cleaned_data['album']
        else:
            album = CatAlbum(
                title=curator_album_create_form.cleaned_data['title'],
                subtitle=curator_album_create_form.cleaned_data['subtitle'],
            )
            album.save()
        ret['album_id'] = album.id
        for k, v in selection.iteritems():
            upload_form = CatCuratorPhotoUploadForm(v)
            if upload_form.is_valid():
                if upload_form.cleaned_data['institution']:
                    upload_form.cleaned_data['institution'] = upload_form.cleaned_data['institution'].split(',')[0]
                    source = Source.objects.filter(description=upload_form.cleaned_data['institution']).first()
                    if not source:
                        source = Source(
                            name=upload_form.cleaned_data['institution'],
                            description=upload_form.cleaned_data['institution']
                        )
                        source.save()
                else:
                    source = Source.objects.get(name='AJP')
                if upload_form.cleaned_data['id'] and upload_form.cleaned_data['id'] != '':
                    incoming_muis_id = upload_form.cleaned_data['id']
                    if '_' in incoming_muis_id:
                        muis_id = incoming_muis_id.split('_')[0]
                        muis_media_id = incoming_muis_id.split('_')[1]
                    else:
                        muis_id = incoming_muis_id
                        muis_media_id = None
                    if muis_media_id:
                        existing_photo = CatPhoto.objects.filter(source=source, muis_id=muis_id,
                                                                 muis_media_id=muis_media_id).first()
                    else:
                        existing_photo = CatPhoto.objects.filter(source=source, muis_id=muis_id).first()
                    if not existing_photo:
                        new_photo = None
                        try:
                            new_photo = CatPhoto(
                                title=upload_form.cleaned_data['title'].rstrip().encode('utf-8'),
                                author=upload_form.cleaned_data['creators'].encode('utf-8'),
                                source=source,
                                source_key=upload_form.cleaned_data['identifyingNumber'],
                                source_url=upload_form.cleaned_data['urlToRecord'],
                                muis_id=upload_form.cleaned_data['id'].split('_')[0],
                                muis_media_id=upload_form.cleaned_data['id'].split('_')[1] if len(upload_form.cleaned_data['id'].split('_')) > 1 else None,
                                flip=upload_form.cleaned_data['flip'],
                                invert=upload_form.cleaned_data['invert'],
                                stereo=upload_form.cleaned_data['stereo'],
                                rotated=upload_form.cleaned_data['rotated']
                            )
                            new_photo.save()
                            opener = urllib2.build_opener()
                            opener.addheaders = [('User-Agent', 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/34.0.1847.137 Safari/537.36')]
                            img_response = opener.open(upload_form.cleaned_data['imageUrl'])
                            new_photo.image.save('cat-muis.jpg', ContentFile(img_response.read()))
                            photo_path = MEDIA_ROOT + '/' + str(new_photo.image)
                            if new_photo.invert:
                                img = Image.open(photo_path)
                                inverted_grayscale_image = ImageOps.invert(img).convert('L')
                                inverted_grayscale_image.save(photo_path)
                            if new_photo.rotated > 0:
                                img = Image.open(photo_path)
                                rot = img.rotate(new_photo.rotated, expand=1)
                                rot.save(photo_path)
                                new_photo.width = rot.size[0]
                                new_photo.height = rot.size[1]
                                new_photo.save()
                            if new_photo.flip:
                                img = Image.open(photo_path)
                                flipped = img.transpose(Image.FLIP_LEFT_RIGHT)
                                flipped.save(photo_path)
                            ret['photos'][k] = {}
                            ret['photos'][k]['message'] = _('OK')
                            new_photo.save()
                            if album:
                                album.photos.add(new_photo)
                            ret['photos'][k]['success'] = True
                        except:
                            if new_photo:
                                new_photo.image.delete()
                                new_photo.delete()
                            ret['photos'][k] = {}
                            ret['photos'][k]['error'] = _('Error uploading file')
                    else:
                        if album:
                            album.photos.add(existing_photo)
                        ret['photos'][k] = {}
                        ret['photos'][k]['success'] = True
                        ret['photos'][k]['message'] = _('Photo already exists in Sift')
        if album:
            album.save()
    else:
        if not selection or len(selection) == 0:
            error = _('Please add photos to your album')
        else:
            error = _('Not enough data submitted')
        ret = {
            'error': error
        }

    return HttpResponse(dumps(ret), content_type='application/json')