From 882036e8861d48f32155000d57dbb8a22f18ebd1 Mon Sep 17 00:00:00 2001 From: fwink Date: Thu, 21 Jan 2010 02:10:09 +0000 Subject: [PATCH] MINOR: Initial - still in prgress git-svn-id: svn://svn.silverstripe.com/silverstripe/open/phpinstaller/trunk@97321 467b73ca-7a2a-4603-9d3b-597d59a354a9 --- BeMoreHuman/_config.php | 37 ++++ BeMoreHuman/code/BeMoreHumanPage.php | 114 ++++++++++ BeMoreHuman/code/HowHumansWinImage.php | 6 + BeMoreHuman/code/Page.php | 75 +++++++ BeMoreHuman/code/WallImage.php | 6 + twittermirror/_config.php | 23 ++ twittermirror/code/TwitterMirror.php | 284 +++++++++++++++++++++++++ twittermirror/code/model/Tweet.php | 60 ++++++ 8 files changed, 605 insertions(+) create mode 100644 BeMoreHuman/_config.php create mode 100644 BeMoreHuman/code/BeMoreHumanPage.php create mode 100644 BeMoreHuman/code/HowHumansWinImage.php create mode 100755 BeMoreHuman/code/Page.php create mode 100644 BeMoreHuman/code/WallImage.php create mode 100644 twittermirror/_config.php create mode 100644 twittermirror/code/TwitterMirror.php create mode 100644 twittermirror/code/model/Tweet.php diff --git a/BeMoreHuman/_config.php b/BeMoreHuman/_config.php new file mode 100644 index 0000000..75afd42 --- /dev/null +++ b/BeMoreHuman/_config.php @@ -0,0 +1,37 @@ + diff --git a/BeMoreHuman/code/BeMoreHumanPage.php b/BeMoreHuman/code/BeMoreHumanPage.php new file mode 100644 index 0000000..5a7f610 --- /dev/null +++ b/BeMoreHuman/code/BeMoreHumanPage.php @@ -0,0 +1,114 @@ + 'Varchar', //Where to put the uploaded images + 'PathToWallPictures' => 'Varchar', //Where to put the Wall images + 'HeadText' => "HTMLText", // Text above the the Form + 'WhatIsTheWall' => "HTMLText", // Text explaining what is the wall + 'FooterNotHidden' => "HTMLText", // Text in The footer which is not hidden + 'FooterHidden' => "HTMLText", // Text in The footer which is hidden at first + ); + + static $defaults = array( + 'PathToUploadedPictures' => 'assets/HowHumansWin/', + 'PathToWallPictures' => 'assets/TheWall/' + ); + + static $has_many = array( + 'HowHumansWinImages' => 'HowHumansWinImage', + 'WallImages' =>'WallImage' + + ); + + /** + * Overwrites SiteTree.getCMSFields to change the CMS form behaviour, + * i.e. by adding form fields for the additional attributes defined in + * {@link CataloguePage::$db}. + */ + function getCMSFields() { + $fields = parent::getCMSFields(); + + $fields->addFieldsToTab('Root.Content.BeMoreHuman', + array( + new TextField('PathToUploadedPictures','Path where to store the uploaded images from the form (e.g. assets/HowHumansWin/'), + new TextField('PathToWallPictures','Path where to store the images from the wall (e.g. assets/TheWall/)'), + new TextareaField('HeadText','Text before the form:',5,30), + new TextareaField('WhatIsTheWall','What is the wall:',10,30), + new TextareaField('FooterNotHidden','Footer - text always visible:',5,30), + new TextareaField('FooterHidden','Footer - text extends the upper one:',10,30) + )); + + // return the modified fieldset. + return $fields; + } +} + +/** + * Controller Class for Main BeMoreHumanPage + */ +class BeMoreHumanPage_Controller extends Page_Controller { + + /** + * The Form + * + */ + function Form(){ + // Create fields + $fields = new FieldSet( + new TextField('Name'), + new SimpleImageField( + "PictureName", + "Upload image below" + ), + new TextareaField('Text') + ); + + // Create actions + $actions = new FieldSet( + new FormAction('TellHowHumansWinForm', 'Send') + ); + + return new Form($this, 'HowHumansWinForm', $fields, $actions); + }// end Form + + function TellHowHumansWinForm(){ + + } + + function getWallImageLink(){ + $image=DataObject::get_one('WallImage',NULL,false,'id DESC'); + if($image){ + $link=$image.Link(); + } + else{ + $link=Director::baseURL().$this->PathToWallPictures . "/default.jpg"; + } + return $link; + } + + /** + * ThereAreTweets() + * Checks if there are tweets + * @returns bool true if there are otherwise false + * + */ + function ThereAreTweets(){ + return false; + } + + function LatestTweets($howmany=4){ + return new DatObjectSet; + } + +} // end Controller + + +/** + * BeMoreHumanPage Exception Class + */ +class BeMoreHumanPage_Exception extends Exception { } diff --git a/BeMoreHuman/code/HowHumansWinImage.php b/BeMoreHuman/code/HowHumansWinImage.php new file mode 100644 index 0000000..847385e --- /dev/null +++ b/BeMoreHuman/code/HowHumansWinImage.php @@ -0,0 +1,6 @@ + diff --git a/BeMoreHuman/code/Page.php b/BeMoreHuman/code/Page.php new file mode 100755 index 0000000..7d9d708 --- /dev/null +++ b/BeMoreHuman/code/Page.php @@ -0,0 +1,75 @@ + + * array ( + * 'action', // anyone can access this action + * 'action' => true, // same as above + * 'action' => 'ADMIN', // you must have ADMIN permissions to access this action + * 'action' => '->checkAction' // you can only access this action if $this->checkAction() returns true + * ); + * + * + * @var array + */ + public static $allowed_actions = array ( + 'SearchForm' + ); + + public function init() { + parent::init(); + + // Note: you should use SS template require tags inside your templates + // instead of putting Requirements calls here. However these are + // included so that our older themes still work + Requirements::themedCSS("layout"); + Requirements::themedCSS("typography"); + Requirements::themedCSS("form"); + } + + /** + * Site search form + */ + function SearchForm() { + $searchText = isset($_REQUEST['Search']) ? $_REQUEST['Search'] : 'Search'; + $fields = new FieldSet( + new TextField("Search", "", $searchText) + ); + $actions = new FieldSet( + new FormAction('results', 'Search') + ); + + return new SearchForm($this, "SearchForm", $fields, $actions); + } + + /** + * Process and render search results + */ + function results($data, $form){ + $data = array( + 'Results' => $form->getResults(), + 'Query' => $form->getSearchQuery(), + 'Title' => 'Search Results' + ); + + return $this->customise($data)->renderWith(array('Page_results', 'Page')); + } + +} + +?> \ No newline at end of file diff --git a/BeMoreHuman/code/WallImage.php b/BeMoreHuman/code/WallImage.php new file mode 100644 index 0000000..6e28016 --- /dev/null +++ b/BeMoreHuman/code/WallImage.php @@ -0,0 +1,6 @@ + diff --git a/twittermirror/_config.php b/twittermirror/_config.php new file mode 100644 index 0000000..4ef6599 --- /dev/null +++ b/twittermirror/_config.php @@ -0,0 +1,23 @@ + 'DEBUG', +* 1 => 'INFO', +* 2 => 'NOTICE', +* 3 => 'WARNING', +* 4 => 'ERROR', +* 5 => 'CRITICAL', +* 6 => 'Turn logging off' +*/ +TwitterMirror_Controller::setLogThis(1); + +?> diff --git a/twittermirror/code/TwitterMirror.php b/twittermirror/code/TwitterMirror.php new file mode 100644 index 0000000..7562fc8 --- /dev/null +++ b/twittermirror/code/TwitterMirror.php @@ -0,0 +1,284 @@ + 'DEBUG', + 1 => 'INFO', + 2 => 'NOTICE', + 3 => 'WARNING', + 4 => 'ERROR', + 5 => 'CRITICAL', + 6 => 'Turn logging off', + 'DEBUG' => 0, + 'INFO' => 1, + 'NOTICE' => 2, + 'WARNING' => 3, + 'ERROR' => 4, + 'CRITICAL' => 5, + 'Turn logging off' => 6, + ); + + // Getter / Setter + public function setLogThis($val){ + if(is_numeric($val) and ($val > 0) and ($val <= 5 )) self::$LogThis = $val; + return; + } + + public function getLogThis(){ + return self::$LogThis; + } + + public function setSearchterm($val){ + $val=trim($val); + if(!$val) return; + self::$Searchterm = $val; + } + + public function getSearchterm(){ + return self::$Searchterm; + } + + public function setTwitteruser($val){ + $val=trim($val); + if(!$val) return; + self::$twitteruser =$val; + } + + public function getTwitteruser(){ + return self::$twitteruser ; + } + + public function setTwitterpass($val){ + $val=trim($val); + if(!$val) return; + self::$twitterpass =$val; + } + + public function getTwitterpass(){ + return self::$twitterpass; + } + + /** + * twitter_hits_left() + * + * @return the numer of api-hits left or -1 on error + * + */ + private function twitter_hits_left(){ + $rate= new RestfulService("http://twitter.com/account/rate_limit_status.json", 0); + $rate->basicAuth(self::$twitteruser,self::$twitterpass ); + $response = $rate->request(); // + if($response->isError()){ + //@TODO Errorhandling + $errormsg= $response->getStatusCode() .' '. $response->getStatusDescription(); + $this->logit("$errormsg",4); + return -1; + } + $arryvalues = Convert::json2array($response->getbody()); + $this->logit("API requests left: " . $arryvalues['remaining_hits'], 1); + return $arryvalues['remaining_hits']; + } + + /** + * has_to_stop($myStartuptime) + * checks if there are at least 2 twitter api requests left + * and it must been less than 45 seconds the program has started + * (it is assumed that the program is run every minute, + * therfore 45 sec maximum runtime) + * @param $myStartuptime time in ticks when the program started + * + * @return bool true if one of the abort criterias are matched + * false if not + * + */ + private function has_to_stop($myStartuptime){ + // check runtime to avoid race conditions + if((time() - $myStartuptime ) > 45){ + $this->logit("We have to stop because " . time() ." > $myStartuptime + 45", 2); + return true; + } + //if we can get another page + $hl=0; + if(($hl=$this->twitter_hits_left()) <= 2){ + $this->logit("We have to stop because there are only $hl API-requests left",2); + return true; + } + $this->logit("$hl API-requests left",1); + return false; + } + + /** + * logit($text='--MARK--',$loglevel=1) + * echos $text to STDOUT if $loglevel is greater or equal $LogThis, which + * can be set in _config.php via + * TwitterMirror_Controller::setLogThis(1); + * Valid values are 0 .. 6 + * Where 6 turns the logging off + * 0 => 'DEBUG', + * 1 => 'INFO', + * 2 => 'NOTICE', + * 3 => 'WARNING', + * 4 => 'ERROR', + * 5 => 'CRITICAL', + * 6 => 'Turn logging off' + * + * @param $text Text to log + * @param $loglevel Level of importance of the log-entry + * + * + */ + + private function logit($text='--MARK--',$loglevel=1){ //all without logleve are handeled as Info + if($loglevel < $this->LogThis) return; + echo time() . " [". $this->getSearchterm() ."] " .$this->LogLevels[$loglevel]. ": " . $text . "\n"; + } + + + /** + * index() + * get's the newest feeds from twitter marked with {Searchterm} + * and stores them in the database; + * + */ + + public function index() { + $myStartuptime=time(); // keep the start time, because in 59s we run again ;0) + $this->logit("Starting at: $myStartuptime",0); + $searchterm=$this->getSearchterm(); + if($this->has_to_stop($myStartuptime)) return ; // we need at least 2 api hits left and a bit of time + $max_id=0; + $filter="searchterm = '". $searchterm ."'"; + $this->logit("searchterm=[$searchterm]\n filter is ==>$filter<==",0); + $tweet=DataObject::get_one('Tweet',$filter, false,'twitterid DESC'); + if($tweet){ + $max_id = $tweet->twitterid; + } + $this->logit("max_id is $max_id",0); + $params = array( + 'q' => $searchterm, + 'show_user' => 'true', + 'rpp' => 100, + ); + + if($max_id) $params['since_id']=$max_id; // search only for newer tweets + //Check for new tweets with #bemorehuman (searchterm) + //http://search.twitter.com/search.json?q=%23bemorehuman&show_user=true&rpp=100 + $rest= new RestfulService("http://search.twitter.com/search.json", 0); + $rest->basicAuth(self::$twitteruser,self::$twitterpass ); + $DoneWithIt=false; + while(!$DoneWithIt){ + $rest->setQueryString($params); + $response = $rest->request(); // + if($response->isError()){ + //@TODO Errorhandling + $errormsg= $response->getStatusCode() .' '. $response->getStatusDescription(); + $this->logit($errormsg,4); + return ; + } + $obj=Convert::json2obj($response->getbody()); + + // checking the results + $results=$obj->results; + if(!is_array($results)){ + // This should always be an array even if empty, so we assume the complete + // object to be corrupt. + //@TODO Throw Fatal Error or something simular + $this->logit("results should be an array",5); + die; + } + + if(($i = count($results)) <= 0){ + $DoneWithIt=true; + } + else{ + $this->logit("processing $i new entires",0); + //Process the resuls + echo "\n\n"; + foreach( $results as $result){ + $tweet = new Tweet; + $d=DateTime::createFromFormat("D, j M Y H:i:s O", $result->created_at); //"Thu, 14 Jan 2010 02:46:45 +0000" + $tweet->created_at = $d->format("Y-m-d H:i:s"); + $tweet->twitterid = $result->id; + $tweet->text = $result->text; + if(isset($result->iso_language_code)) $tweet->iso_language_code = $result->iso_language_code; + else $tweet->iso_language_code = "xx"; + if(isset($result->profile_image_url)) $tweet->profile_image_url = $result->profile_image_url; + $tweet->from_user_id = $result->from_user_id; + $tweet->from_user = $result->from_user_id; + if(isset($result->to_user_id)) $tweet->to_user_id = $result->to_user_id; + if(isset($result->to_user)) $tweet->to_user = $result->to_user; + $tweet->searchterm = $searchterm; + $tweet->write(); + $this->logit("Adding tweet " . $tweet->twitterid. " from " .$tweet->from_user . " (". $tweet->text .")",0); + //return; + } + //all results from this page should be in the DB now + // let's see if there is more to come + if(isset($obj->next_page)){ + $t1=strpos($obj->next_page,'page='); + if($t1){ + $t=substr($obj->next_page,$t1 + 5); //page= + $t2=strpos($t,'&'); + if($t2){ + $t=substr($t,0,$t2); + } + $params['page']=$t + 0; //Set the page parameter + } + else{ + // "no page= found\n"; + $this->logit("No next page info in next_page",2); + $DoneWithIt=true; + } + } + else{ + $this->logit("No next page",0); + $DoneWithIt=true; + } + if($this->has_to_stop($myStartuptime)) return; + } + } + return ; + } +} + + +?> diff --git a/twittermirror/code/model/Tweet.php b/twittermirror/code/model/Tweet.php new file mode 100644 index 0000000..74da7a4 --- /dev/null +++ b/twittermirror/code/model/Tweet.php @@ -0,0 +1,60 @@ + "SSDatetime", + "twitterid" => "Varchar", //is is in json + "text" => "Varchar(150)", + "iso_language_code" => "Varchar(5)", + "profile_image_url" =>"Varchar", + "from_user" =>"Varchar", + "from_user_id" => "Varchar", + "to_user" => "Varchar", + "to_user_id" => "Varchar", + "searchterm" => "Varchar" + ); + +} + +class Tweet_Controller extends Controller{ + + private function TweetLink($ext='.json'){ + if ($this->twitterid) + return "http://twitter.com/statuses/show/" . $this->twitterid . $ext; + return NULL; + } + + public function TweetLinkXML(){ + return TweetLink('.xml'); + } + + public function TweetLinkJSON(){ + return TweetLink('.json'); + } + + public function Link2FromTwitterUser(){ + if($this->from_user_id){ + return "http://twitter.com/" . $this->from_user_id; + } + return NULL; + } + + public function Link2ToTwitterUser(){ + if($this->to_user_id){ + return "http://twitter.com/" . $this->to_user_id; + } + return NULL; + } + + public function Link2Twitter($searchterm='#bemorehuman'){ + if(!$searchterm) return NULL; + if($this->from_user_id){ + return "http://twitter.com/#search?q=" . urlencode($searchterm); + } + return NULL; + } +} + + +?>