localdisk

PHP とか Java とか Web とか好きなことを書きます。

Services_Yourfilehostを作った

みんなの夜のお供として定評のあるYourFileHost.com(会社で開かないように注意して下さい)。もちろん僕も大好きなのですが、使いにくいことこの上ありません。何回か続けて動画をみようとしたらログイン画面とかでるし。いい加減いらっとしたので、カッとなって作りました。後悔はしていない。

<?php
class Services_Yourfilehost {
    private $_url;
    private $_params;
    private $_query;
    public function __construct($url = null) {
        if ($url === null) throw new Exception;
        if (!$this->_varidateUrl($url)) throw new Exception;
        $this->_url = $url;
        $this->_connect($url);
    }
    public function __get($name) {
        if (array_key_exists($name, $this->_query)) {
            return urldecode($this->_query[$name]);
        }
        return null;
    }
    private function _connect($url) {
        $html = file_get_contents($url);
        $html = preg_replace('/iso-8859-1/i', 'UTF-8', $html);
        $html = mb_convert_encoding($html, 'UTF-8', 'iso-8859-1');

        $doc = new DOMDocument;
        @$doc->loadHTML($html);
        $xpath = new DOMXPath($doc);
        $tags = $xpath->query('//param[@name="movie"]');
        if ($tags->length === 0) {
            return null;
        }
        $tag = $tags->item(0);
        $value = parse_url($tag->getAttribute('value'));
        $params = $this->_setParams(explode('&', $value['query']));
        $this->_query = $this->_setParams(explode('&', file_get_contents(urldecode($params['video']))));
    }
    private function _varidateUrl($url) {
        $hash = parse_url($url);
        if (!$hash) return false;
        if ($hash['host'] !== 'www.yourfilehost.com' ||
            $hash['path'] !== '/media.php') {
            return false;
        }
        $this->_params = $this->_setParams(explode('&', $hash['query']));
        if (!array_key_exists('file', $this->_params)) {
            return false;
        }
        return true;
    }
    private function _setParams($params) {
        $q = array();
        foreach ($params as $param) {
            list($key, $value) = explode('=', $param);
            $q[$key] = $value;
        }
        return $q;
    }
}

使い方はこんなかんじ。

<?php
$y = new Services_Yourfilehost('http://www.yourfilehost.com/media.php?cat=video&file=hogehogehoge.wmv');
var_dump($y->video_id); // flvファイル取得
var_dump($y->photo);    // サムネイル取得

ぶっちゃけWWW::YourFileHostのPHP版です。ちゃんとコメント書いたらOpenpearにリリースするかも。



4ヶ月振りにブログ書いたと思ったらこれだよ!

(追記)
Tube8(会社で開いたらダメ!ゼッタイ!)でも同じような方法で取得できますね。
embed要素のflashvars属性からサムネイルとflvが取得できる。YourFileHostよりも単純。R-18動画のマッシュアップサイトでも作るか。
(追記その2)
Openpearにリリースしました。