Created
November 19, 2017 07:17
-
-
Save zxkane/c62a41255a9170e794ee9d9818dc0df4 to your computer and use it in GitHub Desktop.
download from 5tps
This file contains hidden or 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
# -*- coding: utf-8 -*- | |
# encoding: utf-8 | |
''' | |
@author: kane | |
''' | |
import argparse | |
import os, errno | |
import requests | |
def download(url, localFileName, headers): | |
# NOTE the stream=True parameter | |
r = requests.get(url, stream=True, headers = headers) | |
with open(localFileName, 'wb') as f: | |
for chunk in r.iter_content(chunk_size=1024): | |
if chunk: # filter out keep-alive new chunks | |
f.write(chunk) | |
#f.flush() commented by recommendation from J.F.Sebastian | |
return localFileName | |
def mkdir_p(path): | |
try: | |
os.makedirs(path) | |
except OSError as exc: # Python >2.5 | |
if exc.errno == errno.EEXIST and os.path.isdir(path): | |
pass | |
else: raise | |
class C(object): | |
pass | |
def main(): | |
c = C() | |
parser = argparse.ArgumentParser(description='Download mp3 from 5tps') | |
parser.add_argument('-t', '--targetPath', dest='target', help='the target path', required = True) | |
parser.parse_args(namespace=c) | |
if not c.target: | |
parser.print_help() | |
sys.exit(-1) | |
AUDIO_URL = u"http://180h.ysts8.com:8000/通俗小说/天才医生/%s.mp3?126550121591x1511075507x126844734345-78f32262977e43f4ef546054" | |
mkdir_p(c.target) | |
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36', | |
} | |
start = 1 | |
end = 1235 | |
for episode in range(start, end): | |
episodeUrl = None | |
if episode >= 1000: | |
episodeUrl = AUDIO_URL %(episode) | |
else: | |
episodeUrl = AUDIO_URL %('{0:03d}'.format(episode)) | |
localFileName = '%s/%s.mp3' %(c.target, '{0:04d}'.format(episode)) | |
print "[%s]Downloading episode %s from url %s\n" %(episode, episode, episodeUrl) | |
download(episodeUrl, localFileName, headers) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment