Ejemplo n.º 1
0
def cache_embed(request):
    if not request.POST:
        return HttpResponseBadRequest("cache_embed requires POST")
    url = request.POST.get('url')
    if not url:
        return HttpResponseBadRequest("POST a url, and I'll happily cache it")
    maxwidth = request.POST.get('maxwidth')
    
    #try memcache first
    key = make_cache_key(url, maxwidth)
    cached_response = cache.get(key)
    if cached_response and type(cached_response) == type(dict()):    
        cached_response['cache'] = 'memcache'
        return HttpResponse(json.dumps(cached_response), mimetype="application/json")

    #then the database
    try:
        saved = SavedEmbed.objects.get(url=url, maxwidth=maxwidth)
        response = saved.response
        response['html'] = saved.html
        response['cache'] = 'database'
        cache.set(key, response) #and save it to memcache
        return HttpResponse(json.dumps(response), mimetype="application/json")
    except SavedEmbed.DoesNotExist:
        pass

    #if we've never seen it before, call the embedly API
    client = Embedly(key=settings.EMBEDLY_KEY, user_agent=USER_AGENT)
    if maxwidth:
       oembed = client.oembed(url, maxwidth=maxwidth)
    else:
       oembed = client.oembed(url)
    if oembed.error:
        return HttpResponseServerError('Error embedding %s.\n %s' % (url,oembed.error))

    #save result to database
    row, created = SavedEmbed.objects.get_or_create(url=url, maxwidth=maxwidth,
                    defaults={'type': oembed.type})
    row.provider_name = oembed.provider_name
    row.response = json.dumps(oembed.data)

    if oembed.type == 'photo':
        html = '<img src="%s" width="%s" height="%s" />' % (oembed.url,
               oembed.width, oembed.height)
    else:
        html = oembed.html

    if html:
        row.html = html
        row.last_updated = datetime.now()
        row.save()

    #and to memcache
    cache.set(key, row.response, 86400)
    response = row.response
    response['html'] = row.html #overwrite for custom oembed types
    response['cache'] = "none"
    return HttpResponse(json.dumps(response), mimetype="application/json")
Ejemplo n.º 2
0
def get_oembed_data(context_var, maxwidth=None, maxheight=None):
    '''
    A custom templatetag that returns an object representing 
        all oembed data for a given url found in a context variable
    Usage:
    {% load embed_filters %}
    {% with context_var="Check out my cool photo: http://www.flickr.com/photos/visualpanic/233508614/" %}
        {{ context_var }}

        {% get_oembed_data context_var maxwidth=400 as oembed %}
        <div class="embed-data">
            {{oembed.title}} <br />
            {{oembed.description}} <br />
            {{oembed.html}} <br />
        </div>

    {% endwith %}

    Uses the Embedly API to get oembed data. Always look to the db/cache first,
        and then fall back to the Embedly API for speed 
        (at the cost of accuracy & updated urls).
    '''

    url = _get_url_from_context_variable(context_var)
    if not url:
        return {}

    #if maxwidth or maxheight is None, the unique_together constraint does not work
    #for now, just filter and check first element instead of using objects.get
    try:
        saved_embed = SavedEmbed.objects.filter(url=url, maxwidth=maxwidth, maxheight=maxheight)[0]
    except IndexError:
        client = Embedly(key=settings.EMBEDLY_KEY, user_agent=USER_AGENT)
        try:
            if maxwidth and maxheight:
                oembed = client.oembed(url, maxwidth=maxwidth, maxheight=maxheight)
            elif maxwidth:
                oembed = client.oembed(url, maxwidth=maxwidth)
            elif maxheight:
                oembed = client.oembed(url, maxheight=maxheight)
            else:
                oembed = client.oembed(url)
        except: #TODO: don't catch all exceptions
            return {}

        if oembed.error:
            return {}

        saved_embed, created = SavedEmbed.objects.get_or_create(
                url=url,
                maxwidth=maxwidth,
                maxheight=maxheight,
                defaults={
                    'type': oembed.type,
                    'oembed_data': oembed.data
                })

    return saved_embed.oembed_data
Ejemplo n.º 3
0
    def find_embed(self, url, max_width=None, key=None):
        from embedly import Embedly

        # Get embedly key
        if key is None:
            key = self.get_key()

        # Get embedly client
        client = Embedly(key=key)

        # Call embedly
        if max_width is not None:
            oembed = client.oembed(url, maxwidth=max_width, better=False)
        else:
            oembed = client.oembed(url, better=False)

        # Check for error
        if oembed.get('error'):
            if oembed['error_code'] in [401, 403]:
                raise AccessDeniedEmbedlyException
            elif oembed['error_code'] == 404:
                raise EmbedNotFoundException
            else:
                raise EmbedlyException

        # Convert photos into HTML
        if oembed['type'] == 'photo':
            html = '<img src="%s" />' % (oembed['url'], )
        else:
            html = oembed.get('html')

        # Return embed as a dict
        return {
            'title':
            oembed['title'] if 'title' in oembed else '',
            'author_name':
            oembed['author_name'] if 'author_name' in oembed else '',
            'provider_name':
            oembed['provider_name'] if 'provider_name' in oembed else '',
            'type':
            oembed['type'],
            'thumbnail_url':
            oembed.get('thumbnail_url'),
            'width':
            oembed.get('width'),
            'height':
            oembed.get('height'),
            'html':
            html,
        }
