mirror of
https://github.com/element-hq/synapse
synced 2024-08-20 02:40:22 +00:00
fix assorted redirect, unicode and screenscraping bugs
This commit is contained in:
parent
683e564815
commit
c60b751694
1 changed files with 89 additions and 71 deletions
|
@ -74,84 +74,93 @@ class PreviewUrlResource(BaseMediaResource):
|
||||||
elif self._is_html(media_info['media_type']):
|
elif self._is_html(media_info['media_type']):
|
||||||
# TODO: somehow stop a big HTML tree from exploding synapse's RAM
|
# TODO: somehow stop a big HTML tree from exploding synapse's RAM
|
||||||
|
|
||||||
# XXX: can't work out how to make lxml ignore UTF8 decoding errors
|
def _calc_og():
|
||||||
# so slurp as a string at this point.
|
# suck it up into lxml and define our OG response.
|
||||||
file = open(media_info['filename'])
|
# if we see any URLs in the OG response, then spider them
|
||||||
body = file.read()
|
# (although the client could choose to do this by asking for previews of those URLs to avoid DoSing the server)
|
||||||
file.close()
|
|
||||||
# FIXME: we shouldn't be forcing utf-8 if the page isn't actually utf-8...
|
|
||||||
tree = html.fromstring(body.decode('utf-8','ignore'))
|
|
||||||
# tree = html.parse(media_info['filename'])
|
|
||||||
|
|
||||||
# suck it up into lxml and define our OG response.
|
# "og:type" : "article"
|
||||||
# if we see any URLs in the OG response, then spider them
|
# "og:url" : "https://twitter.com/matrixdotorg/status/684074366691356672"
|
||||||
# (although the client could choose to do this by asking for previews of those URLs to avoid DoSing the server)
|
# "og:title" : "Matrix on Twitter"
|
||||||
|
# "og:image" : "https://pbs.twimg.com/profile_images/500400952029888512/yI0qtFi7_400x400.png"
|
||||||
|
# "og:description" : "Synapse 0.12 is out! Lots of polishing, performance & bugfixes: /sync API, /r0 prefix, fulltext search, 3PID invites https://t.co/5alhXLLEGP"
|
||||||
|
# "og:site_name" : "Twitter"
|
||||||
|
|
||||||
|
# or:
|
||||||
|
|
||||||
# "og:type" : "article"
|
# "og:type" : "video",
|
||||||
# "og:url" : "https://twitter.com/matrixdotorg/status/684074366691356672"
|
# "og:url" : "https://www.youtube.com/watch?v=LXDBoHyjmtw",
|
||||||
# "og:title" : "Matrix on Twitter"
|
# "og:site_name" : "YouTube",
|
||||||
# "og:image" : "https://pbs.twimg.com/profile_images/500400952029888512/yI0qtFi7_400x400.png"
|
# "og:video:type" : "application/x-shockwave-flash",
|
||||||
# "og:description" : "Synapse 0.12 is out! Lots of polishing, performance & bugfixes: /sync API, /r0 prefix, fulltext search, 3PID invites https://t.co/5alhXLLEGP"
|
# "og:description" : " ",
|
||||||
# "og:site_name" : "Twitter"
|
# "og:title" : "RemoteJam - Matrix team hack for Disrupt Europe Hackathon",
|
||||||
|
# "og:image" : "https://i.ytimg.com/vi/LXDBoHyjmtw/maxresdefault.jpg",
|
||||||
# or:
|
# "og:video:url" : "http://www.youtube.com/v/LXDBoHyjmtw?version=3&autohide=1",
|
||||||
|
# "og:video:width" : "1280"
|
||||||
|
# "og:video:height" : "720",
|
||||||
|
# "og:video:secure_url": "https://www.youtube.com/v/LXDBoHyjmtw?version=3&autohide=1",
|
||||||
|
|
||||||
# "og:type" : "video",
|
og = {}
|
||||||
# "og:url" : "https://www.youtube.com/watch?v=LXDBoHyjmtw",
|
for tag in tree.xpath("//*/meta[starts-with(@property, 'og:')]"):
|
||||||
# "og:site_name" : "YouTube",
|
og[tag.attrib['property']] = tag.attrib['content']
|
||||||
# "og:video:type" : "application/x-shockwave-flash",
|
|
||||||
# "og:description" : " ",
|
|
||||||
# "og:title" : "RemoteJam - Matrix team hack for Disrupt Europe Hackathon",
|
|
||||||
# "og:image" : "https://i.ytimg.com/vi/LXDBoHyjmtw/maxresdefault.jpg",
|
|
||||||
# "og:video:url" : "http://www.youtube.com/v/LXDBoHyjmtw?version=3&autohide=1",
|
|
||||||
# "og:video:width" : "1280"
|
|
||||||
# "og:video:height" : "720",
|
|
||||||
# "og:video:secure_url": "https://www.youtube.com/v/LXDBoHyjmtw?version=3&autohide=1",
|
|
||||||
|
|
||||||
og = {}
|
if 'og:title' not in og:
|
||||||
for tag in tree.xpath("//*/meta[starts-with(@property, 'og:')]"):
|
# do some basic spidering of the HTML
|
||||||
og[tag.attrib['property']] = tag.attrib['content']
|
title = tree.xpath("(//title)[1] | (//h1)[1] | (//h2)[1] | (//h3)[1]")
|
||||||
|
og['og:title'] = title[0].text if title else None
|
||||||
|
|
||||||
if not og:
|
|
||||||
# do some basic spidering of the HTML
|
|
||||||
title = tree.xpath("(//title)[1] | (//h1)[1] | (//h2)[1] | (//h3)[1]")
|
|
||||||
og['og:title'] = title[0].text if title else None
|
|
||||||
|
|
||||||
images = [ i for i in tree.xpath("//img") if 'src' in i.attrib ]
|
if 'og:image' not in og:
|
||||||
big_images = [ i for i in images if (
|
meta_image = tree.xpath("//*/meta[@itemprop='image']/@content");
|
||||||
'width' in i.attrib and 'height' in i.attrib and
|
if meta_image:
|
||||||
i.attrib['width'] > 64 and i.attrib['height'] > 64
|
og['og:image'] = self._rebase_url(meta_image[0], media_info['uri'])
|
||||||
)]
|
|
||||||
big_images = big_images.sort(key=lambda i: (-1 * int(i.attrib['width']) * int(i.attrib['height'])))
|
|
||||||
images = big_images if big_images else images
|
|
||||||
|
|
||||||
if images:
|
|
||||||
base = list(urlparse(media_info['uri']))
|
|
||||||
src = list(urlparse(images[0].attrib['src']))
|
|
||||||
if not src[0] and not src[1]:
|
|
||||||
src[0] = base[0]
|
|
||||||
src[1] = base[1]
|
|
||||||
if not src[2].startswith('/'):
|
|
||||||
src[2] = re.sub(r'/[^/]+$', '/', base[2]) + src[2]
|
|
||||||
og['og:image'] = urlunparse(src)
|
|
||||||
|
|
||||||
text_nodes = tree.xpath("//h1/text() | //h2/text() | //h3/text() | //p/text() | //div/text() | //span/text() | //a/text()")
|
|
||||||
# text_nodes = tree.xpath("//h1/text() | //h2/text() | //h3/text() | //p/text() | //div/text()")
|
|
||||||
text = ''
|
|
||||||
for text_node in text_nodes:
|
|
||||||
if len(text) < 1024:
|
|
||||||
text += text_node + ' '
|
|
||||||
else:
|
else:
|
||||||
break
|
images = [ i for i in tree.xpath("//img") if 'src' in i.attrib ]
|
||||||
text = re.sub(r'[\t ]+', ' ', text)
|
big_images = [ i for i in images if (
|
||||||
text = re.sub(r'[\t \r\n]*[\r\n]+', '\n', text)
|
'width' in i.attrib and 'height' in i.attrib and
|
||||||
text = text.strip()[:1024]
|
i.attrib['width'] > 64 and i.attrib['height'] > 64
|
||||||
og['og:description'] = text if text else None
|
)]
|
||||||
|
big_images = big_images.sort(key=lambda i: (-1 * int(i.attrib['width']) * int(i.attrib['height'])))
|
||||||
|
images = big_images if big_images else images
|
||||||
|
|
||||||
|
if images:
|
||||||
|
og['og:image'] = self._rebase_url(images[0].attrib['src'], media_info['uri'])
|
||||||
|
|
||||||
|
if 'og:description' not in og:
|
||||||
|
meta_description = tree.xpath("//*/meta[@name='description']/@content");
|
||||||
|
if meta_description:
|
||||||
|
og['og:description'] = meta_description[0]
|
||||||
|
else:
|
||||||
|
text_nodes = tree.xpath("//h1/text() | //h2/text() | //h3/text() | //p/text() | //div/text() | //span/text() | //a/text()")
|
||||||
|
# text_nodes = tree.xpath("//h1/text() | //h2/text() | //h3/text() | //p/text() | //div/text()")
|
||||||
|
text = ''
|
||||||
|
for text_node in text_nodes:
|
||||||
|
if len(text) < 500:
|
||||||
|
text += text_node + ' '
|
||||||
|
else:
|
||||||
|
break
|
||||||
|
text = re.sub(r'[\t ]+', ' ', text)
|
||||||
|
text = re.sub(r'[\t \r\n]*[\r\n]+', '\n', text)
|
||||||
|
text = text.strip()[:500]
|
||||||
|
og['og:description'] = text if text else None
|
||||||
|
|
||||||
|
# TODO: extract a favicon?
|
||||||
|
# TODO: turn any OG media URLs into mxc URLs to capture and thumbnail them too
|
||||||
|
# TODO: store our OG details in a cache (and expire them when stale)
|
||||||
|
# TODO: delete the content to stop diskfilling, as we only ever cared about its OG
|
||||||
|
return og
|
||||||
|
|
||||||
|
try:
|
||||||
|
tree = html.parse(media_info['filename'])
|
||||||
|
og = _calc_og()
|
||||||
|
except UnicodeDecodeError:
|
||||||
|
# XXX: evil evil bodge
|
||||||
|
file = open(media_info['filename'])
|
||||||
|
body = file.read()
|
||||||
|
file.close()
|
||||||
|
tree = html.fromstring(body.decode('utf-8','ignore'))
|
||||||
|
og = _calc_og()
|
||||||
|
|
||||||
# TODO: extract a favicon?
|
|
||||||
# TODO: turn any OG media URLs into mxc URLs to capture and thumbnail them too
|
|
||||||
# TODO: store our OG details in a cache (and expire them when stale)
|
|
||||||
# TODO: delete the content to stop diskfilling, as we only ever cared about its OG
|
|
||||||
else:
|
else:
|
||||||
logger.warn("Failed to find any OG data in %s", url)
|
logger.warn("Failed to find any OG data in %s", url)
|
||||||
og = {}
|
og = {}
|
||||||
|
@ -173,6 +182,15 @@ class PreviewUrlResource(BaseMediaResource):
|
||||||
)
|
)
|
||||||
raise
|
raise
|
||||||
|
|
||||||
|
def _rebase_url(self, url, base):
|
||||||
|
base = list(urlparse(base))
|
||||||
|
url = list(urlparse(url))
|
||||||
|
if not url[0] and not url[1]:
|
||||||
|
url[0] = base[0]
|
||||||
|
url[1] = base[1]
|
||||||
|
if not url[2].startswith('/'):
|
||||||
|
url[2] = re.sub(r'/[^/]+$', '/', base[2]) + url[2]
|
||||||
|
return urlunparse(url)
|
||||||
|
|
||||||
@defer.inlineCallbacks
|
@defer.inlineCallbacks
|
||||||
def _download_url(self, url, user):
|
def _download_url(self, url, user):
|
||||||
|
@ -223,7 +241,7 @@ class PreviewUrlResource(BaseMediaResource):
|
||||||
download_name = None
|
download_name = None
|
||||||
|
|
||||||
yield self.store.store_local_media(
|
yield self.store.store_local_media(
|
||||||
media_id=fname,
|
media_id=file_id,
|
||||||
media_type=media_type,
|
media_type=media_type,
|
||||||
time_now_ms=self.clock.time_msec(),
|
time_now_ms=self.clock.time_msec(),
|
||||||
upload_name=download_name,
|
upload_name=download_name,
|
||||||
|
|
Loading…
Reference in a new issue