Skip to content

Commit

Permalink
UriManagerのFC2動画url解決バグを修正 #15
Browse files Browse the repository at this point in the history
  • Loading branch information
app2641 committed Jul 1, 2014
1 parent dde5e09 commit a5dfbe2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/Midnight/Crawler/UriManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
class UriManager
{

/**
* @var string
**/
private $raw_url;


/**
* 分解されたurlデータ
*
Expand Down Expand Up @@ -34,6 +40,7 @@ private function _getBaseUrl ()
**/
public function resolve ($url)
{
$this->raw_url = $url;
$this->parse_data = parse_url($url);
if (! isset($this->parse_data['host'])) return false;

Expand Down Expand Up @@ -103,10 +110,19 @@ private function _resolveAsgToUrl ()
**/
public function _resolveFc2Url ()
{
// ja/a/content あるいは content を url に含む場合、
// それが既に動画のurlとなっている為、そのまま返す
// pattern: http://video.fc2.com/(ja/a/|)content
$pattern = $this->parse_data['scheme'].':\/\/'.$this->parse_data['host'].'(\/ja\/a|)\/content';
if (preg_match('/^'.$pattern.'/', $this->raw_url)) {
return $this->raw_url;
}

$base_url = $this->_getBaseUrl();
if (! isset($this->parse_data['query'])) return $base_url;

// iという値を抽出する
// iという値を抽出してurlを構築する
// e.g. http://video.fc2.com/flv2.swf?i=20130827LybYzuyu&d=2185
preg_match('/i=([a-zA-Z0-9]*).*/', $this->parse_data['query'], $matches);
if (! isset($matches[1])) return $base_url;

Expand Down
5 changes: 5 additions & 0 deletions tests/Midnight/Crawler/UriManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ public function FC2のurlを整形する場合 ()
$url = $this->manager->resolve($url);
$this->assertEquals('http://video.fc2.com/content/20130827LybYzuyu', $url);

// 正しい動画url
$url = 'http://video.fc2.com/ja/a/content/20140518019MUw9N/';
$url = $this->manager->resolve($url);
$this->assertEquals('http://video.fc2.com/ja/a/content/20140518019MUw9N/', $url);

// 誤った動画url
$url = 'http://video.fc2.com/flv2.swf?d31sd=adflkadg';
$url = $this->manager->resolve($url);
Expand Down

0 comments on commit a5dfbe2

Please sign in to comment.