Copie de Tumblr sur Shaarli
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

212 lines
9.2 KiB

  1. <?php
  2. function mylog($message) {
  3. echo $message.'<br>'."\n";
  4. file_put_contents(sprintf('%s/import.log', dirname(__FILE__)), $message."\n", FILE_APPEND);
  5. }
  6. mylog('start');
  7. // Pulling config from ini file
  8. $ini_file = sprintf('%s/tumblr.ini', dirname(__FILE__));
  9. if (!is_readable($ini_file)) {
  10. die('You must have a tumblr.ini file');
  11. }
  12. $config = parse_ini_file($ini_file);
  13. $tumblr_blog = $config['tumblr'];
  14. $api_key = $config['api_key'];
  15. $private = $config['private'];
  16. $shaarli_dir = sprintf('%s/%s', dirname(__FILE__), $config['shaarli_dir']);
  17. // Loading every needed class
  18. require 'vendor/autoload.php';
  19. // Shaarli library
  20. require_once sprintf('%sapplication/ApplicationUtils.php', $shaarli_dir);
  21. require_once sprintf('%sapplication/Cache.php', $shaarli_dir);
  22. require_once sprintf('%sapplication/CachedPage.php', $shaarli_dir);
  23. require_once sprintf('%sapplication/config/ConfigManager.php', $shaarli_dir);
  24. require_once sprintf('%sapplication/config/ConfigPlugin.php', $shaarli_dir);
  25. require_once sprintf('%sapplication/FeedBuilder.php', $shaarli_dir);
  26. require_once sprintf('%sapplication/FileUtils.php', $shaarli_dir);
  27. require_once sprintf('%sapplication/HttpUtils.php', $shaarli_dir);
  28. require_once sprintf('%sapplication/Languages.php', $shaarli_dir);
  29. require_once sprintf('%sapplication/LinkDB.php', $shaarli_dir);
  30. require_once sprintf('%sapplication/LinkFilter.php', $shaarli_dir);
  31. require_once sprintf('%sapplication/LinkUtils.php', $shaarli_dir);
  32. require_once sprintf('%sapplication/NetscapeBookmarkUtils.php', $shaarli_dir);
  33. require_once sprintf('%sapplication/PageBuilder.php', $shaarli_dir);
  34. require_once sprintf('%sapplication/TimeZone.php', $shaarli_dir);
  35. require_once sprintf('%sapplication/Url.php', $shaarli_dir);
  36. require_once sprintf('%sapplication/Utils.php', $shaarli_dir);
  37. require_once sprintf('%sapplication/PluginManager.php', $shaarli_dir);
  38. require_once sprintf('%sapplication/Router.php', $shaarli_dir);
  39. require_once sprintf('%sapplication/Updater.php', $shaarli_dir);
  40. // Initialize variables
  41. // DO NOT TOUCH
  42. $offset = 0;
  43. $per_page = 20;
  44. $API_post_url = sprintf('https://api.tumblr.com/v2/blog/%s/posts?api_key=%s&limit=1&offset=%u', $tumblr_blog, $api_key, 0);
  45. $converter = new \League\HTMLToMarkdown\HtmlConverter();
  46. $importCount = 0;
  47. $alreadyCount = 0;
  48. $conf = new ConfigManager();
  49. $pagecache = sprintf('%s/pagecache', $shaarli_dir);
  50. $linkDb = new LinkDB(
  51. sprintf('%s/data/datastore.php', $shaarli_dir),
  52. true,
  53. false,
  54. '',
  55. true
  56. );
  57. mylog('Backup '.$tumblr_blog);
  58. // First request to determine the number of tumblr post to save in Shaarli
  59. $json_response = file_get_contents($API_post_url);
  60. $json_response = json_decode($json_response, true);
  61. mylog('First request result : '.var_export($json_response, true));
  62. $total_count = $json_response['response']['total_posts'];
  63. mylog('Found '.$total_count.' entries on Tumblr');
  64. $loop = ceil($total_count / $per_page);
  65. mylog('We are going to do '.$loop.' loops');
  66. // Tumblr API can fetch 20 posts each time, so we need to paginate.
  67. for( $i = 0; $i < $loop; $i++) {
  68. mylog('--- LOOP '.($i+1).' ---');
  69. $API_post_url = sprintf('https://api.tumblr.com/v2/blog/%s/posts?api_key=%s&offset=%u', $tumblr_blog, $api_key, ($per_page*$i));
  70. $json_response = file_get_contents($API_post_url);
  71. $json_response = json_decode($json_response, true);
  72. if (!isset($json_response['response'])) {
  73. die(sprintf('wrong reponse %s', var_export($json_response,true)));
  74. }
  75. foreach ($json_response['response']['posts'] as $post) {
  76. $newLink = [];
  77. switch($post['type']) {
  78. case 'text':
  79. $newLink = [
  80. 'title' => $post['title'],
  81. 'url' => $post['post_url'],
  82. 'description' => $post['body'],
  83. 'private' => $private,
  84. 'linkdate' => $post['date'],
  85. 'tags' => array_merge($post['tags'], ['tumblr', 'tumblr_text'])
  86. ];
  87. break;
  88. case 'photo':
  89. $big_pic = $post['photos'][0]['alt_sizes'][0];
  90. $newLink = [
  91. 'title' => $post['summary'],
  92. 'url' => $post['post_url'],
  93. 'description' => sprintf('<img src="%s" alt="tumblr" /><p>%s</p>',$big_pic['url'], $post['caption']),
  94. 'private' => $private,
  95. 'linkdate' => $post['date'],
  96. 'tags' => array_merge($post['tags'], ['tumblr', 'tumblr_photo'])
  97. ];
  98. break;
  99. case 'quote':
  100. $url = empty($post['source_url']) ? $post['post_url'] : $post['source_url'];
  101. $newLink = [
  102. 'title' => $post['text'],
  103. 'url' => $url,
  104. 'description' => $post['source'],
  105. 'private' => $private,
  106. 'linkdate' => $post['date'],
  107. 'tags' => array_merge($post['tags'], ['tumblr', 'tumblr_quote'])
  108. ];
  109. break;
  110. case 'link':
  111. $newLink = [
  112. 'title' => $post['title'],
  113. 'url' => $post['url'],
  114. 'description' => $post['description'],
  115. 'private' => $private,
  116. 'linkdate' => $post['date'],
  117. 'tags' => array_merge($post['tags'], ['tumblr', 'tumblr_link'])
  118. ];
  119. break;
  120. case 'chat':
  121. $newLink = [
  122. 'title' => $post['title'],
  123. 'url' => $post['post_url'],
  124. 'description' => $post['body'],
  125. 'private' => $private,
  126. 'linkdate' => $post['date'],
  127. 'tags' => array_merge($post['tags'], ['tumblr', 'tumblr_chat'])
  128. ];
  129. break;
  130. case 'audio':
  131. $url = empty($post['source_url']) ? $post['post_url'] : $post['source_url'];
  132. $newLink = [
  133. 'title' => $post['source_title'],
  134. 'url' => $url,
  135. 'description' => $post['caption'].$post['player'][0]['embed_code'],
  136. 'private' => $private,
  137. 'linkdate' => $post['date'],
  138. 'tags' => array_merge($post['tags'], ['tumblr', 'tumblr_audio'])
  139. ];
  140. break;
  141. case 'video':
  142. $url = empty($post['source_url']) ? $post['post_url'] : $post['source_url'];
  143. $newLink = [
  144. 'title' => $post['source_title'],
  145. 'url' => $url,
  146. 'description' => $post['caption'].$post['player'][0]['embed_code'],
  147. 'private' => $private,
  148. 'linkdate' => $post['date'],
  149. 'tags' => array_merge($post['tags'], ['tumblr', 'tumblr_video'])
  150. ];
  151. break;
  152. case 'answer':
  153. $newLink = [
  154. 'title' => $post['question'],
  155. 'url' => $post['post_url'],
  156. 'description' => $post['answer'],
  157. 'private' => $private,
  158. 'linkdate' => $post['date'],
  159. 'tags' => array_merge($post['tags'], ['tumblr', 'tumblr_answer'])
  160. ];
  161. break;
  162. default:
  163. mylog('No URL found for '.var_export($post,true));
  164. }
  165. $existingLink = $linkDb->getLinkFromUrl($newLink['url']);
  166. // If the link already exists, we don't do anything (we don't want to break anything)
  167. if ($existingLink !== false) {
  168. mylog($newLink['url'].' already exists.');
  169. $alreadyCount++;
  170. continue;
  171. }
  172. // no HTML in title
  173. $newLink['title'] = strip_tags($newLink['title']);
  174. // description in markdown
  175. $newLink['description'] = $converter->convert($newLink['description']);
  176. // tags are string separated with a space
  177. $newLink['tags'] = implode(' ', $newLink['tags']);
  178. // Add a new link
  179. $newLinkDate = DateTime::createFromFormat('Y-m-d H:i:s T', $newLink['linkdate']);
  180. while (!empty($linkDb[$newLinkDate->format(LinkDB::LINK_DATE_FORMAT)])) {
  181. // Ensure the date/time is not already used
  182. // - this hack is necessary as the date/time acts as a primary key
  183. // - apply 1 second increments until an unused index is found
  184. // See https://github.com/shaarli/Shaarli/issues/351
  185. $newLinkDate->add(new DateInterval('PT1S'));
  186. }
  187. $linkDbDate = $newLinkDate->format(LinkDB::LINK_DATE_FORMAT);
  188. $newLink['linkdate'] = $linkDbDate;
  189. $linkDb[$linkDbDate] = $newLink;
  190. mylog($newLink['url']. ' added!');
  191. $importCount++;
  192. }
  193. }
  194. // Finished
  195. mylog('On '.$total_count.', added '.$importCount.', ignored '.$alreadyCount.' and '.($total_count-$importCount-$alreadyCount).' errors.');
  196. // Saving
  197. $linkDb->savedb($pagecache);