#2362 twitter: cronjob

This commit is contained in:
sebastian 2010-02-16 15:19:02 +00:00
parent f183ef664d
commit e650f878fd
7 changed files with 113 additions and 64 deletions

View File

@ -158,6 +158,7 @@ install: configure_apache
${MKDIR} ${USR_VAR_DIR}/Campcaster/htmlUI/var/templates/system ${MKDIR} ${USR_VAR_DIR}/Campcaster/htmlUI/var/templates/system
${MKDIR} ${USR_VAR_DIR}/Campcaster/htmlUI/var/templates/playlist ${MKDIR} ${USR_VAR_DIR}/Campcaster/htmlUI/var/templates/playlist
${MKDIR} ${USR_VAR_DIR}/Campcaster/htmlUI/var/templates/subjects ${MKDIR} ${USR_VAR_DIR}/Campcaster/htmlUI/var/templates/subjects
${MKDIR} ${USR_VAR_DIR}/Campcaster/htmlUI/var/install
${CP} ${VAR_DIR}/redirect.php ${USR_VAR_DIR}/Campcaster/index.php ${CP} ${VAR_DIR}/redirect.php ${USR_VAR_DIR}/Campcaster/index.php
${CP} ${VAR_DIR}/redirect.php ${USR_VAR_DIR}/Campcaster/htmlUI/index.php ${CP} ${VAR_DIR}/redirect.php ${USR_VAR_DIR}/Campcaster/htmlUI/index.php
@ -212,7 +213,7 @@ install: configure_apache
${CP} ${VAR_DIR}/templates/subjects/*.tpl \ ${CP} ${VAR_DIR}/templates/subjects/*.tpl \
${USR_VAR_DIR}/Campcaster/htmlUI/var/templates/subjects ${USR_VAR_DIR}/Campcaster/htmlUI/var/templates/subjects
${MKDIR} ${USR_VAR_DIR}/Campcaster/htmlUI/var/templates_c ${CP} ${VAR_DIR}/install/install.php ${USR_VAR_DIR}/Campcaster/htmlUI/var/install/
chgrp ${APACHE_GROUP} ${USR_VAR_DIR}/Campcaster/htmlUI/var/templates_c chgrp ${APACHE_GROUP} ${USR_VAR_DIR}/Campcaster/htmlUI/var/templates_c
chgrp ${APACHE_GROUP} ${USR_VAR_DIR}/Campcaster/htmlUI/var/html/img chgrp ${APACHE_GROUP} ${USR_VAR_DIR}/Campcaster/htmlUI/var/html/img
@ -224,6 +225,8 @@ install: configure_apache
${RM} ${WWW_DOCROOT}/campcaster ${RM} ${WWW_DOCROOT}/campcaster
ln -sf ${USR_VAR_DIR}/Campcaster ${WWW_DOCROOT}/campcaster ln -sf ${USR_VAR_DIR}/Campcaster ${WWW_DOCROOT}/campcaster
-cd var/install && php -q install.php
configure_apache: configure_apache:
ifeq (@CONFIGURE_APACHE@,yes) ifeq (@CONFIGURE_APACHE@,yes)

View File

@ -1,22 +0,0 @@
<?php
require_once(dirname(__FILE__).'/../ui_handler_init.php');
require_once("../Input.php");
if (get_magic_quotes_gpc()) {
$_REQUEST = Input::CleanMagicQuotes($_REQUEST);
}
if ($feed = $uiHandler->TWITTER->getFeed()) {
if ($uiHandler->TWITTER->needsUpdate()) {
print "Prepare for update...\n";
if ($res = $uiHandler->TWITTER->sendFeed($feed)) {
print "Feed id {$res->id}\n $feed";
} else {
print "Update failed, check auth data.";
}
} else {
print "Update interval not reached.";
}
} else {
print "No playlist found.";
}

View File

@ -0,0 +1,37 @@
<?php
require_once(dirname(__FILE__).'/../ui_conf.php');
require_once(dirname(__FILE__).'/../ui_handler.class.php');
$uiHandler = new uiHandler($CC_CONFIG);
$uiHandler->init();
if (is_array($argv)) {
define('CRON_DEBUG', array_search('debug', $argv));
} else {
define('CRON_DEBUG', true);
print '<pre>';
}
$uiHandler->sessid = $_COOKIE[$CC_CONFIG['authCookieName']] = Alib::Login('scheduler', 'change_me');
if (!$uiHandler->sessid) {
print "Alib::Login failed\n";
exit(1);
}
if ($uiHandler->TWITTER->needsUpdate()) {
if ($feed = $uiHandler->TWITTER->getFeed()) {
if (CRON_DEBUG) print "Prepare for update...\n";
if ($res = $uiHandler->TWITTER->sendFeed($feed)) {
if (CRON_DEBUG) print "Post with feed id: {$res->id}\nContent: $feed";
} else {
print "Update failed, check auth data.\n";
exit(1);
}
} else {
if (CRON_DEBUG) print "No playlist found at offset time.\n";
}
} else {
if (CRON_DEBUG) print "Update interval not reached.\n";
}

View File

@ -0,0 +1,36 @@
<?php
// Do not allow remote execution
$arr = array_diff_assoc($_SERVER, $_ENV);
if (isset($arr["DOCUMENT_ROOT"]) && ($arr["DOCUMENT_ROOT"] != "") ) {
header("HTTP/1.1 400");
header("Content-type: text/plain; charset=UTF-8");
echo "400 Not executable\r\n";
exit(1);
}
//------------------------------------------------------------------------
// Install twitter Cron job
//------------------------------------------------------------------------
require_once(dirname(__FILE__).'/../../../storageServer/var/cron/Cron.php');
$command = '/usr/bin/php '.realpath(dirname(__FILE__).'/../html/ui_twitterCron.php');
$cron = new Cron();
$access = $cron->openCrontab('write');
if ($access != 'write') {
do {
$r = $cron->forceWriteable();
} while ($r);
}
foreach ($cron->ct->getByType(CRON_CMD) as $line) {
if (preg_match('/ui_twitterCron\.php/', $line['command'])) {
$cron->closeCrontab();
echo " * Twitter cron job already exists.\n";
exit;
}
}
echo " * Adding twitter cron job...";
$cron->ct->addCron('*', '*', '*', '*', '*', $command);
$cron->closeCrontab();
echo "Done\n";
?>

View File

@ -259,7 +259,7 @@ class twitter{
if($this->username !== false && $this->password !== false) if($this->username !== false && $this->password !== false)
curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password); curl_setopt($ch, CURLOPT_USERPWD, $this->username.':'.$this->password);
curl_setopt($ch, CURLOPT_VERBOSE, 1); curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_NOBODY, 0); curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent); curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);

View File

@ -114,7 +114,7 @@ define('UI_PL_ELEM_FADEOUT', 'fadeOut');
define('UI_BACKUPTOKEN_KEY', 'backupToken'); define('UI_BACKUPTOKEN_KEY', 'backupToken');
define('UI_RESTORETOKEN_KEY', 'restoreToken'); define('UI_RESTORETOKEN_KEY', 'restoreToken');
require_once('../../../storageServer/var/conf.php'); require_once(dirname(__FILE__).'/../../storageServer/var/conf.php');
define('UI_VERSION', CAMPCASTER_VERSION); define('UI_VERSION', CAMPCASTER_VERSION);
define('UI_VERSION_FULLNAME', 'Campcaster '.UI_VERSION); define('UI_VERSION_FULLNAME', 'Campcaster '.UI_VERSION);
define('UI_COPYRIGHT_DATE', CAMPCASTER_COPYRIGHT_DATE); define('UI_COPYRIGHT_DATE', CAMPCASTER_COPYRIGHT_DATE);
@ -151,7 +151,7 @@ $CC_CONFIG = array_merge($CC_CONFIG,
); );
require_once(dirname(__FILE__).'/ui_base.inc.php'); require_once(dirname(__FILE__).'/ui_base.inc.php');
require_once('../../../storageServer/var/GreenBox.php'); require_once(dirname(__FILE__).'/../../storageServer/var/GreenBox.php');
require_once(dirname(__FILE__).'/formmask/generic.inc.php'); require_once(dirname(__FILE__).'/formmask/generic.inc.php');
require_once('DB.php'); require_once('DB.php');

View File

@ -1,10 +1,20 @@
<?php <?php
class uiTwitter { class uiTwitter {
private $Base; private $Base;
private $sesttings = array();
/**
* Time in sec
*
* @var int
*/
private $runtime = 10;
public function __construct(&$uiBase) public function __construct(&$uiBase)
{ {
$this->Base =& $uiBase; $this->Base =& $uiBase;
$this->loadSettings();
} }
private static function getSettingFormMask() private static function getSettingFormMask()
@ -156,40 +166,30 @@ class uiTwitter {
return $formmask; return $formmask;
} }
private function getSettings() private function loadSettings()
{ {
static $settings;
if (is_array($settings)) {
return $settings;
}
$settings = array();
$mask = uiTwitter::getSettingFormMask(); $mask = uiTwitter::getSettingFormMask();
foreach($mask as $key => $val) { foreach($mask as $key => $val) {
if (isset($val['isPref']) && $val['isPref'] && !$val['hiddenPref']) { if (isset($val['isPref']) && $val['isPref']) {
$element = isset($val['element']) ? $val['element'] : null; $element = isset($val['element']) ? $val['element'] : null;
$p = $this->Base->gb->loadGroupPref($this->Base->sessid, 'StationPrefs', $element); $p = $this->Base->gb->loadGroupPref($this->Base->sessid, 'StationPrefs', $element);
if (is_string($p)) { if (is_string($p)) {
$settings[$element] = $p; $this->settings[$element] = $p;
} }
} }
} }
return $settings;
} }
public function getSettingsForm() public function getSettingsForm()
{ {
$mask = uiTwitter::getSettingFormMask(); $mask = uiTwitter::getSettingFormMask();
$form = new HTML_QuickForm('twitter', UI_STANDARD_FORM_METHOD, UI_HANDLER);# $form = new HTML_QuickForm('twitter', UI_STANDARD_FORM_METHOD, UI_HANDLER);#
$settings = $this->getSettings();
foreach($mask as $key => $val) { foreach($mask as $key => $val) {
if (isset($val['isPref']) && $val['isPref']) { if (isset($val['isPref']) && $val['isPref'] && !$val['hiddenPref']) {
$element = isset($val['element']) ? $val['element'] : null; $element = isset($val['element']) ? $val['element'] : null;
$p = $settings[$element]; $p = $this->settings[$element];
if (is_string($p)) { if (is_string($p)) {
$mask[$key]['default'] = $p; $mask[$key]['default'] = $p;
} }
@ -219,7 +219,7 @@ class uiTwitter {
$result = $this->Base->gb->saveGroupPref($this->Base->sessid, 'StationPrefs', $val['element'], $formdata[$val['element']]); $result = $this->Base->gb->saveGroupPref($this->Base->sessid, 'StationPrefs', $val['element'], $formdata[$val['element']]);
if (PEAR::isError($result)) if (PEAR::isError($result))
$this->_retMsg('Error while saving twitter settings.'); $this->_retMsg('Error while saving twitter settings.');
} else { } elseif (!$val['hiddenPref']) {
$this->Base->gb->delGroupPref($this->Base->sessid, 'StationPrefs', $val['element']); $this->Base->gb->delGroupPref($this->Base->sessid, 'StationPrefs', $val['element']);
} }
} }
@ -229,9 +229,7 @@ class uiTwitter {
} }
public function getFeed($p_useSampledata = false) public function getFeed($p_useSampledata = false)
{ {
$settings = $this->getSettings();
if ($p_useSampledata) { if ($p_useSampledata) {
$whatsplaying = array( $whatsplaying = array(
"tracktitle" => "Gimme Shelter", "tracktitle" => "Gimme Shelter",
@ -239,7 +237,7 @@ class uiTwitter {
"playlisttitle" => "The Blues Hour" "playlisttitle" => "The Blues Hour"
); );
} else { } else {
$whatsplaying = $this->getWhatsplaying($settings['twitter-offset']); $whatsplaying = $this->getWhatsplaying($this->settings['twitter-offset']);
} }
if (!$whatsplaying) { if (!$whatsplaying) {
@ -249,26 +247,26 @@ class uiTwitter {
//////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////
// create twitter tweet sample // create twitter tweet sample
// TWEET PREFIX // TWEET PREFIX
if (!empty($settings['twitter-prefix'])) { if (!empty($this->settings['twitter-prefix'])) {
$tweetprefix = $settings['twitter-prefix'] . " "; $tweetprefix = $this->settings['twitter-prefix'] . " ";
} else { } else {
$tweetprefix = ""; $tweetprefix = "";
} }
// TWEET SUFFIX // TWEET SUFFIX
if (!empty($settings['twitter-suffix'])) { if (!empty($this->settings['twitter-suffix'])) {
$tweetsuffix = " " . $settings['twitter-suffix']; $tweetsuffix = " " . $this->settings['twitter-suffix'];
} else { } else {
$tweetsuffix = ""; $tweetsuffix = "";
} }
if (!empty($settings['twitter-url'])) { if (!empty($this->settings['twitter-url'])) {
$tweetsuffix = $tweetsuffix . " " . self::GetTinyUrl($settings['twitter-url']); $tweetsuffix = $tweetsuffix . " " . self::GetTinyUrl($this->settings['twitter-url']);
} }
// TWEET BODY // TWEET BODY
$tweetbody = array(); $tweetbody = array();
if ($settings['twitter-has_tracktitle']) { $tweetbody[] = $whatsplaying['tracktitle']; } if ($this->settings['twitter-has_tracktitle']) { $tweetbody[] = $whatsplaying['tracktitle']; }
if ($settings['twitter-has_trackartist']) { $tweetbody[] = $whatsplaying['trackartist']; } if ($this->settings['twitter-has_trackartist']) { $tweetbody[] = $whatsplaying['trackartist']; }
if ($settings['twitter-has_playlisttitle']) { $tweetbody[] = $whatsplaying['playlisttitle']; } if ($this->settings['twitter-has_playlisttitle']) { $tweetbody[] = $whatsplaying['playlisttitle']; }
if ($settings['twitter-has_stationname']) { $tweetbody[] = $this->Base->STATIONPREFS['stationName']; } if ($this->settings['twitter-has_stationname']) { $tweetbody[] = $this->Base->STATIONPREFS['stationName']; }
$tweetbody = implode (". ",$tweetbody); $tweetbody = implode (". ",$tweetbody);
@ -319,23 +317,20 @@ class uiTwitter {
public function sendFeed($p_feed) public function sendFeed($p_feed)
{ {
$settings = $this->getSettings();
$twitter = new twitter(); $twitter = new twitter();
$twitter->username = $settings['twitter-login']; $twitter->username = $this->settings['twitter-login'];
$twitter->password = $settings['twitter-password']; $twitter->password = $this->settings['twitter-password'];
if ($twitter->update($p_feed)) { if ($res = $twitter->update($p_feed)) {
$this->Base->gb->saveGroupPref($this->Base->sessid, 'StationPrefs', 'twitter-lastupdate', time()); $this->Base->gb->saveGroupPref($this->Base->sessid, 'StationPrefs', 'twitter-lastupdate', time());
return true; return $res;
} }
return false; return false;
} }
public function needsUpdate() public function needsUpdate()
{ {
$settings = $this->getSettings(); if (time() - $this->Base->gb->loadGroupPref($this->Base->sessid, 'StationPrefs', 'twitter-lastupdate') + $this->runtime > $this->settings['twitter-interval']) {
if (time() - $this->Base->gb->loadGroupPref($this->Base->sessid, 'StationPrefs', 'twitter-lastupdate') > $settings['twitter-interval']) {
return true; return true;
} }
return false; return false;