def get_artwork(self, **kwargs):

        import random
        import requests

        try:
            response = requests.get(self.api_url, timeout=1, params=self.parameters).json()
        except:
            return False, 'victoriaalbertmuseum' # If an error occurs here, the API is most likely no longer accepting requests

        response = access(response, ['records'])

        artwork = {}

        available_choices = range(len(response))
        image_url = None

        while not image_url:
            choice = random.choice(available_choices)
            image_url = access(response, [choice, 'fields', 'primary_image_id'])
            if image_url:
                artwork['image_url'] = "http://media.vam.ac.uk/media/thira/collection_images/{0}/{1}.jpg".format(image_url[:6], image_url)
                break
            else:
                available_choices.remove(choice)
        else:
            return False, 'victoriaalbertmuseum' # If none of the choices had a valid image

        artwork['from_api'] = 'Victoria and Albert Museum'
        artwork['external_id'] = access(response, [choice, 'pk'])
        if not artwork['external_id']:
            return False, 'victoriaalbertmuseum'

        artwork['title'] = access(response, [choice, 'fields', 'title'])

        if not artwork['title']:
            artwork['title'] = 'Untitled'

        # artwork['external_url'] = access(response, [])
        artwork['source'] = 'Victoria and Albert Museum'
        artwork['museum'] = 'Victoria and Albert Museum'
        artwork['artist'] = access(response, [choice, 'fields', 'artist'])
        artwork['art_type'] = access(response, [choice, 'fields', 'object'])
        # artwork['description'] = access(response, [])
        artwork['date'] = access(response, [choice, 'fields', 'date_text'])

        # print artwork

        try:
            artwork_model = self.save_artwork_details(**artwork)
        except:
            return False, 'victoriaalbertmuseum'

        return artwork_model, 'victoriaalbertmuseum'
Пример #2
0
    def get_artwork(self, **kwargs):

        import random
        import requests
        from requests.auth import HTTPBasicAuth

        from apps.mash.museum_apis.art_credentials import luce_username, luce_password

        artwork_id = random.randint(self.random_boundaries[0], self.random_boundaries[1])
        self.parameters['start'] = artwork_id

        try:
            response = requests.get(self.api_url, timeout=0.1, params=self.parameters, auth=HTTPBasicAuth(luce_username, luce_password)).json()
        except:
            return False, 'luce' # If an error occurs here, the API is most likely no longer accepting requests

        artwork = {}

        artwork['from_api'] = 'luce'
        artwork['external_id'] = artwork_id

        artwork['image_url'] = access(response, ['response','docs',0,'descriptiveNonRepeating','online_media','media',0,'content'])
        artwork['title'] = access(response, ['response', 'docs', 0, 'descriptiveNonRepeating', 'title', 'content'])
        artwork['external_url'] = access(response, ['response', 'docs', 0, 'descriptiveNonRepeating', 'record_link'])
        artwork['source'] = access(response, ['response', 'docs', 0, 'freetext', 'dataSource', 0, 'content'])
        artwork['artist'] = access(response, ['response', 'docs', 0, 'freetext', 'name', 0, 'content'])
        artwork['art_type'] = access(response, ['response', 'docs', 0, 'freetext', 'objectType', 0, 'content'])
        artwork['description'] = access(response, ['response', 'docs', 0, 'freetext', 'physicalDescription', 0, 'content'])
        artwork['date'] = access(response, ['response', 'docs', 0, 'freetext', 'date', 0, 'content'])

        try:
            artwork_model = self.save_artwork_details(**artwork)
        except:
            return False, 'luce'

        # Sample model instance
        # {'created_at': datetime.datetime(2014, 3, 31, 2, 18, 46, 161577, tzinfo=<UTC>), 'art_type': u'Graphic Arts-Print', 'description': u'lithograph on paper', 'artist': u'Louis Silverstein, born New York City 1919-1994', 'museum': u'', 'title': u'Crucible, from the American Absract Artists 50th Anniversary Print Portfolio', '_state': <django.db.models.base.ModelState object at 0x1041cde90>, 'from_api': u'luce', 'updated_at': datetime.datetime(2014, 3, 31, 2, 18, 46, 161608, tzinfo=<UTC>), 'source': u'Smithsonian American Art Museum', 'image_url': u'http://americanart.si.edu/images/1987/1987.52.39_1a.jpg', 'date': u'1987', 'external_id': u'15515', 'id': 1, 'external_url': u'http://americanart.si.edu/collections/search/artwork/?id=22412'}

        return artwork_model, 'luce'
