PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/inc/instagramService.php

https://bitbucket.org/thejeshgn/kannu
PHP | 106 lines | 92 code | 8 blank | 6 comment | 5 complexity | 8d82718bd72adb9d66911c708c3e1ecb MD5 | raw file
Possible License(s): GPL-3.0
  1. <?php
  2. class InstagramService extends F3instance {
  3. function startswith4($haystack, $needle) {
  4. return strpos($haystack, $needle) === 0;
  5. }
  6. function pull_data(){
  7. $this->set('title','PostBox - Instagram Data Pull');
  8. $instagram_api_clinet_id = 'b9d4b604105648168c671293d10cc67e';
  9. $instagram_api_url = 'https://api.instagram.com/v1/tags/openpostboxindia/media/recent?client_id='.$instagram_api_clinet_id;
  10. $data_pull_messages = array();
  11. $ch = curl_init ($instagram_api_url);
  12. curl_setopt($ch, CURLOPT_HEADER, 0);
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  14. curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
  15. $json=curl_exec($ch);
  16. curl_close ($ch);
  17. $data = json_decode ($json,true);
  18. //var_dump($data);
  19. if($data['meta']['code']==200){
  20. //echo 'sucess';
  21. $picture_dicts = $data['data'];
  22. foreach($picture_dicts as $pic){
  23. $tags = implode(', ', $pic['tags']);
  24. $pincode = 0;
  25. foreach($pic['tags'] as $tag){
  26. if($this->startswith4($tag, 'pin')){
  27. $pincode = substr($tag,3,10);
  28. }
  29. }
  30. $lat = $pic['location']['latitude'];
  31. $lan = $pic['location']['longitude'];
  32. $created_time = $pic['created_time'];
  33. $picture_url = $pic['images']['standard_resolution']['url'];
  34. $post_id = $pic['id'];
  35. $username = 'instagram-'.$pic["user"]["username"];
  36. $website = '';//$pic["user"]["website"];
  37. $caption = $pic["caption"]["text"];
  38. //check if post_id exists, if yes then go to next one. else insert
  39. $data_pull_messages[] = "Processing the post_id=".$post_id;
  40. $q = 'select count(*) as count_posts from post_box where post_id="'.$post_id.'"';
  41. $POSTBOX_DB=F3::get('POSTBOX_DB');
  42. $POSTBOX_DB->sql($q);
  43. //print '\n'.$q;
  44. $count_posts = 0;
  45. foreach (F3::get('POSTBOX_DB->result') as $row){
  46. $count_posts = $row['count_posts'];
  47. }
  48. if ($count_posts == 0){
  49. $data_pull_messages[] = "Lets INSERT.";
  50. $i = 'insert into post_box( post_id , picture_url , tags , lat , lan , created_time , username , website,pincode, caption, provider) values('.'"'.$post_id .'","'.$picture_url .'","'.$tags .'","'.$lat .'","'.$lan .'","'.$created_time .'","'.$username .'","'.$website .'","'.$pincode .'","'.$caption .'"'.',"Instagram")';
  51. //print $i;
  52. $POSTBOX_DB->sql($i);
  53. }else{
  54. $data_pull_messages[] = "Already exists.";
  55. }
  56. }
  57. }
  58. $this->set('LANGUAGE','en-US');
  59. $this->set('sub','sub_data_pull.html');
  60. $this->set('data_pull_messages',$data_pull_messages);
  61. $out=$this->render('basic/layout.html');
  62. $this->set('sub_out_put',$out);
  63. $this->set('LANGUAGE','en-US');
  64. echo $this->render('basic/main.html');
  65. }
  66. function pull_images(){
  67. $this->set('title','PostBox - Instagram Pull Pictures');
  68. $data_pull_messages = array();
  69. $POSTBOX_DB=F3::get('POSTBOX_DB');
  70. $q = 'select post_id,picture_url from post_box where img is null';
  71. $data_pull_messages[] = "Staring the process.";
  72. $POSTBOX_DB->sql($q);
  73. foreach (F3::get('POSTBOX_DB->result') as $row){
  74. $post_id = $row["post_id"];
  75. $picture_url = $row["picture_url"];
  76. $ch = curl_init ($picture_url);
  77. curl_setopt($ch, CURLOPT_HEADER, 0);
  78. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  79. curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
  80. $content=curl_exec($ch);
  81. curl_close ($ch);
  82. $img = base64_encode($content);
  83. $q = 'update post_box set img=:img where post_id=:post_id';
  84. $data_pull_messages[] = "add the img for the post_id=".$post_id;
  85. $POSTBOX_DB->exec($q,array(':post_id' => $post_id, ':img' =>$img));
  86. }
  87. $data_pull_messages[] = "Process complete.";
  88. //echo implode($data_pull_messages);
  89. $this->set('LANGUAGE','en-US');
  90. $this->set('sub','sub_data_pull.html');
  91. $this->set('data_pull_messages',$data_pull_messages);
  92. $out=$this->render('basic/layout.html');
  93. $this->set('sub_out_put',$out);
  94. $this->set('LANGUAGE','en-US');
  95. echo $this->render('basic/main.html');
  96. }
  97. }//calss end
  98. ?>