Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/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