Created
August 13, 2016 05:14
-
-
Save busterb/e6365b175fb8199eca1762552453252a to your computer and use it in GitHub Desktop.
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 | |
import os, sys, urllib2, sgmllib, time, random, subprocess, glob | |
class MyParser(sgmllib.SGMLParser): | |
def parse(self, s): | |
self.inside_title = False | |
self.title = "" | |
self.feed(s) | |
self.close() | |
def __init__(self, verbose=0): | |
sgmllib.SGMLParser.__init__(self, verbose) | |
self.hyperlinks = [] | |
def start_a(self, attributes): | |
for name, value in attributes: | |
if name == "href" and value.find('episode') >= 0: | |
#if name == "href" and value.find('okonomiyaki') >= 0: | |
self.hyperlinks.append(value) | |
def handle_data(self, data): | |
if self.inside_title and self.title == "": | |
self.title = data | |
def start_title(self, attributes): | |
self.inside_title = True | |
def end_title(self): | |
self.inside_title = False | |
def get_hyperlinks(self): | |
return self.hyperlinks | |
def get_title(self): | |
return ' '.join(self.title.split()[2:]).split("Full episodes")[0].strip() | |
if len(sys.argv) < 2: | |
print("usage %s [series]" % sys.argv[0]) | |
sys.exit(1) | |
series = sys.argv[1] | |
url = "http://www.crunchyroll.com/%s/videos" % series | |
f = urllib2.urlopen(url) | |
s = f.read() | |
myparser = MyParser() | |
myparser.parse(s) | |
title = myparser.get_title() | |
links = myparser.get_hyperlinks() | |
try: | |
os.mkdir(title) | |
except: | |
pass | |
dls = [] | |
for link in links: | |
id = '-'.join(link.split('-')[-2:]) | |
pattern = "%s/*%s*" % (title, id) | |
if len(glob.glob(pattern)) == 0: | |
dls.append(subprocess.Popen(["youtube-dl", | |
"https://www.crunchyroll.com%s" % link], cwd = title)) | |
for dl in dls: | |
dl.wait() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment