Skip to main content

library for embedding ('unfurling') content from other sites into your own site

Project description

PyUnfurl

PyUnfurl is a Python library for embedding ('unfurling') content from other sites into your own site, similar to the way slack expands links.
Useful for easily embedding tweets, YouTube videos, StackOverflow posts or content from any other site.

Features

Installation

Use the package manager pip to install pyunfurl.

pip install pyunfurl

Usage

import pyunfurl
pyunfurl.unfurl('https://www.youtube.com/watch?v=aBr2kKAHN6M') 

This will return a dict similar to the oembed spec:

{
	'method': 'oembed', # one of 'oembed', 'twitter_card', 'open_graph', 'meta_tags', 'custom'
	'site': 'YouTube', 
	'domain': 'youtube.com', 
	'title': 'Live Views of Starman', 
	'description': '', 
	'image': '', 
	'favicon': '',  # only included if no image was found 
	'url': 'https://www.youtube.com/watch?v=aBr2kKAHN6M', 
	'type': 'rich', 
	'html': '\n<iframe width=" 480" height="270" src="https://www.youtube.com/embed/aBr2kKAHN6M?feature=oembed" frameborder="0" allowfullscreen="allowfullscreen"></iframe>\n'
}

You can use the html entry to directly include the content on your site.

Styling

For non oembed content (eg OpenGraph entries such as StackOverflow) PyUnfurl returns a html structure like the following:

pyunfurl.unfurl('https://stackoverflow.com/questions/509211/understanding-slice-notation')['html']
<div class="unfurl unfurl-image">
    <a rel="noopener nofollow" target="_blank" href="https://stackoverflow.com/questions/509211/understanding-slice-notation">
        <img src="https://cdn.sstatic.net/Sites/stackoverflow/img/apple-touch-icon@2.png?v=73d79a89bded">
    </a>
    <div class="unfurl-content">
        <a class="unfurl-title" href="https://stackoverflow.com/questions/509211/understanding-slice-notation">Understanding slice notation</a>
        <div>I need a good explanation (references are a plus) on Python's slice notation. 
To me, this notation needs a bit of picking up. 
It looks extremely powerful, but I haven't quite got my head around...</div>
        <a class="unfurl-domain" href="https://stackoverflow.com/questions/509211/understanding-slice-notation">stackoverflow.com</a>
    </div>
</div>

You can style this any way you want, a good starting point is to include the following css on your site:

 div.unfurl{
        font-family: helvetica, arial, sans-serif;
        font-size:15px;
        border:1px solid #ebebeb;
        border-radius: 8px;
        display:flex;
        align-items: center;
        text-align: left;
    }
    div.unfurl img {
        width: 120px;
        background-color: #ebebeb;
     }
    div.unfurl-favicon img {
        width: 80px;
        padding:20px;
        background-color: #ebebeb;
    }
    div.unfurl-content a{
        color:black;
        display: inline-block;
        font-weight: bold;
        text-decoration: none;
        line-height:30px;
        text-overflow: ellipsis;
        overflow: hidden;
        min-width: 10px;
        white-space: nowrap;
        width:100%;
    }
    div.unfurl-content a.unfurl-domain{
        color:gray;
        font-weight: normal;
    }
    div.unfurl-content{
        flex:1;
        margin-left:10px;
        margin-right:10px;
        min-width: 10px;
        width:100%;
    }
    div.unfurl-content div{
        line-height:18px;
        max-height:54px;
        overflow:hidden;
    }

This will generate a card looking like:

img

Contributing

Pull requests are welcome. PyUnfurl supports some custom integrations for sites that doesnt return any meta tags, if you want to improve the integration for a specific site you can look at the hackernews example.

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyunfurl-0.1.5.tar.gz (18.0 kB view details)

Uploaded Source

Built Distribution

pyunfurl-0.1.5-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

Details for the file pyunfurl-0.1.5.tar.gz.

File metadata

  • Download URL: pyunfurl-0.1.5.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.1

File hashes

Hashes for pyunfurl-0.1.5.tar.gz
Algorithm Hash digest
SHA256 f42a92190e92921bb2c359217cfae0e27404dea0a625f91f91c54c0e86c26624
MD5 5e96b45f459ad376514aefc8d4f555a0
BLAKE2b-256 694eb1d046e842dbd20a7dde11a798575a99e49da6ccee3a0cb410f509effb93

See more details on using hashes here.

File details

Details for the file pyunfurl-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: pyunfurl-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 17.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.32.2 CPython/3.6.1

File hashes

Hashes for pyunfurl-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 60e98a5a24a7d652a114f4b986162e9f7a04b17272fcc28196579cd95b77faea
MD5 de9f77cfe0ddbfed801eb95238c539b2
BLAKE2b-256 b6d5914374e61e354de1f33f9bc8e4f300ecbdc54ad8caeafd6e4d9be20da61a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page