From 558d9aabab7e01c2e2b506aa362c70a568b953aa Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 10 Sep 2015 21:57:25 +0200 Subject: Move fetching content in a separate class --- src/Wallabag/CoreBundle/Helper/ContentProxy.php | 60 +++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/Wallabag/CoreBundle/Helper/ContentProxy.php (limited to 'src/Wallabag/CoreBundle/Helper') diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php new file mode 100644 index 00000000..2dd70e51 --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php @@ -0,0 +1,60 @@ +graby = $graby; + } + + /** + * Fetch content using graby and hydrate given entry with results information. + * In case we couldn't find content, we'll try to use Open Graph data + * + * @param Entry $entry Entry to update + * @param string $url Url to grab content for + * + * @return Entry + */ + public function updateEntry(Entry $entry, $url) + { + $content = $this->graby->fetchContent($url); + + $title = $content['title']; + if (!$title && isset($content['open_graph']['og_title'])) { + $title = $content['open_graph']['og_title']; + } + + $html = $content['html']; + if (false === $html) { + $html = '

Unable to retrieve readable content.

'; + + if (isset($content['open_graph']['og_description'])) { + $html .= '

But we found a short description:

'; + $html .= $content['open_graph']['og_description']; + } + } + + $entry->setUrl($content['url'] ?: $url); + $entry->setTitle($title); + $entry->setContent($html); + $entry->setMimetype($content['content_type']); + + if (isset($content['open_graph']['og_image'])) { + $entry->setPreviewPicture($content['open_graph']['og_image']); + } + + return $entry; + } +} -- cgit v1.2.3