Ejemplo n.º 4
0
    def find_embed(self, url, max_width=None, key=None):
        from embedly import Embedly

        # Get embedly key
        if key is None:
            key = self.get_key()

        # Get embedly client
        client = Embedly(key=key)

        # Call embedly
        if max_width is not None:
            oembed = client.oembed(url, maxwidth=max_width, better=False)
        else:
            oembed = client.oembed(url, better=False)

        # Check for error
        if oembed.get("error"):
            if oembed["error_code"] in [401, 403]:
                raise AccessDeniedEmbedlyException
            elif oembed["error_code"] == 404:
                raise EmbedNotFoundException
            else:
                raise EmbedlyException

        # Convert photos into HTML
        if oembed["type"] == "photo":
            html = '<img src="%s" alt="">' % (oembed["url"], )
        else:
            html = oembed.get("html")

        # Return embed as a dict
        return {
            "title":
            oembed["title"] if "title" in oembed else "",
            "author_name":
            oembed["author_name"] if "author_name" in oembed else "",
            "provider_name":
            oembed["provider_name"] if "provider_name" in oembed else "",
            "type":
            oembed["type"],
            "thumbnail_url":
            oembed.get("thumbnail_url"),
            "width":
            oembed.get("width"),
            "height":
            oembed.get("height"),
            "html":
            html,
        }
Ejemplo n.º 5
0
def embedly(url, max_width=None, key=None):
    from embedly import Embedly

    # Get embedly key
    if key is None:
        try:
            key = settings.WAGTAILEMBEDS_EMBEDLY_KEY
        except AttributeError:
            key = settings.EMBEDLY_KEY
            warnings.warn(
                "EMBEDLY_KEY is now deprecated. Use WAGTAILEMBEDS_EMBEDLY_KEY instead",
                RemovedInWagtail14Warning)

    # Get embedly client
    client = Embedly(key=key)

    # Call embedly
    if max_width is not None:
        oembed = client.oembed(url, maxwidth=max_width, better=False)
    else:
        oembed = client.oembed(url, better=False)

    # Check for error
    if oembed.get('error'):
        if oembed['error_code'] in [401, 403]:
            raise AccessDeniedEmbedlyException
        elif oembed['error_code'] == 404:
            raise EmbedNotFoundException
        else:
            raise EmbedlyException

    # Convert photos into HTML
    if oembed['type'] == 'photo':
        html = '<img src="%s" />' % (oembed['url'], )
    else:
        html = oembed.get('html')

    # Return embed as a dict
    return {
        'title': oembed['title'] if 'title' in oembed else '',
        'author_name':
        oembed['author_name'] if 'author_name' in oembed else '',
        'provider_name':
        oembed['provider_name'] if 'provider_name' in oembed else '',
        'type': oembed['type'],
        'thumbnail_url': oembed.get('thumbnail_url'),
        'width': oembed.get('width'),
        'height': oembed.get('height'),
        'html': html,
    }
Ejemplo n.º 6
0
def embed_replace(match, maxwidth=None):
    url = match.group(1)

    key = make_cache_key(url, maxwidth)
    cached_html = cache.get(key)

    if cached_html:
        return cached_html

    # call embedly API
    client = Embedly(key=settings.EMBEDLY_KEY, user_agent=USER_AGENT)
    if maxwidth:
        oembed = client.oembed(url, maxwidth=maxwidth)
    else:
        oembed = client.oembed(url)

    # check database
    if oembed.error:
        try:
            html = SavedEmbed.objects.get(url=url, maxwidth=maxwidth).html
            cache.set(key, html)
            return html
        except SavedEmbed.DoesNotExist:
            err_code = (oembed.data['error_code']
                        if 'error_code' in oembed.data else 'Unknown')
            LOG.warn('Error fetching %s (%s)', url, err_code)
            return url

    # save result to database
    row, created = SavedEmbed.objects.get_or_create(url=url, maxwidth=maxwidth,
                defaults={'type': oembed.type})

    if oembed.type == 'photo':
        html = '<img src="%s" ' % oembed.url
        if maxwidth:
            html += 'width="%s" />' % maxwidth
        else:
            html += 'width="%s" height="%s" />' % (oembed.width, oembed.height)
    else:
        html = oembed.html

    if html:
        row.html = html
        row.last_updated = datetime.now()
        row.save()

    # set cache
    cache.set(key, html, 86400)
    return html
