Last active
July 19, 2017 08:03
-
-
Save zhiyue/f49c8e951d5339b9b0c763f0ce8e1a6e to your computer and use it in GitHub Desktop.
download_miaopai_video.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# -*- coding: utf-8 -*- | |
""" | |
""" | |
import urlparse | |
import requests | |
headers = { | |
"Accept": "*/*", | |
"Accept-Encoding": "gzip, deflate, sdch", | |
"Accept-Language": "zh-CN,zh;q=0.8,en;q=0.6", | |
"Connection": "keep-alive", | |
"Host": "api.miaopai.com", | |
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko)" | |
" Chrome/50.0.2661.102 Safari/537.36", | |
"X-Requested-With": "ShockwaveFlash/23.0.0.166" | |
} | |
def get_mp4_addr(base_url): | |
api_url = 'http://api.miaopai.com/m/v2_channel.json' | |
rs = urlparse.urlparse(base_url) | |
q = urlparse.parse_qs(rs.query) | |
scid = q['scid'][0] | |
params = { | |
'fillType': '259', | |
'scid': scid, | |
'vend': 'miaopai' | |
} | |
headers['Referer'] = base_url | |
r = requests.get(api_url, params=params, headers=headers) | |
msg = r.json()['msg'] | |
result = r.json()['result'] | |
file_name = result['ext']['ft'] | |
if msg == 'OK': | |
stream_type = result['stream']['and'] | |
base = result['stream']['base'] | |
sign_str = result['stream']['sign'] | |
mp4_addr = '%s?%s&f=/%s.%s' % (base, sign_str, scid, stream_type) | |
return mp4_addr, '%s.%s' % (file_name, stream_type) | |
def download_video(mp4_addr, file_name): | |
r = requests.get(mp4_addr, stream=True) | |
if r.status_code == 200: | |
with open(file_name, 'wb') as f: | |
for chunk in r.iter_content(chunk_size=4096): | |
if chunk: | |
f.write(chunk) | |
if __name__ == '__main__': | |
url = 'http://wscdn.miaopai.com/splayer2.2.0.swf?scid=GcezAtC7sRC6~1S3GXxLkA__&token=&autopause=false&fromweibo=false' | |
mp4_addr, file_name = get_mp4_addr(url) | |
download_video(mp4_addr, file_name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
python3将urlparse改为urllib.parse