Skip to content

Commit

Permalink
Re-structured code and Fixed Login Issue
Browse files Browse the repository at this point in the history
Re-structured code and Fixed Login Issue.
  • Loading branch information
Xonshiz committed May 16, 2019
1 parent af9d09a commit 24d1a38
Show file tree
Hide file tree
Showing 25 changed files with 809 additions and 951 deletions.
4 changes: 3 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,6 @@
- Fix for #39 [2018.01.27]
- Fix for #46 [2018.01.29]
- Fix for #45 [2018.01.29]
- Temp fix for login #65, #66 [2018.10.11]
- Temp fix for login #65, #66 [2018.10.11]
- Login Issue Fixed [2019.05.16]
- Re-structured the code for better maintainance and re-usability. [2019.05.16]
19 changes: 11 additions & 8 deletions anime_dl/AnimeDL.py → anime_dl/Anime_dl.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@ def __init__(self, url, username, password, resolution, language, skipper, logge
exit()
else:

sites.crunchyroll.CrunchyRoll(
sites.crunchyroll.Crunchyroll(
url=url[0], password=password, username=username, resolution=resolution, language=language,
skipper=skipper, logger=logger, episode_range=episode_range)

elif website == "VRV":
print("Under development...")
print("Not Implemented")
exit(1)
# if not url[0] or not username[0] or not password[0]:
# print("Please enter the required arguments. Run __main__.py --help")
# exit()
Expand All @@ -39,11 +40,13 @@ def __init__(self, url, username, password, resolution, language, skipper, logge
# sites.vrv.Vrv(url=url, password=password, username=username, resolution=resolution)

elif website == "Funimation":
if not url[0] or not username[0] or not password[0]:
print("Please enter the required arguments. Run __main__.py --help")
exit()
else:
sites.funimation.Funimation(url[0], username, password, resolution, language)
print("Not Implemented")
exit(1)
# if not url[0] or not username[0] or not password[0]:
# print("Please enter the required arguments. Run __main__.py --help")
# exit()
# else:
# sites.funimation.Funimation(url[0], username, password, resolution, language)

def honcho(self, url):
# Verify that we have a sane url and return which website it belongs
Expand All @@ -67,4 +70,4 @@ def honcho(self, url):
return "Crunchyroll"

elif domain in ["www.vrv.co", "vrv.co"]:
return "VRV"
return "VRV"
4 changes: 3 additions & 1 deletion anime_dl/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
import common
import external
import sites
import external

20 changes: 9 additions & 11 deletions anime_dl/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,19 @@

"""
__author__ = "Xonshiz"
__email__ = "xonshiz@psychoticelites.com"
__email__ = "xonshiz@gmail.com"
"""
from AnimeDL import *
# from AnimeDL import *
# from anime_dl import AnimeDL
from Anime_dl import *
from sys import exit
from version import __version__
import argparse
import logging
import platform


class main():
class Main():
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Anime_DL downloads anime from CrunchyRoll and Funimation.')
parser = argparse.ArgumentParser(description='anime_dl downloads anime from CrunchyRoll and Funimation.')

parser.add_argument('--version', action='store_true', help='Shows version and exits.')

Expand All @@ -39,14 +37,14 @@ class main():
if args.verbose:
logging.basicConfig(format='%(levelname)s: %(message)s', filename="Error Log.log", level=logging.DEBUG)
logging.debug('You have successfully set the Debugging On.')
logging.debug("Arguments Provided : %s" % (args))
logging.debug("Arguments Provided : {0}".format(args))
logging.debug(
"Operating System : %s - %s - %s" % (platform.system(), platform.release(), platform.version()))
logging.debug("Python Version : %s (%s)" % (platform.python_version(), platform.architecture()[0]))
"Operating System : {0} - {1} - {2}".format(platform.system(), platform.release(), platform.version()))
logging.debug("Python Version : {0} ({1})".format(platform.python_version(), platform.architecture()[0]))
logger = "True"

if args.version:
print("Current Version : %s" % __version__)
print("Current Version : {0}".format(__version__))
exit()

if args.skip:
Expand All @@ -72,4 +70,4 @@ class main():

AnimeDL(url=args.input, username=args.username, password=args.password,
resolution=args.resolution, language=args.language, skipper=skipper,
logger=logger, episode_range=args.range)
logger=logger, episode_range=args.range)
44 changes: 0 additions & 44 deletions anime_dl/animeName.py

This file was deleted.

3 changes: 3 additions & 0 deletions anime_dl/common/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import browser_instance
import downloader
import misc
95 changes: 95 additions & 0 deletions anime_dl/common/browser_instance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from cfscrape import create_scraper
from requests import session
from bs4 import BeautifulSoup
import re


def page_downloader(url, scrapper_delay=5, **kwargs):
headers = kwargs.get("headers")
if not headers:
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',
'Accept-Encoding': 'gzip, deflate'
}

sess = session()
sess = create_scraper(sess, delay=scrapper_delay)

connection = sess.get(url, headers=headers, cookies=kwargs.get("cookies"))

if connection.status_code != 200:
print("Whoops! Seems like I can't connect to website.")
print("It's showing : %s" % connection)
print("Run this script with the --verbose argument and report the issue along with log file on Github.")
# raise Warning("can't connect to website %s" % manga_url)
return False, None, None
else:
page_source = BeautifulSoup(connection.text.encode("utf-8"), "html.parser")
connection_cookies = sess.cookies

return True, page_source, connection_cookies


def login_crunchyroll(url, username, password, country):
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/' + country + 'login'
}

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

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")
initial_cookies = sess.cookies
csrf_token = re.search(r'login_form\[\_token\]\"\ value\=\"(.*?)\"',
str(initial_page_source)).group(1)

payload = {
'login_form[name]': '%s' % username,
'login_form[password]': '%s' % password,
'login_form[redirect_url]': '/',
'login_form[_token]': '%s' % csrf_token
}

login_post = sess.post(
url='https://www.crunchyroll.com/' + country + 'login',
data=payload,
headers=headers,
cookies=initial_cookies)

login_check_response, login_cookies = login_check(html_source=login_post.text.encode('utf-8'), cookies=login_post.cookies)
if login_check_response:
print("Logged in successfully...")
return True, login_cookies, csrf_token
else:
print("Unable to Log you in. Check credentials again.")
return False, None, None
else:
# print("Couldn't connect to the login page...")
# print("Website returned : %s" % str(initial_page_fetch.status_code))
return False, None, None


def login_check(html_source, cookies=None):
# Open the page and check the title. CrunchyRoll redirects the user and the title has the text "Redirecting...".
# If this is not found, you're probably not logged in and you'll just get 360p or 480p.
if "href=\"/logout\"" in html_source:
return True, cookies
else:
print("Let me check again...")
second_try_response, html_source, cookies = page_downloader(url="https://www.crunchyroll.com/", cookies=cookies)
if second_try_response:
if "href=\"/logout\"" in html_source:
return True, cookies
else:
return False, cookies
10 changes: 4 additions & 6 deletions anime_dl/downloader.py → anime_dl/common/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
from tqdm import tqdm


class downloader(object):
class Downloader(object):

def File_Downloader(self, ddl, fileName, referer, cookies):
def file_downloader(self, ddl, file_name, referer, cookies):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36',
'Territory': 'US',
Expand All @@ -19,9 +19,7 @@ def File_Downloader(self, ddl, fileName, referer, cookies):
sess = create_scraper(sess)

dlr = sess.get(ddl, stream=True, cookies = cookies, headers = headers) # Downloading the content using python.
with open(fileName, "wb") as handle:
with open(file_name, "wb") as handle:
for data in tqdm(dlr.iter_content(chunk_size=1024)): # Added chunk size to speed up the downloads
handle.write(data)
print("Download has been completed.") # Viola

# coding: utf8
print("Download has been completed.") # Viola
9 changes: 9 additions & 0 deletions anime_dl/common/misc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-


def duplicate_remover(seq):
# https://stackoverflow.com/a/480227
seen = set()
seen_add = seen.add
return [x for x in seq if not (x in seen or seen_add(x))]
8 changes: 4 additions & 4 deletions anime_dl/external/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import external.aes
import external.compat
import external.socks
import external.utils
import aes
import compat
import socks
import utils
2 changes: 1 addition & 1 deletion anime_dl/external/aes.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,4 +330,4 @@ def inc(data):
return data


__all__ = ['aes_encrypt', 'key_expansion', 'aes_ctr_decrypt', 'aes_cbc_decrypt', 'aes_decrypt_text']
__all__ = ['aes_encrypt', 'key_expansion', 'aes_ctr_decrypt', 'aes_cbc_decrypt', 'aes_decrypt_text']
2 changes: 1 addition & 1 deletion anime_dl/external/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -2928,4 +2928,4 @@ def compat_struct_unpack(spec, *args):
'compat_xml_parse_error',
'compat_xpath',
'workaround_optparse_bug9161',
]
]
2 changes: 1 addition & 1 deletion anime_dl/external/socks.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,4 +269,4 @@ def connect(self, address):
self._make_proxy(socket.socket.connect, address)

def connect_ex(self, address):
return self._make_proxy(socket.socket.connect_ex, address)
return self._make_proxy(socket.socket.connect_ex, address)
2 changes: 1 addition & 1 deletion anime_dl/external/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3574,4 +3574,4 @@ def write_xattr(path, key, value):
raise XAttrUnavailableError(
"Couldn't find a tool to set the xattrs. "
"Install either the python 'xattr' module, "
"or the 'xattr' binary.")
"or the 'xattr' binary.")
4 changes: 1 addition & 3 deletions anime_dl/sites/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import sites.crunchyroll
import sites.funimation
import sites.vrv
import crunchyroll
Loading

0 comments on commit 24d1a38

Please sign in to comment.