def embed(url=None): consumer = oembed.OEmbedConsumer() endpoint = oembed.OEmbedEndpoint('http://www.youtube.com/oembed', [ 'http://*.youtube.com/*', 'https://*.youtube.com/*', 'http://*.youtube-nocookie.com/*', 'https://*.youtube-nocookie.com/*', ]) consumer.addEndpoint(endpoint) try: html = consumer.embed(url)['html'] except oembed.OEmbedNoEndpoint: html = None if html: # youtube has a trailing ")" at the moment html = html.rstrip(')') # convert iframe src from http to https, to avoid mixed security blocking when used on an https page # and convert to youtube-nocookie.com html = BeautifulSoup(html) embed_url = html.find('iframe').get('src') if embed_url: embed_url = urlparse(embed_url) if embed_url.scheme == 'http': embed_url = urlunparse(['https'] + list(embed_url[1:])) else: embed_url = embed_url.geturl() embed_url = embed_url.replace('www.youtube.com', 'www.youtube-nocookie.com') html.find('iframe')['src'] = embed_url return jinja2.Markup('<p>%s</p>' % html) return '[[embed url=%s]]' % url
def regex_deviantart(self, event): damatch = re.compile( '((?:(?:https?|ftp|file)://|www\.|ftp\.)(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[-A-Z0-9+&@#/%=~_|$?!:,.])*(?:\([-A-Z0-9+&@#/%=~_|$?!:,.]*\)|[A-Z0-9+&@#/%=~_|$]))', re.I) damatches = damatch.findall(event.message) for x in damatches: try: consumer = oembed.OEmbedConsumer() endpoint = oembed.OEmbedEndpoint( 'http://backend.deviantart.com/oembed', [ 'http://*.deviantart.com/art/*', 'http://fav.me/*', 'http://sta.sh/*', 'http://*.deviantart.com/*#/d*' ]) consumer.addEndpoint(endpoint) response = consumer.embed(x).getData() out = [] if 'title' in response: out.append("Title: {}".format(response[u'title'])) if 'author_name' in response: out.append("Artist: {}".format(response[u'author_name'])) if 'rating' in response: out.append("Rating: {}".format(response[u'rating'])) if 'url' in response: out.append("Direct Url: {}".format(response[u'url'])) self.send_message(event.respond, " | ".join(out).encode( 'utf-8', 'replace')) except oembed.OEmbedNoEndpoint: pass except urllib2.HTTPError: pass
def initialize_consumer(self): if self.consumer is None: consumer = oembed.OEmbedConsumer() _enpoints = load_all_endpoints() for endpoint in _enpoints: consumer.addEndpoint(endpoint) self.consumer = consumer
def get_insta_user(short_link, debug=1): """ Get instagram userid from a link posted on twitter """ print "Fetching instagram user. " try: response = urllib2.urlopen( short_link) # Some shortened url, eg: http://t.co/z8P2xNzT8k url_destination_https = response.url url_destination = url_destination_https.replace('https', 'http', 1) # from link get the media_id consumer = oembed.OEmbedConsumer() endpoint = oembed.OEmbedEndpoint( 'https://api.instagram.com/oembed?url=', ['http://instagr.am/p/*']) consumer.addEndpoint(endpoint) media_id_code = re.split('/', url_destination)[-2] url_destination = 'http://instagr.am/p/' + media_id_code response = consumer.embed(url_destination) media_id = response['media_id'] api = InstagramAPI(client_id=iconf.client_id, client_secret=iconf.client_secret) except: if debug: print 'Unable to find picture from link.' return 'null' try: media = api.media(media_id) return [media.user.id, media.user.username] except: if debug: print 'Unable to fetch instagram ID - most likely private user' return 'null'
def initialize_consumer(self): if self.consumer is None: consumer = oembed.OEmbedConsumer() consumer.addEndpoint(self.endpoint) self.consumer = consumer
def oembed_consumer(self): consumer = oembed.OEmbedConsumer() endpoint = oembed.OEmbedEndpoint( self.oembed_endpoint, self.oembed_schema, ) consumer.addEndpoint(endpoint) return consumer
def prepare_oembed_consumer(self): allowed_endpoints = self.getConfig('allowed_endpoints', AVAILABLE_ENDPOINTS) consumer = oembed.OEmbedConsumer() [consumer.addEndpoint(v) for k,v in ENDPOINTS.items() if k in allowed_endpoints] return consumer
def initialize_consumer(self): if self.consumer is None: consumer = oembed.OEmbedConsumer() _enpoints = endpoints.load_all_endpoints(embedly_apikey=self.embedly_apikey) for endpoint in _enpoints: consumer.addEndpoint(endpoint) self.consumer = consumer
def testBrokenEndpoint(self): consumer = oembed.OEmbedConsumer() ep = oembed.OEmbedEndpoint('http://localhost') ep.addUrlScheme('http://localhost/*') consumer.addEndpoint(ep) self.assertRaises(urllib2.URLError, consumer.embed, \ 'http://localhost/test')
def prepare_oembed_consumer(self): allowed_endpoints = self.getConfig('allowed_endpoints', DEFAULT_ENDPOINTS) consumer = oembed.OEmbedConsumer() if allowed_endpoints: for endpoint in allowed_endpoints: consumer.addEndpoint(endpoint) return consumer
def embed(url=None): consumer = oembed.OEmbedConsumer() endpoint = oembed.OEmbedEndpoint( 'http://www.youtube.com/oembed', ['http://*.youtube.com/*', 'https://*.youtube.com/*']) consumer.addEndpoint(endpoint) try: return jinja2.Markup('<div class="grid-20">%s</div>' % consumer.embed(url)['html']) except oembed.OEmbedNoEndpoint as e: return '[[embed url=%s]]' % url
def get_oembed(url): """ Get oembed object, convert it to dict and get products list. :param url: string, page URL :return: list, list of products """ consumer = oembed.OEmbedConsumer() endpoint = oembed.OEmbedEndpoint(url + '.oembed', ['https://suzyshier.com/*']) consumer.addEndpoint(endpoint) response = consumer.embed(url) return response.getData()['products']
def testResponses(self): consumer = oembed.OEmbedConsumer() ep = oembed.OEmbedEndpoint('http://www.flickr.com/services/oembed') ep.addUrlScheme('http://*.flickr.com/*') consumer.addEndpoint(ep) #json resp = consumer.embed( 'http://www.flickr.com/photos/wizardbt/2584979382/', format='json') #xml resp = consumer.embed( 'http://www.flickr.com/photos/wizardbt/2584979382/', format='xml') #resource not found self.assertRaises(urllib2.HTTPError, consumer.embed, \ 'http://www.flickr.com/photos/wizardbt/', \ format='json')
def testEmbed(self): consumer = oembed.OEmbedConsumer() ep1 = oembed.OEmbedEndpoint('http://www.flickr.com/services/oembed') ep1.addUrlScheme('http://*.flickr.com/*') ep2 = oembed.OEmbedEndpoint( 'http://api.pownce.com/2.1/oembed.{format}') ep2.addUrlScheme('http://*.pownce.com/*') consumer.addEndpoint(ep1) consumer.addEndpoint(ep2) #invalid format self.assertRaises(oembed.OEmbedInvalidRequest, consumer.embed, \ 'http://www.flickr.com/photos/wizardbt/2584979382/', \ format='text') #no matching endpoint for the url self.assertRaises(oembed.OEmbedNoEndpoint, consumer.embed, \ 'http://google.com/123456')
def testGettersAndSetters(self): consumer = oembed.OEmbedConsumer() ep1 = oembed.OEmbedEndpoint('http://www.flickr.com/services/oembed') ep2 = oembed.OEmbedEndpoint( 'http://api.pownce.com/2.1/oembed.{format}') ep3 = oembed.OEmbedEndpoint('http://www.vimeo.com/api/oembed.{format}') #adding consumer.addEndpoint(ep1) consumer.addEndpoint(ep2) consumer.addEndpoint(ep3) self.assertEqual(len(consumer.getEndpoints()), 3) #removing one consumer.delEndpoint(ep2) self.assertEqual(len(consumer.getEndpoints()), 2) #clearing all! consumer.clearEndpoints() self.assertEqual(len(consumer.getEndpoints()), 0)
def embed(url=None): consumer = oembed.OEmbedConsumer() endpoint = oembed.OEmbedEndpoint('http://www.youtube.com/oembed', [str('http://*.youtube.com/*'), str('https://*.youtube.com/*'), str('http://*.youtube-nocookie.com/*'), str('https://*.youtube-nocookie.com/*'), ]) consumer.addEndpoint(endpoint) # workaround for https://github.com/abarmat/python-oembed/pull/9 not being implemented yet with socket_default_timeout(5): try: html = consumer.embed(url)['html'] except oembed.OEmbedNoEndpoint: html = None except six.moves.urllib.error.HTTPError as e: if e.code == 404: return 'Video not available' else: raise if html: # youtube has a trailing ")" at the moment html = html.rstrip(')') # convert iframe src from http to https, to avoid mixed security blocking when used on an https page # and convert to youtube-nocookie.com html = BeautifulSoup(html, 'html.parser') embed_url = html.find('iframe').get('src') if embed_url: embed_url = urlparse(embed_url) if embed_url.scheme == 'http': embed_url = urlunparse(['https'] + list(embed_url[1:])) else: embed_url = embed_url.geturl() embed_url = embed_url.replace('www.youtube.com', 'www.youtube-nocookie.com') html.find('iframe')['src'] = embed_url return jinja2.Markup('<p>%s</p>' % html) return '[[embed url=%s]]' % url
}, { "regex": "http://(?:www\\.)?scribd\\.com/.*", "endpoint": "http://www.scribd.com/services/oembed", "name": "Scribd" }, { "regex": "http://(?:www\\.)?blip\\.tv/.*", "endpoint": "http://blip.tv/oembed/", "name": "Blip.tv" }, { "regex": "http://.*", "endpoint": "http://oohembed.com/oohembed/", "name": "Catchall (OohEmbed)" }, ] _consumer = oembed.OEmbedConsumer() for d in config: _consumer.addEndpoint( oembed.OEmbedEndpoint(d['endpoint'], ['regex:%s' % d['regex']])) def oembed_expand(url, **opts): try: return _consumer.embed(url, **opts).getData() except (oembed.OEmbedError, urllib2.HTTPError): return None
def testNoEndpoints(self): consumer = oembed.OEmbedConsumer() self.assertRaises(oembed.OEmbedNoEndpoint, consumer.embed, \ 'http://www.flickr.com/photos/wizardbt/2584979382/')
def get_easy_variables(website_rules, url, settings): """Stuff that can be found without parsing the DOM -- its easy""" website_variables = {} website_variables["URL"] = { 'content': url.full_url, 'strength': 'high', 'type': 'attr' } website_variables["DOMAIN"] = { 'content': url.domain, 'strength': 'high', 'type': 'text' } website_variables["DOMAIN_CSS"] = { 'content': url.domain.replace('.', '_'), 'strength': 'high', 'type': 'text' } website_variables["PROTOCOL"] = { 'content': url.protocol, 'strength': 'high', 'type': 'text' } if '_template' in website_rules: website_variables['_template'] = website_rules['_template'] if '_urlregex' in website_rules: pattern = website_rules['_urlregex'] match = re.search(pattern, url.full_url) print(pattern, url.full_url) if match: for index, group in enumerate(match.groups()): if not group: continue key = "URLREGEX(" + str(index) + ")" website_variables[key] = {'content': group, 'type': 'text'} else: print("no match") return None if '_oembed' in website_rules: consumer = oembed.OEmbedConsumer() if '_oembed_schemes' in website_rules: schemes = website_rules['_oembed_schemes'] else: schemes = 'http://*/' + url.domain + '/*' endpoint = oembed.OEmbedEndpoint(website_rules['_oembed'], schemes) consumer.addEndpoint(endpoint) try: response = consumer.embed(url.full_url) except: response = None print('no oembed for URL:', url.full_url) if response: for key, value in response.getData().items(): website_variables[key] = { 'content': str(value), 'type': 'text', 'strength': 'high' } if 'width' in website_variables and 'height' in website_variables: try: height = int(website_variables['height']['content']) width = int(website_variables['width']['content']) except ValueError: # sometimes these are null pass else: prcnt_ratio = (height / width) * 100 prcnt_ratio = str(round(prcnt_ratio, 2)) website_variables['RESPONSIVE_PADDING'] = { 'content': 'padding-bottom: ' + prcnt_ratio + '%;', 'type': 'text', 'strength': 'high' } else: # no response so remove overwritting template if '_template' in website_variables: del website_variables["_template"] del website_rules["_template"] return website_variables
def _VIDEO_COVER_IMAGE(conference, eid, type='front', thumb=False): return None VIDEO_COVER_IMAGE = getattr(settings, 'CONFERENCE_VIDEO_COVER_IMAGE', _VIDEO_COVER_IMAGE) _OEMBED_PROVIDERS = (('https://www.youtube.com/oembed', ('https://www.youtube.com/*', 'http://www.youtube.com/*')), ('http://vimeo.com/api/oembed.json', ('http://vimeo.com/*', 'https://vimeo.com/*', 'http://vimeo.com/groups/*/videos/*', 'https://vimeo.com/groups/*/videos/*')), ('http://lab.viddler.com/services/oembed/', ('http://*.viddler.com/*', ))) OEMBED_PROVIDERS = getattr(settings, 'CONFERENCE_OEMBED_PROVIDERS', _OEMBED_PROVIDERS) import oembed OEMBED_CONSUMER = oembed.OEmbedConsumer() for p, urls in OEMBED_PROVIDERS: endpoint = oembed.OEmbedEndpoint(p, urls) OEMBED_CONSUMER.addEndpoint(endpoint) OEMBED_URL_FIX = ((r'https?://vimeopro.com.*/(\d+)$', r'https://vimeo.com/\1'), )
def setUp(self): self.consumer = oembed.OEmbedConsumer() self.endpoint = oembed.OEmbedEndpoint(LIVE, URLS) self.consumer.addEndpoint(self.endpoint) self.maxDiff = None
def setUp(self): self.consumer = oembed.OEmbedConsumer() self.endpoint = oembed.OEmbedEndpoint(LOCAL, URLS) self.consumer.addEndpoint(self.endpoint)