public static function Init() { self::$LOG = 'thumb-queue.log'; self::$TABLE = 'tbx_thumb_queue'; self::$SCRIPT = 'ThumbQueue.php'; self::$CACHE_STATS = 'thumb-queue-stats'; self::$CACHE_PID = 'thumb-queue-pid'; self::$CACHE_STOP = 'thumb-queue-stop'; }
$database_xml = str_replace('<autocomplete>#Pending,Active,Disabled</autocomplete>', '<autocomplete>#Pending,Scheduled,Active,Disabled</autocomplete>', $database_xml); file_put_contents(INCLUDES_DIR . '/database.xml', $database_xml); // Force a reload of the schema $schema = GetDBSchema(true); #### Update database.xml ========================================================================================================== #### Create database tables ======================================================================================================= // Create tbx_imported if doesn't already exist $DB->Update(GetDBCreate('tbx_imported')); // Create tbx_thumb_queue if doesn't already exist $DB->Update(GetDBCreate('tbx_thumb_queue')); // Create tbx_search_term_new if doesn't already exist $DB->Update(GetDBCreate('tbx_search_term_new')); #### Create database tables ======================================================================================================= #### Update database ============================================================================================================== // Prepare for new QueueProcessor stats format $stats = ThumbQueue::LoadStats(); if (!isset($stats[ThumbQueue::STAT_PROCESSED_ITEMS])) { Cache_MySQL::Remove('thumb-queue-stats'); } // Prepare for new QueueProcessor stats format $stats = ConversionQueue::LoadStats(); if (!isset($stats[ConversionQueue::STAT_PROCESSED_ITEMS])) { Cache_MySQL::Remove('conversion-queue-stats'); } // Update sponsor video counts if ($DB->QuerySingleColumn('SELECT MAX(`videos`) FROM `tbx_sponsor`') == 0) { UpdateSponsorStats(); } // Update status in tbx_video_feed $DB->Update("ALTER TABLE `tbx_video_feed` MODIFY `status` ENUM('Pending','Scheduled','Active','Disabled') NOT NULL"); // Add image_id to tbx_category
function tbxThumbQueueStart() { ThumbQueue::Start(); JSON::Success('The thumbnail queue has been started'); }
public static function Import($settings) { $DB = GetDB(); ProgressBarShow('pb-import'); $file = TEMP_DIR . '/' . File::Sanitize($settings['import_file']); $fp = fopen($file, 'r'); $filesize = filesize($file); $line = $read = $imported = 0; $expected = count($settings['fields']); while (!feof($fp)) { $line++; $string = fgets($fp); $read += strlen($string); $data = explode($settings['delimiter'], trim($string)); ProgressBarUpdate('pb-import', $read / $filesize * 100); // Line does not have the expected number of fields if (count($data) != $expected) { continue; } $video = array(); $defaults = array('category_id' => $settings['category_id'], 'sponsor_id' => $settings['sponsor_id'], 'username' => $settings['username'], 'duration' => Format::DurationToSeconds($settings['duration']), 'status' => $settings['status'], 'next_status' => $settings['status'], 'allow_comments' => $settings['allow_comments'], 'allow_ratings' => $settings['allow_ratings'], 'allow_embedding' => $settings['allow_embedding'], 'is_private' => $settings['is_private'], 'date_added' => Database_MySQL::Now(), 'is_featured' => 0, 'is_user_submitted' => 0, 'conversion_failed' => 0, 'tags' => null, 'title' => null, 'description' => null); foreach ($settings['fields'] as $index => $field) { if (!empty($field)) { $video[$field] = trim($data[$index]); } } // Setup clips $clips = array(); $thumbs = array(); $clip_type = 'URL'; if (isset($video['embed_code'])) { // Cannot convert or thumbnail from embed code $settings['flag_convert'] = $settings['flag_thumb'] = false; $clips[] = $video['embed_code']; $clip_type = 'Embed'; } else { if (isset($video['gallery_url'])) { $http = new HTTP(); if (!$http->Get($video['gallery_url'])) { // Broken gallery URL, continue continue; } list($thumbs, $clips) = Video_Source_Gallery::ExtractUrls($http->url, $http->body); } else { if (!isset($video['video_url']) && isset($video['base_video_url'])) { if (!preg_match('~/$~', $video['base_video_url'])) { $video['base_video_url'] .= '/'; } foreach (explode(',', $video['video_filename']) as $filename) { $clips[] = $video['base_video_url'] . $filename; } } else { $clips[] = $video['video_url']; } } } // Check for duplicate clips $duplicate = false; foreach ($clips as $clip) { if (!Request::Get('flag_skip_imported_check') && $DB->QueryCount('SELECT COUNT(*) FROM `tbx_imported` WHERE `video_url`=?', array($clip)) > 0) { $duplicate = true; } $DB->Update('REPLACE INTO `tbx_imported` VALUES (?)', array($clip)); } // Dupe found if ($duplicate) { continue; } // Setup thumbs if (!isset($video['gallery_url']) && !isset($video['thumbnail_url']) && isset($video['base_thumbnail_url'])) { if (!preg_match('~/$~', $video['base_thumbnail_url'])) { $video['base_thumbnail_url'] .= '/'; } foreach (explode(',', String::FormatCommaSeparated($video['thumbnail_filename'])) as $filename) { $thumbs[] = $video['base_thumbnail_url'] . $filename; } } else { if (!isset($video['gallery_url']) && isset($video['thumbnail_url'])) { $thumbs[] = $video['thumbnail_url']; } } // Setup duration if (isset($video['duration_seconds'])) { $video['duration'] = $video['duration_seconds']; } else { if (isset($video['duration_formatted'])) { $video['duration'] = Format::DurationToSeconds($video['duration_formatted']); } } // Use description for title if (empty($video['title'])) { $video['title'] = isset($video['description']) ? $video['description'] : ''; } // Use title for description if (empty($video['description'])) { $video['description'] = isset($video['title']) ? $video['title'] : ''; } // Use title for tags if (empty($video['tags'])) { $video['tags'] = isset($video['title']) ? $video['title'] : ''; } // Setup category if (isset($video['category']) && ($category_id = $DB->QuerySingleColumn('SELECT `category_id` FROM `tbx_category` WHERE `name` LIKE ?', array($video['category']))) !== false) { $video['category_id'] = $category_id; } else { if (($category_id = GetBestCategory($video['title'] . ' ' . $video['description'])) !== null) { $video['category_id'] = $category_id; } } // Merge in the defaults $video = array_merge($defaults, $video); // Format tags and convert to UTF-8 $video['tags'] = Tags::Format($video['tags']); $video = String::ToUTF8($video); if (Request::Get('flag_convert')) { $video['status'] = STATUS_QUEUED; } // Add to database $video['video_id'] = DatabaseAdd('tbx_video', $video); DatabaseAdd('tbx_video_custom', $video); DatabaseAdd('tbx_video_stat', $video); if ($video['is_private']) { $video['private_id'] = sha1(uniqid(mt_rand(), true)); DatabaseAdd('tbx_video_private', $video); } if ($video['status'] == STATUS_QUEUED) { $video['queued'] = time(); DatabaseAdd('tbx_conversion_queue', $video); } if (Request::Get('flag_thumb')) { $video['queued'] = time(); DatabaseAdd('tbx_thumb_queue', $video); } if ($video['status'] == STATUS_ACTIVE && !$video['is_private']) { Tags::AddToFrequency($video['tags']); } // Add clips foreach ($clips as $clip) { DatabaseAdd('tbx_video_clip', array('video_id' => $video['video_id'], 'type' => $clip_type, 'clip' => $clip)); } $dir = new Video_Dir(Video_Dir::DirNameFromId($video['video_id'])); // Process thumbs $thumb_ids = array(); foreach ($thumbs as $thumb) { $http = new HTTP(); if ($http->Get($thumb, $thumb)) { if (Video_Thumbnail::CanResize()) { $thumb_temp = $dir->AddTempFromVar($http->body, 'jpg'); $thumb_file = Video_Thumbnail::Resize($thumb_temp, Config::Get('thumb_size'), Config::Get('thumb_quality'), $dir->GetThumbsDir()); } else { $thumb_file = $dir->AddThumbFromVar($http->body); } if (!empty($thumb_file)) { $thumb_ids[] = DatabaseAdd('tbx_video_thumbnail', array('video_id' => $video['video_id'], 'thumbnail' => str_replace(Config::Get('document_root'), '', $thumb_file))); } } } // Determine number of thumbnails and select random display thumbnail $num_thumbnails = count($thumb_ids); $display_thumbnail = null; if ($num_thumbnails > 0) { // Select display thumbnail randomly from the first 40% $display_thumbnail = $thumb_ids[rand(0, floor(0.4 * $num_thumbnails))]; } DatabaseUpdate('tbx_video', array('video_id' => $video['video_id'], 'num_thumbnails' => $num_thumbnails, 'display_thumbnail' => $display_thumbnail)); $imported++; } fclose($fp); UpdateCategoryStats(); UpdateSponsorStats($settings['sponsor_id']); $t = new Template(); $t->ClearCache('categories.tpl'); ProgressBarHide('pb-import', NumberFormatInteger($imported) . ' videos have been imported!'); // Start up the thumbnail and converson queues if needed if (!Config::Get('flag_using_cron')) { if (Request::Get('flag_convert')) { ConversionQueue::Start(); } if (Request::Get('flag_thumb')) { ThumbQueue::Start(); } } File::Delete($file); }
} $DB = GetDB(); $DB->RestoreTables($args['file']); break; case '--feed-read': ReadFromFields(); break; case '--stats-rollover': StatsRollover(true); break; case '--process-queue': case '--run-convert-queue': ConversionQueue::Start(); break; case '--run-thumb-queue': ThumbQueue::Start(); break; case '--activate-scheduled': ActivateScheduledVideos(); break; } function ActivateScheduledVideos() { $DB = GetDB(); $args = ParseCommandLine(); $queries = array(); if (!isset($args['sort']) || empty($args['sort'])) { $args['sort'] = 'RAND()'; } if (!isset($args['sort-direction']) || empty($args['sort-direction'])) { $args['sort-direction'] = SQL::SORT_ASC;
?> <?php echo ResizeableColumn('Videos Processed', $stats[ThumbQueue::STAT_PROCESSED_ITEMS], true); ?> <?php echo ResizeableColumn('Failed', $stats[ThumbQueue::STAT_FAILED_ITEMS], true); ?> <?php echo ResizeableColumn('Average Time', $stats[ThumbQueue::STAT_AVERAGE_TIME]); ?> <?php echo ResizeableColumn('Average Wait', $stats[ThumbQueue::STAT_AVERAGE_WAIT]); ?> <script language="JavaScript" type="text/javascript"> $(function() { $('#dialog-button-start').attr('disabled', '<?php echo ThumbQueue::IsStartable() ? '' : 'disabled'; ?> '); $('#dialog-button-stop').attr('disabled', '<?php echo ThumbQueue::IsRunning() ? '' : 'disabled'; ?> '); $('#dialog-button-clear').attr('disabled', '<?php echo !ThumbQueue::IsRunning() ? '' : 'disabled'; ?> '); }); </script>
public function Import() { $imported = 0; $http = new HTTP(); if ($http->Get($this->feed['feed_url'])) { $xml = simplexml_load_string($this->ToUTF8($http->body), 'XML_Element', LIBXML_NOERROR, LIBXML_NOWARNING, LIBXML_NOCDATA); if ($xml !== false) { $DB = GetDB(); foreach ($xml->xpath('//videos/video') as $xvideo) { // Check for duplicates, and skip if ($DB->QueryCount('SELECT COUNT(*) FROM `tbx_video_feed_history` WHERE `feed_id`=? AND `unique_id`=?', array($this->feed['feed_id'], $xvideo->id->val()))) { continue; } // Setup defaults $video = $this->defaults; $video['title'] = $xvideo->title->val(); $video['description'] = $xvideo->description->val(); $video['tags'] = Tags::Format($xvideo->tags->val()); if (empty($video['description'])) { $video['description'] = $video['title']; } // Process <clips> $clips = array(); $screens = array(); foreach ($xvideo->xpath('./clips/clip') as $xclip) { $video['duration'] += $xclip->duration; $clip_url = $xvideo->clip_url->val(); $flv = $xclip->flv->val(); // Account for malformed feeds where the clip_url contains the URL to the video // file rather than the required root URL if (strstr($clip_url, $flv) === false) { $clip_url = $clip_url . $flv; } $clips[] = array('type' => 'URL', 'clip' => $clip_url); foreach ($xclip->xpath('./screens/screen') as $xscreen) { $screen_url = $xvideo->screen_url->val(); $screen = $xscreen->val(); // Account for malformed feeds where the screen_url contains the URL to the image // file rather than the required root URL if (strstr($screen_url, $screen) === false) { $screen_url = $screen_url . $screen; } $screens[] = array('thumbnail' => $screen_url); } } if (count($clips) > 0) { $best_category = GetBestCategory(join(' ', array($video['title'], $video['description'], $video['tags']))); if (!empty($best_category)) { $video['category_id'] = $best_category; } if ($this->feed['flag_convert']) { $video['status'] = STATUS_QUEUED; $video['next_status'] = $this->feed['status']; } $video['video_id'] = DatabaseAdd('tbx_video', $video); DatabaseAdd('tbx_video_custom', $video); DatabaseAdd('tbx_video_stat', $video); if (!$video['is_private']) { Tags::AddToFrequency($video['tags']); } $video['queued'] = time(); if ($this->feed['flag_convert']) { DatabaseAdd('tbx_conversion_queue', $video); } if ($this->feed['flag_thumb']) { DatabaseAdd('tbx_thumb_queue', $video); } UpdateCategoryStats($video['category_id']); $video_dir = new Video_Dir(Video_Dir::DirNameFromId($video['video_id'])); foreach ($clips as $clip) { $clip['video_id'] = $video['video_id']; DatabaseAdd('tbx_video_clip', $clip); } $display_thumbnail = null; foreach ($screens as $screen) { $thttp = new HTTP(); if ($thttp->Get($screen['thumbnail'], $screen['thumbnail'])) { $temp_file = $video_dir->AddTempFromVar($thttp->body, JPG_EXTENSION); $imgsize = @getimagesize($temp_file); if ($imgsize !== false) { if (Video_Thumbnail::CanResize()) { $local_filename = Video_Thumbnail::Resize($temp_file, Config::Get('thumb_size'), Config::Get('thumb_quality'), $video_dir->GetThumbsDir()); } else { $local_filename = $video_dir->AddThumbFromFile($temp_file, JPG_EXTENSION); } $local_filename = str_replace(Config::Get('document_root'), '', $local_filename); $thumb_id = DatabaseAdd('tbx_video_thumbnail', array('video_id' => $video['video_id'], 'thumbnail' => $local_filename)); if (empty($display_thumbnail)) { $display_thumbnail = $thumb_id; } } } } $video_dir->ClearTemp(); if (!empty($display_thumbnail)) { $DB->Update('UPDATE `tbx_video` SET `display_thumbnail`=? WHERE `video_id`=?', array($display_thumbnail, $video['video_id'])); } $DB->Update('INSERT INTO `tbx_video_feed_history` VALUES (?,?)', array($this->feed['feed_id'], $xvideo->id->val())); $imported++; } } $DB->Update('UPDATE `tbx_video_feed` SET `date_last_read`=? WHERE `feed_id`=?', array(Database_MySQL::Now(), $this->feed['feed_id'])); UpdateSponsorStats($this->feed['sponsor_id']); } // Start up the thumbnail and converson queues if needed if (!Config::Get('flag_using_cron')) { if ($this->feed['flag_convert']) { ConversionQueue::Start(); } if ($this->feed['flag_thumb']) { ThumbQueue::Start(); } } } return $imported; }