Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Addresses changes to auth on Madokami #77

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion cum/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import re
import requests
import sys

import http.cookiejar

class BaseConfig(object):
def __init__(self):
Expand Down Expand Up @@ -109,8 +109,15 @@ def write(self):
class MadokamiConfig(object):
def __init__(self, config, dict):
self._config = config
self.cookie_path = os.path.join(cum_dir, 'madokami.cookies')
self.password = dict.get('password', None)
self.username = dict.get('username', None)
try:
self.authenticate()
except exceptions.LoginError:
pass

self.cookies = self.get_cookies()

@property
def login(self):
Expand All @@ -123,6 +130,39 @@ def login(self):
self.password = click.prompt('Madokami password', hide_input=True)
return (self.username, self.password)

def authenticate(self):
"""Attempt to sign in with stored cookies. If cookies are invalid,
expired, or missing, attempt to sign in using basic auth.
"""
url = 'https://manga.madokami.al/'
try:
self.session = requests.Session()
c = self.get_cookies()
r = self.session.get(url, cookies=c)

if r.status_code != 200:
c.clear()
self.set_cookies(c)
raise exceptions.LoginError()

except (FileNotFoundError, exceptions.LoginError) as e:
self.session.auth = requests.auth.HTTPBasicAuth(*self.login)
r = self.session.get(url)
self.set_cookies(r.cookies)

if r.status_code != 200:
raise exceptions.LoginError('Madokami login error')

def get_cookies(self):
jar = http.cookiejar.LWPCookieJar(filename=self.cookie_path)
jar.load(ignore_expires=True,ignore_discard=True)
return jar

def set_cookies(self, cookie):
jar = http.cookiejar.LWPCookieJar(filename=self.cookie_path)
for c in cookie:
jar.set_cookie(c)
jar.save(ignore_expires=True,ignore_discard=True)

def get():
"""Returns the active config object."""
Expand Down
20 changes: 12 additions & 8 deletions cum/scrapers/madokami.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ class MadokamiSeries(BaseSeries):
def __init__(self, url, **kwargs):
super().__init__(url, **kwargs)
self.session = requests.Session()
self.session.auth = requests.auth.HTTPBasicAuth(*config
.get().madokami.login)
r = self.session.get(url)

r = self.session.get(url, cookies=config.get().madokami.cookies)

if r.status_code == 401:
raise exceptions.LoginError('Madokami login error')
self.soup = BeautifulSoup(r.text, config.get().html_parser)
Expand Down Expand Up @@ -80,11 +80,8 @@ def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)

def download(self):
if not self.session.auth:
self.session.auth = requests.auth.HTTPBasicAuth(*config
.get()
.madokami.login)
with closing(self.session.get(self.url, stream=True)) as r:
with closing(self.session.get(self.url,
cookies=config.get().madokami.cookies, stream=True)) as r:
if r.status_code == 401:
raise exceptions.LoginError('Madokami login error')
total_length = r.headers.get('content-length')
Expand All @@ -107,3 +104,10 @@ def from_url(url):
if chapter.url == url:
return chapter
return None

def available(self):
r = requests.head(self.url, cookies=config.get().madokami.cookies)
if r.status_code == 404:
return False
else:
return True
2 changes: 1 addition & 1 deletion cum/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__version__ = '0.9.1'
__version__ = 'git-04c7d86'
__version_name__ = 'Morino Kirin-chan'


Expand Down