def format_element(bfo, max_photos=''):
    """Return an image of the record, suitable for the Open Graph protocol.

    Will look for any icon stored with the record, and will fallback to any
    image file attached to the record. Returns nothing when no image is found.

    Some optional structured properties are not considered, for optimizing both generation of the page
    and page size.

    @param max_photos: the maximum number of photos to display
    """
    if not CFG_WEBSEARCH_ENABLE_OPENGRAPH:
        return ""
    bibarchive = BibRecDocs(bfo.recID)
    bibdocs = bibarchive.list_bibdocs()
    tags = []
    images = []

    if max_photos.isdigit():
        max_photos = int(max_photos)
    else:
        max_photos = len(bibdocs)

    for doc in bibdocs[:max_photos]:
        found_icons = []
        found_image_url = ''
        for docfile in doc.list_latest_files():
            if docfile.is_icon():
                found_icons.append((docfile.get_size(), docfile.get_url()))
            elif get_superformat_from_format(docfile.get_format()).lower() in [".jpg", ".gif", ".jpeg", ".png"]:
                found_image_url = docfile.get_url()
        found_icons.sort()

        for icon_size, icon_url in found_icons:
            images.append((icon_url, icon_url.replace(CFG_SITE_URL, CFG_SITE_SECURE_URL)))
        if found_image_url:
            images.append((found_image_url, found_image_url.replace(CFG_SITE_URL, CFG_SITE_SECURE_URL)))

    if CFG_CERN_SITE:
        # Add some more pictures from metadata
        additional_images = [(image_url, image_url.replace("http://mediaarchive.cern.ch/", "https://mediastream.cern.ch")) for image_url in bfo.fields("8567_u") if image_url.split('.')[-1] in ('jpg', 'png', 'jpeg', 'gif') and 'A5' in image_url]
        images.extend(additional_images)

    tags = ['<meta property="og:image" content="%s" />%s' % (image_url, image_url != image_secure_url and '\n<meta property="og:image:secure_url" content="%s" />' % image_secure_url or "") for image_url, image_secure_url in images]

    return "\n".join(tags)
Esempio n. 2
0
def format_element(bfo, max_photos='', one_icon_per_bibdoc='yes', twitter_card_type='photo', use_webjournal_featured_image='no'):
    """Return an image of the record, suitable for the Open Graph protocol.

    Will look for any icon stored with the record, and will fallback to any
    image file attached to the record. Returns nothing when no image is found.

    Some optional structured properties are not considered, for optimizing both generation of the page
    and page size.

    @param max_photos: the maximum number of photos to display
    @param one_icon_per_bibdoc: shall we keep just one icon per bibdoc in the output (not repetition of same preview in multiple sizes)?
    @param twitter_card_type: the type of Twitter card: 'photo' (single photo) or 'gallery'. Fall back to 'photo' if not enough pictures for a 'gallery'.
    @param use_webjournal_featured_image: if 'yes', use the "featured image" (as defined in bfe_webjournal_articles_overview) as image for the Twitter Card
    """
    if not CFG_WEBSEARCH_ENABLE_OPENGRAPH:
        return ""
    bibarchive = BibRecDocs(bfo.recID)
    bibdocs = bibarchive.list_bibdocs()
    tags = []
    images = []

    if max_photos.isdigit():
        max_photos = int(max_photos)
    else:
        max_photos = len(bibdocs)

    for doc in bibdocs[:max_photos]:
        found_icons = []
        found_image_url = ''
        found_image_size = 0
        for docfile in doc.list_latest_files(list_hidden=False):
            if docfile.is_icon():
                found_icons.append((docfile.get_size(), docfile.get_url()))
            elif get_superformat_from_format(docfile.get_format()).lower() in [".jpg", ".gif", ".jpeg", ".png"]:
                found_image_url = docfile.get_url()
                found_image_size = docfile.get_size()
        found_icons.sort()

        # We might have found several icons for the same file: keep
        # middle-size one
        if found_icons:
            if one_icon_per_bibdoc.lower() == 'yes':
                found_icons = [found_icons[len(found_icons)/2]]
        for icon_size, icon_url in found_icons:
            images.append((icon_url, icon_url.replace(CFG_SITE_URL, CFG_SITE_SECURE_URL), icon_size))
        # Link to main file too (?)
        if found_image_url:
            images.append((found_image_url, found_image_url.replace(CFG_SITE_URL, CFG_SITE_SECURE_URL), found_image_size))

    if CFG_CERN_SITE:
        # Add some more pictures from metadata
        dummy_size = 500*1024 # we don't we to check image size, we just make one (see Twitter Card limit)
        additional_images = [(image_url, image_url.replace("http://mediaarchive.cern.ch/", "https://mediastream.cern.ch"), dummy_size) for image_url in bfo.fields("8567_u") if image_url.split('.')[-1] in ('jpg', 'png', 'jpeg', 'gif') and 'A5' in image_url]
        images.extend(additional_images)

    tags = ['<meta property="og:image" content="%s" />%s' % (image_url, image_url != image_secure_url and '\n<meta property="og:image:secure_url" content="%s" />' % image_secure_url or "") for image_url, image_secure_url, image_size in images]

    # Twitter Card

    if use_webjournal_featured_image.lower() == 'yes':
        # First look for the prefered image, if available. Note that
        # it might be a remote one.
        try:
            from invenio.bibformat_elements import bfe_webjournal_articles_overview
            image_url = bfe_webjournal_articles_overview._get_feature_image(bfo)
            image_secure_url = image_url.replace('http:', 'https:')
            image_size = 500 * 1024 # TODO: check for real image size
            if image_url.strip():
                images.insert(0, (image_url, image_secure_url, image_size))
        except:
            pass

    # Filter out images that would not be compatible
    twitter_compatible_images = [image_url for image_url, image_secure_url, image_size in images if \
                                 image_size < 1024*1024][:4] #Max 1MB according to Twitter Card APIs, max 4 photos
    twitter_card_tags = []
    if len(twitter_compatible_images) == 4 and twitter_card_type == 'gallery':
        twitter_card_tags = ['<meta name="twitter:image%i" content="%s" />' % \
                             (twitter_compatible_images.index(image_url), image_url) \
                             for image_url in twitter_compatible_images]
    elif twitter_compatible_images:
        twitter_card_tags = ['<meta name="twitter:image" content="%s" />' % twitter_compatible_images[0]]

    tags = twitter_card_tags + tags

    return "\n".join(tags)
Esempio n. 3
0
def format_element(bfo, max_photos=''):
    """Return an image of the record, suitable for the Open Graph protocol.

    Will look for any icon stored with the record, and will fallback to any
    image file attached to the record. Returns nothing when no image is found.

    Some optional structured properties are not considered, for optimizing both generation of the page
    and page size.

    @param max_photos: the maximum number of photos to display
    """
    if not CFG_WEBSEARCH_ENABLE_OPENGRAPH:
        return ""
    bibarchive = BibRecDocs(bfo.recID)
    bibdocs = bibarchive.list_bibdocs()
    tags = []
    images = []

    if max_photos.isdigit():
        max_photos = int(max_photos)
    else:
        max_photos = len(bibdocs)

    for doc in bibdocs[:max_photos]:
        found_icons = []
        found_image_url = ''
        for docfile in doc.list_latest_files():
            if docfile.is_icon():
                found_icons.append((docfile.get_size(), docfile.get_url()))
            elif get_superformat_from_format(docfile.get_format()).lower() in [
                    ".jpg", ".gif", ".jpeg", ".png"
            ]:
                found_image_url = docfile.get_url()
        found_icons.sort()

        for icon_size, icon_url in found_icons:
            images.append(
                (icon_url, icon_url.replace(CFG_SITE_URL,
                                            CFG_SITE_SECURE_URL)))
        if found_image_url:
            images.append((found_image_url,
                           found_image_url.replace(CFG_SITE_URL,
                                                   CFG_SITE_SECURE_URL)))

    if CFG_CERN_SITE:
        # Add some more pictures from metadata
        additional_images = [
            (image_url,
             image_url.replace("http://mediaarchive.cern.ch/",
                               "https://mediastream.cern.ch"))
            for image_url in bfo.fields("8567_u")
            if image_url.split('.')[-1] in ('jpg', 'png', 'jpeg',
                                            'gif') and 'A5' in image_url
        ]
        images.extend(additional_images)

    tags = [
        '<meta property="og:image" content="%s" />%s' %
        (image_url, image_url != image_secure_url
         and '\n<meta property="og:image:secure_url" content="%s" />' %
         image_secure_url or "") for image_url, image_secure_url in images
    ]

    return "\n".join(tags)
def format_element(bfo):
    """
    Return the video of the record, suitable for the Open Graph protocol.
    """
    if not CFG_WEBSEARCH_ENABLE_OPENGRAPH:
        return ""
    bibarchive = BibRecDocs(bfo.recID)
    bibdocs = bibarchive.list_bibdocs()
    additional_tags = ""
    tags = []
    videos = []
    images = []

    for doc in bibdocs:
        found_icons = []
        found_image_url = ""
        for docfile in doc.list_latest_files():
            if docfile.is_icon():
                found_icons.append((docfile.get_size(), docfile.get_url()))
            elif get_superformat_from_format(docfile.get_format()).lower() in [".mp4", ".webm", ".ogv"]:
                found_image_url = docfile.get_url()
        found_icons.sort()

        for icon_size, icon_url in found_icons:
            images.append((icon_url, icon_url.replace(CFG_SITE_URL, CFG_SITE_SECURE_URL)))
        if found_image_url:
            videos.append((found_image_url, found_image_url.replace(CFG_SITE_URL, CFG_SITE_SECURE_URL)))

    if CFG_CERN_SITE:
        mp4_urls = [
            url.replace("http://mediaarchive.cern.ch", "https://mediastream.cern.ch")
            for url in bfo.fields("8567_u")
            if url.endswith(".mp4")
        ]
        img_urls = [
            url.replace("http://mediaarchive.cern.ch", "https://mediastream.cern.ch")
            for url in bfo.fields("8567_u")
            if url.endswith(".jpg") or url.endswith(".png")
        ]

        if mp4_urls:
            mp4_url = mp4_urls[0]
            if "4/3" in bfo.field("300__b"):
                width = "640"
                height = "480"
            else:
                width = "640"
                height = "360"
            additional_tags += """
                <meta property="og:video" content="%(CFG_CERN_PLAYER_URL)s?file=%(mp4_url_relative)s&streamer=%(CFG_STREAMER_URL)s&provider=rtmp&stretching=exactfit&image=%(image_url)s" />
                <meta property="og:video:height" content="%(height)s" />
                <meta property="og:video:width" content="%(width)s" />
                <meta property="og:video:type" content="application/x-shockwave-flash" />
                <meta property="og:video" content="%(mp4_url)s" />
                <meta property="og:video:type" content="video/mp4" />

                <link rel="image_src" href="%(image_url)s" />
                <link rel="video_src" href="%(CFG_CERN_PLAYER_URL)s?file=%(mp4_url_relative)s&streamer=%(CFG_STREAMER_URL)s&provider=rtmp&stretching=exactfit&image=%(image_url)s"/>
                """ % {
                "CFG_CERN_PLAYER_URL": "https://cds.cern.ch/mediaplayer.swf",
                "CFG_STREAMER_URL": "rtmp://wowza.cern.ch:1935/vod",
                "width": width,
                "height": height,
                "image_url": img_urls and img_urls[0] or "",
                "mp4_url": mp4_url.replace("http://mediaarchive.cern.ch", "https://mediastream.cern.ch"),
                "mp4_url_relative": "/" + "/".join(mp4_url.split("/")[4:]),
            }

    tags = [
        '<meta property="og:image" content="%s" />%s'
        % (
            image_url,
            image_url != image_secure_url
            and '\n<meta property="og:image:secure_url" content="%s" />' % image_secure_url
            or "",
        )
        for image_url, image_secure_url in images
    ]
    tags.extend(
        [
            '<meta property="og:video" content="%s" />%s'
            % (
                video_url,
                video_url != video_secure_url
                and '\n<meta property="og:video:secure_url" content="%s" />' % video_secure_url
                or "",
            )
            for video_url, video_secure_url in videos
        ]
    )

    return "\n".join(tags) + additional_tags
def format_element(bfo):
    """
    Return the video of the record, suitable for the Open Graph protocol.
    """
    if not CFG_WEBSEARCH_ENABLE_OPENGRAPH:
        return ""
    bibarchive = BibRecDocs(bfo.recID)
    bibdocs = bibarchive.list_bibdocs()
    additional_tags = ""
    tags = []
    videos = []
    images = []

    for doc in bibdocs:
        found_icons = []
        found_image_url = ''
        for docfile in doc.list_latest_files():
            if docfile.is_icon():
                found_icons.append((docfile.get_size(), docfile.get_url()))
            elif get_superformat_from_format(
                    docfile.get_format()).lower() in [".mp4", '.webm', '.ogv']:
                found_image_url = docfile.get_url()
        found_icons.sort()

        for icon_size, icon_url in found_icons:
            images.append(
                (icon_url, icon_url.replace(CFG_SITE_URL,
                                            CFG_SITE_SECURE_URL)))
        if found_image_url:
            videos.append((found_image_url,
                           found_image_url.replace(CFG_SITE_URL,
                                                   CFG_SITE_SECURE_URL)))

    if CFG_CERN_SITE:
        mp4_urls = [url.replace('http://mediaarchive.cern.ch', 'https://mediastream.cern.ch') \
                    for url in bfo.fields('8567_u') if url.endswith('.mp4')]
        img_urls = [url.replace('http://mediaarchive.cern.ch', 'https://mediastream.cern.ch') \
                    for url in bfo.fields('8567_u') if url.endswith('.jpg') or url.endswith('.png')]

        if mp4_urls:
            mp4_url = mp4_urls[0]
            if "4/3" in bfo.field("300__b"):
                width = "640"
                height = "480"
            else:
                width = "640"
                height = "360"
            additional_tags += '''
                <meta property="og:video" content="%(CFG_CERN_PLAYER_URL)s?file=%(mp4_url_relative)s&streamer=%(CFG_STREAMER_URL)s&provider=rtmp&stretching=exactfit&image=%(image_url)s" />
                <meta property="og:video:height" content="%(height)s" />
                <meta property="og:video:width" content="%(width)s" />
                <meta property="og:video:type" content="application/x-shockwave-flash" />
                <meta property="og:video" content="%(mp4_url)s" />
                <meta property="og:video:type" content="video/mp4" />

                <link rel="image_src" href="%(image_url)s" />
                <link rel="video_src" href="%(CFG_CERN_PLAYER_URL)s?file=%(mp4_url_relative)s&streamer=%(CFG_STREAMER_URL)s&provider=rtmp&stretching=exactfit&image=%(image_url)s"/>
                ''' % {
                'CFG_CERN_PLAYER_URL':
                "https://cdsweb.cern.ch/mediaplayer.swf",
                'CFG_STREAMER_URL':
                "rtmp://wowza.cern.ch:1935/vod",
                'width':
                width,
                'height':
                height,
                'image_url':
                img_urls and img_urls[0] or '',
                'mp4_url':
                mp4_url.replace('http://mediaarchive.cern.ch',
                                'https://mediastream.cern.ch'),
                'mp4_url_relative':
                '/' + '/'.join(mp4_url.split('/')[4:])
            }

    tags = [
        '<meta property="og:image" content="%s" />%s' %
        (image_url, image_url != image_secure_url
         and '\n<meta property="og:image:secure_url" content="%s" />' %
         image_secure_url or "") for image_url, image_secure_url in images
    ]
    tags.extend([
        '<meta property="og:video" content="%s" />%s' %
        (video_url, video_url != video_secure_url
         and '\n<meta property="og:video:secure_url" content="%s" />' %
         video_secure_url or "") for video_url, video_secure_url in videos
    ])

    return "\n".join(tags) + additional_tags
