Matt1972 Posted December 1, 2019 Share Posted December 1, 2019 This is a section of my code that goes and gets videos from any video site. What I’m having issues with is having it post a random title, that are all in a text file called titles.txt. What I’m wanting to accomplish is have my script go pull the video embed code, the duration, the tags, etc. Which all works fine until I get to the title part where it pulls locally from the server from the text file. I'm not an advance programmer so not sure if I am even approaching this correct way. Here is the code I have that is responsible for putting the title in. I appreciate your help. $video = array( ‘user_id’ => $this->user_id, ‘status’ => $this->status, ‘site’ => ‘my site’, ‘id’ => ‘’, ‘embeddable’ => true, ‘url’ => ‘’, ‘titles’ => ‘’, ‘title’ => ‘’, ‘description’ => ‘’, ‘tags’ => ‘’, ‘category’ => ‘’, ‘thumbs’ => array(), ‘duration’ => 0, ‘embed’ => ‘’ ); function random_title () { $titles = file (“titles.txt”, FILE_IGNORE_NEW_LINES); $num = rand (0, intval (count ($titles) / 3)) * 3; return ucwords($titles[$num]); } //Title if(preg_match(’/title="(.*?)"/’, $match, $matches_title)) { $video[‘title’] = random_title(). ’ - My site name’; } else { $this->errors[] = ‘Failed to get video title for ‘.$video[‘url’].’!’; if (!$this->debug) continue; else $debug_e[] = ‘TITLE’; } Quote Link to comment Share on other sites More sharing options...
Barand Posted December 1, 2019 Share Posted December 1, 2019 Perhaps you could explain the rationale behind this method for getting the title... $num = rand (0, intval (count ($titles) / 3)) * 3 Quote Link to comment Share on other sites More sharing options...
Matt1972 Posted December 1, 2019 Author Share Posted December 1, 2019 4 minutes ago, Barand said: Perhaps you could explain the rationale behind this method for getting the title... $num = rand (0, intval (count ($titles) / 3)) * 3 @Barand To be honest I had found that code online that I used with a Youtube script. I'm not even sure if that is the correct way to do it. I'm learning php so really kinda just fumbling through this. Quote Link to comment Share on other sites More sharing options...
Barand Posted December 1, 2019 Share Posted December 1, 2019 It isn't a sound method. Suppose you have 3 titles. intval(count($titles)/3) = 1, so you now get a random integer between 0 and 1 which you multiply by 3. Therefore $num will be 0 or 3. The titles are indexed 0, 1 and 2. IE index 3 doesn't exist. See the problem? Use array_rand() $random_title = $titles[array_rand($titles)]; Quote Link to comment Share on other sites More sharing options...
Matt1972 Posted December 1, 2019 Author Share Posted December 1, 2019 @Barand So are you saying replace this line: $num = rand (0, intval (count ($titles) / 3)) * 3; With this line: $random_title = $titles[array_rand($titles)]; So I would end up with this: function random_title () { $titles = file (“titles.txt”, FILE_IGNORE_NEW_LINES); $random_title = $titles[array_rand($titles)]; return ucwords($titles[$num]); } I tried to run my script with that but it still does not work. Below is the entire php file for this script. <?php class MEmbed_xvideos { public $url; public $user_id; public $category; public $status; public $video; public $errors = array(); public $debug_e = array(); public $debug_w = array(); public $message; private $overflow = 500; public $video_already = 0; public $video_added = 0; public $debug; public function __construct($url, $user_id, $category, $status, $debug) { $this->url = $url; $this->user_id = $user_id; $this->category = $category; $this->status = $status; $this->debug = $debug; } public function get_videos() { $count = 0; $curl = new VCurl(); $html = clean_html($curl->saveToString($this->url)); if ($html) { if (preg_match_all('/<div id="video_(.*?)<\/p><\/div>/', $html, $matches)) { foreach ($matches[0] as $match) { unset($debug_e); unset($debug_w); ++$count; if ($count > $this->overflow) { $this->errors[] = 'Overflow reached (500)! Aborting!'; return false; } $video = array( 'user_id' => $this->user_id, 'status' => $this->status, 'site' => 'xvideos', 'id' => '', 'embeddable' => true, 'url' => '', 'titles' => '', 'title' => '', 'description' => '', 'tags' => '', 'category' => '', 'thumbs' => array(), 'duration' => 0, 'embed' => '' ); function random_title () { $titles = file (“titles.txt”, FILE_IGNORE_NEW_LINES); $random_title = $titles[array_rand($titles)]; return ucwords($titles[$num]); } //Video ID if (preg_match('/id="video_(.*?)"/', $match, $matches_id)) { $video['id'] = trim($matches_id[1]); } else { if (!$this->debug) continue; else $debug_e[] = 'ID'; } //Embed Code $video['embed'] = '<iframe src="https://www.videosite.com/embedframe/' . $video['id'] . '" frameborder=0 width=' . E_WIDTH . ' height=' . E_HEIGHT . ' scrolling=no style="background-color:#000"></iframe>'; if (already_added($video['embed'])) { ++$this->video_already; continue; } //URL if(preg_match('/<a href="(.*?)"/', $match, $matches_url)) { $video['url'] = "http://www.videosite.com" . trim($matches_url[1]); } else { $this->errors[] = 'Failed to get video URL for ID: '.$video['id'].'!'; if (!$this->debug) continue; else $debug_e[] = 'URL'; } //Title if(preg_match('/title="(.*?)"/', $match, $matches_title, $random_title)) { $video['title'] = $random_title(). ’ - My site name’; } else { $this->errors[] = 'Failed to get video title for '.$video['url'].'!'; if (!$this->debug) continue; else $debug_e[] = 'TITLE'; } //Duration if(preg_match('/"duration">(.*?)</', $match, $matches_duration)) { $duration = str_replace(' min',':', $matches_duration[1]); $duration = str_replace(' sec','', $duration); if (strpos($duration, ':') !== false) { $duration = trim($duration, ':'); if (strpos($duration, ':') !== false) { $video['duration'] = duration_to_seconds($duration); } else { $video['duration'] = duration_to_seconds($duration.':00'); } } else { $video['duration'] = duration_to_seconds('00:'.$duration); } } else { $this->errors[] = 'Failed to get video duration for '.$video['url'].'!'; if (!$this->debug) continue; else $debug_e[] = 'DURATION'; } //Thumbnails if (preg_match('/data-src="(.*?)"/', $match, $matches_thumb)) { $thumb_url = $matches_thumb[1]; $thumb_url = str_replace('.jpg', '', $thumb_url); $thumb_url = substr($thumb_url, 0, strrpos($thumb_url, '.')); for ($i=1;$i<=20;$i++) { $video['thumbs'][] = $thumb_url.'.' . $i . '.jpg'; } } else { $this->errors[] = 'Failed to get video thumbnails for '.$video['url'].'!'; if (!$this->debug) continue; else $debug_e[] = 'THUMBS'; } //Get Video Page $html_video = clean_html($curl->saveToString($video['url'])); //Categories $video['category'] = ''; //Tags if (preg_match_all('/<a href="\/tags\/(.*?)">(.*?)<\/a>/', $html_video, $matches_tags)) { foreach ($matches_tags[2] as $k => $v) { if ($matches_tags[1][$k] != '') { $video['tags'] = $video['tags'] . str_replace(' ','-',$v) . ' '; } } $video['tags'] = trim(strtolower($video['tags'])); } else { $debug_w[] = 'TAGS'; } //Check Embeddable Content if (!strpos($html_video, 'Embed</span>')) { $video['embeddable'] = false; $debug_w[] = 'EMBEDDABLE'; if (end($matches[0]) !== $match) continue; } //Debug Mode if ($this->debug) { echo "Match Content (". $count ."): <textarea style='width:100%' rows=10>".$match."</textarea><br>"; if ($debug_e) echo "Errors: " . implode(', ',$debug_e) . "<br>"; if ($debug_w) echo "Warnings: " . implode(', ',$debug_w) . "<br>"; echo "<pre>"; print_r($video); echo "</pre>"; exit; } //Add Video if (add_video($video)) { ++$this->video_added; } else { $this->errors[] = 'Failed to add '.$video['url'].'!'; } } //Foreach Loop - END } else { $this->errors[] = 'Failed to find embeddable videos on the specified page!'; } } else { $this->errors[] = 'Failed to get html code for specified url!'; } if (!$this->errors) { return true; } return false; } } ?> Quote Link to comment Share on other sites More sharing options...
Barand Posted December 1, 2019 Share Posted December 1, 2019 Stop blindly copying and pasting. Apply a little thought to what the code is doing. You'll learn more that way. function random_title () { $titles = file (“titles.txt”, FILE_IGNORE_NEW_LINES); $random_title = $titles[array_rand($titles)]; return ucwords($random_title); } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.