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

/bin/funkatron_twittertrends.php

https://github.com/funkatron/twitter-stats-tracker
PHP | 72 lines | 30 code | 14 blank | 28 comment | 1 complexity | 51cf207595609f5507d6611bdd5fd3c6 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #!/usr/bin/php
  2. <?php
  3. $url = 'http://search.twitter.com/trends.json';
  4. $json = file_get_contents($url);
  5. //echo "<pre>"; echo print_r($json, true); echo "</pre>";
  6. if (!$json) {
  7. trigger_error("getting trends data failed -- exiting", E_USER_ERROR);
  8. }
  9. $trends = json_decode($json);
  10. $rank = count($trends->trends);
  11. foreach($trends->trends as &$trend) {
  12. $trend->as_of = $trends->as_of;
  13. $trend->rank = $rank--;
  14. $trend->unixtime = strtotime($trend->as_of);
  15. }
  16. /*
  17. We need to make an object and assign the new docs to a property called "docs"
  18. */
  19. $newdocs->docs = $trends->trends;
  20. $newjson = json_encode($newdocs);
  21. //echo "<pre>"; echo print_r($newjson, true); echo "</pre>";
  22. $cdb_url = 'http://127.0.0.1:5984/twitter_trends/_bulk_docs';
  23. $http = new HttpRequest($cdb_url, HttpRequest::METH_POST);
  24. $http->setContentType('application/json');
  25. $http->setRawPostData($newjson);
  26. $http->send();
  27. //echo "<pre>"; echo print_r($http->getResponseMessage(), true); echo "</pre>";
  28. // echo "<pre>"; echo print_r($newjson, true); echo "</pre>";
  29. //exit();
  30. // update views
  31. $url = 'http://127.0.0.1:5984/twitter_trends/_view/rank/byminute?group=true';
  32. $json_counts = file_get_contents($url);
  33. $url = 'http://127.0.0.1:5984/twitter_trends/_view/rank/byhour?group=true';
  34. $json_counts = file_get_contents($url);
  35. $url = 'http://127.0.0.1:5984/twitter_trends/_view/rank/byday?group=true';
  36. $json_counts = file_get_contents($url);
  37. $url = 'http://127.0.0.1:5984/twitter_trends/_view/rank/bydayofweek?group=true';
  38. $json_counts = file_get_contents($url);
  39. $url = 'http://127.0.0.1:5984/twitter_trends/_view/rank/bymonth?group=true';
  40. $json_counts = file_get_contents($url);
  41. //
  42. // $counts = json_decode($json_counts);
  43. //
  44. // foreach($counts->rows as &$row) {
  45. // $row->key = strip_tags(stripslashes($row->key));
  46. // // echo "<pre>"; echo print_r($row->key, true); echo "</pre>";
  47. // }
  48. //
  49. // // echo "<pre>"; echo print_r($counts, true); echo "</pre>";
  50. //
  51. // $json_counts = json_encode($counts);
  52. //
  53. // // echo "<pre>"; echo print_r($json_counts, true); echo "</pre>";
  54. //
  55. //
  56. // // $json_counts = strip_tags(stripslashes($json_counts));
  57. //
  58. // file_put_contents('/var/www/twittersource.info/htdocs/twitter-trends.json', $json_counts);