def format_element(bfo):
    """
    Return the video of the record, suitable for the Open Graph protocol.
    """
    if not CFG_WEBSEARCH_ENABLE_OPENGRAPH:
        return ""
    bibarchive = BibRecDocs(bfo.recID)
    bibdocs = bibarchive.list_bibdocs()
    additional_tags = ""
    tags = []
    videos = []
    images = []

    for doc in bibdocs:
        found_icons = []
        found_image_url = ''
        for docfile in doc.list_latest_files():
            if docfile.is_icon():
                found_icons.append((docfile.get_size(), docfile.get_url()))
            elif get_superformat_from_format(docfile.get_format()).lower() in [".mp4", '.webm', '.ogv']:
                found_image_url = docfile.get_url()
        found_icons.sort()

        for icon_size, icon_url in found_icons:
            images.append((icon_url, icon_url.replace(CFG_SITE_URL, CFG_SITE_SECURE_URL)))
        if found_image_url:
            videos.append((found_image_url, found_image_url.replace(CFG_SITE_URL, CFG_SITE_SECURE_URL)))

    if CFG_CERN_SITE:
        mp4_urls = [url.replace('http://mediaarchive.cern.ch', 'https://mediastream.cern.ch') \
                    for url in bfo.fields('8567_u') if url.endswith('.mp4')]
        img_urls = [url.replace('http://mediaarchive.cern.ch', 'https://mediastream.cern.ch') \
                    for url in bfo.fields('8567_u') if url.endswith('.jpg') or url.endswith('.png')]

        if mp4_urls:
            mp4_url = mp4_urls[0]
            if "4/3" in bfo.field("300__b"):
                width = "640"
                height = "480"
            else:
                width = "640"
                height = "360"
            additional_tags += '''
                <meta property="og:video" content="%(CFG_CERN_PLAYER_URL)s?file=%(mp4_url_relative)s&streamer=%(CFG_STREAMER_URL)s&provider=rtmp&stretching=exactfit&image=%(image_url)s" />
                <meta property="og:video:height" content="%(height)s" />
                <meta property="og:video:width" content="%(width)s" />
                <meta property="og:video:type" content="application/x-shockwave-flash" />
                <meta property="og:video" content="%(mp4_url)s" />
                <meta property="og:video:type" content="video/mp4" />
                <meta property="og:image" content="%(image_url)s" />
                <meta name="twitter:player:height" content="%(height)s" />
                <meta name="twitter:player:width" content="%(width)s" />

                <link rel="image_src" href="%(image_url)s" />
                <link rel="video_src" href="%(CFG_CERN_PLAYER_URL)s?file=%(mp4_url_relative)s&streamer=%(CFG_STREAMER_URL)s&provider=rtmp&stretching=exactfit&image=%(image_url)s"/>
                ''' % {'CFG_CERN_PLAYER_URL': "https://cds.cern.ch/mediaplayer.swf",
                       'CFG_STREAMER_URL': "rtmp://wowza.cern.ch:1935/vod",
                       'width': width,
                       'height': height,
                       'image_url': img_urls and img_urls[0] or '',
                       'mp4_url': mp4_url.replace('http://mediaarchive.cern.ch', 'https://mediastream.cern.ch'),
                       'mp4_url_relative': '/' + '/'.join(mp4_url.split('/')[4:])}
            try:
                from invenio.media_utils import generate_embedding_url
                embed_url = generate_embedding_url(bfo.field('037__a'))
                additional_tags += '''<meta name="twitter:player" content="%s"/>''' % cgi.escape(embed_url, quote=True).replace('http://', 'https://', 1)
            except:
                pass


    tags = ['<meta property="og:image" content="%s" />%s' % (image_url, image_url != image_secure_url and '\n<meta property="og:image:secure_url" content="%s" />' % image_secure_url or "") for image_url, image_secure_url in images]
    tags.extend(['<meta property="og:video" content="%s" />%s' % (video_url, video_url != video_secure_url and '\n<meta property="og:video:secure_url" content="%s" />' % video_secure_url or "") for video_url, video_secure_url in videos])


    return "\n".join(tags) + additional_tags