Skip to content

Instantly share code, notes, and snippets.

@GreeeenApple
Created October 5, 2016 02:56
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
import requests
import argparse
import os
parser = argparse.ArgumentParser(description="Anitube Downloader")
parser.add_argument("url", nargs="*",
help="anitube url(list). example:http://a http://b")
parser.add_argument("--path", type=str,
default="/home/{user}/Videos/".format(user=os.environ["USERNAME"]),
help="videos save path. default:/home/user/Videos/")
parser.add_argument("--nohd", action="store_false", default=True,
help="video hd quality default=true.")
args = parser.parse_args()
save_path = args.path
HD = args.nohd
conf_url_str = "http://www.anitube.se/player/config.php?key="
for url in args.url:
res = requests.get(url).text
# make conf_url
begin = res.find(conf_url_str)
end = res[begin:].find("\"></script>")
conf_url = res[begin:begin + end]
print "conf_url", conf_url
conf_res = requests.get(conf_url).text
# 720p HD
tmp = conf_res.find("file:") + conf_res[conf_res.find("file:") + 1:].find("file:") # 2nd 'file:' length
begin = tmp + conf_res[tmp:].find("http:")
end = conf_res[begin:].find("\",")
hd_url = conf_res[begin: begin + end]
# 360p SD
tmp = conf_res.find("file:") # 1st 'file:' length
begin = tmp + conf_res[tmp:].find("http:")
end = conf_res[begin:].find("\",")
sd_url = conf_res[begin: begin + end]
if HD:
print "HD"
if HD and (hd_url.encode("utf-8")[0] == "h") and (len(hd_url.split()) == 1) and (hd_url.encode("utf-8")[-1] != " "):
file_url = hd_url
print "file quality HD"
else:
file_url = sd_url
print "file quality SD"
print "file_url", file_url
filename = url.split("/")[-1]
extension = file_url.split(".")[-1]
filename = filename + "." + extension
print "filename", filename
with open(save_path + filename, "wb") as handle:
res = requests.get(file_url, stream=True)
if not res.ok:
print filename, "download error"
exit(1)
for block in res.iter_content(1024 * 1024):
handle.write(block)
print filename, "done."
print save_path + filename
print "all done."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment