Skip to content

Instantly share code, notes, and snippets.

@PeterDing
Last active December 24, 2015 23:48
Show Gist options
  • Save PeterDing/6882512 to your computer and use it in GitHub Desktop.
Save PeterDing/6882512 to your computer and use it in GitHub Desktop.
反解析 flvxz.com
#!/usr/bin/env python2
# vim: set fileencoding=utf8
import base64, requests, time, os, sys, argparse, random, subprocess
s = '\x1b[1;%dm%s\x1b[0m' # terminual color template
############################################################
# wget exit status
wget_es = {
0: "No problems occurred.",
2: "User interference.",
1<<8: "Generic error code.",
2<<8: "Parse error - for instance, when parsing command-line optio.wgetrc or .netrc...",
3<<8: "File I/O error.",
4<<8: "Network failure.",
5<<8: "SSL verification failure.",
6<<8: "Username/password authentication failure.",
7<<8: "Protocol errors.",
8<<8: "Server issued an error response."
}
############################################################
headers = {
"Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding":"text/html",
"Accept-Language":"en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4,zh-TW;q=0.2",
"Content-Type":"application/x-www-form-urlencoded",
"Referer":"http://www.baidu.com/",
"User-Agent":"Mozilla/5.0 (X11; Linux i686) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1700.77 Safari/537.36"
}
wget_template = 'wget -c -nv -O "%s.tmp" %s'
api = 'http://api.flvxz.com/jsonp/purejson/url/%s'
def download(infos):
if not os.path.exists(infos['dir_']):
os.mkdir(infos['dir_'])
#else:
#if os.path.exists(infos['filename']):
#return 0
num = random.randint(0, 7) % 7
col = s % (num + 90, infos['filename'])
print '\n ++ 正在下载: %s' % col
cmd = wget_template % (infos['filename'], infos['durl'])
subprocess.call(cmd, shell=True)
try:
while True:
ii = subprocess.check_output('ps a | grep "wget"', shell=True).partition('\n')[-1]
if ii != '':
time.sleep(3)
else:
break
except:
subprocess.call('killall wget', shell=True)
wget_exit_status_info = wget_es[2]
print('\n\n ----### \x1b[1;91mERROR\x1b[0m ==> \x1b[1;91m%d (%s)\x1b[0m ###--- \n\n' % (2, wget_exit_status_info))
print s % (91, ' ===> '), cmd
sys.exit(1)
os.rename('%s.tmp' % infos['filename'], infos['filename'])
def main(url):
encode_url = base64.b64encode(url.replace('://', ':##'))
url = api % encode_url
j = requests.get(url).json()[-1]
print s % (92, ' -- %s' % j['quality'].encode('utf8'))
yes = True if len(j['files']) > 1 else False
dir_ = os.path.join(os.getcwd(), j['title'].encode('utf8')) if yes else os.getcwd()
n = 1
for i in j['files']:
infos = {
'filename': os.path.join(dir_, '%s_%s.%s' % (j['title'].encode('utf8'), n, i['ftype'].encode('utf8'))),
'durl': i['furl'].encode('utf8'),
'dir_': dir_
}
download(infos)
n += 1
#os.system('mplayer -playlist /tmp/tmp_video_urls')
if __name__ == '__main__':
p = argparse.ArgumentParser(description='flvxz')
p.add_argument('url', help='site url')
args = p.parse_args()
main(args.url)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment