PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/classes/seeds/TumblrSeed.php

https://gitlab.com/x33n/platform
PHP | 161 lines | 119 code | 10 blank | 32 comment | 19 complexity | c0c38dc78aa41fb9999858660efaea42 MD5 | raw file
  1. <?php
  2. /**
  3. * Tumblr library wrapper and public feed fetcher
  4. *
  5. * @package platform.org.cashmusic
  6. * @author CASH Music
  7. * @link http://cashmusic.org/
  8. *
  9. * Copyright (c) 2013, CASH Music
  10. * Licensed under the GNU Lesser General Public License version 3.
  11. * See http://www.gnu.org/licenses/lgpl-3.0.html
  12. *
  13. *
  14. * This file is generously sponsored by sponsored by Bangsplat - blog.bangsplatpresents.com
  15. *
  16. **/
  17. class TumblrSeed extends SeedBase {
  18. protected $twitter;
  19. public function __construct($user_id=false,$connection_id=false) {
  20. $this->settings_type = 'com.tumblr';
  21. $this->user_id = $user_id;
  22. $this->connection_id = $connection_id;
  23. $this->primeCache();
  24. if ($user_id && $connection_id) {
  25. $this->connectDB();
  26. if ($this->getCASHConnection()) {
  27. // fire up an instance of the lib
  28. } else {
  29. // error out — potentially to special error message page.
  30. // probably not doing any kind of authorized tumblr stuff at first.
  31. }
  32. }
  33. }
  34. public function getTumblrFeed($tumblr_domain,$start_at=0,$tagged=false,$post_types=false) {
  35. if ($tumblr_domain) {
  36. $default_post_types = array(
  37. 'regular' => true,
  38. 'link' => true,
  39. 'quote' => false,
  40. 'photo' => true,
  41. 'conversation' => false,
  42. 'video' => true,
  43. 'audio' => true,
  44. 'answer' => false
  45. );
  46. if (is_array($post_types)) {
  47. $final_post_types = array_merge($default_post_types, $post_types);
  48. } else {
  49. $final_post_types = $default_post_types;
  50. }
  51. $tumblr_domain = str_replace(array('http://','/'),'',$tumblr_domain);
  52. $tumblr_url = 'http://' . $tumblr_domain . '/api/read/json?start=' . $start_at . '&num=30';
  53. if ($tagged) {
  54. $tumblr_url .= '&tagged=' . urlencode($tagged);
  55. }
  56. $feed_data = $this->getCachedURL('com.tumblr', 'domain_' . str_replace('.','',$tumblr_domain) . $start_at, $tumblr_url, 'raw', false);
  57. if ($feed_data) {
  58. // tumblr's funny, JSONP only, so we cache its return and strip of some extra
  59. $feed_data = str_replace('var tumblr_api_read = ','',$feed_data); // strip off the variable declaration
  60. $feed_data = substr($feed_data,0,strlen($feed_data)-2); // and the trailing semicolon+newline
  61. // decode the trimmed content, then return just the posts
  62. $feed_data = json_decode($feed_data);
  63. $feed_data = $feed_data->posts;
  64. // make a dummy array to save final posts
  65. $final_feed_data = array();
  66. // loop through all the posts, filter by type
  67. foreach ($feed_data as $post) {
  68. if ($final_post_types[$post->type]) {
  69. $final_feed_data[] = $post;
  70. }
  71. }
  72. $feed_data = $final_feed_data;
  73. }
  74. return $feed_data;
  75. } else {
  76. return false;
  77. }
  78. }
  79. public function prepMarkup($post,$summarize=false) {
  80. $innermarkup = false;
  81. switch ($post->type) {
  82. case 'regular':
  83. if ($summarize) {
  84. $textbody = '';
  85. $textbodyarray = explode('.',strip_tags($post->{'regular-body'}));
  86. if (count($textbodyarray) > 3) {
  87. $textbodyarray = array_slice($textbodyarray,0,3);
  88. }
  89. $textbody = implode('.',$textbodyarray) . '...';
  90. } else {
  91. $textbody = $post->{'regular-body'};
  92. }
  93. $innermarkup = "<div class=\"cashmusic_social cashmusic_tumblr\">"
  94. . '<h2><a href="' . $post->{'url-with-slug'} . '" target="_blank">' . $post->{$post->type . '-title'} . '</a></h2><div>' . $textbody . '</div><div class="cashmusic_social_date"><a href="' . $post->{'url-with-slug'} . '" target="_blank">' . CASHSystem::formatTimeAgo($post->{'unix-timestamp'}) . ' / tumblr</a> </div>'
  95. . '<div class="cashmusic_clearall">.</div></div>';
  96. break;
  97. case 'photo':
  98. $innermarkup = "<div class=\"cashmusic_social cashmusic_tumblr\">"
  99. . '<div class="cashmusic_social_photo"><img src="' . $post->{'photo-url-500'} . '" width="100%" alt="" /><br />' . $post->{'photo-caption'} . '</div><div class="cashmusic_social_date"><a href="' . $post->{'url-with-slug'} . '" target="_blank">' . CASHSystem::formatTimeAgo($post->{'unix-timestamp'}) . ' / tumblr</a> </div>'
  100. . '<div class="cashmusic_clearall">.</div></div>';
  101. break;
  102. case 'video':
  103. $innermarkup = "<div class=\"cashmusic_social cashmusic_tumblr\">"
  104. . '<div class="cashmusic_social_video"><div class="cashmusic_social_video_container">' . $post->{'video-player'} . '</div><br />' . $post->{'video-caption'} . '</div><div class="cashmusic_social_date"><a href="' . $post->{'url-with-slug'} . '" target="_blank">' . CASHSystem::formatTimeAgo($post->{'unix-timestamp'}) . ' / tumblr</a> </div>'
  105. . '<div class="cashmusic_clearall">.</div></div>';
  106. break;
  107. case 'audio':
  108. $innermarkup = "<div class=\"cashmusic_social cashmusic_tumblr\">"
  109. . '<div class="cashmusic_social_audio"><a><div class="cashmusic_social_audio_container">' . $post->{'audio-player'} . '</div><br />' . $post->{'audio-caption'} . '</div><div class="cashmusic_social_date"><a href="' . $post->{'url-with-slug'} . '" target="_blank">' . CASHSystem::formatTimeAgo($post->{'unix-timestamp'}) . ' / tumblr</a> </div>'
  110. . '<div class="cashmusic_clearall">.</div></div>';
  111. break;
  112. case 'link':
  113. $description = '';
  114. if ($summarize && !empty($post->{'link-description'})) {
  115. $descriptionarray = explode('.',strip_tags($post->{'link-description'}));
  116. if (count($descriptionarray) > 3) {
  117. $descriptionarray = array_slice($descriptionarray,0,3);
  118. }
  119. $description = implode('.',$descriptionarray) . '...';
  120. } else {
  121. $description = $post->{'link-description'};
  122. }
  123. $innermarkup = "<div class=\"cashmusic_social cashmusic_tumblr\">"
  124. . '<div class="cashmusic_social_link"><a href="' . $post->{'link-url'} . '">' . $post->{'link-text'} . '</a><div>' . $description . '</div></div><div class="cashmusic_social_date"><a href="' . $post->{'url-with-slug'} . '" target="_blank">' . CASHSystem::formatTimeAgo($post->{'unix-timestamp'}) . ' / tumblr</a> </div>'
  125. . '<div class="cashmusic_clearall">.</div></div>';
  126. break;
  127. case 'answer':
  128. $innermarkup = "<div class=\"cashmusic_social cashmusic_tumblr\">"
  129. . '<div class="cashmusic_social_answer"><span class="cashmusic_social_answer_q">' . $post->question . '</span><span class="cashmusic_social_answer_a">' . $post->answer . '</span></div><div class="cashmusic_social_date"><a href="' . $post->{'url-with-slug'} . '" target="_blank">' . CASHSystem::formatTimeAgo($post->{'unix-timestamp'}) . ' / tumblr</a> </div>'
  130. . '<div class="cashmusic_clearall">.</div></div>';
  131. break;
  132. case 'quote':
  133. $innermarkup = "<div class=\"cashmusic_social cashmusic_tumblr\">"
  134. . '<div class="cashmusic_social_quote"><span class="cashmusic_social_quote">' . $post->{'quote-text'} . '</span><span class="cashmusic_social_quote_src">' . $post->{'quote-source'} . '</span></div><div class="cashmusic_social_date"><a href="' . $post->{'url-with-slug'} . '" target="_blank">' . CASHSystem::formatTimeAgo($post->{'unix-timestamp'}) . ' / tumblr</a> </div>'
  135. . '<div class="cashmusic_clearall">.</div></div>';
  136. break;
  137. }
  138. return $innermarkup;
  139. /*
  140. The CSS to go along with the video container:
  141. Thanks to http://www.alistapart.com/articles/creating-intrinsic-ratios-for-video/
  142. echo '<style type="text/css">';
  143. echo '.cashmusic_video_container {position:relative;padding-bottom:56.25%;padding-top:30px;height:0;overflow:hidden;}';
  144. echo '.cashmusic_video_container iframe, .cashmusic_video_container object, .cashmusic_video_container embed {position:absolute;top:0;left:0;width:100%;height:100%;}';
  145. echo '</style>';
  146. */
  147. }
  148. } // END class
  149. ?>