Ejemplo n.º 7
0
def embedly(url, max_width=None, key=None):
    from embedly import Embedly

    # Get embedly key
    if key is None:
        try:
            key = settings.WAGTAILEMBEDS_EMBEDLY_KEY
        except AttributeError:
            key = settings.EMBEDLY_KEY
            warnings.warn(
                "EMBEDLY_KEY is now deprecated. Use WAGTAILEMBEDS_EMBEDLY_KEY instead",
                RemovedInWagtail14Warning)

    # Get embedly client
    client = Embedly(key=key)

    # Call embedly
    if max_width is not None:
        oembed = client.oembed(url, maxwidth=max_width, better=False)
    else:
        oembed = client.oembed(url, better=False)

    # Check for error
    if oembed.get('error'):
        if oembed['error_code'] in [401, 403]:
            raise AccessDeniedEmbedlyException
        elif oembed['error_code'] == 404:
            raise EmbedNotFoundException
        else:
            raise EmbedlyException

    # Convert photos into HTML
    if oembed['type'] == 'photo':
        html = '<img src="%s" />' % (oembed['url'], )
    else:
        html = oembed.get('html')

    # Return embed as a dict
    return {
        'title': oembed['title'] if 'title' in oembed else '',
        'author_name': oembed['author_name'] if 'author_name' in oembed else '',
        'provider_name': oembed['provider_name'] if 'provider_name' in oembed else '',
        'type': oembed['type'],
        'thumbnail_url': oembed.get('thumbnail_url'),
        'width': oembed.get('width'),
        'height': oembed.get('height'),
        'html': html,
    }
Ejemplo n.º 8
0
def replace(match):
    url = match.group('url')
    try:
        url_validate(url)
        expiration_date = datetime.now() - settings.EMBEDLY_CACHE_EXPIRES
        embedded_urls = EmbeddedUrl.objects.filter(
            original_url=url,
            created_on__gte=expiration_date).order_by('-created_on')
        embedded_url = None
        if embedded_urls.exists():
            embedded_url = embedded_urls[0]
        else:
            embedly_key = getattr(settings, 'EMBEDLY_KEY', False)
            if embedly_key:
                client = Embedly(embedly_key)
                obj = client.oembed(url, maxwidth=460)
                embedded_url = EmbeddedUrl(original_url=obj.original_url,
                                           html=(obj.html or ''),
                                           extra_data=obj.dict)
                embedded_url.save()
        if embedded_url and embedded_url.html:
            return embedded_url.html
    except ValidationError:
        return '[embed:Invalid Url]'
    return DEFAULT_EMBED % (url, url)
Ejemplo n.º 9
0
def replace(match):
    url = match.group('url')
    kind = match.group('kind')
    external_task = (match.group('kind') == 'externaltask')
    try:
        url_validate(url)
    except ValidationError:
        return '[%s:Invalid Url]' % kind
    for prefix in settings.P2PU_EMBEDS:
        if url.startswith(prefix):
            return render_to_string('richtext/_p2pu_embed.html', {'url': url})
    if not external_task:
        expiration_date = datetime.now() - settings.EMBEDLY_CACHE_EXPIRES
        embedded_urls = EmbeddedUrl.objects.filter(original_url=url,
            created_on__gte=expiration_date).order_by('-created_on')
        embedded_url = None
        if embedded_urls.exists():
            embedded_url = embedded_urls[0]
        else:
            embedly_key = getattr(settings, 'EMBEDLY_KEY', False)
            if embedly_key:
                client = Embedly(embedly_key)
                obj = client.oembed(url, maxwidth=460)
                embedded_url = EmbeddedUrl(original_url=obj.original_url,
                    html=(obj.html or ''), extra_data=obj.dict)
                embedded_url.save()
        if embedded_url and embedded_url.html:
            return embedded_url.html
    context = {'url': url, 'external_task': external_task}
    return render_to_string('richtext/_external_link.html', context)
