def tryParseUrl(self, url: str) -> Optional[FicId]: # by default, we simply try to look up the url in existing chapters or fics chaps = FicChapter.select({'url': url}) if len(chaps) == 1: fic = Fic.get((chaps[0].ficId, )) if fic is not None: return FicId( FicType(fic.sourceId), fic.localId, chaps[0].chapterId, False ) fics = Fic.select({'url': url}) if len(fics) == 1: return FicId(FicType(fics[0].sourceId), fics[0].localId) raise NotImplementedError()
def tryParseUrl(self, url: str) -> Optional[FicId]: if not url.startswith(self.baseUrl): return None # by default, we simply try to look up the url in existing chapters or fics chaps = FicChapter.select({'url': url}) if len(chaps) == 1: fic = Fic.get((chaps[0].ficId, )) if fic is not None: ftype = FicType(fic.sourceId) return FicId(ftype, fic.localId, chaps[0].chapterId, False) fics = Fic.select({'url': url}) if len(fics) == 1: ftype = FicType(fics[0].sourceId) return FicId(ftype, fics[0].localId) leftover = url[len(self.baseUrl):] if not leftover.endswith('.html'): return None ps = leftover.split('/') if len(ps) != 3 or ps[0] != 'authors': return None author = ps[1] storyId = ps[2] suffixes = ['01a.html', '.html'] for suffix in suffixes: if storyId.endswith(suffix): storyId = storyId[:-len(suffix)] # note: seems to be safe to lowercase these lid = (author + '/' + storyId).lower() #print(lid) # make lid author/story ? # TODO: we need some sort of local lid mapping... raise NotImplementedError()
def __tryParse(ident: str) -> Optional['FicId']: if len(ident.strip()) < 1: return None # strip view-source from potential urls if ident.startswith('view-source:http'): ident = ident[len('view-source:'):] # guess url next if ident.startswith('http'): return FicId.tryParseUrl(ident) # check for link{site}(id) style idents for ftype in adapters: a = adapters[ftype] if a is None: continue if a.botLinkSuffix is None: continue l = 'link{}('.format(a.botLinkSuffix) if ident.startswith(l) and ident.endswith(')'): mid = ident[len(l):-1] if not mid.isnumeric(): return FicId.tryParse(ident) return FicId(ftype, mid, ambiguous=False) # maybe it's an actual story id from store import Fic parts = ident.split('/') if parts[0].isnumeric(): fic = Fic.get((int(parts[0]), )) if fic is not None: fid = fic.fid() if len(parts) == 2 and parts[1].isnumeric(): fid.chapterId = int(parts[1]) fid.ambiguous = False return fid # or maybe it's a url id... potential = Fic.select({'urlId': parts[0]}) if len(potential) == 1: fid = potential[0].fid() if len(parts) == 2 and parts[1].isnumeric(): fid.chapterId = int(parts[1]) fid.ambiguous = False return fid # assume numeric is ffnet if ident.isnumeric(): return FicId(FicType.ff_net, ident) # guess story/chapter on ffnet if len(parts) == 2 and parts[1].isnumeric(): cid = int(parts[1]) return FicId(FicType.ff_net, parts[0], cid, ambiguous=False) # try prepending https protocol if ident.find('://') < 0: ident = 'https://' + ident return FicId.tryParseUrl(ident) # just guess manual adapter return FicId.tryParseFallback(ident)