射手网突然停止下载了,现在看个电影都很费事,还好射手网的接口没有关闭。看了下接口文档,自己用php写了个脚本,可以直接根据影片文件下载匹配的字幕。

把代码方在/usr/local/bin/目录,文件名zimu,并chmod +x /usr/local/bin/zimu,然后这样用:

zimu 电影文件名

就可以自动下载字幕了
可以加参数“debug”, “all”
比如

zimu test.mkv all

则下载全部匹配的字幕

window用户的话,可以保存成zimu.php然后使用

php zimu.php 电影文件名

这样来下载

不多说,直接上代码

#!/usr/bin/env php
<?php

$file = $_SERVER['argv'][1];

if (!$file)
{
 echo "缺少参数\n";
 exit;
}
if (!is_file($file))
{
 echo "文件不存在\n";
    exit;
}
$ftotallen = filesize($file);
if ($ftotallen<8192)
{
    echo "文件太小\n";
 exit;
}





$offsets[] = 4096;
$offsets[] = floor($ftotallen / 3 * 2);
$offsets[] = floor($ftotallen / 3);
$offsets[] = $ftotallen - 8192;

$fp = fopen($file, "r");

foreach($offsets as $offset)
{
    fseek($fp, $offset, SEEK_SET);
    $data  = fread($fp, 4096);
  $md5[] = md5($data);
}

fclose($fp);

echo $md5_str = implode(';', $md5);

echo "\n";

function curl_post($url, array $post = NULL, array $options = array())
{
    $defaults = array(
        CURLOPT_POST => 1,
        CURLOPT_HEADER => 0,
        CURLOPT_URL => $url,
        CURLOPT_FRESH_CONNECT => 1,
        CURLOPT_RETURNTRANSFER => 1,
        CURLOPT_FORBID_REUSE => 1,
        CURLOPT_TIMEOUT => 4,
        CURLOPT_POSTFIELDS => http_build_query($post)
    );

    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    if( ! $result = curl_exec($ch))
    {
        trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
}

function curl_get($url, array $get = NULL, array $options = array())
{
    $defaults = array(
        CURLOPT_URL => $url. (strpos($url, '?') === FALSE ? '?' : ''). ($get ? http_build_query($get): ''),
        CURLOPT_HEADER => 0,
        CURLOPT_RETURNTRANSFER => TRUE,
        CURLOPT_TIMEOUT => 4
    );

    $ch = curl_init();
    curl_setopt_array($ch, ($options + $defaults));
    if( ! $result = curl_exec($ch))
    {
        trigger_error(curl_error($ch));
    }
    curl_close($ch);
    return $result;
}


$data = array
(
    'filehash' => $md5_str,
   'pathinfo' => $file,
  'format'   => 'json',
   'lang'     => 'Chn',
);

$r = curl_post('https://www.shooter.cn/api/subapi.php', $data);

$rs = @json_decode($r, true);

if (!$rs)
{
  echo "获取字幕失败\n";
   exit;
}

if (in_array('debug', $_SERVER['argv']))
{
    print_r($rs);
}

$get_all = in_array('all', $_SERVER['argv']);

foreach ($rs as $key => $item)
{
    $url = $item['Files'][0];

    $content = curl_get($url['Link']);
    if (!strlen($content))
    {
     echo "获取失败 {$url['Link']}\n";
    }

    if (in_array('utf8', $_SERVER['argv']))
    {
        if (extension_loaded('mbstring'))
        {
            $content = mb_convert_encoding($content, 'UTF-8', 'GBK');
        }
        else
        {
            $content = iconv('UTF-8', 'gbk//IGNORE', $content);
        }
    }

    if (file_put_contents(substr($file, 0, strrpos($file, '.')).($key>0?'.'.$key:'').'.'.$url['Ext'], $content)>0)
    {
      echo "success {$url['Link']}\n";
    }
    else
    {
       echo "fail {$url['Link']}\n";
    }

    if (!$get_all)break;
}

echo "done.\n";