PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/bin/funkatron_twitterpublic_convert.php

https://github.com/funkatron/twitter-stats-tracker
PHP | 79 lines | 41 code | 24 blank | 14 comment | 4 complexity | aa7ffbda7f66eb36f2865bdc7e0eb7d0 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #!/usr/bin/php
  2. <?php
  3. /*
  4. get DB data
  5. */
  6. $url = 'http://127.0.0.1:5984/publictweets/';
  7. $json = file_get_contents($url);
  8. $dbdata = json_decode($json);
  9. $total = $dbdata->doc_count;
  10. $amount = 100;
  11. $startkey = null;
  12. for ($x=$amount;;) {
  13. echo "<pre>"; echo print_r($x, true); echo "</pre>\n";
  14. $url = 'http://127.0.0.1:5984/publictweets/_all_docs?count='.$x;
  15. if ($startkey) {
  16. $url .= '&startkey="'.$startkey.'"';
  17. }
  18. echo "<pre>"; echo print_r($url, true); echo "</pre>\n";
  19. $json = file_get_contents($url);
  20. if (!$json) {
  21. trigger_error("getting public timeline failed -- exiting", E_USER_ERROR);
  22. }
  23. $alltweets = json_decode($json);
  24. if (count($alltweets->rows) < 1) {
  25. exit('done');
  26. }
  27. /*
  28. Get key from last element, then reset array pointer
  29. */
  30. $startkey = end($alltweets->rows)->key;
  31. reset($alltweets->rows);
  32. foreach($alltweets->rows as $doc) {
  33. $docurl = 'http://127.0.0.1:5984/publictweets/'.$doc->id;
  34. $docjson = file_get_contents($docurl);
  35. $tweet = json_decode($docjson);
  36. // echo "<pre>"; echo print_r($tweet, true); echo "</pre>";
  37. $newdoc->date = strtotime($tweet->created_at);
  38. $newdoc->source = $tweet->source;
  39. $newjson = json_encode($newdoc);
  40. // echo "<pre>"; echo print_r($newdoc, true); echo "</pre>";
  41. $cdb_url = 'http://127.0.0.1:5984/publictweets_sources/'.$tweet->id;
  42. echo "<pre>"; echo print_r($cdb_url,true); echo "</pre>\n";
  43. // echo "<pre>"; echo print_r($newjson, true); echo "</pre>";
  44. // echo "<pre>"; echo print_r(json_encode($newdoc), true); echo "</pre>";
  45. $http = new HttpRequest($cdb_url, HttpRequest::METH_PUT);
  46. // $putdata = json_encode($newjson);
  47. // echo "$putdata\n";
  48. $http->setPutData($newjson);
  49. $http->setContentType('application/json');
  50. $httpresp = $http->send();
  51. echo "<pre>Response"; var_dump($http->getRawResponseMessage()); echo "</pre>\n\n\n";
  52. // sleep(1);
  53. unset($newdoc);
  54. }
  55. }