PageRenderTime 31ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/gavel-guy/find-expiring-auctions.php

https://github.com/danfinnie/twitter-auctioneer
PHP | 128 lines | 95 code | 23 blank | 10 comment | 2 complexity | 6d220f8e2bae2e274007c21bcdbfade4 MD5 | raw file
  1. <?php
  2. require dirname(__FILE__) . '/../vendor/autoload.php';
  3. use Guzzle\Http\Client;
  4. use Guzzle\Plugin\Oauth\OauthPlugin;
  5. set_time_limit(0);
  6. $last_action_timeout = 60;
  7. /**
  8. * Notification states
  9. * 0 - Nothing sent
  10. * 1 - Auction announced
  11. * 2 - You have been outbid notifications
  12. * 3 - Someone won
  13. */
  14. while(true) {
  15. sleep(1);
  16. echo "Iterating\n";
  17. $dbh = new PDO("mysql:host=" . getenv('AUCTIONEER_MYSQL_HOST') . ";dbname=" . getenv('AUCTIONEER_MYSQL_DB'), getenv('AUCTIONEER_MYSQL_USER'), getenv('AUCTIONEER_MYSQL_PASSWORD'));
  18. $twitter = new Client('https://api.twitter.com/{version}', array(
  19. 'version' => '1.1',
  20. 'ssl.certificate_authority' => 'system',
  21. ));
  22. $twitter->addSubscriber(new OauthPlugin(array(
  23. 'consumer_key' => getenv('AUCTIONEER_TWITTER_CONSUMER_KEY'),
  24. 'consumer_secret' => getenv('AUCTIONEER_TWITTER_CONSUMER_SECRET'),
  25. 'token' => getenv('AUCTIONEER_TWITTER_ACCESS_TOKEN'),
  26. 'token_secret' => getenv('AUCTIONEER_TWITTER_ACCESS_TOKEN_SECRET'),
  27. )));
  28. $last_bidders_stmt = $dbh->prepare("SELECT twitter_user_name FROM bids WHERE auction_id=? ORDER BY price DESC");
  29. $mark_sent_stmt = $dbh->prepare("UPDATE auctions SET notification_state=2 WHERE auction_id=?");
  30. foreach($dbh->query("
  31. SELECT *
  32. FROM auctions
  33. JOIN (SELECT max(bids.date) as last_action, auctions.auction_id
  34. FROM auctions
  35. JOIN bids
  36. ON bids.auction_id = auctions.auction_id
  37. GROUP BY auctions.auction_id) maxers
  38. ON maxers.auction_id = auctions.auction_id
  39. WHERE last_action < NOW() - 2 * $last_action_timeout
  40. AND winner_user_id IS NULL
  41. AND notification_state = 1
  42. ;") as $row) {
  43. $last_bidders_stmt->bindValue(1, $row['auction_id']);
  44. $last_bidders_stmt->execute();
  45. // Do this in PHP because doing it in MySQL changes the order.
  46. $last_bidders_arr = array_slice(array_unique($last_bidders_stmt->fetchAll(PDO::FETCH_COLUMN)), 1, 3);
  47. if (count($last_bidders_arr) > 0) {
  48. $last_bidders = '@' . implode(" @", $last_bidders_arr);
  49. $tweet = "$last_bidders Going once on the #$row[item]. Bid now or forever hold your peace!";
  50. echo $tweet . "\n";
  51. $twitter
  52. ->post("statuses/update.json")
  53. ->addPostFields(array("status" => $tweet))
  54. ->send();
  55. $mark_sent_stmt->execute(array($row['auction_id']));
  56. } else {
  57. echo "Not enough people to remind about #$row[item], no. $row[auction_id]\n";
  58. }
  59. }
  60. $update_auction = $dbh->prepare("UPDATE auctions SET notification_state=1 WHERE auction_id=?");
  61. foreach($dbh->query("
  62. SELECT *
  63. FROM auctions
  64. WHERE notification_state = 0
  65. ;") as $row) {
  66. $tweet = "We have a #$row[item] up for grabs!";
  67. echo $tweet . "\n";
  68. $update_auction->execute(array($row['auction_id']));
  69. $twitter
  70. ->post("statuses/update.json")
  71. ->addPostFields(array("status" => $tweet))
  72. ->send();
  73. }
  74. $winning_bidder_stmt = $dbh->prepare("select twitter_user_id, twitter_user_name, price from bids where auction_id=? order by price desc, date asc limit 1;");
  75. $update_auction = $dbh->prepare("UPDATE auctions SET price = ?, winner_user_id=? WHERE auction_id=?");
  76. // TODO: This query below is almost exactly like the one above but reminders_sent = 1 not 0. Maybe Tim
  77. // has some magic way of reusing the rest of the code, maybe using chaining???
  78. foreach($dbh->query("
  79. SELECT *
  80. FROM auctions
  81. JOIN (SELECT max(bids.date) as last_action, auctions.auction_id
  82. FROM auctions
  83. JOIN bids
  84. ON bids.auction_id = auctions.auction_id
  85. GROUP BY auctions.auction_id) maxers
  86. ON maxers.auction_id = auctions.auction_id
  87. WHERE last_action < NOW() - $last_action_timeout
  88. AND winner_user_id IS NULL
  89. AND notification_state = 2
  90. ;") as $row) {
  91. $winning_bidder_stmt->bindValue(1, $row['auction_id']);
  92. $winning_bidder_stmt->execute();
  93. $winning_bidder = $winning_bidder_stmt->fetch();
  94. $tweet = "@$winning_bidder[twitter_user_name] won @$row[seller_user_id]'s #$row[item] for $winning_bidder[price] bucks!";
  95. echo $tweet . "\n";
  96. $update_auction->execute(array($winning_bidder['price'], $winning_bidder['twitter_user_id'], $row['auction_id']));
  97. $twitter
  98. ->post("statuses/update.json")
  99. ->addPostFields(array("status" => $tweet))
  100. ->send();
  101. }
  102. }