def test_maxheight(self, get, get_provider): get_provider.return_value = provider = mock.Mock() get.return_value = response = mock.Mock() response.ok = False provider.oembed_url.return_value = 'http://google.com/foo' with self.assertRaises(PyOembedException): oEmbed('foo', maxheight=100) provider.oembed_url.assert_called_once_with('foo') get.assert_called_once_with('http://google.com/foo?maxheight=100')
def test_parser_not_found(self, get, get_provider, get_parser): get_parser.return_value = None get_provider.return_value = provider = mock.Mock() get.return_value = response = mock.Mock() response.ok = True provider.oembed_url.return_value = 'http://google.com/foo' with self.assertRaises(ParserException): oEmbed('foo') provider.oembed_url.assert_called_once_with('foo') get.assert_called_once_with('http://google.com/foo')
def test_success(self, get, get_provider, get_parser, get_data_type): get_data_type.return_value = data_type = mock.Mock() get_parser.return_value = parser = mock.Mock() get_provider.return_value = provider = mock.Mock() get.return_value = response = mock.Mock() response.ok = True response.text = 'bola' parser.content_parse.return_value = {} provider.oembed_url.return_value = 'http://google.com/foo' oEmbed('foo') data_type.validate_data.assert_called_once_with({}) parser.content_parse.assert_called_once_with('bola') provider.oembed_url.assert_called_once_with('foo') get.assert_called_once_with('http://google.com/foo')
def test_data_type_not_found(self, get, get_provider, get_parser, get_data_type): get_data_type.return_value = None get_parser.return_value = parser = mock.Mock() get_provider.return_value = provider = mock.Mock() get.return_value = response = mock.Mock() response.ok = True response.text = 'bola' parser.content_parse.return_value = {} provider.oembed_url.return_value = 'http://google.com/foo' with self.assertRaises(DataTypeException): oEmbed('foo') parser.content_parse.assert_called_once_with('bola') provider.oembed_url.assert_called_once_with('foo') get.assert_called_once_with('http://google.com/foo')
def get_oembed_data(url: str, maxwidth: int = 640, maxheight: int = 480) -> Optional[UrlEmbedData]: try: data = oEmbed(url, maxwidth=maxwidth, maxheight=maxheight) except (PyOembedException, json.decoder.JSONDecodeError, requests.exceptions.ConnectionError): return None oembed_resource_type = data.get("type", "") image = data.get("url", data.get("image")) thumbnail = data.get("thumbnail_url") html = data.get("html", "") if oembed_resource_type == "photo" and image: return UrlOEmbedData( image=image, type="photo", title=data.get("title"), description=data.get("description"), ) if oembed_resource_type == "video" and html and thumbnail: return UrlOEmbedData( image=thumbnail, type="video", html=strip_cdata(html), title=data.get("title"), description=data.get("description"), ) # Otherwise, use the title/description from pyembed as the basis # for our other parsers return UrlEmbedData( title=data.get("title"), description=data.get("description"), )
def getOembed(): url = request.form["address"] address = oEmbed(url) if data['type'] == 'video': return render_template('video.html',data_list=data) elif data['type'] == 'link': return render_template('link.html',data_list=data)
def get_oembed_data( url: str, maxwidth: Optional[int] = 640, maxheight: Optional[int] = 480) -> Optional[Dict[str, Any]]: try: data = oEmbed(url, maxwidth=maxwidth, maxheight=maxheight) except PyOembedException: return None type_ = data.get('type', '') image = data.get('url', data.get('image')) thumbnail = data.get('thumbnail_url') html = data.pop('html', '') if type_ == 'photo' and image: data['image'] = image # Add a key to identify oembed metadata as opposed to other metadata data['oembed'] = True elif type_ == 'video' and html and thumbnail: data['html'] = get_safe_html(html) data['image'] = thumbnail # Add a key to identify oembed metadata as opposed to other metadata data['oembed'] = True return data
def get_oembed_data(url, maxwidth=640, maxheight=480): # type: (text_type, Optional[int], Optional[int]) -> Any try: data = oEmbed(url, maxwidth=maxwidth, maxheight=maxheight) except PyOembedException: return None data['image'] = data.get('thumbnail_url') return data
def get_oembed_data(url: str, maxwidth: Optional[int]=640, maxheight: Optional[int]=480) -> Optional[Dict[Any, Any]]: try: data = oEmbed(url, maxwidth=maxwidth, maxheight=maxheight) except PyOembedException: return None data['image'] = data.get('thumbnail_url') return data
def get_oembed_data( url: str, maxwidth: Optional[int] = 640, maxheight: Optional[int] = 480) -> Optional[Dict[str, Any]]: try: data = oEmbed(url, maxwidth=maxwidth, maxheight=maxheight) except PyOembedException: return None data['image'] = data.get('thumbnail_url') return data
def index(request): template = loader.get_template('index.html') idList = Set([]) f = open("mapletweets", "r") for line in f.readlines(): if line: # empty strings equate to false idList.add(line) f.close() htmlstrings = "" for tweetid in idList: url = 'https://twitter.com/MaplecroftRisk/status/' + str(tweetid) # handle does not matter, only id try: # fetch embeded version of tweet data = oEmbed(url) # lots of optimisation to do around this htmlstrings += data['html'] except PyOembedException, e: print 'An error was ocurred: %s' % e
def oembed_parse(data): """ Iterate through links and embed relavant data while leaving the content intact. """ soup = BeautifulSoup(data) links = soup.findAll('a') embeds = [] for link in links: try: oembed_data = oEmbed(link['href']) if oembed_data.get('html'): embeds.append('<p class="oembed">' + oembed_data['html'] + '</p>') except: pass if len(embeds): for oembed in embeds: data = data + oembed return data
def get_oembed_data(url: str, maxwidth: int = 640, maxheight: int = 480) -> Optional[Dict[str, Any]]: try: data = oEmbed(url, maxwidth=maxwidth, maxheight=maxheight) except (PyOembedException, json.decoder.JSONDecodeError, requests.exceptions.ConnectionError): return None oembed_resource_type = data.get("type", "") image = data.get("url", data.get("image")) thumbnail = data.get("thumbnail_url") html = data.pop("html", "") if oembed_resource_type == "photo" and image: return dict( oembed=True, image=image, type=oembed_resource_type, title=data.get("title"), description=data.get("description"), ) if oembed_resource_type == "video" and html and thumbnail: return dict( oembed=True, image=thumbnail, type=oembed_resource_type, html=strip_cdata(html), title=data.get("title"), description=data.get("description"), ) # Otherwise, start with just the embed type. return dict( type=oembed_resource_type, title=data.get("title"), description=data.get("description"), )
def get_oembed_data( url: str, maxwidth: Optional[int] = 640, maxheight: Optional[int] = 480) -> Optional[Dict[str, Any]]: try: data = oEmbed(url, maxwidth=maxwidth, maxheight=maxheight) except PyOembedException: return None oembed_resource_type = data.get('type', '') image = data.get('url', data.get('image')) thumbnail = data.get('thumbnail_url') html = data.pop('html', '') if oembed_resource_type == 'photo' and image: return dict( oembed=True, image=image, type=oembed_resource_type, title=data.get('title'), description=data.get('description'), ) if oembed_resource_type == 'video' and html and thumbnail: return dict( oembed=True, image=thumbnail, type=oembed_resource_type, html=strip_cdata(html), title=data.get('title'), description=data.get('description'), ) # Otherwise, start with just the embed type. return dict( type=oembed_resource_type, title=data.get('title'), description=data.get('description'), )
def run(self): if 'maxheight' not in self.options \ and 'OEMBED_MAXHEIGHT' in current_app.config: self.options['maxheight'] = current_app.config['OEMBED_MAXHEIGHT'] if 'maxwidth' not in self.options \ and 'OEMBED_MAXWIDTH' in current_app.config: self.options['maxwidth'] = current_app.config['OEMBED_MAXWIDTH'] try: data = oEmbed(directives.uri(self.arguments[0]), maxheight=self.options.get('maxheight'), maxwidth=self.options.get('maxwidth')) except PyOembedException, err: try: data = {'type': 'link'} with closing(urlopen(directives.uri(self.arguments[0]))) as fp: html = BeautifulSoup(fp.read()) try: data['title'] = html.head.title.text except: pass except Exception, err: raise self.error('Error in "%s" directive: %s' % (self.name, err))
from pyoembed import oEmbed, PyOembedException data = oEmbed('https://www.instagram.com/p/BUawPlPF_Rx/', maxwidth=640, maxheight=480) print(data)
def test_provider_not_found(self, get_provider): get_provider.return_value = None with self.assertRaises(ProviderException): oEmbed('foo')
# Twitter 'http://twitter.com/rafaelmartins/status/424199153364008960', 'http://www.twitter.com/rafaelmartins/status/424199153364008960', 'https://twitter.com/rafaelmartins/status/424199153364008960', 'https://www.twitter.com/rafaelmartins/status/424199153364008960', # Spotify 'http://open.spotify.com/track/0jkz56cwzYKs5xO1xwhZmr', 'http://play.spotify.com/track/6oTb6ZMymRaepsn1lQeOpa', 'https://open.spotify.com/track/0jkz56cwzYKs5xO1xwhZmr', 'https://play.spotify.com/track/6oTb6ZMymRaepsn1lQeOpa', ] if __name__ == '__main__': errors = 0 for i in URLS: try: print(i, '...', end=' ') sys.stdout.flush() oEmbed(i) except PyOembedException as e: print('fail') print(e) errors += 1 else: print('ok') print('Errors: %d' % errors) sys.exit(1 if errors > 0 else 0)
def get_oembed_html(url): if url not in oembed_cache: res = oEmbed(url, maxwidth=600) oembed_cache[url] = res['html'] return oembed_cache[url]
'http://i.imgur.com/fo06aLJ', # Twitter 'http://twitter.com/rafaelmartins/status/424199153364008960', 'http://www.twitter.com/rafaelmartins/status/424199153364008960', 'https://twitter.com/rafaelmartins/status/424199153364008960', 'https://www.twitter.com/rafaelmartins/status/424199153364008960', # Spotify 'http://open.spotify.com/track/0jkz56cwzYKs5xO1xwhZmr', 'http://play.spotify.com/track/6oTb6ZMymRaepsn1lQeOpa', 'https://open.spotify.com/track/0jkz56cwzYKs5xO1xwhZmr', 'https://play.spotify.com/track/6oTb6ZMymRaepsn1lQeOpa', ] if __name__ == '__main__': errors = 0 for i in URLS: try: print(i, '...', end=' ') sys.stdout.flush() oEmbed(i) except PyOembedException as e: print('fail') print(e) errors += 1 else: print('ok') print('Errors: %d' % errors) sys.exit(1 if errors > 0 else 0)