Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- /*
- * Title: SyncStory
- * Filename: syncstory_script.php
- * Description:
- * Author: Steve Aquilano
- */
- ################################################################################
- ### set dependencies
- ################################################################################
- ini_set('include_path', '/home/saa359/scripts/lib/');
- require_once('Arc90/Service/Twitter.php');
- ################################################################################
- ### global variables
- ################################################################################
- $scriptName = $_SERVER['PHP_SELF'];
- $debug = false; // 'true' or 'false'
- ################################################################################
- ### mysql database info
- ################################################################################
- $mySqlHostname = "localhost";
- $mySqlUsername = "xxx";
- $mySqlPassword = "xxx";
- $mySqlDatabase = "xxx";
- $connection = mysql_connect($mySqlHostname, $mySqlUsername, $mySqlPassword);
- $db_selected = mysql_select_db($mySqlDatabase, $connection);
- ################################################################################
- ### create new twitter object
- ################################################################################
- $twitter = new Arc90_Service_Twitter('syncstory', '3ister3an');
- ################################################################################
- ### check followers. friend any that you (syncstory) aren't following. Req. for DM
- ################################################################################
- # first check for followers
- $params = array();
- //$params['id'] = 'ID_OF_USER_TO_GET_FRIENDS_FOR';
- //$params['page'] = '0';
- $params['lite'] = TRUE; //removes status
- $response = $twitter->getFollowers('json', $params);
- $return = $response->getData(); // Print the XML response
- $followersArray = ( json_decode($return) );
- //print_r($followArray);
- // declare variable so that if there are no new followers, no error
- $idToFollowArray = array();
- for ( $i = 0; $i < sizeof($followersArray); $i++) {
- $idToFollowArray[$i] = $followersArray[$i]->id;
- }
- if($debug) {
- $return = $response->getData(); // Print the XML response
- print_r( json_decode($return) );
- }
- # if not in table blulmntfollow, create friendship/follow and log to db
- $followIdMatch = 0;
- for ( $i = 0; $i < sizeof($idToFollowArray); $i++) {
- # define query statement
- $SqlStatement = "SELECT twitterid FROM blulmntfollow WHERE twitterid=$idToFollowArray[$i]";
- # Run the query on the database through the connection
- $result = mysql_query($SqlStatement,$connection);
- if (!$result)
- die("Error " . mysql_errno() . " : " . mysql_error());
- while($row = mysql_fetch_array($result,MYSQL_ASSOC))
- $followIdMatch = $row['twitterid'];
- # if the sender does not exist in the db, add them
- if(!($followIdMatch)) {
- # FIRST -- FOLLOW THEM/Create Friendship (required to send them a DM)
- $idOfUserToBefriend = $idToFollowArray[$i];
- $response = $twitter->createFriendship($idOfUserToBefriend, 'json');
- if($debug) {
- $return = $response->getData();
- print_r( json_decode($return) );
- }
- # define query statement
- $SqlStatement = "INSERT INTO blulmntfollow (twitterid, following)
- VALUES ($idToFollowArray[$i], 1)";
- # Run the query on the database through the connection
- $result = mysql_query($SqlStatement,$connection);
- if (!$result)
- die("Error " . mysql_errno() . " : " . mysql_error());
- }
- else {
- if($debug)
- echo "***You are already following user " . $idToFollowArray[$i] . "\n";
- }
- }
- ################################################################################
- ### get new direct messages since last script run
- ################################################################################
- // pull last_dm_id from blulmntinfo table in db
- # define query statement
- $SqlStatement = "SELECT last_dm_id FROM blulmntinfo ORDER BY last_dm_id DESC LIMIT 1";
- # Run the query on the database through the connection
- $result = mysql_query($SqlStatement,$connection);
- if (!$result)
- die("Error " . mysql_errno() . " : " . mysql_error());
- while($row = mysql_fetch_array($result,MYSQL_ASSOC))
- $lastDmId = $row['last_dm_id'];
- $params = array();
- //$params['page'] = '0';
- //$params['since'] = mktime(date("H")-4, date("i"), date("s")); //unix timestamp;
- $params['since_id'] = $lastDmId; // default '0'
- $response = $twitter->getMessages('json', $params);
- $return = $response->getData();
- $dmArray = json_decode($return);
- //print_r($dmArray);
- //define variables so that if there is no new dm - it doesn't cause an error.
- $senderIdArray = array();
- $senderSnArray = array();
- $dmIdArray = array();
- $dmTextArray = array();
- for ( $i =0 ; $i < sizeof($dmArray); $i++ ) {
- $senderIdArray[$i] = $dmArray[0]->sender_id;
- $senderSnArray[$i] = $dmArray[0]->sender_screen_name;
- $dmIdArray[$i] = $dmArray[0]->id;
- $dmTextArray[$i] = $dmArray[0]->text;
- }
- if($debug) {
- echo "***Sender id:\n";
- print_r($senderIdArray);
- echo "***Sender screen name:\n";
- print_r($senderSnArray);
- echo "***DM id:\n";
- print_r($dmIdArray);
- echo "***DM text:\n";
- print_r($dmTextArray);
- }
- ################################################################################
- ### if new user, add to database, send first line of adventure
- ################################################################################
- $senderIdMatch = 0;
- for ( $i = 0; $i < sizeof($senderIdArray); $i++) {
- #log all dm message id's for retireve dm's since_id above
- # define query statement
- $SqlStatement = "INSERT INTO blulmntinfo (last_dm_id) VALUES ($dmIdArray[$i])";
- # Run the query on the database through the connection
- $result = mysql_query($SqlStatement,$connection);
- if (!$result)
- die("Error " . mysql_errno() . " : " . mysql_error());
- # compare twitterid to see if it is already in the database
- # define query statement
- $SqlStatement = "SELECT twitterid FROM blulmntusers WHERE twitterid=$senderIdArray[$i]";
- # Run the query on the database through the connection
- $result = mysql_query($SqlStatement,$connection);
- if (!$result)
- die("Error " . mysql_errno() . " : " . mysql_error());
- while($row = mysql_fetch_array($result,MYSQL_ASSOC))
- $senderIdMatch = $row['twitterid'];
- # if the sender does not exist in the db, add them
- if(!($senderIdMatch)) {
- ### MOVED ABOVE ### EVENTUALLY DELETE ###
- // # FIRST -- FOLLOW THEM/Create Friendship (required to send them a DM)
- // $idOfUserToBefriend = $senderIdArray[$i];
- // $response = $twitter->createFriendship($idOfUserToBefriend, 'json');
- // if($debug) {
- // $return = $response->getData();
- // print_r( json_decode($return) );
- // }
- # define query statement
- $SqlStatement = "INSERT INTO blulmntusers (twitterid, twittername)
- VALUES ($senderIdArray[$i], '$senderSnArray[$i]')";
- # Run the query on the database through the connection
- $result = mysql_query($SqlStatement,$connection);
- if (!$result)
- die("Error " . mysql_errno() . " : " . mysql_error());
- # pull first line of adventure from db and send to sender
- $SqlStatement = "SELECT name FROM narrative WHERE id=1";
- $result = mysql_query($SqlStatement,$connection);
- if (!$result)
- die("Error " . mysql_errno() . " : " . mysql_error());
- while($row = mysql_fetch_array($result,MYSQL_ASSOC))
- $rawDmTextToSend = $row['name'];
- // get from db and replace [a] and [b] with options
- $SqlStatement = "SELECT n.id, s.name FROM narrative n, selection s, narrative_x_selection x WHERE x.narrative_id=n.id AND x.selection_id=s.id AND x.narrative_id=1";
- $result = mysql_query($SqlStatement,$connection);
- if (!$result)
- die("Error " . mysql_errno() . " : " . mysql_error());
- for ($j = 0; $row = mysql_fetch_array($result,MYSQL_ASSOC); $j++) {
- $trigger[$j] = $row['name'];
- }
- $patterns = array("/\[a\]/", "/\[b\]/");
- $replacements = array($trigger[0], $trigger[1]);
- $dmTextToSend = preg_replace($patterns, $replacements, $rawDmTextToSend);
- // finally send dm to user
- $sendToUser = "$senderIdArray[$i]"; // can be username or userid
- $response = $twitter->sendMessage($sendToUser, $dmTextToSend, 'json');
- if($debug)
- echo $return = $response->getData();
- }
- else {
- if($debug)
- echo "***Sender already exists \n";
- }
- }
- ?>
Add Comment
Please, Sign In to add comment