def __init__(self, name): """ Initialize a concept by a name, which can be an alias (it does not have to be the canonical name). This specified name will be saved as an Alias. A canonical name will be looked for; if one is found it will be used as the slug for this Concept. """ self.aliases.append(Alias(name)) # Try to get a canonical URI # and derive the slug from that. self.uri = knowledge.uri_for_name(name) if self.uri: self.slug = self.uri.split('/')[-1] k = knowledge.knowledge_for(uri=self.uri, fallback=True) self.commonness = knowledge.commonness_for_uri(self.uri) self.summary = k['summary'] self.name = k['name'] # Download the image. if k['image'] is not None: ext = splitext(k['image'])[-1].lower() self.image = storage.save_from_url( k['image'], '{0}{1}'.format(hash(self.slug), ext)) # If no URI was found, # generate our own slug. # Note: A problem here is that it assumes that # this particular name is the canonical one, # and that we don't collect any information for it. else: self.slug = slugify(name)
def __init__(self, name): """ Initialize a concept by a name, which can be an alias (it does not have to be the canonical name). This specified name will be saved as an Alias. A canonical name will be looked for; if one is found it will be used as the slug for this Concept. """ self.aliases.append(Alias(name)) # Try to get a canonical URI # and derive the slug from that. self.uri = knowledge.uri_for_name(name) if self.uri: self.slug = self.uri.split('/')[-1] k = knowledge.knowledge_for(uri=self.uri, fallback=True) self.commonness = knowledge.commonness_for_uri(self.uri) self.summary = k['summary'] self.name = k['name'] # Download the image. if k['image'] is not None: ext = splitext(k['image'])[-1].lower() self.image = storage.save_from_url(k['image'], '{0}{1}'.format(hash(self.slug), ext)) # If no URI was found, # generate our own slug. # Note: A problem here is that it assumes that # this particular name is the canonical one, # and that we don't collect any information for it. else: self.slug = slugify(name)
def test_save_from_url(self): # Mock out S3/Boto. key = MagicMock() self.create_patch('argos.util.storage.S3Connection', return_value=MagicMock()) self.create_patch('argos.util.storage.Key', return_value=key) # Mock response for downloading. image_data = open('tests/data/image.jpg', 'rb').read() mock_response = BytesIO(image_data) mock_response.headers = { 'Accept-Ranges': 'bytes', 'Last-Modified': 'Wed, 05 Sep 2013 08:53:26 GMT', 'Content-Length': str(len(image_data)) } mock_urlopen = self.create_patch('urllib.request.urlopen', return_value=mock_response) storage.save_from_url('http://someurl.com/image.jpg', 'downloaded.jpg') # Get the bytes data that the key was given. called_data = key.set_contents_from_file.call_args_list[0][0][0].getvalue() self.assertEqual(called_data, image_data) self.assertEqual(key.key, 'downloaded.jpg')
def extract_image(entry_data, filename): """ Extracts and saves a representative image for the entry. This preserves the file extension of the remote file, preferencing it over the one specified by the user. """ image_url = None if entry_data.top_image: remote_image_url = entry_data.top_image.src # Occasionally this url comes back as empty. if remote_image_url: ext = splitext(remote_image_url)[-1].lower() image_url = storage.save_from_url(remote_image_url, '{0}{1}'.format(filename, ext)) return image_url