Skip to content

Commit

Permalink
Add new phptwitterbot
Browse files Browse the repository at this point in the history
  • Loading branch information
ornicar committed Apr 30, 2010
1 parent 99eb744 commit 916e961
Show file tree
Hide file tree
Showing 57 changed files with 22,084 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/vendor/phptwitterbot/.gitignore
@@ -0,0 +1 @@
nbproject
15 changes: 15 additions & 0 deletions lib/vendor/phptwitterbot/CHANGELOG
@@ -0,0 +1,15 @@
v2.0.0alpha1 - 2009-12-16

* Added a command line interface executable to manipulate phptwitterbot
* Replaced Twitter class by TwitterApiClient, able to use Identi.ca twitter-compliant API
* Added TwitterBot::processBotTimeline() method
* More tests added
* Added Tweet class to represent a tweet
* Added TwitterBotsFarm class to handle multiple bots configurations, in a fancy YAML file
* Added periodicity check for execution
* Added a way to process direct messages
* Added checks when following followers

v1.0.0 - 2009-04-22

* Created initial 1.0 version
21 changes: 21 additions & 0 deletions lib/vendor/phptwitterbot/LICENSE
@@ -0,0 +1,21 @@
The MIT License

Copyright (c) 2009 Nicolas Perriault

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
118 changes: 118 additions & 0 deletions lib/vendor/phptwitterbot/README.markdown
@@ -0,0 +1,118 @@
PHPTwitterBot v2 Documentation
==============================

Introduction
------------