Ejemplo n.º 10
0
def replace(match):
    url = match.group('url')
    try:
        url_validate(url)
        if match.group('kind') == 'externaltask':
            return '<button class="external-task" data-url="%s">%s</button>' % (
                url, # TODO: Should we escape/encode this somehow?
                _('Start This Task')
                )
        expiration_date = datetime.now() - settings.EMBEDLY_CACHE_EXPIRES
        embedded_urls = EmbeddedUrl.objects.filter(original_url=url,
            created_on__gte=expiration_date).order_by('-created_on')
        embedded_url = None
        if embedded_urls.exists():
            embedded_url = embedded_urls[0]
        else:
            embedly_key = getattr(settings, 'EMBEDLY_KEY', False)
            if embedly_key:
                client = Embedly(embedly_key)
                obj = client.oembed(url, maxwidth=460)
                embedded_url = EmbeddedUrl(original_url=obj.original_url,
                    html=(obj.html or ''), extra_data=obj.dict)
                embedded_url.save()
        if embedded_url and embedded_url.html:
            return embedded_url.html
    except ValidationError:
        return '[embed:Invalid Url]'
    return DEFAULT_EMBED % (url, url)
Ejemplo n.º 11
0
def _add_embedly_data(instance):
    import json
    from embedly import Embedly
    from main.api2 import APIException
    # for security, we can't let the client set embedly_data
    assert instance.original_url
    assert not instance.embedly_data
    client = Embedly(settings.EMBEDLY_KEY)
    obj = client.oembed(instance.original_url, 
        autoplay=False,
        maxwidth=600,
        # Fix overlay issues with flash under chrome/linux:
        wmode='transparent'
    )
    if obj.invalid:
        raise APIException('The submitted link is invalid')
    elif obj.type is 'error':
        raise APIException('Embedly error', obj.error_code)
    elif not obj.html:
        raise APIException('No embed html received')
    else:
        assert obj.provider_name.lower() in settings.ALLOWED_EMBEDLY_PROVIDERS
        instance.width = obj.width
        instance.height = obj.height
        instance.embedly_data = json.dumps(dict(obj))
Ejemplo n.º 12
0
def getIframeForVideo(videoUrl):
    pwManager = getUtility(IPasswordManager, 'embedly')
    key = pwManager.username
    client = Embedly(key)
    embed = client.oembed(videoUrl, maxheight=377)
    if embed.error:
        return None
    return embed.html
Ejemplo n.º 13
0
def detect_embedded_content(text):
    results = []

    client = Embedly(key=EMBEDLY_KEY, user_agent=USER_AGENT)
    for url in re.findall(EMBED_REGEX, text):
        results.append(client.oembed(url))

    return results
Ejemplo n.º 14
0
def get_embed_embedly(url, max_width=None):
    # Check database
    try:
        return Embed.objects.get(url=url, max_width=max_width)
    except Embed.DoesNotExist:
        pass

    try:
        # Call embedly API
        client = Embedly(key=settings.EMBEDLY_KEY)
    except AttributeError:
        return None
    if max_width is not None:
        oembed = client.oembed(url, maxwidth=max_width, better=False)
    else:
        oembed = client.oembed(url, better=False)

    # Check for error
    if oembed.get('error'):
        return None

    # Save result to database
    row, created = Embed.objects.get_or_create(
        url=url,
        max_width=max_width,
        defaults={
            'type': oembed['type'],
            'title': oembed['title'],
            'thumbnail_url': oembed.get('thumbnail_url'),
            'width': oembed.get('width'),
            'height': oembed.get('height')
        }
    )

    if oembed['type'] == 'photo':
        html = '<img src="%s" />' % (oembed['url'], )
    else:
        html = oembed.get('html')

    if html:
        row.html = html
        row.last_updated = datetime.now()
        row.save()

    # Return new embed
    return row
Ejemplo n.º 15
0
    def create_embed(self, options):
        embed = Embedly(settings.EMBEDLY_API_KEY)

        obj = embed.oembed(options['url'], width=options['width'], height=options['height'])
        el = etree.Element('div')
        el.set('class', 'post-embed')
        el.append(html.fromstring(obj.html))
        return html.tostring(el)
Ejemplo n.º 16
0
def get_embed_object(url):
    if not settings.EMBEDLY_KEY:
        raise ImproperlyConfigured("You have not specified an Embedly key in your settings file.")

    try:
        client = Embedly(settings.EMBEDLY_KEY)
        resp = client.oembed(url, maxwidth=settings.EMBED_MAX_WIDTH, maxheight=settings.EMBED_MAX_HEIGHT)
    except Exception, e:
        raise EmbedlyException("There was an issue with your embed URL. Please check that it is valid and try again")
