Skip to content

Commit

Permalink
Added New Listener and Function to Base
Browse files Browse the repository at this point in the history
  • Loading branch information
TimTims committed Jan 9, 2015
1 parent 69cf04e commit e945c5c
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
50 changes: 50 additions & 0 deletions Classes/Command/Youtube.php
@@ -0,0 +1,50 @@
<?php
// Namespace
namespace Listener;

/**
*
* @package IRCBot
* @subpackage Listener
* @author NeXxGeN (https://github.com/NeXxGeN)
*/
class Youtube extends \Library\IRC\Listener\Base {

private $apiUri = "http://gdata.youtube.com/feeds/api/videos/%s";

/**
* Main function to execute when listen even occurs
*/
public function execute($data)
{
$ytTitle = $this->getYtTitle($data);
if ($ytTitle)
{
$args = $this->getArguments($data);
$this->say(sprintf("01,00You00,05Tube %s", $ytTitle),$args[2]);
}
}

private function getYtTitle($data)
{
preg_match('#(?<=v=)[a-zA-Z0-9-]+(?=&)|(?<=v\/)[^&\n]+|(?<=v=)[^&\n]+|(?<=youtu.be/)[^&\n]+#', $data, $matches);
if (isset($matches[0]))
{
$ytApi = sprintf($this->apiUri, $matches[0]);
$Ytdata = $this->fetch($ytApi);
preg_match("/(?<=<title type=\'text\'>).*(?=<\/title>)/", $Ytdata, $ytTitle);
return $ytTitle[0];
}
return false;
}


/**
* Returns keywords that listener is listening to.
*
* @return array
*/
public function getKeywords() {
return array("PRIVMSG");
}
}
33 changes: 33 additions & 0 deletions Classes/Library/IRC/Listener/Base.php
Expand Up @@ -64,4 +64,37 @@ protected function getArguments($data) {

return array_map($func, $args);
}
/**
* Fetches data from $uri
*
* @param string $uri
* @return string
*/
protected function fetch($uri) {

$this->bot->log("Fetching from URI: " . $uri);

// create curl resource
$ch = curl_init();

// set url
curl_setopt($ch, CURLOPT_URL, $uri);

// Set a user agent. Some sites require it (e.g. GitHub API).
curl_setopt($ch, CURLOPT_USERAGENT, 'WildPHP/IRCBot');

//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FRESH_CONNECT, 1);

// $output contains the output string
$output = curl_exec($ch);

// close curl resource to free up system resources
curl_close($ch);

$this->bot->log("Data fetched: " . $output);

return $output;
}
}

0 comments on commit e945c5c

Please sign in to comment.