@@ -6,7 +6,7 @@ discard block |
||
| 6 | 6 | use Jacobemerick\Web\Domain\Stream\Goodread\MysqlGoodreadRepository as GoodreadRepository; |
| 7 | 7 | |
| 8 | 8 | $client = new Client([ |
| 9 | - 'base_uri' => 'http://www.goodreads.com', |
|
| 9 | + 'base_uri' => 'http://www.goodreads.com', |
|
| 10 | 10 | ]); |
| 11 | 11 | |
| 12 | 12 | $goodreadRepository = new GoodreadRepository($container['db_connection_locator']); |
@@ -20,23 +20,23 @@ discard block |
||
| 20 | 20 | $reviews = simplexml_load_string($reviews, 'SimpleXMLElement', LIBXML_NOCDATA); |
| 21 | 21 | |
| 22 | 22 | foreach ($reviews->channel->item as $review) { |
| 23 | - $datetime = new DateTime((string) $review->pubDate); |
|
| 24 | - if ($datetime <= $mostRecentReviewDateTime) { |
|
| 25 | - break; |
|
| 26 | - } |
|
| 27 | - |
|
| 28 | - $uniqueReviewCheck = $goodreadRepository->getReviewByPermalink((string) $review->guid); |
|
| 29 | - if ($uniqueReviewCheck !== false) { |
|
| 30 | - continue; |
|
| 31 | - } |
|
| 32 | - |
|
| 33 | - $datetime->setTimezone($container['default_timezone']); |
|
| 34 | - $metadata = json_decode(json_encode($review), true); |
|
| 35 | - |
|
| 36 | - $goodreadRepository->insertReview( |
|
| 37 | - (string) $review->guid, |
|
| 38 | - (string) $review->book_id, |
|
| 39 | - $datetime, |
|
| 40 | - $metadata |
|
| 41 | - ); |
|
| 23 | + $datetime = new DateTime((string) $review->pubDate); |
|
| 24 | + if ($datetime <= $mostRecentReviewDateTime) { |
|
| 25 | + break; |
|
| 26 | + } |
|
| 27 | + |
|
| 28 | + $uniqueReviewCheck = $goodreadRepository->getReviewByPermalink((string) $review->guid); |
|
| 29 | + if ($uniqueReviewCheck !== false) { |
|
| 30 | + continue; |
|
| 31 | + } |
|
| 32 | + |
|
| 33 | + $datetime->setTimezone($container['default_timezone']); |
|
| 34 | + $metadata = json_decode(json_encode($review), true); |
|
| 35 | + |
|
| 36 | + $goodreadRepository->insertReview( |
|
| 37 | + (string) $review->guid, |
|
| 38 | + (string) $review->book_id, |
|
| 39 | + $datetime, |
|
| 40 | + $metadata |
|
| 41 | + ); |
|
| 42 | 42 | } |
@@ -4,17 +4,17 @@ discard block |
||
| 4 | 4 | |
| 5 | 5 | $options = getopt('f:'); |
| 6 | 6 | if (empty($options['f'])) { |
| 7 | - exit('Must pass in a file with the f parameter.'); |
|
| 7 | + exit('Must pass in a file with the f parameter.'); |
|
| 8 | 8 | } |
| 9 | 9 | |
| 10 | 10 | use Abraham\TwitterOAuth\TwitterOAuth; |
| 11 | 11 | use Jacobemerick\Web\Domain\Stream\Twitter\MysqlTwitterRepository as TwitterRepository; |
| 12 | 12 | |
| 13 | 13 | $client = new TwitterOAuth( |
| 14 | - $config->twitter->consumer_key, |
|
| 15 | - $config->twitter->consumer_secret, |
|
| 16 | - $config->twitter->access_token, |
|
| 17 | - $config->twitter->access_token_secret |
|
| 14 | + $config->twitter->consumer_key, |
|
| 15 | + $config->twitter->consumer_secret, |
|
| 16 | + $config->twitter->access_token, |
|
| 17 | + $config->twitter->access_token_secret |
|
| 18 | 18 | ); |
| 19 | 19 | $client->setDecodeJsonAsArray(true); |
| 20 | 20 | |
@@ -24,29 +24,29 @@ discard block |
||
| 24 | 24 | |
| 25 | 25 | $handle = fopen(__DIR__ . '/' . $options['f'], 'r'); |
| 26 | 26 | while ($row = fgets($handle)) { |
| 27 | - array_push($idList, trim($row)); |
|
| 28 | - if (count($idList) == 100) { |
|
| 29 | - $tweetLookup = $client->get('statuses/lookup', [ |
|
| 30 | - 'id' => implode(',', $idList), |
|
| 31 | - 'trim_user' => true, |
|
| 32 | - ]); |
|
| 33 | - |
|
| 34 | - foreach ($tweetLookup as $tweet) { |
|
| 35 | - $uniqueTweetCheck = $twitterRepository->getTwitterByTweetId($tweet['id_str']); |
|
| 36 | - if ($uniqueTweetCheck !== false) { |
|
| 37 | - if ($uniqueTweetCheck['metadata'] != json_encode($tweet)) { |
|
| 38 | - $twitterRepository->updateTweetMetadata($tweet['id_str'], $tweet); |
|
| 39 | - } |
|
| 40 | - continue; |
|
| 41 | - } |
|
| 42 | - |
|
| 43 | - $twitterRepository->insertTweet( |
|
| 44 | - $tweet['id_str'], |
|
| 45 | - (new DateTime($tweet['created_at']))->setTimezone($container['default_timezone']), |
|
| 46 | - $tweet |
|
| 47 | - ); |
|
| 48 | - } |
|
| 49 | - $idList = []; |
|
| 50 | - } |
|
| 27 | + array_push($idList, trim($row)); |
|
| 28 | + if (count($idList) == 100) { |
|
| 29 | + $tweetLookup = $client->get('statuses/lookup', [ |
|
| 30 | + 'id' => implode(',', $idList), |
|
| 31 | + 'trim_user' => true, |
|
| 32 | + ]); |
|
| 33 | + |
|
| 34 | + foreach ($tweetLookup as $tweet) { |
|
| 35 | + $uniqueTweetCheck = $twitterRepository->getTwitterByTweetId($tweet['id_str']); |
|
| 36 | + if ($uniqueTweetCheck !== false) { |
|
| 37 | + if ($uniqueTweetCheck['metadata'] != json_encode($tweet)) { |
|
| 38 | + $twitterRepository->updateTweetMetadata($tweet['id_str'], $tweet); |
|
| 39 | + } |
|
| 40 | + continue; |
|
| 41 | + } |
|
| 42 | + |
|
| 43 | + $twitterRepository->insertTweet( |
|
| 44 | + $tweet['id_str'], |
|
| 45 | + (new DateTime($tweet['created_at']))->setTimezone($container['default_timezone']), |
|
| 46 | + $tweet |
|
| 47 | + ); |
|
| 48 | + } |
|
| 49 | + $idList = []; |
|
| 50 | + } |
|
| 51 | 51 | } |
| 52 | 52 | fclose($handle); |
@@ -8,76 +8,76 @@ |
||
| 8 | 8 | class MysqlGoodreadRepository implements GoodreadRepositoryInterface |
| 9 | 9 | { |
| 10 | 10 | |
| 11 | - /** @var ConnectionLocator */ |
|
| 12 | - protected $connections; |
|
| 11 | + /** @var ConnectionLocator */ |
|
| 12 | + protected $connections; |
|
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @param ConnectonLocator $connections |
|
| 16 | - */ |
|
| 17 | - public function __construct(ConnectionLocator $connections) |
|
| 18 | - { |
|
| 19 | - $this->connections = $connections; |
|
| 20 | - } |
|
| 14 | + /** |
|
| 15 | + * @param ConnectonLocator $connections |
|
| 16 | + */ |
|
| 17 | + public function __construct(ConnectionLocator $connections) |
|
| 18 | + { |
|
| 19 | + $this->connections = $connections; |
|
| 20 | + } |
|
| 21 | 21 | |
| 22 | - /** |
|
| 23 | - * @param integer $limit |
|
| 24 | - * @param integer $offset |
|
| 25 | - * |
|
| 26 | - * @return array|false |
|
| 27 | - */ |
|
| 28 | - public function getReviews($limit = null, $offset = 0) |
|
| 29 | - { |
|
| 30 | - $query = " |
|
| 22 | + /** |
|
| 23 | + * @param integer $limit |
|
| 24 | + * @param integer $offset |
|
| 25 | + * |
|
| 26 | + * @return array|false |
|
| 27 | + */ |
|
| 28 | + public function getReviews($limit = null, $offset = 0) |
|
| 29 | + { |
|
| 30 | + $query = " |
|
| 31 | 31 | SELECT `id`, `permalink`, `datetime` |
| 32 | 32 | FROM `jpemeric_stream`.`goodread` |
| 33 | 33 | ORDER BY `datetime` DESC"; |
| 34 | - if (!is_null($limit)) { |
|
| 35 | - $query .= " |
|
| 34 | + if (!is_null($limit)) { |
|
| 35 | + $query .= " |
|
| 36 | 36 | LIMIT {$offset}, {$limit}"; |
| 37 | - } |
|
| 37 | + } |
|
| 38 | 38 | |
| 39 | - return $this |
|
| 40 | - ->connections |
|
| 41 | - ->getRead() |
|
| 42 | - ->fetchAll($query); |
|
| 43 | - } |
|
| 39 | + return $this |
|
| 40 | + ->connections |
|
| 41 | + ->getRead() |
|
| 42 | + ->fetchAll($query); |
|
| 43 | + } |
|
| 44 | 44 | |
| 45 | - public function getReviewByPermalink($permalink) |
|
| 46 | - { |
|
| 47 | - $query = " |
|
| 45 | + public function getReviewByPermalink($permalink) |
|
| 46 | + { |
|
| 47 | + $query = " |
|
| 48 | 48 | SELECT * |
| 49 | 49 | FROM `jpemeric_stream`.`goodread` |
| 50 | 50 | WHERE `permalink` = :permalink |
| 51 | 51 | LIMIT 1"; |
| 52 | 52 | |
| 53 | - $bindings = [ |
|
| 54 | - 'permalink' => $permalink, |
|
| 55 | - ]; |
|
| 53 | + $bindings = [ |
|
| 54 | + 'permalink' => $permalink, |
|
| 55 | + ]; |
|
| 56 | 56 | |
| 57 | - return $this |
|
| 58 | - ->connections |
|
| 59 | - ->getRead() |
|
| 60 | - ->fetchOne($query, $bindings); |
|
| 61 | - } |
|
| 57 | + return $this |
|
| 58 | + ->connections |
|
| 59 | + ->getRead() |
|
| 60 | + ->fetchOne($query, $bindings); |
|
| 61 | + } |
|
| 62 | 62 | |
| 63 | - public function insertReview($permalink, $bookId, DateTime $datetime, array $metadata) |
|
| 64 | - { |
|
| 65 | - $query = " |
|
| 63 | + public function insertReview($permalink, $bookId, DateTime $datetime, array $metadata) |
|
| 64 | + { |
|
| 65 | + $query = " |
|
| 66 | 66 | INSERT INTO `jpemeric_stream`.`goodread` |
| 67 | 67 | (`permalink`, `book_id`, `datetime`, `metadata`) |
| 68 | 68 | VALUES |
| 69 | 69 | (:permalink, :book_id, :datetime, :metadata)"; |
| 70 | 70 | |
| 71 | - $bindings = [ |
|
| 72 | - 'permalink' => $permalink, |
|
| 73 | - 'book_id' => $bookId, |
|
| 74 | - 'datetime' => $datetime->format('Y-m-d H:i:s'), |
|
| 75 | - 'metadata' => json_encode($metadata), |
|
| 76 | - ]; |
|
| 71 | + $bindings = [ |
|
| 72 | + 'permalink' => $permalink, |
|
| 73 | + 'book_id' => $bookId, |
|
| 74 | + 'datetime' => $datetime->format('Y-m-d H:i:s'), |
|
| 75 | + 'metadata' => json_encode($metadata), |
|
| 76 | + ]; |
|
| 77 | 77 | |
| 78 | - return $this |
|
| 79 | - ->connections |
|
| 80 | - ->getWrite() |
|
| 81 | - ->perform($query, $bindings); |
|
| 82 | - } |
|
| 78 | + return $this |
|
| 79 | + ->connections |
|
| 80 | + ->getWrite() |
|
| 81 | + ->perform($query, $bindings); |
|
| 82 | + } |
|
| 83 | 83 | } |