Ejemplo n.º 17
0
def embed_replace(match, maxwidth=None):
    url = match.group(1)

    key = make_cache_key(url, maxwidth)
    cached_html = cache.get(key)

    if cached_html:
        return cached_html

    # call embedly API
    client = Embedly(key=settings.EMBEDLY_KEY, user_agent=USER_AGENT)
    if maxwidth:
        oembed = client.oembed(url, maxwidth=maxwidth)
    else:
        oembed = client.oembed(url)

    # check database
    if oembed.error:
        try:
            html = SavedEmbed.objects.get(url=url, maxwidth=maxwidth).html
            cache.set(key, html)
            return html
        except SavedEmbed.DoesNotExist:
            return "Error embedding %s" % url

    # save result to database
    row, created = SavedEmbed.objects.get_or_create(url=url, maxwidth=maxwidth, defaults={"type": oembed.type})

    if oembed.type == "photo":
        html = u'<img src="%s" width="%s" height="%s" />' % (oembed.url, oembed.width, oembed.height)
    elif oembed.type == "link":
        html = u'Link to: <a href="{url}">{title}</a>'.format(url=oembed.url, title=oembed.title)
    else:
        html = oembed.html

    if html:
        row.html = html
        row.last_updated = datetime.now()
        row.save()

    # set cache
    cache.set(key, html, 86400)
    return html
Ejemplo n.º 18
0
    def find_embed(self, url, max_width=None, key=None):
        from embedly import Embedly

        # Get embedly key
        if key is None:
            key = self.get_key()

        # Get embedly client
        client = Embedly(key=key)

        # Call embedly
        if max_width is not None:
            oembed = client.oembed(url, maxwidth=max_width, better=False)
        else:
            oembed = client.oembed(url, better=False)

        # Check for error
        if oembed.get('error'):
            if oembed['error_code'] in [401, 403]:
                raise AccessDeniedEmbedlyException
            elif oembed['error_code'] == 404:
                raise EmbedNotFoundException
            else:
                raise EmbedlyException

        # Convert photos into HTML
        if oembed['type'] == 'photo':
            html = '<img src="%s" />' % (oembed['url'], )
        else:
            html = oembed.get('html')

        # Return embed as a dict
        return {
            'title': oembed['title'] if 'title' in oembed else '',
            'author_name': oembed['author_name'] if 'author_name' in oembed else '',
            'provider_name': oembed['provider_name'] if 'provider_name' in oembed else '',
            'type': oembed['type'],
            'thumbnail_url': oembed.get('thumbnail_url'),
            'width': oembed.get('width'),
            'height': oembed.get('height'),
            'html': html,
        }
Ejemplo n.º 19
0
def embedly(url, max_width=None, key=None):
    from embedly import Embedly

    # Get embedly key
    if key is None:
        key = settings.EMBEDLY_KEY

    # Get embedly client
    client = Embedly(key=key)

    # Call embedly
    if max_width is not None:
        oembed = client.oembed(url, maxwidth=max_width, better=False)
    else:
        oembed = client.oembed(url, better=False)

    # Check for error
    if oembed.get("error"):
        if oembed["error_code"] in [401, 403]:
            raise AccessDeniedEmbedlyException
        elif oembed["error_code"] == 404:
            raise EmbedNotFoundException
        else:
            raise EmbedlyException

    # Convert photos into HTML
    if oembed["type"] == "photo":
        html = '<img src="%s" />' % (oembed["url"],)
    else:
        html = oembed.get("html")

    # Return embed as a dict
    return {
        "title": oembed["title"] if "title" in oembed else "",
        "author_name": oembed["author_name"] if "author_name" in oembed else "",
        "provider_name": oembed["provider_name"] if "provider_name" in oembed else "",
        "type": oembed["type"],
        "thumbnail_url": oembed.get("thumbnail_url"),
        "width": oembed.get("width"),
        "height": oembed.get("height"),
        "html": html,
    }
def get_info_if_active(url):
    oembed = None
    if not ACTIVE:
        return oembed
    client = Embedly(settings.EMBEDLY_KEY)
    try:
        oe = client.oembed(url, maxwidth=None if not hasattr(settings,'EMBEDLY_MAXWIDTH') else settings.EMBEDLY_MAXWIDTH)
        if not oe.error:
            oembed = oe
    except httplib2.ServerNotFoundError, e:
        pass # Can't connect to server.
Ejemplo n.º 21
0
  def noembed(self):
    """
    use noembed MILLER_EMBEDLY_API_KEY to get videos from url
    """
    if self.url:
      logger.debug('document {pk:%s, url:%s} init embedly' % (self.pk, self.url))

      from embedly import Embedly

      client = Embedly(settings.MILLER_EMBEDLY_API_KEY)
      embed = client.oembed(self.url, raw=True)
      self.contents = embed['raw']
Ejemplo n.º 22
0
def set_media():
    link = request.form.get('value')
    contribution = get_contribution(get_referer())
    client = Embedly('a54233f91473419f9a947e1300f27f9b')
    obj = client.oembed(link)
    meta = obj.__dict__
    media = {
        'type': request.form.get('type'),
        'url': meta.get('url'),
        'meta': meta
    }
    contrib = update_media(media, get_referer())
    return dumps(contrib)
