Advertisement
pawelnowacki

Untitled

Apr 18th, 2020
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.28 KB | None | 0 0
  1. <?php
  2.  
  3. // Mozesz tez uzyc publicznej wersji pod adresem:
  4. // https://teleit.pl/wyk.php?id=[id_znaleziska]&s=[rozdzielczosc]
  5. // np. http://teleit.pl/wyk.php?id=5454911&s=16
  6.  
  7.  
  8. $id = $_SERVER['argv'][1] ?? ($_GET['id'] ?? false);
  9. if(!$id)
  10.     print 'ups...';
  11.  
  12. $res = $_SERVER['argv'][2] ?? ($_GET['s'] >= 10 ? $_GET['s'] : 13);
  13.  
  14. define('API_KEY','wpisz_swoj_api_key');
  15. define('API_SECRET','wpis_swoj_api_secret');
  16. define('API_URL','https://a2.wykop.pl/');
  17.  
  18.  
  19. $votes = ['up' => 'Links/Upvoters/','down' => 'Links/Downvoters/'];
  20.  
  21.  
  22.  
  23.  
  24.  
  25. function get_curl($method, $parameters, $return_transfer = true, $is_post = false)
  26. {
  27.  
  28.     $curl_options = [
  29.         CURLOPT_URL => API_URL . $method . (!empty($parameters) ? '?' . http_build_query($parameters) : ''),
  30.         CURLOPT_RETURNTRANSFER => $return_transfer,
  31.         CURLOPT_SSL_VERIFYHOST => 0,
  32.         CURLOPT_SSL_VERIFYPEER => 0,
  33.         CURLOPT_RETURNTRANSFER  => true,
  34.         CURLOPT_HEADER          => false,
  35.         CURLOPT_AUTOREFERER     => true,
  36.         CURLOPT_CONNECTTIMEOUT  => 15,
  37.         CURLOPT_TIMEOUT         => 15,
  38.         CURLOPT_MAXREDIRS       => 10,
  39.         CURLOPT_POST => $is_post,
  40.         CURLOPT_POSTFIELDS => $is_post??"",
  41.     ];
  42.  
  43.     $curl_options[CURLOPT_HTTPHEADER] = ['Content-Type: application/json','apisign: ' .md5(API_SECRET . $curl_options[CURLOPT_URL])];
  44.  
  45.     $ch = curl_init();
  46.  
  47.     curl_setopt_array($ch, $curl_options);
  48.  
  49.     return $ch;
  50.  
  51. }
  52.  
  53.  
  54. function get_votes($votes_sub_url)
  55. {
  56.     $ch = get_curl($votes_sub_url . $id . '/appkey/' . API_KEY . '/format/json', [], true, false);
  57.  
  58.     $result = curl_exec($ch);
  59.  
  60.     if (curl_errno($ch)) {
  61.         $error_msg = curl_error($ch);
  62.         throw new Exception("Error Processing Request - curl: " . $error_msg, 1);
  63.     }
  64.  
  65.     curl_close($ch);
  66.  
  67.     return json_decode($result)->data ?? [];
  68. }
  69.  
  70.  
  71. $ch_data = ['up' => get_votes($votes['up'] . $id), 'down' => get_votes($votes['down'] . $id)];
  72.  
  73. $tmp = ['up' => 0, 'down' => 0];
  74. $data = [];
  75.  
  76.  
  77. foreach($ch_data as $k=>$v)
  78. {
  79.  
  80.     foreach ($ch_data[$k] as $value) {
  81.        
  82.         $k_tmp = substr($value->date,0,$res);
  83.  
  84.         $data[$k_tmp] ?? $data[$k_tmp] = $tmp;
  85.  
  86.         $data[$k_tmp][$k]++;
  87.  
  88.     }
  89. }
  90.  
  91. ksort($data);
  92. print '<pre>';
  93. foreach ($data as $key => $value) {
  94.     print $key . ' up: ' . str_pad($data[$key]['up'],5,'.', STR_PAD_LEFT) . ' down: ' . str_pad($data[$key]['down'],5,'.', STR_PAD_LEFT) . PHP_EOL;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement