protected function initialize() { parent::initialize(); $parser = new Parser(); $this->logger->debug('load:' . $this->getFilePath()); $this->_data = $parser->parse(file_get_contents($this->getFilePath())); }
public function initialize() { parent::initialize(); $consumerKey = $this->container->getParameter('twitter_consumer_key'); $consumerSecret = $this->container->getParameter('twitter_consumer_secret'); $oauthToken = $this->container->getParameter('twitter_oauth_token'); $oauthTokenSecret = $this->container->getParameter('twitter_oauth_token_secret'); $this->client = new \TwitterOAuth($consumerKey, $consumerSecret, $oauthToken, $oauthTokenSecret); }
/** * DBから指定された$keyテーブルのMapを取得します。 * なお、一度ロードしたDBのデータはキャッシュします。 * * @param string $tableName テーブル名 */ public function getMap($tableName) { if (isset($this->_cache[$tableName])) { $map = $this->_cache[$tableName]; } else { $map = parent::getRepository($tableName)->getMap(); } return $map; }
/** * クロール対象のWoeidを取得します * * @return array */ public function getCrawl() { $rWoeid = parent::getRepository('woeid'); try { $woeids = $rWoeid->getCrawl(); } catch (\Exception $e) { $this->logger->warn('- クロール対象のWoeid取得 失敗'); throw $e; } return $woeids; }
/** * 実行 */ public function run($woeid, $weight = 1) { $this->logger->info('--- START Crawler Trend ---'); // Trend取得 $this->logger->info('- START get Trend by twitter'); try { $trends = $this->container->get('app_service.component_twitter_rest_api_accessor')->getTrendsPlace($woeid); } catch (\Exception $e) { $this->logger->warn('- Failed get Trend by twitter'); throw $e; } $this->logger->info('- END get Trend by twitter'); // 登録 $this->logger->info('- START Regist Trend Data'); // Repository取得 $em = parent::getManager(); $rTrend = parent::getRepository('trend'); try { $em->beginTransaction(); $now = new \DateTime(); foreach ($trends as $_trend) { $trendEntity = $rTrend->getUnityTrend($_trend['name']); // 未登録 if (is_null($trendEntity)) { // 新しいレコード生成 $this->logger->info('新規レコード登録'); $trendEntity = new Trend(); $trendEntity->setContent($_trend['name']); $trendEntity->setWoeid($woeid); if (preg_match('/^#/', $trendEntity->getContent())) { $trendEntity->setType(Trend::TYPE_HASHTAG); } else { $trendEntity->setType(Trend::TYPE_KEYWORD); } } $trendEntity->incrementWeight($weight); $trendEntity->setVolume($_trend['tweet_volume']); $trendEntity->setFound($now); // 登録 $this->logger->info('登録(' . $trendEntity->getContent() . ')'); $em->save($trendEntity); $em->manyFlush(); } $em->commit(); } catch (\Exception $e) { $this->logger->warn('- FAILED Regist Trend Data'); $em->rollback(); throw $e; } $this->logger->info('- END Regist Trend Data'); $this->logger->info('--- END Crawler Trend ---'); }
/** * 指定された期間、WoeIdのトレンドを取得します * * @param int $woeid * @return array */ public function getByPeriod($from = NULL, $to = NULL) { // DBからデータ取得 $rTrend = parent::getRepository('trend'); try { $trends = $rTrend->findAll(); $sum = array(); foreach ($trends as $_trend) { $sum[$_trend->getWoeid()][] = $_trend; } } catch (\Exception $e) { $this->logger->warn('- トレンド取得 失敗'); throw $e; } return $sum; }
/** * 設定を読み込む * * @param string $mailType * @param string $to * @param string $subject * @return array * @throws \LibraryBundle\Exception\DataNotFoundException */ private function _loadSetting($mailType, $to, $subject) { $setting = array('from' => null, 'fromLabel' => null, 'to' => $to, 'subject' => $subject, 'template' => null); // メッセージ設定取得 $config = $this->container->get('lib.config_mail')->getData($mailType); // メーラー設定取得 if (!$this->container->hasParameter('mailer')) { throw new \LibraryBundle\Exception\DataNotFoundException('parameters.mailを設定してください'); } $mailer = $this->container->getParameter('mailer'); // 送信元アカウント情報 if (!isset($mailer[$config['account']])) { throw new \LibraryBundle\Exception\DataNotFoundException($config['account'] . 'を設定してください'); } $account = $mailer[$config['account']]; // 送信者 $setting['from'] = $account['address']; // 返信先 if (isset($account['reply'])) { $setting['reply'] = $account['reply']; } $setting['fromLabel'] = s_empty($account, 'label') ? $account['label'] : null; // 宛先 // force_sendがtrueで設定されていない // 開発環境 or 開発サーバーだったら誤送信防止のためのメールアドレスへ飛ばす if ((!isset($config['force_send']) || $config['force_send'] === false) && (parent::isDevelopment() || parent::isDevelopmentServer())) { if (!isset($mailer['development_to'])) { throw new \LibraryBundle\Exception\DataNotFoundException('parameters.mailer.development_toを設定してください'); } $setting['to'] = $mailer['development_to']; } // 件名 // 件名がどこにもない場合Exception if (is_null($subject) && !s_empty($config, 'subject')) { throw new \LibraryBundle\Exception\DataNotFoundException('subjectを設定してください'); } elseif (is_null($subject) && s_empty($config, 'subject')) { $setting['subject'] = $config['subject']; } // 本文テンプレート $setting['template'] = $config['template']; return $setting; }
/** * WoeidをTwitterから取得して登録します */ public function loadWoeid() { // Woeid取得 try { $woeids = $this->container->get('app_service.component_twitter_rest_api_accessor')->getTrendsAvailable(); } catch (\Exception $e) { $this->logger->warn('- Failed get Woeid by twitter'); throw $e; } // 登録 $em = parent::getManager(); $rWoeid = parent::getRepository('woeid'); try { $em->beginTransaction(); foreach ($woeids as $_woeid) { $woeidEntity = $rWoeid->findOneByWoeid($_woeid['woeid']); if (is_null($woeidEntity)) { $woeidEntity = new Woeid(); $woeidEntity->setWoeid($_woeid['woeid']); $woeidEntity->setName($_woeid['name']); } else { // 名前が変わってないかチェック if ($woeidEntity->getName() !== $_woeid['name']) { $woeidEntity->setName($_woeid['name']); } } // 登録 $this->logger->info(sprintf('登録(%s)', $woeidEntity->getWoeid())); $em->save($woeidEntity); $em->manyFlush(); } $em->commit(); } catch (\Exception $e) { $this->logger->warn('- FAILED Regist Woeid Data'); $em->rollback(); throw $e; } }