/src/Zeega/IngestionBundle/Parser/Tumblr/ParserTumblr.php

https://github.com/Zeega/Zeega · PHP · 123 lines · 112 code · 6 blank · 5 comment · 7 complexity · 5ccbf6375e07ed29ccca884790c6eb63 MD5 · raw file

  1. <?php
  2. namespace Zeega\IngestionBundle\Parser\Tumblr;
  3. use Zeega\IngestionBundle\Parser\Base\ParserAbstract;
  4. use Zeega\DataBundle\Document\Item;
  5. use \DateTime;
  6. class ParserTumblr extends ParserAbstract
  7. {
  8. public function load($url, $parameters = null)
  9. {
  10. $regexMatches = $parameters["regex_matches"];
  11. $blogHostname = $regexMatches[1] . ".tumblr.com";
  12. $postId = $regexMatches[2];
  13. # API key should be moved into a conf file
  14. $apiKey = "F7zV5AJ9F5I2oKPydkeDcIRydMMm5iiyX3By8Mir6sjMZFpbFv";
  15. $apiCallUrl = "http://api.tumblr.com/v2/blog/" . $blogHostname . "/posts?api_key=" . $apiKey . "&id=" . $postId;
  16. $results_str = file_get_contents(($apiCallUrl));
  17. $results_json = json_decode($results_str);
  18. #print(var_dump($results_json));
  19. if($results_json->meta->status == 200){
  20. $item = new Item();
  21. $currentPost = $results_json -> response -> posts[0];
  22. $item->setMediaCreatorUsername($results_json -> response -> blog -> name);
  23. $item->setMediaCreatorRealname($results_json -> response -> blog -> name);
  24. switch($currentPost -> type){
  25. case "photo":
  26. $photoArray = $currentPost -> photos;
  27. $item->setArchive('Tumblr');
  28. $item->setAttributionUri($currentPost->post_url);
  29. $item->setChildItemsCount(count($photoArray)-1);
  30. $item->setMediaDateCreated(new DateTime($currentPost -> date));
  31. $item->setTags($currentPost -> tags);
  32. $item->setTitle(ucwords(str_replace ( '-',' ', $currentPost->slug)));
  33. if(count($photoArray) == 1){
  34. $altSizes = $currentPost -> photos[0] -> alt_sizes;
  35. $img75px = end($altSizes);
  36. $item->setMediaType('Image');
  37. $item->setLayerType('Image');
  38. $item->setThumbnailUrl($img75px -> url);
  39. $item->setUri($currentPost -> photos[0] -> original_size -> url);
  40. $item->setMediaDateCreated(new DateTime($currentPost -> date));
  41. $item->setChildItemsCount(0);
  42. $item->setTags($currentPost -> tags);
  43. }else{
  44. $item->setMediaType('Collection');
  45. $item->setLayerType('Collection');
  46. for($pi=0; $pi < count($photoArray); $pi++){
  47. $photoItem = $photoArray[$pi];
  48. #print(var_dump($photoItem));
  49. #print("***");
  50. #print($photoItem -> original_size -> url);
  51. $altSizes = $photoItem -> alt_sizes;
  52. $img75px = end($altSizes);
  53. $childItem = new Item();
  54. $childItem->setMediaType('Image');
  55. $childItem->setLayerType('Image');
  56. $childItem->setThumbnailUrl($img75px -> url);
  57. $childItem->setUri($photoItem -> original_size -> url);
  58. if(strlen($photoItem -> caption)>0) $childItem->setTitle(strip_tags($currentPost -> caption));
  59. else $childItem->setTitle(ucwords(str_replace ( '-',' ', $currentPost->slug)));
  60. $childItem->setMediaDateCreated(new DateTime($currentPost -> date));
  61. $childItem->setAttributionUri($currentPost->post_url);
  62. $childItem->setChildItemsCount(0);
  63. $childItem->setMediaCreatorUsername($results_json -> response -> blog -> name);
  64. $childItem->setMediaCreatorRealname($results_json -> response -> blog -> name);
  65. $childItem->setTags($currentPost -> tags);
  66. $item->addChildItem($childItem);
  67. }
  68. }
  69. break;
  70. case "video": # not finished
  71. die("Video postings not yet supported.");
  72. $item->setUri($currentPost -> audio_url);
  73. $item->setMediaType('Video');
  74. $item->setLayerType('Video');
  75. $item->setTitle(ucwords(str_replace ( '-',' ', $currentPost->slug)));
  76. $item->setChildItemsCount(0);
  77. $item->setMediaDateCreated(new DateTime($currentPost -> date));
  78. $item->setArchive('Tumblr');
  79. break;
  80. case "audio": # not finished
  81. die("Audio postings not yet supported.");
  82. $item->setUri($currentPost -> audio_url);
  83. $item->setMediaType('Audio');
  84. $item->setLayerType('Audio');
  85. $item->setTitle(ucwords(str_replace ( '-',' ', $currentPost->slug)));
  86. $item->setChildItemsCount(0);
  87. $item->setMediaDateCreated(new DateTime($currentPost -> date));
  88. $item->setArchive('Tumblr');
  89. break;
  90. case "text": # not finished
  91. die("Text postings not yet supported.");
  92. $item->setUri($currentPost -> body);
  93. $item->setMediaType('tumblr_text');
  94. $item->setLayerType('Text');
  95. $item->setTitle(ucwords(str_replace ( '-',' ', $currentPost->slug)));
  96. $item->setChildItemsCount(0);
  97. $item->setMediaDateCreated(new DateTime($currentPost -> date));
  98. $item->setArchive('Tumblr');
  99. $item->setTags($currentPost -> tags);
  100. break;
  101. case "quote": # not finished
  102. die("Quote postings not yet supported.");
  103. $item->setUri($currentPost -> photos[0] -> original_size -> url);
  104. $item->setMediaType('tumblr_quote');
  105. $item->setLayerType('Text');
  106. $item->setTitle(ucwords(str_replace ( '-',' ', $currentPost->slug)));
  107. $item->setChildItemsCount(0);
  108. $item->setMediaDateCreated(new DateTime($currentPost -> date));
  109. $item->setArchive('Tumblr');
  110. break;
  111. }
  112. }else{
  113. die("Failed to retrieve url " . $apiCallUrl);
  114. };
  115. return $this->returnResponse($item, true, false);
  116. }
  117. }