def _get(self, version, method, url_or_urls, **kwargs): """ _get makes the actual call to api.embed.ly """ if not url_or_urls: raise ValueError('%s requires a url or a list of urls given: %s' % (method.title(), url_or_urls)) # a flag we can use instead of calling isinstance() all the time multi = isinstance(url_or_urls, list) # throw an error early for too many URLs if multi and len(url_or_urls) > 20: raise ValueError('Embedly accepts only 20 urls at a time. Url ' 'Count:%s' % len(url_or_urls)) query = '' key = kwargs.get('key', self.key) # make sure that a key was set on the client or passed in if not key: raise ValueError('Requires a key. None given: %s' % key) kwargs['key'] = key query += urlencode(kwargs) if multi: query += '&urls=%s&' % ','.join( [quote(url) for url in url_or_urls]) else: query += '&url=%s' % quote(url_or_urls) url = 'http://api.embed.ly/%s/%s?%s' % (version, method, query) http = httplib2.Http(timeout=self.timeout) headers = {'User-Agent': self.user_agent, 'Connection': 'close'} resp, content = http.request(url, headers=headers) if resp['status'] == '200': data = json.loads(content.decode('utf-8')) if kwargs.get('raw', False): data['raw'] = content else: data = { 'type': 'error', 'error': True, 'error_code': int(resp['status']) } if multi: return map(lambda url, data: Url(data, method, url), url_or_urls, data) return Url(data, method, url_or_urls)
def test_model(self): data = { 'provider_url': 'http://www.google.com/', 'safe': True, 'description': 'Google', 'url': 'http://www.google.com/', 'type': 'html', 'object': {}, 'provider_display': 'www.google.com', 'author_name': None, 'favicon_url': 'http://www.google.com/favicon.ico', 'place': {}, 'author_url': None, 'images': [ {'url': 'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png', 'width': 275, 'height': 95}], 'title': 'Google', 'provider_name': 'Google', 'cache_age': 86400, 'embeds': [] } obj = Url(data, 'preview', 'http://original.url.com/') self.assertEqual(len(obj), 16) self.assertEqual(len(list(obj.values())), 16) self.assertEqual(len(list(obj.keys())), 16) self.assertEqual(len(list(obj.items())), 16) # check for expected data self.assertTrue('type' in list(obj.keys())) self.assertTrue('html' in list(obj.values())) self.assertEqual(obj['type'], 'html') self.assertEqual(obj.get('type'), 'html') self.assertEqual(obj.data['type'], 'html') self.assertEqual(obj.data.get('type'), 'html') # our special attrs shouldn't be in the data dict self.assertFalse('method' in list(obj.keys())) with self.assertRaises(KeyError): obj['method'] # attrs and data dict values should be separate self.assertEqual(obj.original_url, 'http://original.url.com/') obj.new_attr = 'attr value' obj['new_key'] = 'dict value' self.assertEqual(obj.new_attr, 'attr value') self.assertEqual(obj['new_key'], 'dict value')
def test_model(self): data = { u'provider_url': u'http://www.google.com/', u'safe': True, u'description': u'Google', u'original_url': u'http://google.com/', u'url': u'http://www.google.com/', u'type': u'html', u'object': {}, u'provider_display': u'www.google.com', u'author_name': None, u'favicon_url': u'http://www.google.com/favicon.ico', u'place': {}, u'author_url': None, u'images': [ {u'url': u'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png', u'width': 275, u'height': 95}], u'title': u'Google', u'provider_name': u'Google', u'cache_age': 86400, u'embeds': [] } obj = Url(data, 'preview', 'http://google.com/') self.assert_(len(obj) is 17) self.assert_(len(obj.values()) is 17) self.assert_(len(obj.keys()) is 17) self.assert_(len(obj.items()) is 17) self.assert_('type' in obj.keys()) self.assert_('html' in obj.values()) #Get the object self.assert_(obj.type == 'html') self.assert_(obj['type'] == 'html') self.assert_(obj.get('type') == 'html') #nope self.assert_(obj.nothing is None) obj.nothing = 'something' self.assert_(obj.nothing == 'something') obj['nothing'] = 'maybe' self.assert_(obj['nothing'] == 'maybe') del obj['nothing'] self.assert_(obj.nothing is None) #Deep Get attrs self.assert_(obj.images[0].width is 275) self.assert_(obj.images[0].nothing is None) self.assert_(obj.object.type is None)
def test_model(self): data = { 'provider_url': 'http://www.google.com/', 'safe': True, 'description': 'Google', 'url': 'http://www.google.com/', 'type': 'html', 'object': {}, 'provider_display': 'www.google.com', 'author_name': None, 'favicon_url': 'http://www.google.com/favicon.ico', 'place': {}, 'author_url': None, 'images': [ {'url': 'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png', 'width': 275, 'height': 95}], 'title': 'Google', 'provider_name': 'Google', 'cache_age': 86400, 'embeds': [] } obj = Url(data, 'preview', 'http://original.url.com/') self.assertEqual(len(obj), 16) self.assertEqual(len(obj.values()), 16) self.assertEqual(len(obj.keys()), 16) self.assertEqual(len(obj.items()), 16) # check for expected data self.assertTrue('type' in obj.keys()) self.assertTrue('html' in obj.values()) self.assertEqual(obj['type'], 'html') self.assertEqual(obj.get('type'), 'html') self.assertEqual(obj.data['type'], 'html') self.assertEqual(obj.data.get('type'), 'html') # our special attrs shouldn't be in the data dict self.assertFalse('method' in obj.keys()) with self.assertRaises(KeyError): obj['method'] # attrs and data dict values should be separate self.assertEqual(obj.original_url, 'http://original.url.com/') obj.new_attr = 'attr value' obj['new_key'] = 'dict value' self.assertEqual(obj.new_attr, 'attr value') self.assertEqual(obj['new_key'], 'dict value')
def test_model(self): data = { u"provider_url": u"http://www.google.com/", u"safe": True, u"description": u"Google", u"original_url": u"http://google.com/", u"url": u"http://www.google.com/", u"type": u"html", u"object": {}, u"provider_display": u"www.google.com", u"author_name": None, u"favicon_url": u"http://www.google.com/favicon.ico", u"place": {}, u"author_url": None, u"images": [ {u"url": u"http://www.google.com/intl/en_ALL/images/srpr/logo1w.png", u"width": 275, u"height": 95} ], u"title": u"Google", u"provider_name": u"Google", u"cache_age": 86400, u"embeds": [], } obj = Url(data, "preview", "http://google.com/") self.assert_(len(obj) is 17) self.assert_(len(obj.values()) is 17) self.assert_(len(obj.keys()) is 17) self.assert_(len(obj.items()) is 17) self.assert_("type" in obj.keys()) self.assert_("html" in obj.values()) # Get the object self.assert_(obj.type == "html") self.assert_(obj["type"] == "html") self.assert_(obj.get("type") == "html") # nope self.assert_(obj.nothing is None) obj.nothing = "something" self.assert_(obj.nothing == "something") obj["nothing"] = "maybe" self.assert_(obj["nothing"] == "maybe") del obj["nothing"] self.assert_(obj.nothing is None) # Deep Get attrs self.assert_(obj.images[0].width is 275) self.assert_(obj.images[0].nothing is None) self.assert_(obj.object.type is None)
def test_model_data_can_serialize(self): obj = Url({'hash': {'key': 'value'}, 'none': None, 'empty': '', 'float': 1.234, 'int': 1, 'string': 'string', 'array': [0, -1]}) unserialzed = json.loads(json.dumps(obj.data)) self.assertDictEqual(obj.data, unserialzed)
def test_video_is_embedded(self, mock): mock.return_value = Url(data={'type': 'video', 'html': 'foo'}) video = self.test_category.videos.create( title='Foo Video', source_url='http://example.org/', ) call_embed() self.assertEqual(Video.objects.get(pk=video.pk).embed, 'foo')
def test_str_representation(self): unistr = 'I\xf1t\xebrn\xe2ti\xf4n\xe0liz\xe6tion' url = "http://test.com" obj = Url(method=unistr, original_url=url) if sys.version_info[0] == 2: self.assertTrue(unistr.encode('utf-8') in str(obj)) self.assertTrue(url.encode('utf-8') in str(obj)) else: self.assertTrue(unistr in str(obj)) self.assertTrue(url in str(obj))
def test_video_with_no_source_url_is_not_processed(self, mock): mock.return_value = Url(data={'type': 'video', 'html': 'foo'}) attrs = { 'title': 'Foo Video', } video = self.test_category.videos.create(**attrs) call_embed() video = Video.objects.get(pk=video.pk) self.assertEqual(video.embed, '')
def test_live_video_is_kept_as_is(self, mock): mock.return_value = Url(data={'type': 'video', 'html': ''}) attrs = { 'title': 'Foo Video', 'state': Video.STATE_LIVE, 'source_url': 'http://example.org/', } video = self.test_category.videos.create(**attrs) call_embed() self.assertEqual(video.state, Video.STATE_LIVE)
def test_video_is_not_processed_on_embedly_failure(self, mock): mock.return_value = Url(data={'type': 'error', 'html': 'foo'}) attrs = { 'title': 'Foo Video', 'source_url': 'http://example.org', } video = self.test_category.videos.create(**attrs) call_embed() video = Video.objects.get(pk=video.pk) self.assertEqual(video.embed, '')
def test_youtube_videos_with_broken_embed_code_are_fixed(self, mock): mock.return_value = Url(data={'type': 'video', 'html': 'fixed'}) attrs = { 'title': 'Foo Video', 'source_url': 'http://youtube.com/watch?v=AiN71', 'embed': 'broken', } video = self.test_category.videos.create(**attrs) call_embed() self.assertEqual(Video.objects.get(pk=video.pk).embed, 'fixed')
def test_youtube_videos_with_proper_embed_code_are_not_affected( self, mock): mock.return_value = Url(data={'type': 'video', 'html': 'fixed'}) attrs = { 'title': 'Foo Video', 'source_url': 'http://youtube.com/watch?v=AiN71', 'embed': '<iframe src="http://youtube.com/embed/AiN71"></iframe>', } video = self.test_category.videos.create(**attrs) call_embed() self.assertEqual( Video.objects.get(pk=video.pk).embed, '<iframe src="http://youtube.com/embed/AiN71"></iframe>')
def test_draft_video_is_kept_as_is(self, mock): mock.return_value = Url(data={'type': 'video', 'html': 'foo'}) attrs = { 'title': 'Foo Video', 'state': Video.STATE_DRAFT, 'source_url': 'http://example.org/', 'embed': '<iframe src="http://example.org/"></iframe>', } video = self.test_category.videos.create(**attrs) call_embed() # the embed field value hasnt been changed video = Video.objects.get(pk=video.pk) self.assertEqual(video.state, Video.STATE_DRAFT) self.assertEqual(video.embed, '<iframe src="http://example.org/"></iframe>')
def test_draft_video_is_updated_with_thumbnail_picture(self, mock): mock_data = { 'type': 'video', 'thumbnail_url': 'http://example.org/picture.jpg', 'html': 'foo', } mock.return_value = Url(data=mock_data) attrs = { 'title': 'Foo Video', 'source_url': 'http://example.org/', } video = self.test_category.videos.create(**attrs) call_embed() self.assertEqual( Video.objects.get(pk=video.pk).thumbnail_url, 'http://example.org/picture.jpg')
def test_model(self): data = { u'provider_url': u'http://www.google.com/', u'safe': True, u'description': u'Google', u'original_url': u'http://google.com/', u'url': u'http://www.google.com/', u'type': u'html', u'object': {}, u'provider_display': u'www.google.com', u'author_name': None, u'favicon_url': u'http://www.google.com/favicon.ico', u'place': {}, u'author_url': None, u'images': [{ u'url': u'http://www.google.com/intl/en_ALL/images/srpr/logo1w.png', u'width': 275, u'height': 95 }], u'title': u'Google', u'provider_name': u'Google', u'cache_age': 86400, u'embeds': [] } obj = Url(data, 'preview', 'http://google.com/') self.assert_(len(obj) is 17) self.assert_(len(obj.values()) is 17) self.assert_(len(obj.keys()) is 17) self.assert_(len(obj.items()) is 17) self.assert_('type' in obj.keys()) self.assert_('html' in obj.values()) #Get the object self.assert_(obj.type == 'html') self.assert_(obj['type'] == 'html') self.assert_(obj.get('type') == 'html') #nope self.assert_(obj.nothing is None) obj.nothing = 'something' self.assert_(obj.nothing == 'something') obj['nothing'] = 'maybe' self.assert_(obj['nothing'] == 'maybe') del obj['nothing'] self.assert_(obj.nothing is None) #Deep Get attrs self.assert_(obj.images[0].width is 275) self.assert_(obj.images[0].nothing is None) self.assert_(obj.object.type is None)