From 7208cfdaaf807feccd45013a9b2c9a687185a3dc Mon Sep 17 00:00:00 2001 From: David Lynch Date: Tue, 15 Oct 2019 11:14:27 -0500 Subject: [PATCH] Minor readability improvement: use f-strings --- ebook/__init__.py | 8 ++++---- sites/__init__.py | 8 ++++---- sites/ao3.py | 12 ++++++------ sites/fictionlive.py | 6 +++--- sites/royalroad.py | 2 +- sites/xenforo.py | 4 ++-- sites/xenforo2.py | 2 +- 7 files changed, 21 insertions(+), 21 deletions(-) diff --git a/ebook/__init__.py b/ebook/__init__.py index a9f40ee..07ed528 100644 --- a/ebook/__init__.py +++ b/ebook/__init__.py @@ -76,19 +76,19 @@ class CoverOptions: def chapter_html(story, titleprefix=None): chapters = [] for i, chapter in enumerate(story): - title = chapter.title or '#{}'.format(i) + title = chapter.title or f'#{i}' if hasattr(chapter, '__iter__'): # This is a Section chapters.extend(chapter_html(chapter, titleprefix=title)) else: - title = titleprefix and '{}: {}'.format(titleprefix, title) or title + title = titleprefix and f'{titleprefix}: {title}' or title chapters.append(( title, - '{}/chapter{}.html'.format(story.id, i + 1), + f'{story.id}/chapter{i + 1}.html', html_template.format(title=title, text=chapter.contents) )) if story.footnotes: - chapters.append(("Footnotes", '{}/footnotes.html'.format(story.id), html_template.format(title="Footnotes", text='\n\n'.join(story.footnotes)))) + chapters.append(("Footnotes", f'{story.id}/footnotes.html', html_template.format(title="Footnotes", text='\n\n'.join(story.footnotes)))) return chapters diff --git a/sites/__init__.py b/sites/__init__.py index 6ec9c87..cd90332 100644 --- a/sites/__init__.py +++ b/sites/__init__.py @@ -164,14 +164,14 @@ class Site: # epub spec footnotes are all about epub:type on the footnote and the link # http://www.idpf.org/accessibility/guidelines/content/semantics/epub-type.php contents.name = 'div' - contents.attrs['id'] = "footnote{}".format(idx) + contents.attrs['id'] = f'footnote{idx}' contents.attrs['epub:type'] = 'rearnote' # a backlink is essential for Kindle to think of this as a footnote # otherwise it doesn't get the inline-popup treatment # http://kindlegen.s3.amazonaws.com/AmazonKindlePublishingGuidelines.pdf # section 3.9.10 - backlink = self._new_tag('a', href="chapter{}.html#noteback{}".format(chapterid, idx)) + backlink = self._new_tag('a', href=f'chapter{chapterid}.html#noteback{idx}') backlink.string = '^' contents.insert(0, backlink) @@ -181,8 +181,8 @@ class Site: # epub annotations. spoiler_link = self._new_tag('a') spoiler_link.attrs = { - 'id': 'noteback{}'.format(idx), - 'href': "footnotes.html#footnote{}".format(idx), + 'id': f'noteback{idx}', + 'href': f'footnotes.html#footnote{idx}', 'epub:type': 'noteref', } spoiler_link.string = str(idx) diff --git a/sites/ao3.py b/sites/ao3.py index 450b04b..1b414e2 100644 --- a/sites/ao3.py +++ b/sites/ao3.py @@ -48,7 +48,7 @@ class ArchiveOfOurOwn(Site): def _extract_work(self, workid): # Fetch the full work - url = 'http://archiveofourown.org/works/{}?view_adult=true&view_full_work=true'.format(workid) + url = f'http://archiveofourown.org/works/{workid}?view_adult=true&view_full_work=true' logger.info("Extracting full work @ %s", url) soup = self._soup(url) @@ -56,11 +56,11 @@ class ArchiveOfOurOwn(Site): title=soup.select('#workskin > .preface .title')[0].text.strip(), author=soup.select('#workskin .preface .byline a')[0].text.strip(), summary=soup.select('#workskin .preface .summary blockquote')[0].prettify(), - url='http://archiveofourown.org/works/{}'.format(workid) + url=f'http://archiveofourown.org/works/{workid}' ) # Fetch the chapter list as well because it contains info that's not in the full work - nav_soup = self._soup('https://archiveofourown.org/works/{}/navigate'.format(workid)) + nav_soup = self._soup(f'https://archiveofourown.org/works/{workid}/navigate') for index, chapter in enumerate(nav_soup.select('#main ol[role="navigation"] li')): link = chapter.find('a') @@ -73,7 +73,7 @@ class ArchiveOfOurOwn(Site): story.add(Chapter( title=link.string, - contents=self._chapter(soup.find(id='chapter-{}'.format(index + 1))), + contents=self._chapter(soup.find(id=f'chapter-{index + 1}')), date=updated )) @@ -109,12 +109,12 @@ class ArchiveOfOurOwnSeries(ArchiveOfOurOwn): def extract(self, url): seriesid = re.match(r'^https?://archiveofourown\.org/series/(\d+)/?.*', url).group(1) - soup = self._soup('http://archiveofourown.org/series/{}?view_adult=true'.format(seriesid)) + soup = self._soup(f'http://archiveofourown.org/series/{seriesid}?view_adult=true') story = Section( title=soup.select('#main h2.heading')[0].text.strip(), author=soup.select('#main dl.series.meta a[rel="author"]')[0].string, - url='http://archiveofourown.org/series/{}'.format(seriesid) + url=f'http://archiveofourown.org/series/{seriesid}' ) for work in soup.select('#main ul.series li.work'): diff --git a/sites/fictionlive.py b/sites/fictionlive.py index fc2cdd6..c936dd8 100644 --- a/sites/fictionlive.py +++ b/sites/fictionlive.py @@ -22,7 +22,7 @@ class FictionLive(Site): def extract(self, url): workid = re.match(r'^https?://fiction\.live/stories/[^\/]+/([0-9a-zA-Z]+)/?.*', url).group(1) - response = self.session.get('https://fiction.live/api/node/{}'.format(workid)).json() + response = self.session.get(f'https://fiction.live/api/node/{workid}').json() story = Section( title=response['t'], @@ -42,7 +42,7 @@ class FictionLive(Site): # https://fiction.live/api/anonkun/chapters/SBBA49fQavNQMWxFT/1449266444062/1449615394752 # https://fiction.live/api/anonkun/chapters/SBBA49fQavNQMWxFT/1502823848216/9999999999999998 # i.e. format is [current timestamp] / [next timestamp - 1] - chapter_url = 'https://fiction.live/api/anonkun/chapters/{}/{}/{}'.format(workid, currc['ct'], nextc['ct'] - 1) + chapter_url = f'https://fiction.live/api/anonkun/chapters/{workid}/{currc["ct"]}/{nextc["ct"] - 1}' logger.info("Extracting chapter \"%s\" @ %s", currc['title'], chapter_url) data = self.session.get(chapter_url).json() html = [] @@ -70,7 +70,7 @@ class FictionLive(Site): choices.sort(reverse=True) html.append('

