From d809ac9f6c9a64e6d4effc43fbe23265ca6e848b Mon Sep 17 00:00:00 2001 From: Dmitry Kozliuk Date: Thu, 23 Jul 2015 02:47:49 +0300 Subject: [PATCH] Prevent capturing chapters from related stories. When prequel or sequel is posted, editors sometimes make `Next/Previous chapter' links between them. Since it is the only mechanism for chapter detection, additional title check was added. It is heuristical and may need improvement one day. --- fanficfare/adapters/adapter_masseffect2in.py | 24 ++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/fanficfare/adapters/adapter_masseffect2in.py b/fanficfare/adapters/adapter_masseffect2in.py index dd8444b..73047ab 100644 --- a/fanficfare/adapters/adapter_masseffect2in.py +++ b/fanficfare/adapters/adapter_masseffect2in.py @@ -104,6 +104,10 @@ class MassEffect2InAdapter(BaseSiteAdapter): if url: url = self._makeDocumentUrl(self._getDocumentId(url)) following = self._makeChapter(url) + # Do not follow links to related, but different stories (prequels or sequels). + startingStoryTitle = self.story.getMetadata('title') + if not following.isFromStory(startingStoryTitle): + return if forward: yield following for chapter in followChapters(following, forward): @@ -399,6 +403,26 @@ class Chapter(object): Returns a list of chapters' URLs.""" return self._getSiblingChapterUrl({'class': 'tar fr'}) + def isFromStory(self, storyTitle, prefixThreshold=-1): + """Checks if this chapter is from a story different from the given one. + Prefix threshold specifies how long common story title prefix shall be + for chapters from one story: negative value means implementation-defined + optimum, zero inhibits the check, and positive value adjusts threshold.""" + + def getFirstWord(string): + match = re.search(u'^\s*\w+', string, re.UNICODE) + return string[match.start():match.end()] + + thisStoryTitle = self.getStoryTitle() + if prefixThreshold != 0: + if prefixThreshold < 0: + prefixThreshold = min( + len(getFirstWord(storyTitle)), len(getFirstWord(thisStoryTitle))) + else: + prefixThreshold = min( + prefixThreshold, len(storyTitle), len(thisStoryTitle)) + result = len(_getLargestCommonPrefix(storyTitle, thisStoryTitle)) >= prefixThreshold + return result else: return storyTitle != thisStoryTitle