diff --git a/README.md b/README.md
index b107628..0818775 100644
--- a/README.md
+++ b/README.md
@@ -62,9 +62,43 @@ GitHub is the best place to build software together. Over 4 million people use G
https://github.global.ssl.fastly.net/images/modules/open_graph/github-octocat.png
```
+```php
+$linkPreview = new LinkPreview('https://www.youtube.com/watch?v=8ZcmTl_1ER8');
+$parsed = $linkPreview->getParsed();
+foreach ($parsed as $parserName => $link) {
+ echo $parserName . PHP_EOL . PHP_EOL;
+
+ echo $link->getUrl() . PHP_EOL;
+ echo $link->getRealUrl() . PHP_EOL;
+ echo $link->getTitle() . PHP_EOL;
+ echo $link->getDescription() . PHP_EOL;
+ echo $link->getImage() . PHP_EOL;
+ echo $link->getVideoId() . PHP_EOL;
+ echo $link->getEmbedCode() . PHP_EOL;
+}
+```
+
+
+**Output**
+
+```
+youtube
+
+https://www.youtube.com/watch?v=8ZcmTl_1ER8
+http://gdata.youtube.com/feeds/api/videos/8ZcmTl_1ER8?v=2&alt=jsonc
+Epic sax guy 10 hours
+I had to remove my original one so I reuploaded this with much better quality.
+(If you want it sound like previous one, try setting quality to 240p)
+Yeah, I know that video sucks compared to original but no can do :(
+http://i1.ytimg.com/vi/8ZcmTl_1ER8/hqdefault.jpg
+8ZcmTl_1ER8
+
+```
+
## Todo
1. Add more unit tests
2. Update documentation
+3. Add more parsers
## License
diff --git a/src/LinkPreview/LinkPreview.php b/src/LinkPreview/LinkPreview.php
index 6111187..41ae42c 100644
--- a/src/LinkPreview/LinkPreview.php
+++ b/src/LinkPreview/LinkPreview.php
@@ -2,24 +2,17 @@
namespace LinkPreview;
-use LinkPreview\Model\Link;
use LinkPreview\Model\LinkInterface;
use LinkPreview\Parser\GeneralParser;
use LinkPreview\Parser\ParserInterface;
-use LinkPreview\Reader\GeneralReader;
-use LinkPreview\Reader\ReaderInterface;
+use LinkPreview\Parser\YoutubeParser;
class LinkPreview
{
/**
- * @var LinkInterface $link
+ * @var string $url
*/
- private $link;
-
- /**
- * @var ReaderInterface $reader
- */
- private $reader;
+ private $url;
/**
* @var ParserInterface[]
@@ -49,55 +42,17 @@ public function __construct($url = null)
*/
public function setUrl($url)
{
- $this->setLink(new Link($url));
-
- return $this;
- }
-
- /**
- * Set model
- *
- * @param LinkInterface $link Link model
- * @return $this
- */
- public function setLink(LinkInterface $link)
- {
- $this->link = $link;
+ $this->url = $url;
return $this;
}
/**
- * Get model
- *
- * @return LinkInterface
+ * @return string
*/
- public function getLink()
+ public function getUrl()
{
- return $this->link;
- }
-
- /**
- * Get reader
- *
- * @return ReaderInterface
- */
- public function getReader()
- {
- return $this->reader;
- }
-
- /**
- * Set reader
- *
- * @param ReaderInterface $reader
- * @return $this
- */
- public function setReader($reader)
- {
- $this->reader = $reader;
-
- return $this;
+ return $this->url;
}
/**
@@ -190,19 +145,11 @@ public function getParsed()
$this->addDefaultParsers();
}
- if (null === $this->getReader()) {
- $this->setReader(new GeneralReader());
- }
-
- $reader = $this->getReader()->setLink($this->getLink());
- $link = $reader->readLink()->getLink();
- $this->setLink($link);
-
foreach ($this->getParsers() as $name => $parser) {
- $parser->setLink($this->getLink());
+ $parser->getLink()->setUrl($this->getUrl());
if ($parser->isValidParser()) {
- $parsed[$name] = $parser->parseLink()->getLink();
+ $parsed[$name] = $parser->parseLink();
if (!$this->getPropagation()) {
break;
@@ -219,5 +166,6 @@ public function getParsed()
protected function addDefaultParsers()
{
$this->addParser(new GeneralParser());
+ $this->addParser(new YoutubeParser());
}
}
\ No newline at end of file
diff --git a/src/LinkPreview/Model/VideoLink.php b/src/LinkPreview/Model/VideoLink.php
new file mode 100644
index 0000000..45691df
--- /dev/null
+++ b/src/LinkPreview/Model/VideoLink.php
@@ -0,0 +1,77 @@
+video;
+ }
+
+ /**
+ * @param string $video
+ * @return $this
+ */
+ public function setVideo($video)
+ {
+ $this->video = $video;
+
+ return $this;
+ }
+
+ /**
+ * @return string
+ */
+ public function getEmbedCode()
+ {
+ return $this->embedCode;
+ }
+
+ /**
+ * @param string $embedCode
+ * @return $this
+ */
+ public function setEmbedCode($embedCode)
+ {
+ $this->embedCode = $embedCode;
+
+ return $this;
+ }
+
+ /**
+ * @return string
+ */
+ public function getVideoId()
+ {
+ return $this->videoId;
+ }
+
+ /**
+ * @param string $videoId
+ */
+ public function setVideoId($videoId)
+ {
+ $this->videoId = $videoId;
+ }
+
+
+}
\ No newline at end of file
diff --git a/src/LinkPreview/Parser/GeneralParser.php b/src/LinkPreview/Parser/GeneralParser.php
index 23c0dfc..adbc2bd 100644
--- a/src/LinkPreview/Parser/GeneralParser.php
+++ b/src/LinkPreview/Parser/GeneralParser.php
@@ -2,7 +2,10 @@
namespace LinkPreview\Parser;
+use LinkPreview\Model\Link;
use LinkPreview\Model\LinkInterface;
+use LinkPreview\Reader\GeneralReader;
+use LinkPreview\Reader\ReaderInterface;
class GeneralParser implements ParserInterface
{
@@ -29,6 +32,26 @@ class GeneralParser implements ParserInterface
*/
private $link;
+ /**
+ * @var ReaderInterface $reader
+ */
+ private $reader;
+
+ public function __construct(ReaderInterface $reader = null, LinkInterface $link = null)
+ {
+ if (null !== $reader) {
+ $this->setReader($reader);
+ } else {
+ $this->setReader(new GeneralReader());
+ }
+
+ if (null !== $link) {
+ $this->setLink($link);
+ } else {
+ $this->setLink(new Link());
+ }
+ }
+
/**
* @inheritdoc
*/
@@ -37,6 +60,25 @@ public function __toString()
return 'general';
}
+ /**
+ * @return ReaderInterface
+ */
+ public function getReader()
+ {
+ return $this->reader;
+ }
+
+ /**
+ * @param ReaderInterface $reader
+ * @return $this
+ */
+ public function setReader(ReaderInterface $reader)
+ {
+ $this->reader = $reader;
+
+ return $this;
+ }
+
/**
* @inheritdoc
*/
@@ -71,11 +113,19 @@ public function isValidParser()
return $isValid;
}
+ private function readLink()
+ {
+ $reader = $this->getReader()->setLink($this->getLink());
+ $this->setLink($reader->readLink());
+ }
+
/**
* @inheritdoc
*/
public function parseLink()
{
+ $this->readLink();
+
$link = $this->getLink();
if (!strncmp($link->getContentType(), 'text/', strlen('text/'))) {
@@ -88,7 +138,7 @@ public function parseLink()
$link->setImage($link->getRealUrl());
}
- return $this;
+ return $link;
}
/**
diff --git a/src/LinkPreview/Parser/ParserInterface.php b/src/LinkPreview/Parser/ParserInterface.php
index af47011..106c17d 100644
--- a/src/LinkPreview/Parser/ParserInterface.php
+++ b/src/LinkPreview/Parser/ParserInterface.php
@@ -3,9 +3,19 @@
namespace LinkPreview\Parser;
use LinkPreview\Model\LinkInterface;
+use LinkPreview\Reader\ReaderInterface;
interface ParserInterface
{
+ /**
+ * Set default reader and model
+ *
+ *
+ * @param ReaderInterface $reader
+ * @param LinkInterface $link
+ */
+ public function __construct(ReaderInterface $reader = null, LinkInterface $link = null);
+
/**
* Parser name
*
@@ -13,6 +23,21 @@ interface ParserInterface
*/
public function __toString();
+ /**
+ * Set reader
+ *
+ * @param ReaderInterface $reader
+ * @return $this
+ */
+ public function setReader(ReaderInterface $reader);
+
+ /**
+ * Get reader
+ *
+ * @return ReaderInterface
+ */
+ public function getReader();
+
/**
* Set model
*
@@ -38,7 +63,7 @@ public function isValidParser();
/**
* Parse link
*
- * @return $this
+ * @return LinkInterface
*/
public function parseLink();
}
\ No newline at end of file
diff --git a/src/LinkPreview/Parser/YoutubeParser.php b/src/LinkPreview/Parser/YoutubeParser.php
new file mode 100644
index 0000000..e8f6ac8
--- /dev/null
+++ b/src/LinkPreview/Parser/YoutubeParser.php
@@ -0,0 +1,141 @@
+setReader($reader);
+ } else {
+ $this->setReader(new GeneralReader());
+ }
+
+ if (null !== $link) {
+ $this->setLink($link);
+ } else {
+ $this->setLink(new VideoLink());
+ }
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function __toString()
+ {
+ return 'youtube';
+ }
+
+ /**
+ * @return ReaderInterface
+ */
+ public function getReader()
+ {
+ return $this->reader;
+ }
+
+ /**
+ * @param ReaderInterface $reader
+ * @return $this
+ */
+ public function setReader(ReaderInterface $reader)
+ {
+ $this->reader = $reader;
+
+ return $this;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function setLink(LinkInterface $link)
+ {
+ $this->link = $link;
+
+ return $this;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function getLink()
+ {
+ return $this->link;
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function isValidParser()
+ {
+ $isValid = false;
+
+ $link = $this->getLink();
+ $url = $link->getUrl();
+
+ if (is_string($url) && preg_match(static::PATTERN, $url, $matches)) {
+ $link->setVideoId($matches[1]);
+ $isValid = true;
+ }
+
+ return $isValid;
+ }
+
+ private function readLink()
+ {
+ $link = $this->getLink();
+ $originalUrl = $link->getUrl();
+ // change url to api link
+ $link->setUrl(str_replace('[id]', $link->getVideoId(), self::YOUTUBE_API_URL));
+
+ $reader = $this->getReader()->setLink($link);
+ $link = $reader->readLink();
+ // change to original url after reading
+ $link->setUrl($originalUrl);
+
+ $this->setLink($link);
+ }
+
+ /**
+ * @inheritdoc
+ */
+ public function parseLink()
+ {
+ $this->readLink();
+
+ $link = $this->getLink();
+ $data = json_decode($link->getContent());
+
+ $link->setTitle($data->data->title)
+ ->setDescription($data->data->description)
+ ->setImage($data->data->thumbnail->hqDefault)
+ ->setEmbedCode(
+ ''
+ );
+
+ return $link;
+ }
+}
\ No newline at end of file
diff --git a/src/LinkPreview/Reader/GeneralReader.php b/src/LinkPreview/Reader/GeneralReader.php
index 5052426..003e073 100644
--- a/src/LinkPreview/Reader/GeneralReader.php
+++ b/src/LinkPreview/Reader/GeneralReader.php
@@ -44,6 +44,6 @@ public function readLink()
->setContentType($response->getContentType())
->setRealUrl($response->getEffectiveUrl());
- return $this;
+ return $link;
}
}
\ No newline at end of file
diff --git a/src/LinkPreview/Reader/ReaderInterface.php b/src/LinkPreview/Reader/ReaderInterface.php
index 23ee79c..ab41d7b 100644
--- a/src/LinkPreview/Reader/ReaderInterface.php
+++ b/src/LinkPreview/Reader/ReaderInterface.php
@@ -25,7 +25,6 @@ public function getLink();
* Read and update model
*
* @return LinkInterface
- * @return $this
*/
public function readLink();
}
\ No newline at end of file