# -*- coding: utf-8 -*- # Copyright 2011 Fanficdownloader team # # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at # # http://www.apache.org/licenses/LICENSE-2.0 # # Unless required by applicable law or agreed to in writing, software # distributed under the License is distributed on an "AS IS" BASIS, # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. # import datetime import time import logging import fanficdownloader.BeautifulSoup as bs import fanficdownloader.exceptions as exceptions from base_adapter import BaseSiteAdapter, utf8FromSoup, makeDate class TestSiteAdapter(BaseSiteAdapter): def __init__(self, config, url): BaseSiteAdapter.__init__(self, config, url) self.story.setMetadata('siteabbrev','tst1') self.crazystring = u" crazy tests:[bare amp(&) quote(') amp(&) gt(>) lt(<) ATnT(AT&T) pound(£)]" # get storyId from url--url validation guarantees query is only sid=1234 self.story.setMetadata('storyId',self.parsedUrl.query.split('=',)[1]) self.username='' self.is_adult=False @staticmethod def getSiteDomain(): return 'test1.com' def getSiteExampleURLs(self): return "http://"+self.getSiteDomain()+"?sid=1234" def getSiteURLPattern(self): return BaseSiteAdapter.getSiteURLPattern(self)+r'/?\?sid=\d+$' def extractChapterUrlsAndMetadata(self): if self.story.getMetadata('storyId') == '665' and not (self.is_adult or self.getConfig("is_adult")): logging.warn("self.is_adult:%s"%self.is_adult) raise exceptions.AdultCheckRequired(self.url) if self.story.getMetadata('storyId') == '666': raise exceptions.StoryDoesNotExist(self.url) if self.getConfig("username"): self.username = self.getConfig("username") if self.story.getMetadata('storyId') == '668' and self.username != "Me" : raise exceptions.FailedToLogin(self.url,self.username) if self.story.getMetadata('storyId') == '664': self.story.setMetadata(u'title',"Test Story Title "+self.crazystring) else: self.story.setMetadata(u'title',"Test Story Title") self.story.setMetadata('storyUrl',self.url) self.story.setMetadata('description',u'Description '+self.crazystring+u''' Done Some more longer description. "I suck at summaries!" "Better than it sounds!" "My first fic" ''') self.story.setMetadata('datePublished',makeDate("1972-01-31","%Y-%m-%d")) self.story.setMetadata('dateCreated',datetime.datetime.now()) if self.story.getMetadata('storyId') == '669': self.story.setMetadata('dateUpdated',datetime.datetime.now()) else: self.story.setMetadata('dateUpdated',makeDate("1975-01-31","%Y-%m-%d")) self.story.setMetadata('numWords','123456') self.story.setMetadata('status','In-Completed') self.story.setMetadata('rating','Tweenie') self.story.setMetadata('author','Test Author aa') self.story.setMetadata('authorId','98765') self.story.setMetadata('authorUrl','http://author/url') self.story.addToList('warnings','Swearing') self.story.addToList('warnings','Violence') self.story.addToList('category','Harry Potter') self.story.addToList('category','Furbie') self.story.addToList('category','Crossover') self.story.addToList('genre','Fantasy') self.story.addToList('genre','SF') self.story.addToList('genre','Noir') self.chapterUrls = [(u'Prologue '+self.crazystring,self.url+"&chapter=1"), ('Chapter 1, Xenos on Cinnabar',self.url+"&chapter=2"), ('Chapter 2, Sinmay on Kintikin',self.url+"&chapter=3"), ('Chapter 3, Over Cinnabar',self.url+"&chapter=4"), ('Chapter 4',self.url+"&chapter=5"), ('Chapter 5',self.url+"&chapter=6"), ('Chapter 6',self.url+"&chapter=6"), ('Chapter 7',self.url+"&chapter=6"), ('Chapter 8',self.url+"&chapter=6"), ('Chapter 9',self.url+"&chapter=6"), ('Chapter 0',self.url+"&chapter=6"), ('Chapter a',self.url+"&chapter=6"), ('Chapter b',self.url+"&chapter=6"), ('Chapter c',self.url+"&chapter=6"), ('Chapter d',self.url+"&chapter=6"), ('Chapter e',self.url+"&chapter=6"), ('Chapter f',self.url+"&chapter=6"), ('Chapter g',self.url+"&chapter=6"), ('Chapter h',self.url+"&chapter=6"), ('Chapter i',self.url+"&chapter=6"), ('Chapter j',self.url+"&chapter=6"), ('Chapter k',self.url+"&chapter=6"), ('Chapter l',self.url+"&chapter=6"), ('Chapter m',self.url+"&chapter=6"), ('Chapter n',self.url+"&chapter=6"), ] self.story.setMetadata('numChapters',len(self.chapterUrls)) def getChapterText(self, url): logging.debug('Getting chapter text from: %s' % url) if self.story.getMetadata('storyId') == '667': raise exceptions.FailedToDownload("Error downloading Chapter: %s!" % url) if self.story.getMetadata('storyId') == '670': time.sleep(2.0) if "chapter=1" in url : text=u'''
This is a fake adapter for testing purposes. Different storyId's will give different errors:
http://test1.com?sid=664 - Crazy string title
http://test1.com?sid=665 - raises AdultCheckRequired
http://test1.com?sid=666 - raises StoryDoesNotExist
http://test1.com?sid=667 - raises FailedToDownload on chapter 1
http://test1.com?sid=668 - raises FailedToLogin unless username='Me'
http://test1.com?sid=669 - Succeeds with Updated Date=now
http://test1.com?sid=670 - Succeeds, but sleeps 2sec on each chapter
And other storyId will succeed with the same output.
Lorem '''+self.crazystring+''' italics, bold, underline consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
br breaksLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.