Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def request(self, url, **extra_params):
encoded_params = self.encode_params(url, **extra_params)
endpoint_url = self.endpoint
if '?' in endpoint_url:
endpoint_url = '%s&%s' % (endpoint_url.rstrip('&'), encoded_params)
else:
endpoint_url = '%s?%s' % (endpoint_url, encoded_params)
response = self.fetch(endpoint_url)
if response:
return self.handle_response(response, url)
else:
raise ProviderException('Error fetching "%s"' % endpoint_url)
class ProviderException(Exception):
pass
class ProviderNotFoundException(ProviderException):
pass
class InvalidResponseException(ProviderException):
pass
class ProviderException(Exception):
pass
class ProviderNotFoundException(ProviderException):
pass
class InvalidResponseException(ProviderException):
pass
extra = {}
if self.width:
extra['maxwidth'] = self.width
if self.height:
extra['maxheight'] = self.height
extra['autoplay'] = self.autoplay
extra['rel'] = self.show_related
extra['loop'] = self.loop
extra['title'] = False # Vimeo
extra['byline'] = False # Vimeo
extra['portrait'] = False # Vimeo
try:
data = providers.request(self.oembed_url, **extra)
except ProviderNotFoundException, e:
raise ValidationError(e.message)
except ProviderException, e:
raise ValidationError(e.message)
if not data['type'] == 'video':
raise ValidationError('This must be an url for a video. The "%(type)s" type is not supported.' % {'type': data['type']},)
self.type = data.get('type', '')
self.provider = data.get('provider_name', '')
html = data.get('html', '')
if 'provider_name' in data and self.provider in ['YouTube', 'Vimeo']:
# dirty special handling of youtube and vimeo.
# they ignore these parameters over oembed... so we use our own template to render them.
iframe_html = PyQuery(html)
url = iframe_html.attr('src')
params = {
'autoplay': int(self.autoplay),
'loop': int(self.loop),
'rel': int(self.show_related),
'showinfo': 0, # YouTube
def handle(self, *args, **options):
if not args:
raise CommandError("Missing URL parameter")
for url in args:
try:
data = get_oembed_data(url)
except ProviderNotFoundException:
self.stderr.write("* No OEmbed provider found for '{0}'!\n".format(url))
except ProviderException as e:
# Real urllib2 exception is sadly hidden by micawber.
self.stderr.write("* {0}\n".format(e))
else:
self.stdout.write("* OEmbed data for '{0}':\n".format(url))
for key in sorted(data.keys()):
self.stdout.write(" - {0}: {1}\n".format(key, pformat(data[key])))
def extract(text, providers, **params):
all_urls = set()
urls = []
extracted_urls = {}
for url in re.findall(url_re, text):
if url in all_urls:
continue
all_urls.add(url)
urls.append(url)
try:
extracted_urls[url] = providers.request(url, **params)
except ProviderException:
pass
return urls, extracted_urls