Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
refactoring and adding youtube parser
Browse files Browse the repository at this point in the history
  • Loading branch information
kasp3r committed Apr 7, 2014
1 parent 7346996 commit f4ffeb3
Show file tree
Hide file tree
Showing 8 changed files with 340 additions and 66 deletions.
34 changes: 34 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<iframe id="ytplayer" type="text/html" width="640" height="390" src="http://www.youtube.com/embed/8ZcmTl_1ER8" frameborder="0"/>
```

## Todo
1. Add more unit tests
2. Update documentation
3. Add more parsers

## License

Expand Down
72 changes: 10 additions & 62 deletions src/LinkPreview/LinkPreview.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
Expand All @@ -219,5 +166,6 @@ public function getParsed()
protected function addDefaultParsers()
{
$this->addParser(new GeneralParser());
$this->addParser(new YoutubeParser());
}
}
77 changes: 77 additions & 0 deletions src/LinkPreview/Model/VideoLink.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace LinkPreview\Model;

class VideoLink extends Link
{
/**
* @var string $video Url to video
*/
private $video;

/**
* @var string $embedCode Video embed code
*/
private $embedCode;

/**
* @var string $videoId Video identification code
*/
private $videoId;

/**
* @return string
*/
public function getVideo()
{
return $this->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;
}


}
52 changes: 51 additions & 1 deletion src/LinkPreview/Parser/GeneralParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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
*/
Expand All @@ -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
*/
Expand Down Expand Up @@ -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/'))) {
Expand All @@ -88,7 +138,7 @@ public function parseLink()
$link->setImage($link->getRealUrl());
}

return $this;
return $link;
}

/**
Expand Down
27 changes: 26 additions & 1 deletion src/LinkPreview/Parser/ParserInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,41 @@
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
*
* @return string
*/
public function __toString();

/**
* Set reader
*
* @param ReaderInterface $reader
* @return $this
*/
public function setReader(ReaderInterface $reader);

/**
* Get reader
*
* @return ReaderInterface
*/
public function getReader();

/**
* Set model
*
Expand All @@ -38,7 +63,7 @@ public function isValidParser();
/**
* Parse link
*
* @return $this
* @return LinkInterface
*/
public function parseLink();
}
Loading

0 comments on commit f4ffeb3

Please sign in to comment.