Guest User

Untitled

a guest
May 1st, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.70 KB | None | 0 0
  1. <?PHP
  2.  
  3. /*
  4. * Title: SyncStory
  5. * Filename: syncstory_script.php
  6. * Description:
  7. * Author: Steve Aquilano
  8. */
  9.  
  10. ################################################################################
  11. ### set dependencies
  12. ################################################################################
  13. ini_set('include_path', '/home/saa359/scripts/lib/');
  14. require_once('Arc90/Service/Twitter.php');
  15.  
  16.  
  17. ################################################################################
  18. ### global variables
  19. ################################################################################
  20. $scriptName = $_SERVER['PHP_SELF'];
  21.  
  22. $debug = false; // 'true' or 'false'
  23.  
  24.  
  25. ################################################################################
  26. ### mysql database info
  27. ################################################################################
  28. $mySqlHostname = "localhost";
  29. $mySqlUsername = "xxx";
  30. $mySqlPassword = "xxx";
  31. $mySqlDatabase = "xxx";
  32.  
  33. $connection = mysql_connect($mySqlHostname, $mySqlUsername, $mySqlPassword);
  34. $db_selected = mysql_select_db($mySqlDatabase, $connection);
  35.  
  36.  
  37. ################################################################################
  38. ### create new twitter object
  39. ################################################################################
  40. $twitter = new Arc90_Service_Twitter('syncstory', '3ister3an');
  41.  
  42.  
  43. ################################################################################
  44. ### check followers. friend any that you (syncstory) aren't following. Req. for DM
  45. ################################################################################
  46. # first check for followers
  47. $params = array();
  48.  
  49. //$params['id'] = 'ID_OF_USER_TO_GET_FRIENDS_FOR';
  50. //$params['page'] = '0';
  51. $params['lite'] = TRUE; //removes status
  52.  
  53. $response = $twitter->getFollowers('json', $params);
  54.  
  55. $return = $response->getData(); // Print the XML response
  56. $followersArray = ( json_decode($return) );
  57. //print_r($followArray);
  58.  
  59. // declare variable so that if there are no new followers, no error
  60. $idToFollowArray = array();
  61.  
  62. for ( $i = 0; $i < sizeof($followersArray); $i++) {
  63. $idToFollowArray[$i] = $followersArray[$i]->id;
  64. }
  65.  
  66. if($debug) {
  67. $return = $response->getData(); // Print the XML response
  68. print_r( json_decode($return) );
  69. }
  70.  
  71. # if not in table blulmntfollow, create friendship/follow and log to db
  72. $followIdMatch = 0;
  73.  
  74. for ( $i = 0; $i < sizeof($idToFollowArray); $i++) {
  75.  
  76. # define query statement
  77. $SqlStatement = "SELECT twitterid FROM blulmntfollow WHERE twitterid=$idToFollowArray[$i]";
  78.  
  79. # Run the query on the database through the connection
  80. $result = mysql_query($SqlStatement,$connection);
  81. if (!$result)
  82. die("Error " . mysql_errno() . " : " . mysql_error());
  83.  
  84. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  85. $followIdMatch = $row['twitterid'];
  86.  
  87. # if the sender does not exist in the db, add them
  88. if(!($followIdMatch)) {
  89.  
  90. # FIRST -- FOLLOW THEM/Create Friendship (required to send them a DM)
  91. $idOfUserToBefriend = $idToFollowArray[$i];
  92. $response = $twitter->createFriendship($idOfUserToBefriend, 'json');
  93. if($debug) {
  94. $return = $response->getData();
  95. print_r( json_decode($return) );
  96. }
  97.  
  98. # define query statement
  99. $SqlStatement = "INSERT INTO blulmntfollow (twitterid, following)
  100. VALUES ($idToFollowArray[$i], 1)";
  101.  
  102. # Run the query on the database through the connection
  103. $result = mysql_query($SqlStatement,$connection);
  104. if (!$result)
  105. die("Error " . mysql_errno() . " : " . mysql_error());
  106. }
  107. else {
  108. if($debug)
  109. echo "***You are already following user " . $idToFollowArray[$i] . "\n";
  110. }
  111. }
  112.  
  113.  
  114. ################################################################################
  115. ### get new direct messages since last script run
  116. ################################################################################
  117. // pull last_dm_id from blulmntinfo table in db
  118. # define query statement
  119. $SqlStatement = "SELECT last_dm_id FROM blulmntinfo ORDER BY last_dm_id DESC LIMIT 1";
  120.  
  121. # Run the query on the database through the connection
  122. $result = mysql_query($SqlStatement,$connection);
  123. if (!$result)
  124. die("Error " . mysql_errno() . " : " . mysql_error());
  125.  
  126. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  127. $lastDmId = $row['last_dm_id'];
  128.  
  129. $params = array();
  130. //$params['page'] = '0';
  131. //$params['since'] = mktime(date("H")-4, date("i"), date("s")); //unix timestamp;
  132. $params['since_id'] = $lastDmId; // default '0'
  133.  
  134. $response = $twitter->getMessages('json', $params);
  135. $return = $response->getData();
  136. $dmArray = json_decode($return);
  137. //print_r($dmArray);
  138.  
  139. //define variables so that if there is no new dm - it doesn't cause an error.
  140. $senderIdArray = array();
  141. $senderSnArray = array();
  142. $dmIdArray = array();
  143. $dmTextArray = array();
  144.  
  145. for ( $i =0 ; $i < sizeof($dmArray); $i++ ) {
  146. $senderIdArray[$i] = $dmArray[0]->sender_id;
  147. $senderSnArray[$i] = $dmArray[0]->sender_screen_name;
  148. $dmIdArray[$i] = $dmArray[0]->id;
  149. $dmTextArray[$i] = $dmArray[0]->text;
  150. }
  151.  
  152. if($debug) {
  153. echo "***Sender id:\n";
  154. print_r($senderIdArray);
  155. echo "***Sender screen name:\n";
  156. print_r($senderSnArray);
  157. echo "***DM id:\n";
  158. print_r($dmIdArray);
  159. echo "***DM text:\n";
  160. print_r($dmTextArray);
  161. }
  162.  
  163.  
  164. ################################################################################
  165. ### if new user, add to database, send first line of adventure
  166. ################################################################################
  167. $senderIdMatch = 0;
  168.  
  169. for ( $i = 0; $i < sizeof($senderIdArray); $i++) {
  170. #log all dm message id's for retireve dm's since_id above
  171. # define query statement
  172. $SqlStatement = "INSERT INTO blulmntinfo (last_dm_id) VALUES ($dmIdArray[$i])";
  173.  
  174. # Run the query on the database through the connection
  175. $result = mysql_query($SqlStatement,$connection);
  176. if (!$result)
  177. die("Error " . mysql_errno() . " : " . mysql_error());
  178.  
  179. # compare twitterid to see if it is already in the database
  180. # define query statement
  181. $SqlStatement = "SELECT twitterid FROM blulmntusers WHERE twitterid=$senderIdArray[$i]";
  182.  
  183. # Run the query on the database through the connection
  184. $result = mysql_query($SqlStatement,$connection);
  185. if (!$result)
  186. die("Error " . mysql_errno() . " : " . mysql_error());
  187.  
  188. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  189. $senderIdMatch = $row['twitterid'];
  190.  
  191. # if the sender does not exist in the db, add them
  192. if(!($senderIdMatch)) {
  193.  
  194. ### MOVED ABOVE ### EVENTUALLY DELETE ###
  195. // # FIRST -- FOLLOW THEM/Create Friendship (required to send them a DM)
  196. // $idOfUserToBefriend = $senderIdArray[$i];
  197. // $response = $twitter->createFriendship($idOfUserToBefriend, 'json');
  198. // if($debug) {
  199. // $return = $response->getData();
  200. // print_r( json_decode($return) );
  201. // }
  202.  
  203. # define query statement
  204. $SqlStatement = "INSERT INTO blulmntusers (twitterid, twittername)
  205. VALUES ($senderIdArray[$i], '$senderSnArray[$i]')";
  206.  
  207. # Run the query on the database through the connection
  208. $result = mysql_query($SqlStatement,$connection);
  209. if (!$result)
  210. die("Error " . mysql_errno() . " : " . mysql_error());
  211.  
  212. # pull first line of adventure from db and send to sender
  213. $SqlStatement = "SELECT name FROM narrative WHERE id=1";
  214. $result = mysql_query($SqlStatement,$connection);
  215. if (!$result)
  216. die("Error " . mysql_errno() . " : " . mysql_error());
  217.  
  218. while($row = mysql_fetch_array($result,MYSQL_ASSOC))
  219. $rawDmTextToSend = $row['name'];
  220.  
  221. // get from db and replace [a] and [b] with options
  222. $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";
  223. $result = mysql_query($SqlStatement,$connection);
  224. if (!$result)
  225. die("Error " . mysql_errno() . " : " . mysql_error());
  226.  
  227. for ($j = 0; $row = mysql_fetch_array($result,MYSQL_ASSOC); $j++) {
  228. $trigger[$j] = $row['name'];
  229. }
  230. $patterns = array("/\[a\]/", "/\[b\]/");
  231. $replacements = array($trigger[0], $trigger[1]);
  232. $dmTextToSend = preg_replace($patterns, $replacements, $rawDmTextToSend);
  233.  
  234. // finally send dm to user
  235. $sendToUser = "$senderIdArray[$i]"; // can be username or userid
  236. $response = $twitter->sendMessage($sendToUser, $dmTextToSend, 'json');
  237. if($debug)
  238. echo $return = $response->getData();
  239. }
  240. else {
  241. if($debug)
  242. echo "***Sender already exists \n";
  243. }
  244. }
  245.  
  246.  
  247.  
  248. ?>
Add Comment
Please, Sign In to add comment