2018年6月10日 星期日

自動下載抖音影片


女兒愛玩抖音,拍了一些短片。我希望能把它保存起來,於是用 Charles Proxy 研究了一下當中的通訊內容;然後寫了下面的 PHP 程式,配合 crontab 定時執行。只要女兒有公開的影片,程式便會自動下載。
<?php
//----------------------------------------------------------------------------------------
//  Douyin Video Downloader
//----------------------------------------------------------------------------------------
//  Platform: CentOS 7 + PHP 5
//  Written by Pacess HO
//  Copyright Pacess Studio, 2018.  All rights reserved.
//----------------------------------------------------------------------------------------

//  Global variables
$_downloadDirectory = "./__files__/";

$_readLiveList = true;
$_responseFile = "dorothy.json";

$_userID = "6519251381372489395";
$_url = "https://api.tiktokv.com/aweme/v1/aweme/post/?version_code=2.2.1&language=ja"+
        "&app_name=trill&vid=18DD92EA-42E1-848F-4BA4-CDFAB132F79D&app_version=2.2.1"+
        "&carrier_region=HK&is_my_cn=1&channel=App%20Store&mcc_mnc=45406"+
        "&device_id=6539510560658635543&tz_offset=28800&account_region=HK&sys_region=HK"+
        "&aid=1180&screen_width=640&openudid=92046a2f432be94bec4d1b7369d754ab57259918"+
        "&os_api=18&ac=WIFI&os_version=11.4&app_language=ja&tz_name=Asia/Hong_Kong"+
        "&device_platform=iphone&build_number=22102&device_type=iPhone8,4"+
        "&iid=6564579886684473090&idfa=CE02FD8A-E2BA-44B5-884B-C03EBAFC413B"+
        "&count=21&max_cursor=0&min_cursor=0&user_id=$_userID"+
        "&mas=035160f738fae1dd802eef2d7ae9b80526a7d06a0300db4d1378d8&as=".time();

//========================================================================================
//  Main program
$response = null;
if ($_readLiveList == true)  {

   //  Get response from server
   $curl = curl_init($_url);
   curl_setopt($curl, CURLOPT_HEADER, 0);
   curl_setopt($curl, CURLOPT_TIMEOUT, 30);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
   $response = curl_exec($curl);
   curl_close($curl);

   file_put_contents($_responseFile, $response);

}  else  {

   //  Get response from file
   $response = file_get_contents($_responseFile);
}

//----------------------------------------------------------------------------------------
$json = json_decode($response, true);

if (isset($json["aweme_list"]) == false)  {
   echo("### Aweme list not found...\n");
   exit(-1);
}

$awemeArray = $json["aweme_list"];
$videoCount = count($awemeArray);
echo("$videoCount videos found...\n");
foreach ($awemeArray as $aweme)  {

   //  Get video create time, use this value for filename
   if (isset($aweme["create_time"]) == false)  {
      echo("### Create time not found...\n");
      continue;
   }
   $createTime = $aweme["create_time"];

   //  Check if video already exists
   $filename = $_userID."_".$createTime.".mov";
   $filePath = $_downloadDirectory.$filename;

   $exists = file_exists($filePath);
   if ($exists == true)  {
      echo("Skip video [$filename]...");
      continue;
   }

   //----------------------------------------------------------------------------------------
   //  Video not exists, check for video URL
   if (isset($aweme["video"]) == false)  {
      echo("### Video not found...\n");
      continue;
   }

   $videoDictionary = $aweme["video"];
   if (isset($videoDictionary["play_addr"]) == false)  {
      echo("### Video address not found...\n");
      continue;
   }

   $videoAddressArray = $videoDictionary["play_addr"]["url_list"];
   $count = count($videoAddressArray);
   $index = 0;
   if ($index >= $count || $index < 0)  {
      echo("### Invalid address index #$index...\n");
      continue;
   }

   $videoAddress = $videoAddressArray[$index];
   echo("Downloading [$videoAddress]...");

   $curl = curl_init($videoAddress);
   curl_setopt($curl, CURLOPT_HEADER, false);
   curl_setopt($curl, CURLOPT_TIMEOUT, 30);
   curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
   curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
   $response = curl_exec($curl);
   curl_close($curl);

   //  The content is share information only, need to download video with URL inside
   $count = file_put_contents($filePath, $response);
   echo("$count saved.\n");
}

?>

沒有留言: