<commit_before><?php
/**
 * Name:        Video.php
 * Description: Handles video import and serves as an abstraction layer to the database
 * Date:        10/31/13
 * Programmer:  <NAME>
 */

//includes
if(!(defined('ABSPATH'))){
    require_once('../../path.php');
}
require_once(ABSPATH.'includes/controllers/data.php');
require_once(ABSPATH.'includes/libraries/getID3-1.9.7/getid3/getid3.php');

class video {

    //Define variables

        //Database Fields

            //metadata table

            public $index = '';
            public $subset_id = '';
            public $imdb_id                 = '';
            public $cover                   = '';
            public $title                   = '';
            public $plot_simple             = '';
            public $year                    = '';
            public $rated                   = '';
            public $rating                  = '';
            public $runtime                 = '';
            public $genres                  = '';
            public $language                = '';
            public $country                 = '';
            public $actors                  = '';
            public $writers                 = '';
            public $directors               = '';
            public $filming_locations       = '';
            public $season                  = '';
            public $episode                 = '';
            public $episode_name            = '';
            public $episode_description     = '';
            public $comments                = '';


            //media table

            public $media_index             = '';
            public $media_metadata_id       = '';
            public $media_location          = '';
            public $media_comments          = '';

    //Control Variables
    public $item                    = '';
    public $dbc                     = '';
    public $debug                   = true;
    public $output_buffer           = '';


    //read_meta
    public function read_meta($filename){

        /**
         * Reads metadata from a video file
         */

        //Define which file we will read
        $filename = ABSPATH.'content/uploads/'.$filename;
        $this->media_location = $filename;

        //setup getID3
        $getID3 = new getID3;

        // Analyze file and store returned data in $ThisFileInfo
        $ThisFileInfo = $getID3->analyze($filename);
        getid3_lib::CopyTagsToComments($ThisFileInfo);

        //Setup the array
        $meta = array();

        //Setup output buffering
        ob_start();
        echo '<h3>Function: read_meta() called:</h3>'."\r\n";

        //Type of video (tv show, movie, etc) (not supported currently in db)
        /*
        if(isset($ThisFileInfo['tags']['quicktime']['stik'][0])){
            echo "<br />\r\n".'Type:        '.$ThisFileInfo['tags']['quicktime']['stik'][0];
            $meta['type'] = $ThisFileInfo['tags']['quicktime']['stik'][0];
        }else{
            $meta['type'] = '';
        }
        */


        //TV Show Name (name of the series)
        if(isset($ThisFileInfo['tags']['quicktime']['tv_show_name'][0])){
            echo "<br />\r\n".'Name:        '.$ThisFileInfo['tags']['quicktime']['tv_show_name'][0];
            $meta['tv_show_name'] = $ThisFileInfo['tags']['quicktime']['tv_show_name'][0];
            $this->title = $meta['tv_show_name'];
        }else{
            $meta['tv_show_name'] = '';
        }

        //TV Season (typically int)
        if(isset($ThisFileInfo['tags']['quicktime']['tv_season'][0])){
            echo "<br />\r\n".'Season:      '.$ThisFileInfo['tags']['quicktime']['tv_season'][0];
            $meta['tv_season'] = $ThisFileInfo['tags']['quicktime']['tv_season'][0];
            $this->season = $meta['tv_season'];
        }else{
            $meta['tv_season'] = '';
        }

        //TV Episode (typically int)
        if(isset($ThisFileInfo['tags']['quicktime']['tv_episode'][0])){
            echo "<br />\r\n".'Episode:     '.$ThisFileInfo['tags']['quicktime']['tv_episode'][0];
            $meta['tv_episode'] = $ThisFileInfo['tags']['quicktime']['tv_episode'][0];
            $this->episode = $meta['tv_episode'];
        }else{
            $meta['tv_episode'] = '';
        }

        //Description
        if(isset($ThisFileInfo['tags']['quicktime']['description'][0])){
            echo "<br />\r\n".'Description: '.$ThisFileInfo['tags']['quicktime']['description'][0];
            $meta['description'] = $ThisFileInfo['tags']['quicktime']['description'][0];
            $this->episode_description = $meta['description'];
        }else{
            $meta['description'] = '';
        }

        //TV Network Name (not currently supported in db)
        /*
        if(isset($ThisFileInfo['tags']['quicktime']['tv_nework_name'][0])){
            //echo "<br />\r\n".'Network:     '.$ThisFileInfo['tags']['quicktime']['tv_network_name'][0];
            $meta['tv_network_name'] = $ThisFileInfo['tags']['quicktime']['tv_network_name'][0];
        }else{
            $meta['tv_network_name'] = '';
        }
        */

        //End output buffering
        echo "<hr /><br /><br />\r\n\r\n";
        $this->output_buffer = $this->output_buffer.ob_end_flush();

        //Return the metadata
        return $meta;

    }

