From 001f1d5fee2791009bcf521c495abe951f4cae42 Mon Sep 17 00:00:00 2001 From: doe Date: Sat, 9 Aug 2014 03:56:29 +0200 Subject: [PATCH] Removed getHighestWarningLevel, factored out constants --- .../adapters/adapter_fannation.py | 6 --- .../adapters/adapter_themaplebookshelf.py | 4 -- .../adapters/adapter_trekiverseorg.py | 6 --- .../adapters/base_efiction_adapter.py | 49 +++++-------------- 4 files changed, 12 insertions(+), 53 deletions(-) diff --git a/fanficdownloader/adapters/adapter_fannation.py b/fanficdownloader/adapters/adapter_fannation.py index da040594..e9fd8ead 100644 --- a/fanficdownloader/adapters/adapter_fannation.py +++ b/fanficdownloader/adapters/adapter_fannation.py @@ -33,16 +33,10 @@ class FanNationAdapter(BaseEfictionAdapter): def getSiteAbbrev(self): return 'fannation' - @classmethod - def getHighestWarningLevel(self): - return 8 - def handleMetadataPair(self, key, value): if key == 'Romance': for val in re.split("\s*,\s*", value): self.story.addToList('romance', val) - elif key == 'Read': - self.story.setMetadata('readings', value) else: super(FanNationAdapter, self).handleMetadataPair(key, value) diff --git a/fanficdownloader/adapters/adapter_themaplebookshelf.py b/fanficdownloader/adapters/adapter_themaplebookshelf.py index 4cb5e534..1d55deb2 100644 --- a/fanficdownloader/adapters/adapter_themaplebookshelf.py +++ b/fanficdownloader/adapters/adapter_themaplebookshelf.py @@ -36,9 +36,5 @@ class TheMapleBookshelfComSiteAdapter(BaseEfictionAdapter): def getDateFormat(self): return "%b %d, %Y" - @classmethod - def getHighestWarningLevel(self): - return 5 - def getClass(): return TheMapleBookshelfComSiteAdapter diff --git a/fanficdownloader/adapters/adapter_trekiverseorg.py b/fanficdownloader/adapters/adapter_trekiverseorg.py index 0eec94a7..2dc91081 100644 --- a/fanficdownloader/adapters/adapter_trekiverseorg.py +++ b/fanficdownloader/adapters/adapter_trekiverseorg.py @@ -35,10 +35,6 @@ class TrekiverseOrgAdapter(BaseEfictionAdapter): def getSiteAbbrev(cls): return 'trkvs' - @classmethod - def getHighestWarningLevel(cls): - return 4 - @classmethod def getDateFormat(cls): return "%d %b %Y" @@ -50,7 +46,5 @@ class TrekiverseOrgAdapter(BaseEfictionAdapter): def handleMetadataPair(self, key, value): if key == 'Awards': self.story.setMetadata('awards', value) - if key == 'Read': - self.story.setMetadata('readings', value) else: super(TrekiverseOrgAdapter, self).handleMetadataPair(key, value) diff --git a/fanficdownloader/adapters/base_efiction_adapter.py b/fanficdownloader/adapters/base_efiction_adapter.py index ffaf7d2d..118fded9 100644 --- a/fanficdownloader/adapters/base_efiction_adapter.py +++ b/fanficdownloader/adapters/base_efiction_adapter.py @@ -44,6 +44,13 @@ Most of them share common traits: page and cache between extractChapterUrlsAndMetadata and getChapterText """ +# PHP constants +_RUSERSONLY = 'Registered Users Only' +_NOSUCHACCOUNT = "There is no such account on our website" +_WRONGPASSWORD = "That password doesn't match the one in our database" +_USERACCOUNT = 'Member Account' + +# Regular expressions _REGEX_WARING_PARAM = re.compile("warning=(?P\d+)") _REGEX_CHAPTER_B = re.compile("^(?P\d+)\.") _REGEX_CHAPTER_PARAM = re.compile("chapter=(?P\d+)$") @@ -123,10 +130,7 @@ class BaseEfictionAdapter(BaseSiteAdapter): @classmethod def getUrlForPhp(self, php): - return "http://%s%s/%s" % ( - self.getSiteDomain(), - self.getPathToArchive(), - php) + return "http://%s%s/%s" % (self.getSiteDomain(), self.getPathToArchive(), php) @classmethod def getViewStoryUrl(self, storyId): @@ -154,28 +158,28 @@ class BaseEfictionAdapter(BaseSiteAdapter): """ Constant _RUSERSONLY defined in languages/en.php """ - return 'Registered Users Only' + return _RUSERSONLY @classmethod def getMessageThereIsNoSuchAccount(self): """ Constant _NOSUCHACCOUNT defined in languages/en.php """ - return "There is no such account on our website" + return _NOSUCHACCOUNT @classmethod def getMessageWrongPassword(self): """ Constant _WRONGPASSWORD defined in languages/en.php """ - return "That password doesn't match the one in our database" + return _WRONGPASSWORD @classmethod def getMessageMemberAccount(self): """ Constant _USERACCOUNT defined in languages/en.php """ - return 'Member Account' + return _USERACCOUNT ## Login seems to be reasonably standard across eFiction sites. @classmethod @@ -187,35 +191,6 @@ class BaseEfictionAdapter(BaseSiteAdapter): or getMessageThereIsNoSuchAccount in html \ or getMessageWrongPassword in html - @classmethod - def getHighestWarningLevel(cls): - """ - eFiction has a table 'fanfiction_ratings' which contains a list of - ratings with a warningLevel. Every story has a rating. To proceed to a rated - story, the user must either log-in, confirm she's adult or confirm a - warning message, depending on the rating of the story. - - To get a list of possible warning levels on a site, go to the - browse.php page in Chrome, open the Console (F12) and enter - - $$("select[name='rating'] option") - - Choose the highest rating, usually labeled 'R' or 'MA' and use the - value for this method - # - # XXX Unfortunately that doesn't work just that simple. The ratings are not - # in order, i.e. R can have id 3 but PG-13 can have id 8 - # This will give you the options. Trial and Error: Start with the highest - # level and try to open a story with this rating. If you get a - # "Registered Users Only" popup, try it with the next-lower level. When - # you get a regular popup warning, you have the highest warningLevel. - # Set this number as the return value of this function. - # - # Note that the warning confirmation is saved in the session, so you need - # to do it only once when using cookies. - """ - raise NotImplementedError("Must be implemented, please see docstring of getHighestWarningLevel") - def _fetch_to_soup(self, url): """ Fetch a HTML document, fix it and parse it to BeautifulSoup.