PK!̻ pdf2sb/__init__.pyimport tempfile from itertools import chain from pathlib import Path from typing import List, Iterator, Optional import click import gyazo from PIL.Image import Image from pdf2image import convert_from_path __version__ = "0.3.2" def parse_range(expr: str) -> Iterator[int]: """Yield start and end integer pairs from a complex range string like "1-9,12, 15-20,23". >>> list(parse_range("1-9,12, 15-20,23")) [(1, 9), (12, 12), (15, 20), (23, 23)] >>> list(parse_range("1-9,12, 15-20,2-3-4")) Traceback (most recent call last): ... ValueError: format error in 2-3-4 """ for x in expr.split(","): elem = x.split("-") if len(elem) == 1: # a number yield int(elem[0]), int(elem[0]) elif len(elem) == 2: # a range inclusive yield int(elem[0]), int(elem[1]) else: # more than one hyphen raise ValueError(f"format error in {x}") def pdf2sb( pdf_file: str, gyazo_access_token: str, dpi: int, n_spaces: int, pages: Optional[str] = None, ) -> str: """Upload PDF file to Gyazo as images then convert to Scrapbox format.""" client = gyazo.Api(access_token=gyazo_access_token) urls = [] with tempfile.TemporaryDirectory() as tempdir: if pages is None: images: List[Image] = convert_from_path(pdf_file, dpi=dpi, fmt="png") else: images: List[Image] = list( chain.from_iterable( convert_from_path( pdf_file, dpi=dpi, fmt="png", first_page=first, last_page=last ) for first, last in parse_range(pages) ) ) tempdir_p = Path(tempdir) with click.progressbar(images, label="Uploading") as bar: for i, img in enumerate(bar): img_path = tempdir_p / f"{i}.png" img.save(img_path) gyazoimg = client.upload_image(img_path.open("rb")) urls.append(gyazoimg.to_dict()["permalink_url"]) return "".join(f"> [{url}]\n" + "\n" * n_spaces for url in urls) @click.command() @click.argument("filepath", type=click.Path(exists=True)) @click.option( "-t", "--token", envvar="GYAZO_ACCESS_TOKEN", help="Access token for Gyazo. If not specified, use $GYAZO_ACCESS_TOKEN.", ) @click.option( "-d", "--dpi", default=100, show_default=True, help="DPI of generating images." ) @click.option( "-s", "--spaces", default=1, show_default=True, help="Number of spaces after images.", ) @click.option("-p", "--pages", help="PDF pages to upload.") def main( filepath: str, token: str, dpi: int, spaces: int, pages: Optional[str] ) -> None: click.echo( pdf2sb( pdf_file=filepath, gyazo_access_token=token, dpi=dpi, n_spaces=spaces, pages=pages, ), nl=False, ) if __name__ == "__main__": main() PK!HkI#&'pdf2sb-0.3.2.dist-info/entry_points.txtN+I/N.,()*HI3*NPVy\\PK!o..pdf2sb-0.3.2.dist-info/LICENSEMIT License Copyright (c) 2019 Ryo Takahashi Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. PK!HڽTUpdf2sb-0.3.2.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!HHpdf2sb-0.3.2.dist-info/METADATATkO0_q'J Ej<* jnrzMbv 9n`Clh8{91ü4yM ,d7i6`eLT' )pV5 | TLBe̐"COZ\c|?f2PdB^<\[EDz0c^{/W<i*Kn { jT\vG/ܫm%׳< d4 Y ?e+MfQ~Be !} %t\wp!WÞ cDXCCFplz#nzӊ:awTIm]5'c/׆ۆRHZ3VhS#$*kш%Q1|یl^P1œ3(/ű ȅޚ6uxTgh C6Wbj&NPS fX_w7WN/GAA ?!PK!H -pdf2sb-0.3.2.dist-info/RECORDuKC@}D-YH &UFOiZ$s> f4xQʵ({?H۱Ry8ia>x0ΟwM_1+<`3dmJn &=e_~g~`M2TgkK|mƣ{c ~O% Er:Lԓv0 {ŖLQhvڻyI6|[zߡ^*R:cˏ%ŵ\EU\eYZ$FUS#cx M,I%{Su.`'F'VPK!̻ pdf2sb/__init__.pyPK!HkI#&' pdf2sb-0.3.2.dist-info/entry_points.txtPK!o..S pdf2sb-0.3.2.dist-info/LICENSEPK!HڽTUpdf2sb-0.3.2.dist-info/WHEELPK!HHKpdf2sb-0.3.2.dist-info/METADATAPK!H -pdf2sb-0.3.2.dist-info/RECORDPK