예제 #1
0
    def __init__(self,
                 presentation_id,
                 credfile="credentials.json",
                 serviceaccount=False,
                 credjson=None):
        """Initialize slides2html tool.

        Arguments:
            presentation_id {[str]} -- presentation id (e.g "147sFqkzjr_caJrh5f4ZpRRdD0SZP32aGSBkfDNH31PM or full url.")

        Keyword Arguments:
            credfile {str} -- [description] (default: {"credentials.json"})

        Raises:
            RuntimeError -- [In case of invalid credential files.]

        """
        self.presentation_id = presentation_id
        SCOPES = ["https://www.googleapis.com/auth/drive"]

        if not os.path.exists(credfile):
            raise RuntimeError(
                "please provide valid credentials.json file. https://console.developers.google.com/apis/credentials"
            )

        self.credfile = os.path.expanduser(credfile)
        print("credfile: ", self.credfile)

        credentials = None
        service = None
        if serviceaccount:
            if os.path.exists(self.credfile):
                credentials = service_account.Credentials.from_service_account_file(
                    self.credfile, scopes=SCOPES)
            elif credjson:
                credentials = service_account.Credentials.from_service_account_info(
                    **json.loads(credjson), scopes=SCOPES)
            else:
                raise RuntimeError(
                    "invalid credential file or credential json.")
            service = build("slides", "v1", credentials=credentials)
        else:
            userdir = os.path.expanduser("~")
            tokenjson = os.path.join(userdir, ".token.json")
            store = file.Storage(tokenjson)
            credentials = store.get()
            self.credfile = os.path.expanduser(credfile)
            if not credentials or credentials.invalid:
                flow = client.flow_from_clientsecrets(self.credfile, SCOPES)
                credentials = tools.run_flow(flow, store)

            service = build("slides", "v1", http=credentials.authorize(Http()))

        self.downloader = Downloader(presentation_id, service)
        self.generator = Generator(presentation_id)
예제 #2
0
 def do():
     from Jumpscale.tools.googleslides.slides2html.downloader import Downloader
     # presentation should be the guid
     # should extract the presentation if full path
     os.makedirs(destpath, exist_ok=True)
     service = self.service_get("slides", "v1")
     downloader = Downloader(presentation, service, size)
     downloader.download(destpath)
     presentation_dir = j.sal.fs.joinPaths(destpath, presentation)
     os.makedirs(presentation_dir, exist_ok=True)
     slides = [x for x in os.listdir(destpath) if x.endswith(".png") and "_" in x and "background_" not in x]
     for image in slides:
         imagepath = j.sal.fs.joinPaths(destpath, image)
         slideimage = image.split("_",maxsplit=1)[1]   # 00_asdsadasda.png remove the leading zeros and _
         newimagepath = j.sal.fs.joinPaths(presentation_dir, slideimage)
         j.sal.fs.moveFile(imagepath, newimagepath)
     if staticdir:
         j.sal.fs.moveDir(presentation_dir, staticdir)
     return True
예제 #3
0
class Tool:
    def __init__(self,
                 presentation_id,
                 credfile="credentials.json",
                 serviceaccount=False,
                 credjson=None):
        """Initialize slides2html tool.

        Arguments:
            presentation_id {[str]} -- presentation id (e.g "147sFqkzjr_caJrh5f4ZpRRdD0SZP32aGSBkfDNH31PM or full url.")

        Keyword Arguments:
            credfile {str} -- [description] (default: {"credentials.json"})

        Raises:
            RuntimeError -- [In case of invalid credential files.]

        """
        self.presentation_id = presentation_id
        SCOPES = ["https://www.googleapis.com/auth/drive"]

        if not os.path.exists(credfile):
            raise RuntimeError(
                "please provide valid credentials.json file. https://console.developers.google.com/apis/credentials"
            )

        self.credfile = os.path.expanduser(credfile)
        print("credfile: ", self.credfile)

        credentials = None
        service = None
        if serviceaccount:
            if os.path.exists(self.credfile):
                credentials = service_account.Credentials.from_service_account_file(
                    self.credfile, scopes=SCOPES)
            elif credjson:
                credentials = service_account.Credentials.from_service_account_info(
                    **json.loads(credjson), scopes=SCOPES)
            else:
                raise RuntimeError(
                    "invalid credential file or credential json.")
            service = build("slides", "v1", credentials=credentials)
        else:
            userdir = os.path.expanduser("~")
            tokenjson = os.path.join(userdir, ".token.json")
            store = file.Storage(tokenjson)
            credentials = store.get()
            self.credfile = os.path.expanduser(credfile)
            if not credentials or credentials.invalid:
                flow = client.flow_from_clientsecrets(self.credfile, SCOPES)
                credentials = tools.run_flow(flow, store)

            service = build("slides", "v1", http=credentials.authorize(Http()))

        self.downloader = Downloader(presentation_id, service)
        self.generator = Generator(presentation_id)

    def build_revealjs_site(self,
                            destdir="",
                            entryfile="",
                            presentation_dir="",
                            template=BASIC_TEMPLATE):
        """Build reveal.js based website.

        Keyword Arguments:
            destdir {str} -- directory under reveal.js website directory (default: {""})
            entryfile {str} -- index file name (default: presentation id)
            presentation_dir {str} -- directory to save images (default: {""})
            template {[str]} -- [reveal.js template] (default: {BASIC_TEMPLATE})
        """
        self.downloader.download(destdir)
        slides_infos = get_slides_info(destdir)
        html = self.generator.generate_html(slides_infos,
                                            revealjs_template=template)
        if not entryfile:
            entryfile = self.presentation_id

        with open(entryfile, "w") as f:
            f.write(html)

    def convert_to_transparent_background(self, destdir):
        images_to_transparent_background(destdir)

    def set_images_background(self, destdir, bgpath):
        set_background_for_images(destdir, bgpath)