Ejemplo n.º 23
0
def embedly(url):
  try:
    client = Embedly(settings.EMBEDLY_KEY)
    obj = client.oembed(url)

  except:
    return None

  if obj.type == "photo":
    return '<a href="%s" class="embed"><img src="%s"></a>' % (
      obj.url, obj.url)

  return None
Ejemplo n.º 24
0
def get_list_from_embedly(bitly_url_links,KEY):
	client = Embedly(KEY)
	data_dict = dict()
	temp_dict = dict()
	for p,link in enumerate(bitly_url_links):
		link = client.oembed(link)
		temp_dict['title'] = link['title']
		temp_dict['url'] = link['url']
		temp_dict['thumbnail_url'] = link['thumbnail_url']
		temp_dict['thumbnail_width'] = link['thumbnail_width']
		temp_dict['description'] = link['description']
		data_dict[p] = temp_dict
	return data_dict
Ejemplo n.º 25
0
def set_media():
	link = request.form.get('value')
	contribution = get_contribution(get_referer())
	client = Embedly('a54233f91473419f9a947e1300f27f9b')
	obj    = client.oembed(link)
	meta   = obj.__dict__
	media  = {
		'type'    : request.form.get('type'),
		'url'     : meta.get('url'),
		'meta'    : meta
	}
	contrib = update_media(media, get_referer())
	return dumps(contrib)
Ejemplo n.º 26
0
def embedly(url):
    try:
        client = Embedly(settings.EMBEDLY_KEY)
        obj = client.oembed(url)

    except:
        return None

    if obj.type == "photo":
        return '<a href="%s" class="embed"><img src="%s"></a>' % (obj.url,
                                                                  obj.url)

    return None
Ejemplo n.º 27
0
def get_links(text):
    url_regex = re.compile('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|'
                           '(?:%[0-9a-fA-F][0-9a-fA-F]))+')

    urls = url_regex.findall(text)

    embedly = []

    client = Embedly(app.config['EMBEDLY_KEY'])

    for url in urls:
        embedly.append(client.oembed(url))

    return embedly
Ejemplo n.º 28
0
def wrap_with_embedly(url):
    logger.debug(u"calling embedly for {url}".format(
        url=url))

    client = Embedly(os.getenv("EMBEDLY_API_KEY"))
    # if not client.is_supported(url):
    #     return None

    response_dict = client.oembed(url, maxwidth=580, width=580)
    try:
        html = response_dict["html"]
        html = html.replace("http://docs.google", "https://docs.google")
        return html
    except (KeyError, AttributeError):
        return None
Ejemplo n.º 29
0
def get_embed_embedly(url, max_width=None):
    # Check database
    try:
        return Embed.objects.get(url=url, max_width=max_width)
    except Embed.DoesNotExist:
        pass

    client = Embedly(key=settings.EMBEDLY_KEY)
    
    if max_width is not None:
        oembed = client.oembed(url, maxwidth=max_width, better=False)
    else:
        oembed = client.oembed(url, better=False)

    # Check for error
    if oembed.get('error'):
        if oembed['error_code'] in [401,403]:
            raise AccessDeniedEmbedlyException
        elif oembed['error_code'] == 404:
            raise NotFoundEmbedlyException
        else:
            raise EmbedlyException

    return save_embed(url, max_width, oembed)
Ejemplo n.º 30
0
class EmbedlyBackend(object):
    """
    The response object from oembed() is a dict that should look like this:
        response.__dict__ ->
        {'data': {'type': 'error', 'error_code': 400, 'error': True}, 'method': 'oembed', 'original_url': 'bad_url'}
        - or -
        {'data': {u'provider_url': u'http://vimeo.com/', u'description': ...}, 'method': 'oembed', 'original_url': 'http://vimeo.com/1111'}

    """
    response_class = EmbedlyResponse

    def __init__(self):
        try:
            key = getattr(settings, 'EMBEDLY_KEY')
        except AttributeError:
            from django.core.exceptions import ImproperlyConfigured
            raise ImproperlyConfigured(
                '%s requires an API key be specified in settings' %
                type(self).__name__)
        self.client = EmbedlyAPI(key)

    @proxy
    def call(self, url):
        if not url:
            return None

        logger.debug("Embedly call to oembed('%s')" % url)
        try:
            response = self.client.oembed(url)
        except ServerNotFoundError:
            #PY3 use PEP 3134 exception chaining
            import sys
            exc_cls, msg, trace = sys.exc_info()
            raise (InvalidResponseError,
                   "%s: %s" % (exc_cls.__name__, msg),
                   trace)

        response = self.wrap_response_data(
            getattr(response, 'data', None), fresh=True)
        if not response.is_valid():
            logger.warn("%s error: %s" %
                        (type(response).__name__, response._data))
        return response

    @proxy
    def wrap_response_data(self, data, **kwargs):
        return self.response_class(data, **kwargs)
