<?php
class VGrab_nudevibes
{
    var $page;
    var $curl;
    function __construct()
    {
        $this->curl = new VCurl();
    }
    
    function VGrab_nudevibes()
    {
        $this->__construct();
    }

    function getPage( $url ) {
        if ( $this->page = $this->curl->saveToString($url) ) {
            $this->page = trim($this->page);
            $this->page = str_replace("\n", '', $this->page);
            $this->page = str_replace("\r", '', $this->page);
            $this->page = preg_replace('/\s\s+/', ' ', $this->page);
            return true;
        }
        
        return false;
    }
    
    function getVideoTitle()
    {
		preg_match('/<h1>(.*?)<\/h1> <div id="xmoov-flv-player_va">/ms', $this->page, $matches);
        if ( isset($matches['1']) ) {
            return $matches['1'];
        }
    }
    
    function getVideoTags()
    {
		preg_match('/Tags:(.*?)<\/div>/ms', $this->page, $matches);
        if ( isset($matches['1']) ) {
			$links = str_replace('<span class="tag" id="1">', '', $matches['1']);
			$links = str_replace('</span>', '', $links);
			$links  = explode(' ', trim($links));
            $tags   = array();
            foreach ($links as $link) {
                preg_match('/>(.*?)<\/a>/ms', $link, $matches);
                if (isset($matches['1'])) {
                    $tags[] = $matches['1'];
                }
            }

            return implode(' ', $tags);
        }
    }
    
    function getVideoCategory()
    {
        preg_match('/<A href="http:\/\/www.nudevibes.com\/browse\/categories\/videos\/(.*?)">(.*?)<\/A>/ms', $this->page, $matches);
        if ( isset($matches['0']) ) {
            $category   = str_replace('</A>', '', $matches['0']);
            $category   = substr($category, strrpos($category, '>')+1);

            return $category;
        }
    }
    
    function getVideoId()
    {
        preg_match('/so.addVariable\("content_video", "(.*?).flv"\)\;/ms', $this->page, $matches);
        if ( isset($matches['1']) ) {
			$this->video_id = $matches['1'];
            return $this->video_id;
        }
    }
    
    function getVideoUrl()
    {
        return 'http://66.55.139.53/vdata/' .$this->video_id. '.flv';
    }
}