PageRenderTime 57ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/default/models/Cron.php

https://gitlab.com/grayhamster/open-social-media-monitoring
PHP | 217 lines | 182 code | 16 blank | 19 comment | 26 complexity | f95f50e509f627cf07c7ad0982b48fc8 MD5 | raw file
  1. <?php
  2. class Default_Model_Cron
  3. {
  4. /**
  5. * Grabs Twitter and Facebook entries
  6. *
  7. * @param $bootstrap Bootstrap
  8. */
  9. public function run($bootstrap)
  10. {
  11. $active_queries = array();
  12. $campaign = new Default_Model_DbTable_Campaigns();
  13. $keyword = new Default_Model_DbTable_Keyword();
  14. $search = new Default_Model_DbTable_Search();
  15. $influencer = new Default_Model_DbTable_Influencers();
  16. $settings = $bootstrap->getOption('settings');
  17. foreach ($campaign->getCronData() as $obj) {
  18. $active_queries[] = $obj['query_id'];
  19. /**
  20. * Twitter
  21. */
  22. $last_tweet_id = 0;
  23. $base_url = 'http://search.twitter.com/search.json';
  24. $parameters = array(
  25. 'q' => $obj['query_q'],
  26. 'geocode' => $obj['query_geocode'],
  27. 'rpp' => 100,
  28. 'result_type' => 'recent',
  29. 'since_id' => $obj['query_last_twitter'],
  30. 'lang' => $obj['query_lang']
  31. );
  32. $response = $this->_get_file_contents($base_url, $parameters, true);
  33. while(is_object($response) && isset($response->results) && is_array($response->results) && list(,$entry) = each($response->results)) {
  34. if(!$last_tweet_id){
  35. $last_tweet_id = $entry->id_str;
  36. $data = array('query_last_twitter' => $last_tweet_id);
  37. $keyword->update($data, array('query_id = ?' => $obj['query_id']));
  38. }
  39. $data = array(
  40. 'query_id' => $obj['query_id'],
  41. 'search_outer_id' => $entry->id_str,
  42. 'search_source' => 'twitter',
  43. 'search_published' => strtotime($entry->created_at),
  44. 'search_content' => addslashes($entry->text),
  45. 'search_author_name' => addslashes($entry->from_user),
  46. 'search_author_image' => $entry->profile_image_url
  47. );
  48. $search->insert($data);
  49. $data = array(
  50. 'query_id' => $obj['query_id'],
  51. 'search_author_name' => addslashes($entry->from_user),
  52. 'search_source' => 'twitter',
  53. 'cnt' => 1
  54. );
  55. $influencer->insertOnDuplicate($data, 'UPDATE cnt = cnt + 1');
  56. }
  57. /**
  58. * Facebook
  59. */
  60. $last_facebook_post_time = 0;
  61. $base_url = 'https://graph.facebook.com/search';
  62. $parameters = array(
  63. 'q' => $obj['query_q'],
  64. 'type' => 'post',
  65. 'limit' => 100,
  66. 'since' => $obj['query_last_facebook']
  67. );
  68. $response = $this->_get_file_contents($base_url, $parameters, true);
  69. $alchemy_base_url = 'http://access.alchemyapi.com/calls/text/TextGetLanguage';
  70. $lang = array();
  71. while (is_object($response) && isset($response->data) && is_array($response->data) && list(,$entry) = each($response->data)) {
  72. $entry->name = isset($entry->name) ? $entry->name : '';
  73. if (isset($entry->message) && $entry->message) {
  74. $text = $entry->message;
  75. } elseif (isset($entry->story) && $entry->story) {
  76. $text = $entry->story;
  77. } else {
  78. $text = '';
  79. }
  80. if($obj['query_lang'] && $settings['alchemy_api_key']){
  81. $parameters = array(
  82. 'apikey' => $settings['alchemy_api_key'],
  83. 'outputMode' => 'json',
  84. 'text' => $text
  85. );
  86. $lang = $this->_get_file_contents($alchemy_base_url, $parameters, true, true);
  87. }
  88. if (!$obj['query_lang'] || !$settings['alchemy_api_key'] || ($obj['query_lang'] && $obj['query_lang'] == $lang['iso-639-1'])) {
  89. $created_time = strtotime($entry->created_time);
  90. if (!$last_facebook_post_time) {
  91. $last_facebook_post_time = date('n/j/Y H:i:s', $created_time);
  92. $data = array('query_last_facebook' => $last_facebook_post_time);
  93. $keyword->update($data, array('query_id = ?' => $obj['query_id']));
  94. }
  95. $data = array(
  96. 'query_id' => $obj['query_id'],
  97. 'search_outer_id' => $entry->id,
  98. 'search_source' => 'facebook',
  99. 'search_published' => $created_time,
  100. 'search_content' => addslashes($text),
  101. 'search_author_name' => addslashes($entry->from->name),
  102. 'search_title' => addslashes($entry->name)
  103. );
  104. $search->insert($data);
  105. $data = array(
  106. 'query_id' => $obj['query_id'],
  107. 'search_author_name' => addslashes($entry->from->name),
  108. 'search_source' => 'facebook',
  109. 'cnt' => 1
  110. );
  111. $influencer->insertOnDuplicate($data, 'UPDATE cnt = cnt + 1');
  112. }
  113. }
  114. }
  115. // Archiving expired entries.
  116. $expired_entries = $search->fetchAll(array(
  117. 'query_id IN (?)' => implode(', ', $active_queries),
  118. 'search_published < ?' => time() - $settings['keep_history'] * 24 * 3600
  119. ));
  120. if (count($expired_entries)) {
  121. $search_index = new Default_Model_DbTable_SearchIndex();
  122. echo 'Archiving ' . count($expired_entries) . ' entries.'."\n";
  123. foreach ($expired_entries as $a_obj) {
  124. echo '.';
  125. $date = mktime(0, 0, 0, date('n', $a_obj['search_published']), date('j', $a_obj['search_published']), date('Y', $a_obj['search_published']));
  126. $data = array(
  127. 'query_id' => $a_obj['query_id'],
  128. 'index_date' => $date,
  129. 'index_source' => $a_obj['search_source'],
  130. 'index_count' => 1
  131. );
  132. $search_index->insertOnDuplicate($data, 'UPDATE index_count = index_count+1');
  133. $search->delete(array('search_id' => $a_obj['search_id']));
  134. }
  135. }
  136. $search->optimize();
  137. echo "\n";
  138. if(!(int)date('G')){ // If hour == 0, sending email digest
  139. $mail = new Zend_Mail();
  140. $mail->addTo($bootstrap->getOption('authentication.email'))
  141. ->setFrom($settings['default_from'])
  142. ->setSubject('Daily overview of your projects');
  143. $email_html = '<html><body>logo here<hr/><br/>Project(s) in this email:<br/><ul>';
  144. foreach ($campaign->getCampaigns() as $cmpgn) {
  145. $email_html .= '<li><a href="#' . $cmpgn['project_id'] . '">' . $cmpgn['project_name'] . '</a></li>';
  146. }
  147. $email_html .= '</ul><br/><br/>';
  148. foreach ($campaign->getCampaigns() as $cmpgn) {
  149. $email_html .= '<a name="' . $cmpgn['project_id'] . '"></a>
  150. Project: ' . $cmpgn['project_name'] . '<hr/>' .
  151. $this->breakdown_block($cmpgn['project_id'], 'twitter') .
  152. $this->breakdown_block($cmpgn['project_id'], 'facebook') . '<br/>';
  153. }
  154. $email_html .= '</body></html>';
  155. $mail->setBodyHtml($email_html)
  156. ->setBodyText(strip_tags(preg_replace('#<br\s*/?>|<hr\s*/?>|</ul>|</li>|</h\d>#i', "\n", $email_html)))
  157. ->send();
  158. }
  159. }
  160. private function breakdown_block($campaign_id, $source)
  161. {
  162. $search = new Default_Model_DbTable_Search();
  163. $res = $search->getDigestData($campaign_id, $source);
  164. $ret = '<h2>' . ucfirst($source) . ' Breakdown</h2>
  165. <br/>Total mentions in the past 24 hours: ' . count($res) . '<br/><br/>' .
  166. (count($res) ? 'Latest entries:' : '') .
  167. '<ul>';
  168. $cnt = 0;
  169. while ($cnt++ < 10 && list(,$obj) = each($res)) {
  170. $ret .= '<li>' . $obj['search_author_name'] . ': ' . substr(strip_tags($obj['search_content']), 0, 200) . '</li>';
  171. }
  172. $ret .= '</ul>';
  173. return $ret;
  174. }
  175. private function _get_file_contents($url, $parameters, $json_decode = false, $json_assoc = false)
  176. {
  177. $url = $this->_appendQueryParams($url, $parameters);
  178. $response = file_get_contents($url);
  179. if($json_decode){
  180. $response = @json_decode($response, $json_assoc);
  181. }
  182. return $response;
  183. }
  184. /**
  185. * Append the array of parameters to the given URL string
  186. *
  187. * @param string $url
  188. * @param array $params
  189. * @return string
  190. */
  191. private function _appendQueryParams($url, array $params)
  192. {
  193. foreach ($params as $k => $v) {
  194. if(trim($v)){
  195. $url .= strpos($url, '?') === false ? '?' : '&';
  196. $url .= sprintf("%s=%s", $k, urlencode(trim($v)));
  197. }
  198. }
  199. return $url;
  200. }
  201. }