Ejemplo n.º 31
0
def replace(match):
    url = match.group('url')
    try:
        url_validate(url)
        expiration_date = datetime.now() - settings.EMBEDLY_CACHE_EXPIRES
        embedded_urls = EmbeddedUrl.objects.filter(original_url=url,
            created_on__gte=expiration_date).order_by('-created_on')
        embedded_url = None
        if embedded_urls.exists():
            embedded_url = embedded_urls[0]
        else:
            embedly_key = getattr(settings, 'EMBEDLY_KEY', False)
            if embedly_key:
                client = Embedly(embedly_key)
                obj = client.oembed(url)
                embedded_url = EmbeddedUrl(original_url=obj.original_url,
                    html=obj.html, extra_data=obj.dict)
                embedded_url.save()
        if embedded_url and embedded_url.html:
            return embedded_url.html
    except ValidationError:
        return '[embed:Invalid Url]'
    return DEFAULT_EMBED % (url, url)
Ejemplo n.º 32
0
def oembed_replace(srcurl=""):
    r = ""
    client = Embedly()
    obj = client.oembed(srcurl, maxwidth=480)
    # for idx,item in enumerate(oembed_json):
    try:
        title = obj["title"]
    except KeyError:
        title = ""
    try:
        url = obj["url"]
    except KeyError:
        url = ""
    try:
        thumbnail_url = obj["thumbnail_url"]
    except KeyError:
        thumbnail_url = ""

    if obj["type"] == "photo":
        r = (
            ' <a href="http://'
            + srcurl
            + '" target="_blank"><img style="width:480px" src="'
            + url
            + '" alt="'
            + title
            + '"/></a>'
        )
    if obj["type"] == "video":
        r = " " + obj["html"]
    if obj["type"] == "rich":
        r = " " + obj["html"]
    if obj["type"] == "link":
        r = ' <a href="' + url + '">' + title + "</a>"

    return r
from embedly import Embedly
import csv
eKey = "e32471062181449a9fb1b7d0987fe894"
client = Embedly(eKey)
f = open('LinksFromTwitter.html', 'w', )
with open("linkWD.csv") as csvfile:
    csvReader = csv.reader(csvfile, delimiter=';')
    L = {}
    print('<html>', file=f)
    print(' <head>', file=f)
    print(' <title>' + "Description of most popular links from Twitter" + '</title>', file=f)
    print('</head>', file=f)
    print(' <body>', file=f)
    for row in csvReader:
        obj = client.oembed(row[0])
        Title = obj['title']
        Descr = obj['description']
        print('  <hr>', file=f)
        sTitle = str(Title.encode("ascii", "xmlcharrefreplace"))
        print('   <p>', file=f)
        print(sTitle[2:-1], file=f)
        print('   </p>', file=f)
        sDescr = str(Descr.encode("ascii", "xmlcharrefreplace"))
        print('   <p>', file=f)
        print(sDescr[2:-1], file=f)
        print('   </p>', file=f)
        if 'thumbnail_url' in obj:
            Thumb = obj['thumbnail_url']
            print('   <img src="%s">' % Thumb, file=f)
    print(' </body>', file=f)
    print('</html>', file=f)
Ejemplo n.º 34
0
 def set_link_data(self):
     # connect to embedly + get url data
     client = Embedly(EMBEDLY_KEY)
     self.data = client.oembed(self.url).__dict__['data']
     self.name = self.data['title']
Ejemplo n.º 35
0
posts = pd.read_csv('posts.csv', na_filter=False, index_col='id')

client = Embedly('c1df1020e9cc4e1b8abf93c088eacc3d')


start_index = 1080 #start from index number (1 to n)
writeabout_text = False

newposts = posts[start_index-1:]
postsforupdate = newposts[(newposts['thumb_url'] == '') | (newposts['title'] == '')]

for index, row in postsforupdate.iterrows():
  
  # Run against embedly if empty thumb_url or title
  if index >= start_index:
    obj = client.oembed(row['link_url']) #make API call - http://embed.ly/docs/embed/api/endpoints/1/oembed

    if obj.type == 'photo':
      posts.ix[index, 'thumb_url'] = obj.url
    else:
      if row['thumb_url'] == '': # only write in field if empty
        posts.ix[index, 'thumb_url'] = obj.thumbnail_url
      if writeabout_text == True and row['about_text'] == '':
        posts.ix[index, 'about_text'] = obj.description
      if row['title'] == '':
        posts.ix[index, 'title'] = obj.description 
    

    print(index)
    print(posts.ix[index, 'thumb_url'])
    print(posts.ix[index, 'about_text'])
Ejemplo n.º 36
0
    except SavedEmbed.DoesNotExist:
        pass
    except TypeError, e:
        response = {"error": "Error embedding %s\n%s." % (url, e)}
        return HttpResponseServerError(json.dumps(response), mimetype="application/json")
    except SyntaxError:
        # probably has old embed with no response
        saved.delete()
        saved.save()
        # clear it, so we don't get it from the cache again
        return HttpResponseServerError("no embed response for %s" % url)

    # if we've never seen it before, call the embedly API
    client = Embedly(key=settings.EMBEDLY_KEY, user_agent=USER_AGENT)
    if maxwidth:
        oembed = client.oembed(url, maxwidth=maxwidth)
    else:
        oembed = client.oembed(url)
    if oembed.error:
        # print oembed.error
        response = {"error": "Error embedding %s" % url, "reason": oembed.error}
        return HttpResponseServerError(json.dumps(response), mimetype="application/json")

    # save result to database
    try:
        row = SavedEmbed.objects.get(url=url, maxwidth=maxwidth)
    except SavedEmbed.DoesNotExist:
        row = SavedEmbed(response=json.dumps(oembed.data))
        row.url = url
        row.maxwidth = maxwidth
        row.type = oembed.type
Ejemplo n.º 37
0
class UrlPreview(object):

    FIELDS = [
        'author_name', 'description', 'provider_name', 'provider_url',
        'thumbnail_url', 'thumbnail_width', 'thumbnail_height', 'title', 'url'
    ]

    def __init__(self):
        self.client = Embedly(KEY)

    def getPreview(self, url):
        obj = self.client.oembed(url)

        if obj.get('error'):
            return ''

        # fill in some blanks
        for field in self.FIELDS:
            if field not in obj.data:
                obj.data[field] = ''

        return PREVIEW_HTML.format(**obj.data)

    def getData(self, url):
        return self.client.oembed(url)

    def getDataAsText(self, url):
        obj = self.getData(url)
        keys = sorted(obj.data.keys())
        o = ''
        for k in keys:
            o += '%-17s: %s' % (k, obj.data[k]) + '\n'
        return o

    def run(self):
        '''Command line interface'''

        from vlib.cli import CLI

        commands = ['preview_html <url>', 'get_data <url>']
        options = {}
        self.cli = CLI(self.process, commands, options)
        self.cli.process()

    def process(self, *args):
        '''Process Command line requests'''

        from vlib.utils import validate_num_args

        args = list(args)
        cmd = args.pop(0)

        if cmd == 'preview_html':
            validate_num_args('preview_html', 1, args)
            url = args.pop(0)
            return self.getPreview(url)

        elif cmd == 'get_data':
            validate_num_args('get_data', 1, args)
            url = args.pop(0)
            return self.getDataAsText(url)

        else:
            raise UrlPreviewError('Unrecognized command: %s' % cmd)
Ejemplo n.º 38
0
def embed_replace(match, maxwidth=None):
    url = match.group(0)

    key = make_cache_key(url, maxwidth)
    cached_html = cache.get(key)

    if cached_html:
        return cached_html

    # call embedly API
    client = Embedly(key=embeds.EMBEDLY_KEY, user_agent=embeds.USER_AGENT)
    if maxwidth:
        oembed = client.oembed(url, maxwidth=maxwidth)
    else:
        oembed = client.oembed(url)

    # check database
    if oembed.get('error'):
        try:
            html = SavedEmbed.objects.get(url=url, maxwidth=maxwidth).html
            cache.set(key, html)
            return html
        except SavedEmbed.DoesNotExist:
            return 'Error embedding %s' % url

    if oembed['type'] == 'photo':
        template = """
        <div class="embeds"><img src="${url}"" /></div>
        """
    elif oembed['type'] == 'link':
        template = """
        <div class="embeds">
            <a href="${url}">
            <img class="embeds-thumbnail" src="${thumbnail_url}" />
            <div class="embeds-text">
                <span class="embeds-title">${title}</span>
                <p class="embeds-description">${description}</p>
                <span class="embeds-source">Read the article on ${provider_url}</span>
            </div>
            </a>
        </div>"""
    elif oembed.get('html'):
        template = """<div class="embeds">${html}</div>"""
    else:
        template = """<a href="${url}">${url}</a>"""
        html = url

    # Set up defaults and populate with data
    data = {
        'title': '',
        'url': '',
        'thumbnail_url': embeds.DEFAULT_THUMBNAIL,
        'description': '',
        'provider_url': '?'
    }
    data.update(oembed)

    html = string.Template(template).substitute(data)

    # save result to database
    row, created = SavedEmbed.objects.get_or_create(url=url, maxwidth=maxwidth, defaults={
        'type': oembed['type'],
        'html': html
    })

    if not created:
        row.save()

    # set cache
    cache.set(key, html, 86400)

    return html