Skip to content

Instantly share code, notes, and snippets.

@NuarkNoir
Created January 24, 2019 07:29
Show Gist options
  • Save NuarkNoir/678016cd3dcffe4e6861379d56dbce7f to your computer and use it in GitHub Desktop.
Save NuarkNoir/678016cd3dcffe4e6861379d56dbce7f to your computer and use it in GitHub Desktop.
anilandorg anime downloader
import requests
import lxml.html
from collections import OrderedDict
import os
from tqdm import tqdm
cls = lambda: os.system('cls' if os.name=='nt' else 'clear')
safe_string = lambda s: ("".join(x for x in s if (x.isalnum() or x in "._-[] "))).strip()
def get_data():
aid = input("Input anime id >>> ").strip()
html = requests.get("http://aniland.org/group.php?groupid={}".format(aid)).text
dom = lxml.html.fromstring(html)
return OrderedDict({x.attrib["id"][3:]: x.text_content() for x in dom.cssselect(".but_cell")})
def get_episodes(seasonid):
html = requests.get("http://aniland.org/content.php?contentid={}".format(seasonid)).text
dom = lxml.html.fromstring(html)
return [(x.text_content(), *x.attrib["onclick"].split("videoUpdate(")[1].split(")\"")[0].split(",")) for x in dom.cssselect("div.but")]
def download_series(title, series_data):
cls()
path = os.path.join(".", "downloads", safe_string(title))
if not os.path.exists(path):
os.makedirs(path)
for pt, sid, content, num in series_data:
html = requests.get("http://aniland.org/video.php?id={}&content={}&num=1".format(sid, content, num)).text
dom = lxml.html.fromstring(html)
sources = [x.attrib["src"] for x in dom.cssselect("source")]
p720 = sources[0] if "/720/" in sources[0] else sources[1]
p480 = sources[0] if not "/720/" in sources[0] else sources[1]
fname = "{} - {}.mp4".format(title, pt)
print("Downloading `{}`...".format(fname), end=" ")
print("trying 720p...", end=" ")
r = requests.get(p720, stream=True)
if not r.status_code == 200:
print("480p...", end=" ")
r = requests.get(p480, stream=True)
if not r.status_code == 200:
print("failed")
continue
tdp = os.path.join(path, fname)
clen = int(r.headers["Content-Length"])
flen = 0
if os.path.exists(tdp):
flen = os.path.getsize(tdp)
if clen > flen:
print()
with open(tdp, 'wb') as f:
for chunk in tqdm(r.iter_content(1024), total=clen // 1024, unit="bytes"):
f.write(chunk)
else:
print("already done")
def main():
mm_cmd = ""
while not mm_cmd == "e":
data = get_data()
cls()
anichose_cmd = ""
while not anichose_cmd == "e":
idtopos = {i: e for i, e in enumerate(list(data.keys()))}
for i in idtopos:
e = idtopos[i]
print("{}. {}".format(i, data[e]))
anichose_cmd = input("Choose (or [e] to exit) >>> ")
cls()
if not anichose_cmd.isnumeric() or not int(anichose_cmd) in idtopos:
continue
e = idtopos[int(anichose_cmd)]
print("Title: {}".format(data[e]))
print("Loading episodes...", end=" ")
episodes = get_episodes(e)
print("done, got {} episode/s".format(len(episodes)))
start_dwn_cmd = input("Start downloading? (anything to exit or [y] to start) >>> ").strip()
if start_dwn_cmd == "y":
download_series(data[e], episodes)
mm_cmd = input("Anything else? ([any key] to continue or [e] to exit) >>> ").strip()
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment