ndrat

core.pixabay.php

May 26th, 2016
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.42 KB | None | 0 0
  1. <?php
  2.  
  3. // Main Class
  4. require_once 'core.php';
  5.  
  6. Class WpAutomaticPixabay extends wp_automatic {
  7.  
  8. function pixabay_fetch_items($keyword, $camp) {
  9.  
  10. echo "<br>so I should now get some images from pixabay for keyword :" . $keyword;
  11.  
  12. // $api_key = trim(get_option ( 'wp_automatic_pixabay', '' ));
  13. $api_key = '4919f28a0f6d35c6ebd388d65bdfe100';
  14.  
  15. if (trim($api_key) == '') {
  16. echo '<br>Pixabay Api key required ';
  17. exit();
  18. }
  19.  
  20. $camp_opt = unserialize($camp->camp_options);
  21. if (stristr($camp->camp_general, 'a:')) $camp->camp_general = base64_encode($camp->camp_general);
  22. $camp_general = unserialize(base64_decode($camp->camp_general));
  23. $camp_general = array_map('stripslashes', $camp_general);
  24.  
  25. $sortby = $camp_general['cg_fl_order'];
  26.  
  27. // get start-index for this keyword
  28. $query = "select keyword_start ,keyword_id from {$this->wp_prefix}automatic_keywords where keyword_name='$keyword' and keyword_camp={$camp->camp_id}";
  29. $rows = $this->db->get_results($query);
  30. $row = $rows[0];
  31. $kid = $row->keyword_id;
  32. $start = $row->keyword_start;
  33.  
  34. if ($start == 0) $start = 1;
  35.  
  36. if ($start == - 1) {
  37. echo '<- exhausted keyword';
  38.  
  39. if (!in_array('OPT_FL_CACHE', $camp_opt)) {
  40. $start =1;
  41. echo '<br>Cache disabled resetting index to 1';
  42. } else {
  43. //check if it is reactivated or still deactivated
  44. if ($this->is_deactivated($camp->camp_id, $keyword)) {
  45. $start =1;
  46. } else {
  47. //still deactivated
  48. return false;
  49. }
  50. }
  51. }
  52.  
  53. echo ' index:' . $start;
  54.  
  55. // update start index to start+1
  56. $nextstart = $start + 1;
  57.  
  58. $query = "update {$this->wp_prefix}automatic_keywords set keyword_start = $nextstart where keyword_id=$kid ";
  59. $this->db->query($query);
  60.  
  61. var_dump($start);
  62. echo "index buat naik";
  63. // get items
  64. $orderby = $camp_general['cg_fl_order'];
  65.  
  66. /**
  67. * liat page=$start
  68. * nah itu tuh yang bikin naek nya, eh harus nya kaya gitu.
  69. * pokonya kamu cari deh buat index page nya yang mana
  70. * jangan di tulis langsung page=1 itu mah terus terus an ngambil page satu
  71. * sebenernya $start itu gak tau apaan sih di coba coba aja
  72. */
  73. $flink = "https://pixabay.com/api/?key=2432254-cf546e7f7f532e93035b97408&image_type=photo&per_page=5&orientation=horizontal&page=$start";
  74.  
  75. if (in_array('OPT_FL_USER', $camp_opt)) {
  76. echo '<br>Fetching images for specific user ' . $camp_general['cg_fl_user'];
  77. $flink = $flink . '&user_id=' . $camp_general['cg_fl_user'];
  78. // if keyword *
  79. if (trim($keyword) == '*') {
  80. echo '<br>No filtering get all ';
  81. } else {
  82. // specific keyword
  83. $flink = $flink . '&q=' . urlencode($keyword);
  84. }
  85. } else {
  86. // no specific user just text
  87. $flink = $flink . '&q=' . urlencode($keyword);
  88. }
  89.  
  90. echo '<br>';
  91. echo $flink;
  92. echo '<br>';
  93.  
  94. // licensing license
  95. if (in_array('OPT_FL_LICENSE', $camp_opt)) {
  96. $licenses = array();
  97.  
  98. if (in_array('OPT_FL_LICENSE_0', $camp_opt)) $licenses[] = 0;
  99. if (in_array('OPT_FL_LICENSE_1', $camp_opt)) $licenses[] = 1;
  100. if (in_array('OPT_FL_LICENSE_2', $camp_opt)) $licenses[] = 2;
  101. if (in_array('OPT_FL_LICENSE_3', $camp_opt)) $licenses[] = 3;
  102. if (in_array('OPT_FL_LICENSE_4', $camp_opt)) $licenses[] = 4;
  103. if (in_array('OPT_FL_LICENSE_5', $camp_opt)) $licenses[] = 5;
  104. if (in_array('OPT_FL_LICENSE_6', $camp_opt)) $licenses[] = 6;
  105. if (in_array('OPT_FL_LICENSE_7', $camp_opt)) $licenses[] = 7;
  106. if (in_array('OPT_FL_LICENSE_8', $camp_opt)) $licenses[] = 8;
  107. if (in_array('OPT_FL_LICENSE_9', $camp_opt)) $licenses[] = 9;
  108.  
  109. if (count($licenses) > 0) $flink .= "&license=" . implode(',', $licenses);
  110. }
  111.  
  112. // curl get
  113. $x = 'error';
  114. $url = $flink;
  115. curl_setopt($this->ch, CURLOPT_HTTPGET, 1);
  116. curl_setopt($this->ch, CURLOPT_URL, trim($url));
  117. while (trim($x) != '') {
  118. $exec = curl_exec($this->ch);
  119. $x = curl_error($this->ch);
  120. }
  121.  
  122. $results = json_decode($exec, true);
  123.  
  124. // foreach ($results as $k => $v) {
  125. // echo "<pre>".print_r($v, 1)."</pre>";
  126. // echo $v->urls->raw;
  127. // echo "<br>";
  128. // }
  129. // die();
  130.  
  131. if (is_array($results)) {
  132.  
  133. echo '<br>Valid array returned from pixabay ';
  134.  
  135. // $imgs = $results['user']['urls'];
  136.  
  137. $imgs = array();
  138. foreach ($results['hits'] as $result ){
  139. array_push($imgs, array(
  140. 'user' => $result['user'],
  141. 'urls' => $result['webformatURL'],
  142. 'id' => $result['id'],
  143. 'title' => $keyword . ' by ' . $result['user'],
  144. 'likes' => $result['likes'],
  145. 'author' => $result['userImageURL'],
  146. 'links' => $result['pageURL'],
  147. 'tags' => $result['tags'],
  148. ));
  149.  
  150. // echo "result";
  151. // echo '<pre>';
  152. // var_dump($imgs);
  153. // echo '</pre>';
  154. // array_push($imgs, $result['id']);
  155. }
  156.  
  157.  
  158.  
  159. if (is_array($imgs)) {
  160.  
  161. echo '<br>Valid reply array returned with ' . count($imgs) . ' child';
  162.  
  163. if (count($imgs) == 0) {
  164. echo '<br>Keyword have no more images deactivating...';
  165. $query = "update {$this->wp_prefix}automatic_keywords set keyword_start = -1 where keyword_id=$kid ";
  166. $this->db->query($query);
  167. $this->deactivate_key($camp->camp_id, $keyword);
  168. }
  169.  
  170. } else {
  171. echo '<br>Did not find valid image array in the response ';
  172. $imgs = array();
  173. }
  174.  
  175. } else {
  176. echo '<br>Pixabay did not reuturn valid reply array ';
  177. }
  178.  
  179. echo '<ol>';
  180.  
  181. foreach ($imgs as $itm) {
  182. $id = $itm['id'];
  183. $data = serialize($itm);
  184.  
  185. $item_link = "https://pixabay.com/api/?key=2432254-cf546e7f7f532e93035b97408&image_type=photo&per_page=200&orientation=horizontal&$id";
  186. // JIKA DI HILANGI MAKA AKAN MUNCUL DUPLIKAT ID
  187. echo '<li> Link: '.$item_link;
  188.  
  189. if ($this->is_execluded($camp->camp_id, $item_link)) {
  190. echo '<-- Execluded';
  191. continue;
  192. }
  193.  
  194. if (!$this->is_duplicate($item_link)) {
  195. $query = "INSERT INTO {$this->wp_prefix}automatic_general ( item_id , item_status , item_data ,item_type) values ( '$id', '0', '$data' ,'fl_{$camp->camp_id}_$keyword' )";
  196. $this->db->query($query);
  197. } else {
  198. echo ' <- duplicated <a href="' . get_edit_post_link($this->duplicate_id) . '">#' . $this->duplicate_id . '</a>';
  199. }
  200.  
  201. }
  202.  
  203. echo '</ol>';
  204. }
  205.  
  206. /*
  207. * ---* pixabay post ---
  208. */
  209. function pixabay_get_post($camp) {
  210. // $api_key = get_option ( 'wp_automatic_pixabay', '' );
  211. $api_key = '4919f28a0f6d35c6ebd388d65bdfe100';
  212.  
  213. if (trim($api_key) == '') {
  214. echo '<br>Pixabay Api key required visit settings and add it ';
  215. exit();
  216. }
  217.  
  218. $camp_opt = unserialize($camp->camp_options);
  219. $keywords = explode(',', $camp->camp_keywords);
  220.  
  221. foreach ($keywords as $keyword) {
  222.  
  223. $keyword = trim($keyword);
  224.  
  225. //update last keyword
  226. update_post_meta($camp->camp_id, 'last_keyword', trim($keyword));
  227.  
  228. if (trim($keyword) != '') {
  229.  
  230. // getting links from the db for that keyword
  231. $query = "select * from {$this->wp_prefix}automatic_general where item_type= 'fl_{$camp->camp_id}_$keyword' and item_status ='0'";
  232. $this->used_keyword = $keyword;
  233.  
  234. $res = $this->db->get_results($query);
  235.  
  236. // when no links lets get new links
  237. if (count ( $res ) == 0) {
  238. $this->pixabay_fetch_items($keyword, $camp);
  239. // getting links from the db for that keyword
  240.  
  241. $res = $this->db->get_results($query);
  242. }
  243.  
  244. // echo '<pre>';
  245. // var_dump($res);
  246. // echo '<pre>';
  247.  
  248. //check if already duplicated
  249. //deleting duplicated items
  250. $res_count = count($res);
  251. for ($i = 0; $i < $res_count; $i++) {
  252.  
  253. $t_row = $res[$i];
  254. $t_data = unserialize($t_row->item_data);
  255. $id = $t_data['id'];
  256. // echo 'tdataaaaa';
  257. // echo '<pre>';
  258. // var_dump($t_data);
  259. // echo '<pre>';
  260. $t_link_url = "https://pixabay.com/api/?key=2432254-cf546e7f7f532e93035b97408&image_type=photo&per_page=200&orientation=horizontal&$id";
  261.  
  262. if ($this->is_duplicate($t_link_url)) {
  263. echo 'duplicated';
  264. // die();
  265. //duplicated item let's delete
  266. unset($res[$i]);
  267.  
  268. echo '<br>Pixabay image (' . $t_data['title'] . ') found cached but duplicated <a href="' . get_permalink($this->duplicate_id) . '">#' . $this->duplicate_id . '</a>' ;
  269.  
  270. //delete the item
  271. $query = "delete from {$this->wp_prefix}automatic_general where item_id='{$t_row->item_id}' and item_type= 'fl_{$camp->camp_id}_$keyword'";
  272. $this->db->query($query);
  273.  
  274. } else {
  275. break;
  276. }
  277.  
  278. }
  279.  
  280. // check again if valid links found for that keyword otherwise skip it
  281. if (count($res) > 0) {
  282.  
  283. // lets process that link
  284. $ret = $res[$i];
  285.  
  286. $data = unserialize($ret->item_data);
  287.  
  288. $temp['img_title'] = $data['title'];
  289. $temp['img_author'] = $data['user'];
  290. $temp['profile_author'] = $data['author'];
  291. // $temp['img_src'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}.jpg";
  292. // $temp['img_src_s'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_s.jpg";
  293. // $temp['img_src_q'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_q.jpg";
  294. // $temp['img_src_t'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_t.jpg";
  295. // $temp['img_src_m'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_m.jpg";
  296. // $temp['img_src_n'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_n.jpg";
  297. // $temp['img_src_z'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_z.jpg";
  298. // $temp['img_src_c'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_c.jpg";
  299. // $temp['img_src_b'] = "http://farm{$data['farm']}.staticflickr.com/{$data['server']}/{$data['id']}_{$data['secret']}_b.jpg";
  300. // $temp['img_link'] = 'http://unsplash.com/' . $data ['owner'] . '/' . $ret->item_id;
  301. $temp['img_likes'] = $data['likes'];
  302. $temp['img_tags'] = $data['tags'];
  303. $temp['img_src'] = $data['urls'];
  304. $temp['img_link'] = $data['links'];
  305. $temp['img_categories'] = $data['tags'];
  306. $temp['img_download'] = $data['urls'];
  307.  
  308.  
  309. echo '<br>Found Link:<a href="' . $temp['img_link'] . '">' . $temp['img_title'] . '</a>';
  310.  
  311. // getting photo description
  312. // curl get
  313. // $x = 'error';
  314. // $url = "https://api.unsplash.com/photos/{$ret->item_id}?client_id=efb65c79976543a262d51c13 c393c1d59881bce5e17f10c33b4fc0176909cbea";
  315. // curl_setopt($this->ch, CURLOPT_HTTPGET, 1);
  316. // curl_setopt($this->ch, CURLOPT_URL, trim($url));
  317. // while (trim($x) != '') {
  318. // $exec = curl_exec($this->ch);
  319. // $x = curl_error($this->ch);
  320. // }
  321. // echo $url;
  322. // $exec = unserialize($exec);
  323.  
  324. // if (!is_array($exec)) {
  325. // echo '<br> Not valid array ';
  326. // } else {
  327.  
  328. // $temp['img_author_name'] = $exec['photo']['owner']['username'];
  329. // $temp['img_description'] = $exec['photo']['description']['_content'];
  330. // $temp['img_date_posted'] = date('Y-m-d H:i:s', $exec['photo']['dates']['posted']);
  331. // $temp['img_date_taken'] = $exec['photo']['dates']['taken'];
  332. // $temp['img_viewed'] = $exec['photo']['views'];
  333.  
  334. // $tags = '';
  335. // foreach ($exec['photo']['tags']['tag'] as $tag) {
  336. // $tags = $tags . ' , ' . $tag['raw'];
  337. // }
  338.  
  339. // $temp['img_tags'] = $tags;
  340. // }
  341.  
  342. // update the link status to 1
  343. $query = "update {$this->wp_prefix}automatic_general set item_status='1' where item_id='$ret->item_id' and item_type='fl_{$camp->camp_id}_$keyword' ";
  344.  
  345. $this->db->query($query);
  346.  
  347. // if cache not active let's delete the cached videos and reset indexes
  348. if (!in_array('OPT_FL_CACHE', $camp_opt)) {
  349. echo '<br>Cache disabled claring cache ...';
  350. $query = "delete from {$this->wp_prefix}automatic_general where item_type='fl_{$camp->camp_id}_$keyword' and item_status ='0'";
  351. $this->db->query($query);
  352.  
  353. // reset index
  354. $query = "update {$this->wp_prefix}automatic_keywords set keyword_start =1 where keyword_camp={$camp->camp_id}";
  355. $this->db->query($query);
  356. }
  357.  
  358. return $temp;
  359. } else {
  360. echo '<br>No links found for this keyword';
  361. }
  362.  
  363. } // if trim
  364. } // foreach keyword
  365. }
  366.  
  367. }
Add Comment
Please, Sign In to add comment