Skip to content

Commit

Permalink
Crunchyroll fix #74
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicnl committed Jan 22, 2019
1 parent 754a32d commit 632a6a1
Showing 1 changed file with 30 additions and 17 deletions.
47 changes: 30 additions & 17 deletions anime_dl/sites/crunchyroll.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

'''This code Stinx. I'll write a better, faster and compact code when I get time after my exams or in mid.
I literally have NO idea what I was thinking when I wrote this piece of code.
THIS REALLY STINX! Read the code at your own risk.
THIS REALLY STINX!
Also, some strangers went here and wrote some more code that REALLY REALLY STINX.
Read the code at your own risk.
'''


Expand All @@ -37,24 +39,34 @@ def __init__(self, url, password, username, resolution, language, skipper, logge
logging.basicConfig(format='%(levelname)s: %(message)s', filename="Error Log.log", level=logging.DEBUG,
encoding="utf-8")

Crunchy_Show_regex = r'https?://(?:(?P<prefix>www|m)\.)?(?P<url>crunchyroll\.com/(?!(?:news|anime-news|library|forum|launchcalendar|lineup|store|comics|freetrial|login))(?P<id>[\w\-]+))/?(?:\?|$)'
Crunchy_Video_regex = r'https?:\/\/(?:(?P<prefix>www|m)\.)?(?P<url>crunchyroll\.(?:com|fr)/(?:media(?:-|/\?id=)|[^/]*/[^/?&]*?)(?P<video_id>[0-9]+))(?:[/?&]|$)'
# Extract the language from the input URL
Crunchy_Language = re.search(r'.+/([a-z]{2})/.+', url)
if not Crunchy_Language:
print("Could not extract the language from the URL")
return

Crunchy_Language = Crunchy_Language.group(1)


Crunchy_Show_regex = r'https?://(?:(?P<prefix>www|m)\.)?(?P<url>crunchyroll\.com/[a-z]{2}/(?!(?:news|anime-news|library|forum|launchcalendar|lineup|store|comics|freetrial|login))(?P<id>[\w\-]+))/?(?:\?|$)'
Crunchy_Video_regex = r'https?:\/\/(?:(?P<prefix>www|m)\.)?(?P<url>crunchyroll\.(?:com|fr)/[a-z]{2}/(?:media(?:-|/\?id=)|[^/]*/[^/?&]*?)(?P<video_id>[0-9]+))(?:[/?&]|$)'

Crunchy_Show = re.match(Crunchy_Show_regex, url)
Crunchy_Video = re.match(Crunchy_Video_regex, url)

if Crunchy_Video:
cookies, Token = self.webpagedownloader(url=url, username=username[0], password=password[0])
cookies, Token = self.webpagedownloader(url=url, username=username[0], password=password[0], country=Crunchy_Language)
if skipper == "yes":
self.onlySubs(url=url, cookies=cookies)
else:
self.singleEpisode(
url=url, cookies=cookies, token=Token, resolution=resolution)
elif Crunchy_Show:

cookies, Token = self.webpagedownloader(url=url, username=username[0], password=password[0])
cookies, Token = self.webpagedownloader(url=url, username=username[0], password=password[0], country=Crunchy_Language)
self.wholeShow(url=url, cookie=cookies, token=Token, language=language, resolution=resolution,
skipper=skipper, episode_range=episode_range)
else:
print("URL does not look like a show or a video, stopping.")

def login_check(self, htmlsource):
# Open the page and check the title. CrunchyRoll redirects the user and the title has the text "Redirecting...".
Expand All @@ -68,20 +80,20 @@ def login_check(self, htmlsource):
# return False
return True

def webpagedownloader(self, url, username, password):
def webpagedownloader(self, url, username, password, country='fr'):

headers = {
'User-Agent':
'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
'Referer':
'https://www.crunchyroll.com/login'
'https://www.crunchyroll.com/' + country + '/login'
}

sess = requests.session()
sess = cfscrape.create_scraper(sess)
print("Trying to login...")

initial_page_fetch = sess.get(url='https://www.crunchyroll.com/login', headers=headers)
initial_page_fetch = sess.get(url='https://www.crunchyroll.com/' + country + '/login', headers=headers)

if initial_page_fetch.status_code == 200:
initial_page_source = initial_page_fetch.text.encode("utf-8")
Expand All @@ -97,7 +109,7 @@ def webpagedownloader(self, url, username, password):
}

login_post = sess.post(
url='https://www.crunchyroll.com/login',
url='https://www.crunchyroll.com/' + country + '/login',
data=payload,
headers=headers,
cookies=initial_cookies)
Expand Down Expand Up @@ -184,21 +196,21 @@ def singleEpisode(self, url, cookies, token, resolution):
xml_page = xml_page_connect.text.encode("utf-8")

try:
m3u8_file_link = str(re.search(r'<file>(.*?)</file>', xml_page).group(1)).replace("&amp;", "&")
m3u8_file_link = str(re.search(r'<file>(.*?)</file>', xml_page.decode("utf-8")).group(1)).replace("&amp;", "&")
logging.debug("m3u8_file_link : %s", m3u8_file_link)

if not m3u8_file_link:
# If no m3u8 found, try the rtmpdump...
try:
host_link = re.search(r'<host>(.*?)</host>', xml_page).group(1)
host_link = re.search(r'<host>(.*?)</host>', xml_page.decode("utf-8")).group(1)
logging.debug("Found RTMP DUMP!")
print("RTMP streams not supported currently...")
except Exception as NoRtmpDump:
print("No RTMP Streams Found...")
print(NoRtmpDump)
else:
anime_name = re.sub(r'[^A-Za-z0-9\ \-\' \\]+', '', str(
re.search(r'<series_title>(.*?)</series_title>', xml_page).group(1))).title().strip()
re.search(r'<series_title>(.*?)</series_title>', xml_page.decode("utf-8")).group(1))).title().strip()
episode_number = re.search(r'<episode_number>(.*?)</episode_number>',
xml_page.decode("utf-8")).group(1)
video_width = re.search(r'<width>(.*?)</width>', xml_page.decode("utf-8")).group(1)
Expand Down Expand Up @@ -229,7 +241,7 @@ def singleEpisode(self, url, cookies, token, resolution):
pass
else:
self.subFetcher(
xml=str(xml_page),
xml=str(xml_page.decode("utf-8")),
episode_number=episode_number,
file_name=file_name)

Expand Down Expand Up @@ -454,6 +466,7 @@ def subFetcher(self, xml, episode_number, file_name):
'https://www.crunchyroll.com'
}


sess = requests.session()
sess = cfscrape.create_scraper(sess)
for sub_id, sub_lang, sub_lang2 in re.findall(
Expand Down Expand Up @@ -514,9 +527,9 @@ def onlySubs(self, url, cookies):
video_id, url)
xml_page = sess.get(url=infoURL, headers=headers, cookies=cookies).text.encode("utf-8")

# anime_name = re.search(r'<series_title>(.*?)</series_title>', xml_page).group(1)
# anime_name = re.search(r'<series_title>(.*?)</series_title>', xml_page.decode("utf-8")).group(1)
anime_name = re.sub(r'[^A-Za-z0-9\ \-\' \\]+', '',
str(re.search(r'<series_title>(.*?)</series_title>', xml_page).group(1))).title().strip()
str(re.search(r'<series_title>(.*?)</series_title>', xml_page.decode("utf-8")).group(1))).title().strip()

episode_number = re.search(r'<episode_number>(.*?)</episode_number>', xml_page.decode("utf-8")).group(1)
video_width = re.search(r'<width>(.*?)</width>', xml_page.decode("utf-8")).group(1)
Expand All @@ -535,7 +548,7 @@ def onlySubs(self, url, cookies):
if not os.path.exists(output_directory):
os.makedirs(output_directory)

self.subFetcher(xml=xml_page, episode_number=episode_number, file_name=file_name)
self.subFetcher(xml=xml_page.decode('utf-8'), episode_number=episode_number, file_name=file_name)

for sub_file in glob("*.ass"):
try:
Expand Down

0 comments on commit 632a6a1

Please sign in to comment.