/new_demo.php

https://bitbucket.org/michaelku/tumblr-note-stats-v2 · PHP · 260 lines · 174 code · 45 blank · 41 comment · 18 complexity · ced2b0b7e71a009aea6268afff3c1f82 MD5 · raw file

  1. <?php
  2. /**********************************/
  3. /* Step 1: Get all data from file */
  4. /**********************************/
  5. //START TIMER #1
  6. $time = microtime();
  7. $time = explode(' ', $time);
  8. $time = $time[1] + $time[0];
  9. $start = $time;
  10. $notes = file_get_contents('notes.txt', true);
  11. $notes = trim($notes);
  12. //Arrays
  13. $poster_name_array = array();
  14. $poster_link_array = array();
  15. $source_name_array = array();
  16. $source_link_array = array();
  17. $action_array = array();
  18. $permalink_array = array();
  19. $extra_array = array();
  20. //Split by note (<li>)
  21. $by_note = explode("</li>", $notes, -1);
  22. foreach($by_note as $singular_note){
  23. $note = explode('class="action"', $singular_note);
  24. $inner_data = $note[1];
  25. $data_split = explode("</span>", $inner_data);
  26. $post_data = $data_split[0];
  27. $post_data = explode(">", $post_data);
  28. $extra_data = $data_split[1];
  29. $extra_data = explode('<div class="clear"></div>', $extra_data);
  30. $temp_permalink = trim($post_data[0]);
  31. $temp_poster_link = trim($post_data[1]);
  32. $temp_source_link = trim($post_data[3]);
  33. $temp_action = trim($post_data[3]);
  34. $temp_extra = trim($extra_data[1]);
  35. $temp_poster = trim($post_data[2]);
  36. $poster = explode('<', $temp_poster);
  37. $poster = trim($poster[0]);
  38. $has_permalink = strlen($temp_permalink);
  39. //Action
  40. $action = explode(" ", $temp_action);
  41. $action = trim($action[0]);
  42. array_push($action_array, $action);
  43. $action_split = explode(" ", $action);
  44. $action_split = $action_split[0];
  45. if($action_split == "reblogged"){
  46. $permalink = explode('"', $temp_permalink);
  47. $permalink = trim($permalink[1]);
  48. //Source + Source Link
  49. $temp_source = $post_data[4];
  50. $source = explode('<', $temp_source);
  51. $source = trim($source[0]);
  52. $source_link = explode('href="', $temp_source_link);
  53. $source_link = explode('"', $source_link[1]);
  54. $source_link = trim($source_link[0]);
  55. }
  56. else{
  57. $permalink = "none";
  58. $source = "none";
  59. $source_link = "none";
  60. }
  61. array_push($permalink_array, $permalink);
  62. array_push($source_name_array, $source);
  63. array_push($source_link_array, $source_link);
  64. //Poster + Poster Link
  65. $poster_link = explode('href="', $temp_poster_link);
  66. $poster_link = explode('"', $poster_link[1]);
  67. $poster_link = trim($poster_link[0]);
  68. array_push($poster_name_array, $poster);
  69. array_push($poster_link_array, $poster_link);
  70. //Extra
  71. $extra_count = strlen($temp_extra);
  72. if($extra_count > 0){
  73. $extra = explode('"View post">', $temp_extra);
  74. $extra = explode('</a>', $extra[1]);
  75. $extra = trim($extra[0]);
  76. }
  77. else{
  78. $extra = "none";
  79. }
  80. array_push($extra_array, $extra);
  81. }
  82. //STOP TIMER #1
  83. $time = microtime();
  84. $time = explode(' ', $time);
  85. $time = $time[1] + $time[0];
  86. $finish = $time;
  87. $total_time = round(($finish - $start), 4);
  88. echo 'Step #1 took '.$total_time.' seconds. <br />';
  89. /**************************/
  90. /* Step 2: Reverse Arrays */
  91. /**************************/
  92. //START TIMER #2
  93. $time = microtime();
  94. $time = explode(' ', $time);
  95. $time = $time[1] + $time[0];
  96. $start = $time;
  97. $poster_name_array = array_reverse($poster_name_array);
  98. $poster_link_array = array_reverse($poster_link_array);
  99. $source_name_array = array_reverse($source_name_array);
  100. $source_link_array = array_reverse($source_link_array);
  101. $action_array = array_reverse($action_array);
  102. $permalink_array = array_reverse($permalink_array);
  103. $extra_array = array_reverse($extra_array);
  104. $notes_count = count($poster_name_array);
  105. //echo $notes_count;
  106. for($i = 0; $i < $notes_count; $i++){
  107. //Print poster name and link
  108. print "
  109. <a href = '$poster_link_array[$i]'>
  110. $poster_name_array[$i]
  111. </a>
  112. ";
  113. //Determine and print action and source name & link if applicable
  114. if($action_array[$i] == "posted"){
  115. print "posted this";
  116. }
  117. elseif($action_array[$i] == "likes"){
  118. print "liked this";
  119. }
  120. elseif($action_array[$i] == "reblogged"){
  121. print "
  122. <a href = '$permalink_array[$i]'>
  123. reblogged
  124. </a>
  125. this from
  126. <a href = '$source_link_array[$i]'>
  127. $source_name_array[$i]
  128. </a>
  129. ";
  130. }
  131. //Determine and print extras
  132. if($extra_array[$i] != "none"){
  133. print "
  134. and added $extra_array[$i]
  135. ";
  136. }
  137. print "<br />";
  138. }
  139. //STOP TIMER #2
  140. $time = microtime();
  141. $time = explode(' ', $time);
  142. $time = $time[1] + $time[0];
  143. $finish = $time;
  144. $total_time = round(($finish - $start), 4);
  145. echo 'Step #2 took '.$total_time.' seconds. <br />';
  146. /************************/
  147. /* Step 3: Backtracking */
  148. /************************/
  149. //START TIMER #3
  150. $time = microtime();
  151. $time = explode(' ', $time);
  152. $time = $time[1] + $time[0];
  153. $start = $time;
  154. $tree_array = array();
  155. //Removes duplicate entries from poster array for function below
  156. $unique_posters_array = array_unique($poster_name_array);
  157. //Basically, for each unique poster, check for the poster
  158. //in the source array and get the poster.
  159. foreach ($unique_posters_array as $poster) {
  160. $temp_posters_array = array();
  161. $branch_array = array();
  162. $notes_count = count($source_name_array);
  163. for($i = 0; $i < $notes_count; $i++){
  164. if($source_name_array[$i] == $poster){
  165. array_push($temp_posters_array, $poster_name_array[$i]);
  166. }
  167. }
  168. $hits_count = count($temp_posters_array);
  169. if($hits_count > 0){
  170. foreach ($temp_posters_array as $hit) {
  171. array_push($branch_array, $hit);
  172. $tree_array[$poster] = $branch_array;
  173. }
  174. }
  175. }
  176. //Loop through all source posters
  177. $sources = array_keys($tree_array);
  178. $original_poster = $sources[0];
  179. $op_sourced = count($tree_array[$original_poster]);
  180. print "$original_poster - <br />";
  181. for ($i = 0; $i < $op_sourced; $i++) {
  182. $temp_branch = $tree_array[$original_poster][$i];
  183. print "=> $temp_branch";
  184. foreach ($tree_array as $branch => $branch_array) {
  185. $branch_count = count($branch_array);
  186. if($temp_branch == $branch){
  187. for ($j = 0; $j < $branch_count; $j++){
  188. print "
  189. <br />====> $branch_array[$j]
  190. ";
  191. }
  192. }
  193. }
  194. print "<br />";
  195. }
  196. /*
  197. foreach ($tree_array as $branch => $branch_array) {
  198. $branch_count = count($branch_array);
  199. print "$branch - ";
  200. for ($i = 0; $i < $branch_count; $i++){
  201. print "
  202. $branch_array[$i] <br />
  203. ";
  204. }
  205. }
  206. */
  207. //STOP TIMER #3
  208. $time = microtime();
  209. $time = explode(' ', $time);
  210. $time = $time[1] + $time[0];
  211. $finish = $time;
  212. $total_time = round(($finish - $start), 4);
  213. echo 'Step #3 took '.$total_time.' seconds. <br />';
  214. ?>