1. 程式人生 > >分享一個抖音視訊下載程式

分享一個抖音視訊下載程式

在網上呼叫別人的介面來實現的功能 

import requests
import execjs


# 生成引數s
def generateStr(a):
    js = '''
     test = function(a) {
                var c = function() {
                     for (var d = 0,
                     f = new Array(256), g = 0; 256 != g; ++g) {
                          d = g,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1,
                          f[g] = d
                     }
                     return "undefined" != typeof Int32Array ? new Int32Array(f) : f
                } (),
                b = function(g) {
                     for (var j, k, h = -1,
                     f = 0,
                     d = g.length; f < d;) {
                          j = g.charCodeAt(f++),
                          j < 128 ? h = h >>> 8 ^ c[255 & (h ^ j)] : j < 2048 ? (h = h >>> 8 ^ c[255 & (h ^ (192 | j >> 6 & 31))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & j))]) : j >= 55296 && j < 57344 ? (j = (1023 & j) + 64, k = 1023 & g.charCodeAt(f++), h = h >>> 8 ^ c[255 & (h ^ (240 | j >> 8 & 7))], h = h >>> 8 ^ c[255 & (h ^ (128 | j >> 2 & 63))], h = h >>> 8 ^ c[255 & (h ^ (128 | k >> 6 & 15 | (3 & j) << 4))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & k))]) : (h = h >>> 8 ^ c[255 & (h ^ (224 | j >> 12 & 15))], h = h >>> 8 ^ c[255 & (h ^ (128 | j >> 6 & 63))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & j))])
                     }
                     return h ^ -1
                };
                return b(a) >>> 0
          }
     '''
    ctx = execjs.compile(js)
    return ctx.call('test', a)


def run():
    # 第一次訪問的url(獲取cookie)
    base_url = 'http://douyin.iiilab.com'

    # 解析url
    url = 'http://service0.iiilab.com/video/web/douyin'

    # link = 'http://v.douyin.com/d3pqo9/'
    # 抖音需要去水印的連結
    link = input('請輸入連結\n')

    # r是隨機數
    r = execjs.eval('Math.random().toString(10).substring(2)')

    # s是根據隨機數r一定的規則生成的(太長不想分析, 直接用execjs這個庫直接跑js程式碼)
    s = generateStr('{}@{}'.format(link, r))
    headers = {
        'Host': 'douyin.iiilab.com',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3514.0 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'Accept-Encoding': 'gzip,deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9'
    }
    data = {
        'link': link,
        'r': r,
        's': s
    }

    # 使用requests的session保留cookies
    sess = requests.Session()

    # 要加入headers,不然報錯
    sess.headers.update(headers)

    # 模擬正常訪問網頁獲取cookies
    res = sess.get(base_url)

    headers = {
        'Host': 'service0.iiilab.com',
        'Origin': 'http://douyin.iiilab.com',
        'Referer': 'http://douyin.iiilab.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3514.0 Safari/537.36',
        'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
        'Accept': 'application/json, text/javascript, */*; q=0.01',
        'Pragma': 'no-cache',
        'Cache-Control': 'no-cache',
        'Accept-Encoding': 'gzip,deflate',
        'Accept-Language': 'zh-CN,zh;q=0.9'
    }
    sess.headers.update(headers)

    # 獲取返回的資料
    res = sess.post(url, data=data, headers=headers).json()

    if res['retCode'] == 200:
        print(res)
        # 封面
        cover = res['data']['cover']
        # 標題
        title = res['data']['text']
        # 去水印後的視訊
        video = res['data']['video']
    else:
        print(res)


if __name__ == '__main__':
    run()