2012-01-10 09:00:29 +00:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# ci:si:et:sw=4:sts=4:ts=4
|
|
|
|
import re
|
2014-09-30 19:04:46 +00:00
|
|
|
|
|
|
|
from . import cache
|
|
|
|
from .text import find_re
|
|
|
|
from .utils import json, ET
|
2012-01-10 09:00:29 +00:00
|
|
|
|
2016-06-08 13:32:46 +00:00
|
|
|
|
2012-01-10 09:00:29 +00:00
|
|
|
def get_embed_code(url, maxwidth=None, maxheight=None):
|
|
|
|
embed = {}
|
2012-08-15 15:15:40 +00:00
|
|
|
header = cache.get_headers(url)
|
2012-01-10 09:00:29 +00:00
|
|
|
if header.get('content-type', '').startswith('text/html'):
|
2012-09-06 11:25:57 +00:00
|
|
|
html = cache.read_url(url)
|
2016-06-08 13:32:46 +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]
|
|
|
|
|
2012-01-10 09:00:29 +00:00
|
|
|
if json_oembed:
|
2012-08-14 14:12:43 +00:00
|
|
|
oembed_url = find_re(json_oembed[0], 'href="(.*?)"')
|
2012-01-10 09:00:29 +00:00
|
|
|
if maxwidth:
|
|
|
|
oembed_url += '&maxwidth=%d' % maxwidth
|
|
|
|
if maxheight:
|
|
|
|
oembed_url += '&maxheight=%d' % maxheight
|
2012-09-06 11:25:57 +00:00
|
|
|
embed = json.loads(cache.read_url(oembed_url))
|
2012-01-10 09:00:29 +00:00
|
|
|
elif xml_oembed:
|
2016-06-08 13:32:46 +00:00
|
|
|
oembed_url = find_re(xml_oembed[0], 'href="(.*?)"')
|
2012-01-10 09:00:29 +00:00
|
|
|
if maxwidth:
|
|
|
|
oembed_url += '&maxwidth=%d' % maxwidth
|
|
|
|
if maxheight:
|
|
|
|
oembed_url += '&maxheight=%d' % maxheight
|
2012-09-06 11:25:57 +00:00
|
|
|
data = cache.read_url(oembed_url)
|
2012-01-10 09:00:29 +00:00
|
|
|
for e in ET.fromstring(data):
|
|
|
|
embed[e.tag] = e.text
|
|
|
|
return embed
|