') story.add(Chapter( diff --git a/sites/royalroad.py b/sites/royalroad.py index 9c5c9bf..8085edc 100644 --- a/sites/royalroad.py +++ b/sites/royalroad.py @@ -23,7 +23,7 @@ class RoyalRoad(Site): def extract(self, url): workid = re.match(r'^https?://(?:www\.)?%s\.com/fiction/(\d+)/?.*' % self.domain, url).group(1) - soup = self._soup('https://www.{}.com/fiction/{}'.format(self.domain, workid)) + soup = self._soup(f'https://www.{self.domain}.com/fiction/{workid}') # should have gotten redirected, for a valid title original_maxheaders = http.client._MAXHEADERS diff --git a/sites/xenforo.py b/sites/xenforo.py index 8c0967c..35a77ba 100644 --- a/sites/xenforo.py +++ b/sites/xenforo.py @@ -178,7 +178,7 @@ class XenForo(Site): # Note: the fetched threadmarks can contain more placeholder elements to fetch. Ergo, loop. # Good test case: https://forums.sufficientvelocity.com/threads/ignition-mtg-multicross-planeswalker-pc.26099/threadmarks # e.g.:
  • - response = self.session.post('https://{}/index.php?threads/threadmarks/load-range'.format(self.domain), data={ + response = self.session.post(f'https://{self.domain}/index.php?threads/threadmarks/load-range', data={ # I did try a fetch on min/data-min+data-max, but there seems # to be an absolute limit which the API fetch won't override 'min': fetcher.get('data-range-min'), @@ -275,7 +275,7 @@ class XenForo(Site): link.string = spoiler_title.get_text() else: if spoiler_title: - link = '[SPOILER: {}]'.format(spoiler_title.get_text()) + link = f'[SPOILER: {spoiler_title.get_text()}]' else: link = '[SPOILER]' new_spoiler = self._new_tag('div') diff --git a/sites/xenforo2.py b/sites/xenforo2.py index 8bac789..ec08f12 100644 --- a/sites/xenforo2.py +++ b/sites/xenforo2.py @@ -44,7 +44,7 @@ class XenForo2(XenForo): link.string = spoiler_title.get_text() else: if spoiler_title: - link = '[SPOILER: {}]'.format(spoiler_title.get_text()) + link = f'[SPOILER: {spoiler_title.get_text()}]' else: link = '[SPOILER]' new_spoiler = self._new_tag('div')