A simple [Twitter](http://twitter.com/) Bot written in [PHP5](http://php.net/), allowing to search and retweet things.

Features
--------

* Clean OO architecture
* Twitter API client, which can request several implementations of the Twitter API (eg. the identi.ca one)
* Mockable Twitter API server class, to be able to unit-test the whole API without depending on the network connectivity
* A `TwitterBotsFarm` class, configurable with a simple [YAML](http://yaml.org/) file
* Configureable bot methods allowing to callback your own functions/callables
* Command line interface you can use to run configured farms and bots
* Unit-tested using the [http://trac.symfony-project.org/browser/tools/lime lime] testing framework

Installation
------------

You can download the [latest archive](http://github.com/n1k0/phptwitterbot/archives/master), or better checkout the [git](http://git-scm.com/) repository:

$ mkdir ~/mybots && cd ~/mybots && mkdir vendor
$ git clone git://github.com/n1k0/phptwitterbot.git vendor/phptwitterbot
$ ln -s vendor/phptwitterbot/bin/phptwitterbot phptwitterbot
$ php phptwitterbot --help

Then you have to create a bots farm configuration file:

$ mkdir config && touch config/bots.yml

See the next section to learn how to configure this file.

Farm and Bots configuration
---------------------------

A farm is a group of configured bots execution directives, which can be described using the YAML syntax.

Here's a sample farm configuration file:

global:
password: mYGenericPasswOrd # this password will be used by default for all bots
stoponfail: false # won't stop the whole process on error/exception
allow_magic_method: false # will allow php magic methods calls on bot classes
bots:
myfirstbotaccount:
password: mYAccountPasswOrd # this particular bot will use its own password
operations:
searchAndRetweet:
arguments:
terms: "twitter php class" # will search "twitter php class" on twitter timeline and retweet first matched tweets
periodicity: 1200 # will be run every 20 minutes
mysecondbotaccount:
operations:
searchAndRetweet:
arguments:
terms: "#fail" # will search for the "#fail" hashtag
options:
template: "FAIL! @%s: %s" # will render as "FAIL! @foobar: windows sucks #fail" where @foobar is the author of the original tweet
follow: true # will follow the tweet author automatically
periodicity: 600 # will be run every 10 minutes
followFollowers:
periodicity: 86400 # will be run every day

Each sub-element of the `bots` section describes a single bot and its available operations, where the key is the bot username. Of course you still have to create a dedicated Twitter account for each bot.

In the provided example, the `mysecondbotaccount` bot will run the `searchAndRetweet` and `followFollowers` operations whereas the `myfirstbotaccount` bot will only run the `searchAndRetweet` one, each time with the provided parameters, options and the specified periodicity (in seconds).

For instance, the `searchAndRetweet` operation will search for terms into the public twitter timeline and retweet the first matched tweet containing them using a given formatter pattern. Note that the `follow` option will make the bot to follow the author of a matched tweet automatically.

The `followFollowers` operation will check periodically the list of followers for the bot account, and follow every of them back in return.

Check the `TwitterBot.class.php` API to see what are the available other operations.

To run the bots farm once configured, just use the command line interface:

$ php phptwitterbot config/bots.yml

The command line interface
--------------------------

PHPTwitterBot ships with a shiny `phptwitterbot` executable for the command line interface you can find in the `bin` folder of the project codebase. This program allows to run all configured bots farm operations in one call.

### Usage and Options

Note that this program can be executed several ways:

$ php bin/phptwitterbot
$ bin/phptwitterbot
$ cd bin
$ php phptwitterbot
$ ./phptwitterbot
$ sudo ln -s phptwitterbot /usr/bin/phptwitterbot
$ phptwitterbot

The only required argument is the relative or absolute path to where the YAML bots configuration file resides:

$ ./phptwitterbot config/bots_configuration.yml
$ ./phptwitterbot /home/user/my_other_bots_configuration.yml

To run a particular bot, use the --bot option:

$ ./phptwitterbot myBots.yml --bot=myBotName

To set the path of a custom cronlogs file (this file will store the logs of
bot executions):

$ ./phptwitterbot configFile.yml --cronlogs=/tmp/my_cronlogs.log

To enable verbose debugging output, use the --debug option:

$ ./phptwitterbot configFile.yml --debug

To run the whole phptwitterbot unit tests suite, use the --test option:

$ ./phptwitterbot --test
3 changes: 3 additions & 0 deletions lib/vendor/phptwitterbot/bin/phptwitterbot
@@ -0,0 +1,3 @@
#!/usr/bin/env php
<?php
include_once dirname(__FILE__).'/phptwitterbot.php';
110 changes: 110 additions & 0 deletions lib/vendor/phptwitterbot/bin/phptwitterbot.php
@@ -0,0 +1,110 @@
<?php
require_once dirname(__FILE__).'/../lib/TwitterBotsFarm.class.php';

// Check if we have at least a configuration file provided as the first argument

$configFile = null;
$botName = null;
$cronLogsFile = null;
$debug = false;
$forceUpdate = false;

foreach (array_slice($argv, 1) as $argValue)
{
$arg = $name = substr($argValue, strpos($argValue, '--') + 2);

if (false !== strpos($arg, '='))
{
list($name, $optionValue) = explode('=', $arg);
}

switch ($name)
{
case 'bot':
$botName = $optionValue;
break;

case 'cronlogs':
$cronLogsFile = $optionValue;
break;

case 'debug':
$debug = true;
break;

case 'force':
$forceUpdate = true;
break;

case 'help':
exit(help($argv[0]));
break;

case 'test':
echo "Lauching test suite...".PHP_EOL.PHP_EOL;
include dirname(__FILE__).'/../test/bin/prove.php';
break;

default:
$configFile = (file_exists($argValue) ? $argValue : false);
break;
}
}

if (!$configFile)
{
exit(help($argv[0]));
}

try
{
$farm = TwitterBotsFarm::create($configFile, $cronLogsFile, $debug, $forceUpdate);

if (!is_null($botName))
{
$farm->runBot($botName);
}
else
{
$farm->run();
}
}
catch (Exception $e)
{
exit(sprintf('Farm execution stopped with error: "%s"%s', $e->getMessage(), PHP_EOL));
}

function help($scriptName)
{
$year = date('Y');

return <<<EOF
©{$year} Nicolas Perriault - http://code.google.com/p/phptwitterbot
This executable runs a PHPTwitterBot farm from the command line, using a YAML
configuration file.
Usage:
\$ {$scriptName} config/bots_configuration.yml
\$ {$scriptName} /home/user/my_other_bots_configuration.yml
To run a particular bot, use the --bot option:
\$ {$scriptName} myBots.yml --bot=myBotName
To set the path of a custom cronlogs file (this file will store the logs of
bot executions):
\$ {$scriptName} configFile.yml --cronlogs=/tmp/my_cronlogs.log
To enable verbose debugging output, use the --debug option:
\$ {$scriptName} configFile.yml --debug
To run the whole phptwitterbot unit tests suite, use the --test option:
\$ {$scriptName} --test
EOF;
}
50 changes: 50 additions & 0 deletions lib/vendor/phptwitterbot/lib/Tweet.class.php
@@ -0,0 +1,50 @@
<?php
require_once dirname(__FILE__).'/../lib/TwitterUser.class.php';
require_once dirname(__FILE__).'/TwitterEntity.class.php';

/**
* This class represents a Tweet, for unified interface access regarding format
* used by the twitter API
*
* @author Nicolas Perriault <nperriault at gmail dot com>
* @license MIT License
*/
class Tweet extends TwitterEntity
{
public
$created_at,
$id,
$geo,
$text,
$source,
$truncated,
$from_user,
$from_user_id,
$to_user,
$to_user_id,
$in_reply_to_status_id,
$in_reply_to_user_id,
$iso_language_code,
$profile_image_url,
$favorited,
$in_reply_to_screen_name,
$user;

static public function createFromArray(array $array = array())
{
$entity = new self();

foreach ($array as $propertyName => $propertyValue)
{
if (!property_exists($entity, $propertyName))
{
// ignore unknown entities like "metadata"
continue;
}

$entity->$propertyName = parent::cleanValue($propertyValue);
}

return $entity;
}
}
30 changes: 30 additions & 0 deletions lib/vendor/phptwitterbot/lib/TweetCollection.class.php
@@ -0,0 +1,30 @@
<?php
require_once dirname(__FILE__).'/Tweet.class.php';

/**
* Collection of Tweet instances
*
* @author Nicolas Perriault <nperriault at gmail dot com>
* @license MIT License
*/
class TweetCollection extends TwitterEntity
{
static public function createFromJSON($json)
{
$tweets = json_decode($json, true);

if (!is_array($tweets) || !isset($tweets['results']))
{
throw new InvalidArgumentException('Unable to decode JSON response');
}

$tweetCollection = array();

foreach ($tweets['results'] as $tweetArray)
{
$tweetCollection[] = Tweet::createFromArray($tweetArray);
}

return new self($tweetCollection);
}
}

0 comments on commit 916e961

Please sign in to comment.