Пример #3
0
    def get_artwork(self, **kwargs):

        import random
        import requests
        
        try:
            response = requests.get(self.api_url, timeout=15, params=self.parameters).json()
        except:
            return False, 'brooklyn' # If an error occurs here, the API is most likely no longer accepting requests

        artwork = {}

        artwork['from_api'] = 'brooklyn'
        available_choices = range(len(access(response, ['response', 'resultset', 'items'])))

        if len(available_choices) < 1:
            return False, 'brooklyn'

        image_url = None

        while not image_url:
            choice = random.choice(available_choices)

            # Determine the largest available image
            image_choices = access(response, ['response', 'resultset', 'items', choice, 'images'])
            max_available_size = -1
            if image_choices:
                for image_key, image_values in image_choices.items():
                    
                    # Skip non-image key/value pairs like results_limit and total
                    try:
                        int(image_key)
                    except:
                        continue 

                    uri = access(image_values, ['uri'])
                    
                    if not uri:
                        continue

                    available_size = uri.index('/size')
                    if available_size > 0:
                        available_size = int(uri[available_size + 5])

                    if available_size > max_available_size:
                        max_available_size = available_size
                        image_url = uri

                if image_url and max_available_size > 2: # more consistently give larger images
                    artwork['image_url'] = image_url
                    break
                else:
                    available_choices.remove(choice)
        else:
            # Last resort, if only small images are available in the resultset
            if image_url and max_available_size <= 2:

                # Try to get size 4 even if it's not given
                try:
                    r = requests.get(image_url.replace('/size{0}'.format(max_available_size), '/size4'))
                    if r.status_code == 200:
                        artwork['image_url'] = image_url.replace('/size{0}'.format(max_available_size), '/size4')
                    else:
                        artwork['image_url'] = image_url
                except:
                    artwork['image_url'] = image_url
            else:
                return False, 'brooklyn'

        artwork['external_id'] = access(response, ['response', 'resultset', 'items', choice, 'id'])

        if not artwork['external_id']:
            return False, 'brooklyn'

        artwork['title'] = access(response, ['response', 'resultset', 'items', choice, 'title'])
        artwork['external_url'] = access(response, ['response', 'resultset', 'items', choice, 'uri'])
        artwork['source'] = 'Brooklyn Museum'
        
        artwork['art_type'] = access(response, ['response', 'resultset', 'items', choice, 'medium'])
        
        artwork['description'] = access(response, ['response', 'resultset', 'items', choice, 'description'])
        if not artwork['description']:
            artwork['description'] = access(response, ['response', 'resultset', 'items', choice, 'label'])

        artwork['date'] = access(response, ['response', 'resultset', 'items', choice, 'object_date'])

        # Brooklyn API has multiple artists
        all_artists = access(response, ['response', 'resultset', 'items', choice, 'artists'])
        artwork['artist'] = []
        if all_artists:
            for artist in all_artists:
                artwork['artist'].append(artist['name'])

            artwork['artist'] = ', '.join(artwork['artist'])
        else:
            artwork['artist'] = ''

        # Brooklyn API descriptions sometimes have \r\n and HTML characters in them.  Let's remove them

        if artwork['description']:
            artwork['description'] = strip_tags(artwork['description'])
            artwork['description'] = artwork['description'].replace('\\r\\n', '')
        else:
            artwork['description'] = ''

        # Brooklyn API returns lots of escaped characters using backslashes.  Let's remove those.
        for key, value in artwork.items():
            if value:
                artwork[key] = value.replace('\\', '')

        try:
            artwork_model = self.save_artwork_details(**artwork)
        except:
            return False, 'brooklyn'

        # Sample model instance
        # {'created_at': datetime.datetime(2014, 3, 31, 2, 18, 46, 161577, tzinfo=<UTC>), 'art_type': u'Graphic Arts-Print', 'description': u'lithograph on paper', 'artist': u'Louis Silverstein, born New York City 1919-1994', 'museum': u'', 'title': u'Crucible, from the American Absract Artists 50th Anniversary Print Portfolio', '_state': <django.db.models.base.ModelState object at 0x1041cde90>, 'from_api': u'luce', 'updated_at': datetime.datetime(2014, 3, 31, 2, 18, 46, 161608, tzinfo=<UTC>), 'source': u'Smithsonian American Art Museum', 'image_url': u'http://americanart.si.edu/images/1987/1987.52.39_1a.jpg', 'date': u'1987', 'external_id': u'15515', 'id': 1, 'external_url': u'http://americanart.si.edu/collections/search/artwork/?id=22412'}

        return artwork_model, 'brooklyn'
Пример #4
0
    def get_artwork(self, **kwargs):

        import random
        import requests

        try:
            response = requests.get(self.api_url, timeout=5, params=self.parameters).json()
        except:
            return (
                False,
                "waltersmuseum",
            )  # If an error occurs here, the API is most likely no longer accepting requests

        response = access(response, ["Items"])

        artwork = {}

        available_choices = range(len(response))
        image_url = None

        while not image_url:

            choice = random.choice(available_choices)

            for image_size in ("Raw", "Large", "Medium", "Small"):
                image_url = access(response, [choice, "PrimaryImage", image_size])

                if image_url:
                    artwork["image_url"] = image_url
                    break
            else:
                available_choices.remove(choice)

            if image_url:
                break
        else:
            return False, "waltersmuseum"  # If none of the choices had a valid image

        artwork["from_api"] = "Walters Museum"

        artwork["external_id"] = access(response, [choice, "ObjectID"])
        if not artwork["external_id"]:
            return False, "waltersmuseum"

        artwork["title"] = access(response, [choice, "Title"])

        if not artwork["title"] or artwork["title"] == "null":
            artwork["title"] = "Untitled"

        artwork["external_url"] = access(response, [choice, "ResourceURL"])
        artwork["source"] = "Walters Museum"
        artwork["museum"] = "Walters Museum"
        artwork["artist"] = access(response, [choice, "Creator"])
        artwork["art_type"] = access(response, [choice, "Medium"])

        artwork["description"] = access(response, [choice, "Description"])
        if not artwork["description"] or artwork["description"] == "null":
            artwork["description"] = None

        artwork["date"] = access(response, [choice, "DateText"])

        try:
            artwork_model = self.save_artwork_details(**artwork)
        except:
            return False, "waltersmuseum"

        return artwork_model, "waltersmuseum"