    //lookup
    public function lookup(){

        /**
         * Determines if a file is already in the database
         */

        //Setup output buffering
        ob_start();
        echo '<h3>Function: lookup() called:</h3>'."\r\n";

        //Setup the database connection
        $this->dbc = new db;
        $this->dbc->connect();

        //Determine if the name is in the database

        //Prep string for search
        $name = $this->dbc->sanitize($this->media_location);
        $name = strtolower($name);

        //Query the database
        $query = "SELECT * FROM `media` WHERE `location` LIKE '%".$name."%'";
        $array = $this->dbc->query($query);

        //Close the database connection
        $this->dbc->close();

        //Process the results
        if(count($array) >= 1 && isset($array[0]['index'])){

            //The file IS in the db
            echo 'File IS in the database.<br />'."\r\n";
            return true;

        }else{

            //The file is NOT in the db
            echo 'File is NOT in the database.<br />'."\r\n";
            return false;

        }

        //Save the output buffer contents in the output variable
        echo "<hr /><br /><br />\r\n\r\n";
        $this->output_buffer = $this->output_buffer.ob_end_flush();

    }

    //insert
    public function insert(){

        /**
         * Inserts a video file into the database
         */

    }

    //fetch_imdb
    public function fetch_imdb() {

        /**
         * Fetches meta data from IMDb
         */

        //Determine how we will be searching IMDb
        if(!(empty($this->imdb_id))){

            //Search via IMDb id
            $imdb_id = urlencode($this->imdb_id);
            $url = 'http://mymovieapi.com/?ids='.$imdb_id.'&type=json&plot=simple&episode=1&lang=en-US&aka=simple&release=simple&business=0&tech=0';

        }elseif(!(empty($this->title))){<commit_msg>Continued work on video class.<commit_after><?php
/**
 * Name:        Video.php
 * Description: Handles video import and serves as an abstraction layer to the database
 * Date:        10/31/13
 * Programmer:  <NAME>
 */

//includes
if(!(defined('ABSPATH'))){
    require_once('../../path.php');
}
require_once(ABSPATH.'includes/controllers/data.php');
require_once(ABSPATH.'includes/libraries/getID3-1.9.7/getid3/getid3.php');

class video {

    //Define variables

        //Database Fields

            //metadata table

            public $index = '';
            public $subset_id = '';
            public $imdb_id                 = '';
            public $cover                   = '';
            public $title                   = '';
            public $plot_simple             = '';
            public $year                    = '';
            public $rated                   = '';
            public $rating                  = '';
            public $runtime                 = '';
            public $genres                  = '';
            public $language                = '';
            public $country                 = '';
            public $actors                  = '';
            public $writers                 = '';
            public $directors               = '';
            public $filming_locations       = '';
            public $season                  = '';
            public $episode                 = '';
            public $episode_name            = '';
            public $episode_description     = '';
            public $comments                = '';


            //media table

            public $media_index             = '';
            public $media_metadata_id       = '';
            public $media_location          = '';
            public $media_comments          = '';

    //Control Variables
    public $item                    = '';
    public $dbc                     = '';
    public $debug                   = true;
    public $output_buffer           = '';


    //read_meta
    public function read_meta($filename){

        /**
         * Reads metadata from a video file
         */

        //Define which file we will read
        $filename = ABSPATH.'content/uploads/'.$filename;
        $this->media_location = $filename;

        //setup getID3
        $getID3 = new getID3;

        // Analyze file and store returned data in $ThisFileInfo
        $ThisFileInfo = $getID3->analyze($filename);
        getid3_lib::CopyTagsToComments($ThisFileInfo);

        //Setup the array
        $meta = array();

        //Setup output buffering
        ob_start();
        echo '<h3>Function: read_meta() called:</h3>'."\r\n";

        //Type of video (tv show, movie, etc) (not supported currently in db)
        /*
        if(isset($ThisFileInfo['tags']['quicktime']['stik'][0])){
            echo "<br />\r\n".'Type:        '.$ThisFileInfo['tags']['quicktime']['stik'][0];
            $meta['type'] = $ThisFileInfo['tags']['quicktime']['stik'][0];
        }else{
            $meta['type'] = '';
        }
        */


        //TV Show Name (name of the series)
        if(isset($ThisFileInfo['tags']['quicktime']['tv_show_name'][0])){
            echo "<br />\r\n".'Name:        '.$ThisFileInfo['tags']['quicktime']['tv_show_name'][0];
            $meta['tv_show_name'] = $ThisFileInfo['tags']['quicktime']['tv_show_name'][0];
            $this->title = $meta['tv_show_name'];
        }else{
            $meta['tv_show_name'] = '';
        }

        //TV Season (typically int)
        if(isset($ThisFileInfo['tags']['quicktime']['tv_season'][0])){
            echo "<br />\r\n".'Season:      '.$ThisFileInfo['tags']['quicktime']['tv_season'][0];
            $meta['tv_season'] = $ThisFileInfo['tags']['quicktime']['tv_season'][0];
            $this->season = $meta['tv_season'];
        }else{
            $meta['tv_season'] = '';
        }

        //TV Episode (typically int)
        if(isset($ThisFileInfo['tags']['quicktime']['tv_episode'][0])){
            echo "<br />\r\n".'Episode:     '.$ThisFileInfo['tags']['quicktime']['tv_episode'][0];
            $meta['tv_episode'] = $ThisFileInfo['tags']['quicktime']['tv_episode'][0];
            $this->episode = $meta['tv_episode'];
        }else{
            $meta['tv_episode'] = '';
        }

        //Description
        if(isset($ThisFileInfo['tags']['quicktime']['description'][0])){
            echo "<br />\r\n".'Description: '.$ThisFileInfo['tags']['quicktime']['description'][0];
            $meta['description'] = $ThisFileInfo['tags']['quicktime']['description'][0];
            $this->episode_description = $meta['description'];
        }else{
            $meta['description'] = '';
        }

        //TV Network Name (not currently supported in db)
        /*
        if(isset($ThisFileInfo['tags']['quicktime']['tv_nework_name'][0])){
            //echo "<br />\r\n".'Network:     '.$ThisFileInfo['tags']['quicktime']['tv_network_name'][0];
            $meta['tv_network_name'] = $ThisFileInfo['tags']['quicktime']['tv_network_name'][0];
        }else{
            $meta['tv_network_name'] = '';
        }
        */

        //End output buffering
        echo "<hr /><br /><br />\r\n\r\n";
        $this->output_buffer = $this->output_buffer.ob_end_flush();

        //Return the metadata
        return $meta;

    }

    //lookup
    public function lookup(){

        /**
         * Determines if a file is already in the database
         */

        //Setup output buffering
        ob_start();
        echo '<h3>Function: lookup() called:</h3>'."\r\n";

        //Setup the database connection
        $this->dbc = new db;
        $this->dbc->connect();

        //Determine if the name is in the database

        //Prep string for search
        $name = $this->dbc->sanitize($this->media_location);
        $name = strtolower($name);

        //Query the database
        $query = "SELECT * FROM `media` WHERE `location` LIKE '%".$name."%'";
        $array = $this->dbc->query($query);

        //Close the database connection
        $this->dbc->close();

        //Process the results
        if(count($array) >= 1 && isset($array[0]['index'])){

            //The file IS in the db
            echo 'File: '.$this->media_location.' IS in the database.<br />'."\r\n";

            //Save the output buffer contents in the output variable
            echo "<hr /><br /><br />\r\n\r\n";
            $this->output_buffer = $this->output_buffer.ob_end_flush();

            //Return
            return true;

        }else{

            //The file is NOT in the db
            echo 'File: '.$this->media_location.' is NOT in the database.<br />'."\r\n";

            //Save the output buffer contents in the output variable
            echo "<hr /><br /><br />\r\n\r\n";
            $this->output_buffer = $this->output_buffer.ob_end_flush();

            //Return
            return false;

        }

    }

    //insert
    public function insert(){

        /**
         * Inserts a video file into the database
         */

    }

    //fetch_imdb
    public function fetch_imdb() {

        /**
         * Fetches meta data from IMDb
         */

        //Determine how we will be searching IMDb
        if(!(empty($this->imdb_id))){

            //Search via IMDb id
            $imdb_id = urlencode($this->imdb_id);
            $url = 'http://mymovieapi.com/?ids='.$imdb_id.'&type=json&plot=simple&episode=1&lang=en-US&aka=simple&release=simple&business=0&tech=0';

        }elseif(!(empty($this->title))){
{'fraction_non_alphanumeric': 0.16592352384812864, 'fraction_numerical': 0.0059451425483042835, 'mean_word_length': 1.6643268538516918, 'pattern_counts': {'":': 0, '<': 46, '<?xml version=': 0, '>': 96, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3593910', 'n_tokens_mistral': 4256, 'n_tokens_neox': 4088, 'n_words': 853}