Advertisement
Guest User

Untitled

a guest
Jan 14th, 2020
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.89 KB | None | 0 0
  1. <?php
  2. set_time_limit(0);
  3. error_reporting(E_ALL);
  4.  
  5. define('PATH' , dirname(__FILE__) . DIRECTORY_SEPARATOR);
  6.  
  7. require_once 'simple_html_dom.php';
  8.  
  9. $engine = new Tiktok();
  10. $engine->run();
  11.  
  12. class Tiktok {
  13.  
  14.     public $token = '';
  15.     public $cookies = '';
  16.     public $useragent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36';
  17.     public $lang = 'ru';
  18.  
  19.     public $usernames = [];
  20.     public $tags = [];
  21.  
  22.     public function __construct()
  23.     {
  24.         $this->cookies = PATH . 'cookies.txt';
  25.         file_put_contents($this->cookies, '');
  26.     }
  27.  
  28.     public function run()
  29.     {
  30.         $this->enter();
  31.     }
  32.  
  33.     public function enter() {
  34.         // open main tiktok page
  35.         $this->curl('https://www.tiktok.com/ru');
  36.  
  37.         //open tranding
  38.         $this->curl('https://www.tiktok.com/trending/?lang=' . $this->lang);
  39.  
  40.         //open discover
  41.         $response = $this->curl('https://www.tiktok.com/discover?lang=' . $this->lang);
  42.  
  43.         $this->parse_usernames($response['content']);
  44.         $this->parse_tags($response['content']);
  45. //        print_r($this->usernames);
  46. //        print_r($this->tags);
  47.  
  48.         $this->parse_tags_from_usernames();
  49.        
  50.     }
  51.  
  52.     public function parse_tags_from_usernames()
  53.     {
  54.         if (count($this->usernames) > 0) {
  55.             foreach ($this->usernames as $username) {
  56.                 $response = $this->curl('https://www.tiktok.com/'.$username.'?lang=' . $this->lang);
  57.  
  58.                 echo $response['content'];
  59.  
  60.                 die();
  61.             }
  62.         }
  63.     }
  64.    
  65.     public function parse_tags($html)
  66.     {
  67.         $dom = str_get_html($html);
  68.         foreach($dom->find('._card_header_title') as $el)
  69.         {
  70.             $item = $el->innertext;
  71.             if (substr($item, 0, 1) == '#') {
  72.                 $this->add_tag($item);
  73.             }
  74.         }
  75.     }
  76.  
  77.     public function parse_usernames($html)
  78.     {
  79.         $dom = str_get_html($html);
  80.         foreach($dom->find('a') as $a)
  81.         {
  82.             if (mb_strlen($a->href) > 0) {
  83.                 $tmp = explode('/', $a->href);
  84.                 if (count($tmp ) > 0) {
  85.                     foreach ($tmp as $item) {
  86.                         if (substr($item, 0, 1) == '@') {
  87.                             $this->add_username($item);
  88.                             break;
  89.                         }
  90.                     }
  91.                 }
  92.             }
  93.         }
  94.     }
  95.  
  96.     public function add_username($username)
  97.     {
  98.         $username = trim($username);
  99.         if (!in_array($username, $this->usernames)) {
  100.             $this->usernames[] = $username;
  101.         }
  102.     }
  103.     public function add_tag($tag)
  104.     {
  105.         $tag = trim($tag);
  106.         if (!in_array($tag, $this->tags)) {
  107.             $this->tags[] = $tag;
  108.         }
  109.     }
  110.  
  111.     public function curl($url)
  112.     {
  113.         $options = array(
  114.             CURLOPT_RETURNTRANSFER  => true,     // return web page
  115.             CURLOPT_HEADER          => false,    // don't return headers
  116.             CURLOPT_FOLLOWLOCATION  => true,     // follow redirects
  117.             CURLOPT_ENCODING        => "",       // handle all encodings
  118.             CURLOPT_USERAGENT       => $this->useragent, // who am i
  119.             CURLOPT_AUTOREFERER     => true,     // set referer on redirect
  120.             CURLOPT_CONNECTTIMEOUT  => 30,      // timeout on connect
  121.             CURLOPT_TIMEOUT         => 30,      // timeout on response
  122.             CURLOPT_MAXREDIRS       => 10,       // stop after 10 redirects
  123.             CURLOPT_SSL_VERIFYPEER  => false,    // Disabled SSL Cert checks
  124.             CURLOPT_COOKIEFILE      => $this->cookies,
  125.             CURLOPT_COOKIEJAR       => $this->cookies,
  126.             CURLOPT_HTTPHEADER     => [
  127. //                ':authority: www.tiktok.com',
  128. //                ':method: GET',
  129. //                ':path: /ru',
  130. //                ':scheme: https',
  131.                 'accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9',
  132.                 'accept-encoding: gzip, deflate, br',
  133.                 'accept-language: ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7,de;q=0.6',
  134.                 'cache-control: max-age=0',
  135. //                'sec-fetch-mode: navigate',
  136. //                'sec-fetch-site: same-origin',
  137. //                'sec-fetch-user: ?1',
  138. //                'upgrade-insecure-requests: 1',
  139.             ]
  140.         );
  141.  
  142.         $ch      = curl_init( $url );
  143.         curl_setopt_array( $ch, $options );
  144.         $content = curl_exec( $ch );
  145.         $err     = curl_errno( $ch );
  146.         $errmsg  = curl_error( $ch );
  147.         $header  = curl_getinfo( $ch );
  148.         curl_close( $ch );
  149.  
  150.         $header['errno']   = $err;
  151.         $header['errmsg']  = $errmsg;
  152.         $header['content'] = $content;
  153.  
  154.         return $header;
  155.     }
  156.  
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement