forked from phergie/phergie
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of git://github.com/elazar/phergie
- Loading branch information
Showing
13 changed files
with
1,158 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,15 +20,14 @@ | |
*/ | ||
|
||
/** | ||
* Uses a self CTCP PING to ensure that the client connection has not been | ||
* dropped. | ||
* | ||
* @category Phergie | ||
* @package Phergie_Plugin_AudioScrobbler | ||
* @author Phergie Development Team <[email protected]> | ||
* @license http://phergie.org/license New BSD License | ||
* @link http://pear.phergie.org/package/Phergie_Plugin_AudioScrobbler | ||
* @uses Phergie_Plugin_Command pear.phergie.org | ||
* @uses Phergie_Plugin_Http pear.phergie.org | ||
* @uses extension simplexml | ||
*/ | ||
class Phergie_Plugin_AudioScrobbler extends Phergie_Plugin_Abstract | ||
|
@@ -53,9 +52,16 @@ class Phergie_Plugin_AudioScrobbler extends Phergie_Plugin_Abstract | |
* @var string | ||
*/ | ||
protected $query = '?method=user.getrecenttracks&user=%s&api_key=%s'; | ||
|
||
/** | ||
* HTTP plugin | ||
* | ||
* @var Phergie_Plugin_Http | ||
*/ | ||
protected $http; | ||
|
||
/** | ||
* check and load plugin's dependencies. | ||
* Check for dependencies. | ||
* | ||
* @return void | ||
*/ | ||
|
@@ -65,11 +71,13 @@ public function onLoad() | |
$this->fail('SimpleXML php extension is required'); | ||
} | ||
|
||
$this->getPluginHandler()->getPlugin('Command'); | ||
$plugins = $this->getPluginHandler(); | ||
$plugins->getPlugin('Command'); | ||
$this->http = $plugins->getPlugin('Http'); | ||
} | ||
|
||
/** | ||
* Command function to get user's status on last.fm | ||
* Command function to get a user's status on last.fm. | ||
* | ||
* @param string $user User identifier | ||
* | ||
|
@@ -78,13 +86,15 @@ public function onLoad() | |
public function onCommandLastfm($user = null) | ||
{ | ||
if ($key = $this->config['audioscrobbler.lastfm_api_key']) { | ||
$scrobbled = $this->getScrobbled($user, $this->lastfm_url, $key); | ||
$this->doPrivmsg($this->getEvent()->getSource(), $scrobbled); | ||
$scrobbled = $this->getScrobbled($user, $this->lastfmUrl, $key); | ||
if ($scrobbled) { | ||
$this->doPrivmsg($this->getEvent()->getSource(), $scrobbled); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Command function to get users status on libre.fm | ||
* Command function to get a user's status on libre.fm. | ||
* | ||
* @param string $user User identifier | ||
* | ||
|
@@ -93,35 +103,46 @@ public function onCommandLastfm($user = null) | |
public function onCommandLibrefm($user = null) | ||
{ | ||
if ($key = $this->config['audioscrobbler.librefm_api_key']) { | ||
$scrobbled = $this->getScrobbled($user, $this->librefm_url, $key); | ||
$this->doPrivmsg($this->getEvent()->getSource(), $scrobbled); | ||
$scrobbled = $this->getScrobbled($user, $this->librefmUrl, $key); | ||
if ($scrobbled) { | ||
$this->doPrivmsg($this->getEvent()->getSource(), $scrobbled); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Simple Scrobbler API function to get recent track formatted in string | ||
* Simple Scrobbler API function to get a formatted string of the most | ||
* recent track played by a user. | ||
* | ||
* @param string $user User name to lookup | ||
* @param string $user Username to look up | ||
* @param string $url Base URL of the scrobbler service | ||
* @param string $key Scrobbler service API key | ||
* | ||
* @return string A formatted string of the most recent track played. | ||
* @return string Formatted string of the most recent track played | ||
*/ | ||
public function getScrobbled($user, $url, $key) | ||
{ | ||
$user = $user ? $user : $this->getEvent()->getNick(); | ||
$event = $this->getEvent(); | ||
$user = $user ? $user : $event->getNick(); | ||
$url = sprintf($url . $this->query, urlencode($user), urlencode($key)); | ||
|
||
$response = file_get_contents($url); | ||
try { | ||
$xml = new SimpleXMLElement($response); | ||
} | ||
catch (Exception $e) { | ||
return 'Can\'t find status for ' . $user; | ||
|
||
$response = $this->http->get($url); | ||
if ($response->isError()) { | ||
$this->doNotice( | ||
$event->getSource(), | ||
'Can\'t find status for ' . $user . ': HTTP ' . | ||
$response->getCode() . ' ' . $response->getMessage() | ||
); | ||
return false; | ||
} | ||
|
||
$xml = $response->getContent(); | ||
if ($xml->error) { | ||
return 'Can\'t find status for ' . $user; | ||
$this->doNotice( | ||
$event->getSource(), | ||
'Can\'t find status for ' . $user . ': API ' . $xml->error | ||
); | ||
return false; | ||
} | ||
|
||
$recenttracks = $xml->recenttracks; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -27,6 +27,7 @@ | |
* @author Phergie Development Team <[email protected]> | ||
* @license http://phergie.org/license New BSD License | ||
* @link http://pear.phergie.org/package/Phergie_Plugin_BeerScore | ||
* @uses Phergie_Plugin_Http pear.phergie.org | ||
*/ | ||
class Phergie_Plugin_BeerScore extends Phergie_Plugin_Abstract | ||
{ | ||
|
@@ -58,6 +59,23 @@ class Phergie_Plugin_BeerScore extends Phergie_Plugin_Abstract | |
*/ | ||
const API_BASE_URL = 'http://caedmon.net/beerscore/'; | ||
|
||
/** | ||
* HTTP plugin | ||
* | ||
* @var Phergie_Plugin_Http | ||
*/ | ||
protected $http; | ||
|
||
/** | ||
* Checks for dependencies. | ||
* | ||
* @return void | ||
*/ | ||
public function onLoad() | ||
{ | ||
$this->http = $this->getPluginHandler()->getPlugin('Http'); | ||
} | ||
|
||
/** | ||
* Handles beerscore commands. | ||
* | ||
|
@@ -67,17 +85,19 @@ class Phergie_Plugin_BeerScore extends Phergie_Plugin_Abstract | |
*/ | ||
public function onCommandBeerscore($searchstring) | ||
{ | ||
$target = $this->getEvent()->getNick(); | ||
$source = $this->getEvent()->getSource(); | ||
$event = $this->getEvent(); | ||
$target = $event->getNick(); | ||
$source = $event->getSource(); | ||
|
||
$apiurl = self::API_BASE_URL . rawurlencode($searchstring); | ||
$result = json_decode(file_get_contents($apiurl)); | ||
$response = $this->http->get($apiurl); | ||
|
||
if (!$result || !isset($result->type) || !is_array($result->beer)) { | ||
if ($response->isError()) { | ||
$this->doNotice($target, 'Score not found (or failed to contact API)'); | ||
return; | ||
} | ||
|
||
$result = $response->getContent(); | ||
switch ($result->type) { | ||
case self::TYPE_SCORE: | ||
// small enough number to get scores | ||
|
@@ -128,11 +148,9 @@ public function onCommandBeerscore($searchstring) | |
$num = 'at least 100'; | ||
} | ||
$resultsword = (($result->num > 1) ? 'results' : 'result'); | ||
$str = "{$target}: {$num} {$resultsword}; {$beer->searchurl}"; | ||
$str = "{$target}: {$num} {$resultsword}; {$result->searchurl}"; | ||
$this->doPrivmsg($source, $str); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
// vim: language=php tab=4 et indent=4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?php | ||
/** | ||
* Phergie | ||
* | ||
* PHP version 5 | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject to the new BSD license that is bundled | ||
* with this package in the file LICENSE. | ||
* It is also available through the world-wide-web at this URL: | ||
* http://phergie.org/license | ||
* | ||
* @category Phergie | ||
* @package Phergie_Plugin_Cache | ||
* @author Phergie Development Team <[email protected]> | ||
* @copyright 2008-2010 Phergie Development Team (http://phergie.org) | ||
* @license http://phergie.org/license New BSD License | ||
* @link http://pear.phergie.org/package/Phergie_Plugin_Cache | ||
*/ | ||
|
||
/** | ||
* Implements a generic cache to be used by other plugins. | ||
* | ||
* @category Phergie | ||
* @package Phergie_Plugin_Cache | ||
* @author Phergie Development Team <[email protected]> | ||
* @license http://phergie.org/license New BSD License | ||
* @link http://pear.phergie.org/package/Phergie_Plugin_Cache | ||
*/ | ||
class Phergie_Plugin_Cache extends Phergie_Plugin_Abstract | ||
{ | ||
/** | ||
* Key-value data storage for the cache | ||
* | ||
* @var array | ||
*/ | ||
protected $cache = array(); | ||
|
||
/** | ||
* Stores a value in the cache. | ||
* | ||
* @param string $key Key to associate with the value | ||
* @param mixed $data Data to be stored | ||
* @param int|null $ttl Time to live in seconds or NULL for forever | ||
* @param bool $overwrite TRUE to overwrite any existing value | ||
* associated with the specified key | ||
* | ||
* @return bool | ||
*/ | ||
public function store($key, $data, $ttl = 3600, $overwrite = true) | ||
{ | ||
if (!$overwrite && isset($this->cache[$key])) { | ||
return false; | ||
} | ||
|
||
if ($ttl) { | ||
$expires = time()+$ttl; | ||
} else { | ||
$expires = null; | ||
} | ||
|
||
$this->cache[$key] = array('data' => $data, 'expires' => $expires); | ||
return true; | ||
|
||
} | ||
|
||
/** | ||
* Fetches a previously stored value. | ||
* | ||
* @param string $key Key associated with the value | ||
* | ||
* @return mixed Stored value or FALSE if no value or an expired value | ||
* is associated with the specified key | ||
*/ | ||
public function fetch($key) | ||
{ | ||
if (!isset($this->cache[$key])) { | ||
return false; | ||
} | ||
|
||
$item = $this->cache[$key]; | ||
if (!is_null($item['expires']) && $item['expires'] < time()) { | ||
$this->expire($key); | ||
return false; | ||
} | ||
|
||
return $item['data']; | ||
} | ||
|
||
/** | ||
* Expires a value that has exceeded its time to live. | ||
* | ||
* @param string $key Key associated with the value to expire | ||
* | ||
* @return bool | ||
*/ | ||
protected function expire($key) | ||
{ | ||
if (!isset($this->cache[$key])) { | ||
return false; | ||
} | ||
unset($this->cache[$key]); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.