openmedialibrary_platform/Shared/lib/python3.4/site-packages/ox/oembed.py

36 lines
1.2 KiB
Python
Raw Normal View History

2013-10-11 17:28:32 +00:00
# -*- coding: utf-8 -*-
# ci:si:et:sw=4:sts=4:ts=4
import re
2014-09-30 16:15:32 +00:00
from . import cache
from .text import find_re
from .utils import json, ET
2013-10-11 17:28:32 +00:00
2018-12-15 00:08:54 +00:00
2013-10-11 17:28:32 +00:00
def get_embed_code(url, maxwidth=None, maxheight=None):
embed = {}
header = cache.get_headers(url)
if header.get('content-type', '').startswith('text/html'):
html = cache.read_url(url)
2018-12-15 00:08:54 +00:00
links = re.compile('<link.*?>').findall(html)
json_oembed = [l for l in links if 'json+oembed' in l]
xml_oembed = [l for l in links if 'xml+oembed' in l]
2013-10-11 17:28:32 +00:00
if json_oembed:
oembed_url = find_re(json_oembed[0], 'href="(.*?)"')
if maxwidth:
oembed_url += '&maxwidth=%d' % maxwidth
if maxheight:
oembed_url += '&maxheight=%d' % maxheight
embed = json.loads(cache.read_url(oembed_url))
elif xml_oembed:
2018-12-15 00:08:54 +00:00
oembed_url = find_re(xml_oembed[0], 'href="(.*?)"')
2013-10-11 17:28:32 +00:00
if maxwidth:
oembed_url += '&maxwidth=%d' % maxwidth
if maxheight:
oembed_url += '&maxheight=%d' % maxheight
data = cache.read_url(oembed_url)
for e in ET.fromstring(data):
embed[e.tag] = e.text
return embed