0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

CloudSignの締結済PDFや締結証明書をぶっこぬきたいときに使うやつを、Chat-GPT4.0と共著した。

Last updated at Posted at 2023-04-27

どういうときに便利か

CloudSignの締結済データを全てHubbleなど別プロダクトに突っ込みたい日が、人生にはある。
とりあえず思考停止でいい感じのファイル名で全件ダウンロードしてくれる。

他用途への応用は利くか

利く。リクエストよしなに変えてほしい。

公式ドキュメントはどれか

これ。
https://help.cloudsign.jp/ja/articles/2681259-%E3%82%AF%E3%83%A9%E3%82%A6%E3%83%89%E3%82%B5%E3%82%A4%E3%83%B3-web-api-%E5%88%A9%E7%94%A8%E3%82%AC%E3%82%A4%E3%83%89

実際のコードが欲しい。

どうぞ。

import os
import requests

class CloudSignAPI:
    def __init__(self, client_id):
        self.api_base_url = {'root': 'https://api.cloudsign.jp'}
        self.client_id = client_id
        self.access_token = self.get_access_token()

    def get_headers(self):
        return {"Authorization": f"Bearer {self.access_token}"}

    def get_access_token(self):
        headers = {}
        data = {"client_id": self.client_id}
        response = requests.post('{root}/token?client_id={client_id}'.format(**self.api_base_url, **data))

        if response.status_code == 200:
            return response.json()["access_token"]
        else:
            response.raise_for_status()

    def get_documents(self):
        url = f"{self.api_base_url['root']}/documents?status=2"
        response = requests.get(url, headers=self.get_headers())

        if response.status_code == 200:
            json_response = response.json()
            documents = json_response["documents"]
            total_documents = json_response["total"]
            print(json_response["total"])
            print(json_response["page"])
            for page in range(2, int(total_documents/100)+2): #1から始まり、1は完了済
                page_url = f"{url}&page={page}"
                page_response = requests.get(page_url, headers=self.get_headers())
                if page_response.status_code == 200:
                    page_json_response = page_response.json()
                    print(page_json_response["page"])
                    page_documents = page_json_response["documents"]
                    documents.extend(page_documents)
                else:
                    page_response.raise_for_status()

            return {"documents": documents}
        else:
            response.raise_for_status()

    def download_certificate(self, document_id, document_title):
        dir_path = "./certificates"
        url = f"{self.api_base_url['root']}/documents/{document_id}/certificate"
        response = requests.get(url, headers=self.get_headers())

        if response.status_code == 200:
            file_name = f"{dir_path}/cert_{document_id}_{document_title}.pdf"
            with open(file_name, "wb") as f:
                f.write(response.content)
            return file_name
        else:
            response.raise_for_status()

    def download_file(self, document_id, file_id, document_title, file_name):
        dir_path = "./attached_files"
        url = f"{self.api_base_url['root']}/documents/{document_id}/files/{file_id}"
        response = requests.get(url, headers=self.get_headers())

        if response.status_code == 200:
            file_name = f"{dir_path}/file_{document_id}_{file_id}.pdf"
            with open(file_name, "wb") as f:
                f.write(response.content)
            return file_name
        else:
            response.raise_for_status()


if __name__ == "__main__":
    CLIENT_ID = "[INSERT_YOUR_API_CLIENT_ID]"

    cloud_sign_api = CloudSignAPI(CLIENT_ID)

    # Get document list
    documents = cloud_sign_api.get_documents()
    
    # Download files and certificates for each document
    for document in documents["documents"]:
        document_id = document["id"]
        document_title = document["title"]
        document_status = document["status"]
        print(f"Downloading {document_id}: {document_title} Status:{document_status}")

        # Download and save the certificate
        downloaded_certificate_file_name = cloud_sign_api.download_certificate(document_id, document_title)
        print(f"Downloaded certificate for {downloaded_certificate_file_name}")
        # Download and save the file(s) associated with the document
        for file in document["files"]:
            file_id = file["id"]
            file_name = file["name"]
            downloaded_file_name = cloud_sign_api.download_file(document_id, file_id, document_title, file_name)
            print(f"Downloaded file for {downloaded_file_name} - {file_name} in {document_title}")

余談

  • ChatGPT-4.0でコード書きはじめた。セミナー聞きながら共同作業で1.5時間くらい。ほんと良い時代だ…
  • GASへの移植もChatGPT-4.0で2プロンプトほどで達成できた。
  • 当社は絶賛採用中です!採用一覧リンク
    • エンジニアはもちろん。
    • コーポレートでコード書ける人も、絶対活躍できる職場です。
    • 自部署で言えば、経理や経営企画を採用したいこの頃です。
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?