|
| 1 | +import logging |
| 2 | + |
| 3 | +import requests |
| 4 | + |
| 5 | +from substack.exceptions import SubstackAPIException, SubstackRequestException |
| 6 | + |
| 7 | +logger = logging.getLogger(__name__) |
| 8 | + |
| 9 | + |
| 10 | +class Api: |
| 11 | + """ |
| 12 | +
|
| 13 | + A python interface into the Substack API |
| 14 | +
|
| 15 | + """ |
| 16 | + |
| 17 | + def __init__( |
| 18 | + self, |
| 19 | + email: str, |
| 20 | + password: str, |
| 21 | + base_url: str | None = None, |
| 22 | + publication_url: str | None = None, |
| 23 | + debug: bool = False, |
| 24 | + ): |
| 25 | + """ |
| 26 | +
|
| 27 | + To create an instance of the substack.Api class: |
| 28 | + >>> import substack |
| 29 | + >>> api = substack.Api(email="substack email", password="substack password") |
| 30 | +
|
| 31 | + Args: |
| 32 | + email: |
| 33 | + password: |
| 34 | + base_url: |
| 35 | + The base URL to use to contact the Substack API. |
| 36 | + Defaults to https://substack.com/api/v1. |
| 37 | + """ |
| 38 | + self.base_url = base_url or "https://substack.com/api/v1" |
| 39 | + self.publication_url = publication_url |
| 40 | + |
| 41 | + if debug: |
| 42 | + logging.basicConfig() |
| 43 | + logging.getLogger().setLevel(logging.DEBUG) |
| 44 | + |
| 45 | + self._init_session(email, password) |
| 46 | + |
| 47 | + def login(self, email: str, password: str) -> dict: |
| 48 | + """ |
| 49 | +
|
| 50 | + Args: |
| 51 | + email: |
| 52 | + password: |
| 53 | + """ |
| 54 | + |
| 55 | + response = self._session.post( |
| 56 | + f"{self.base_url}/login", |
| 57 | + json={ |
| 58 | + "captcha_response": None, |
| 59 | + "email": email, |
| 60 | + "for_pub": "", |
| 61 | + "password": password, |
| 62 | + "redirect": "/", |
| 63 | + }, |
| 64 | + ) |
| 65 | + return Api._handle_response(response=response) |
| 66 | + |
| 67 | + def _init_session(self, email, password): |
| 68 | + self._session = requests.Session() |
| 69 | + |
| 70 | + self.login(email, password) |
| 71 | + |
| 72 | + @staticmethod |
| 73 | + def _handle_response(response: requests.Response): |
| 74 | + """ |
| 75 | +
|
| 76 | + Internal helper for handling API responses from the Substack server. |
| 77 | + Raises the appropriate exceptions when necessary; otherwise, returns the |
| 78 | + response. |
| 79 | +
|
| 80 | + """ |
| 81 | + |
| 82 | + if not (200 <= response.status_code < 300): |
| 83 | + raise SubstackAPIException(response.status_code, response.text) |
| 84 | + try: |
| 85 | + return response.json() |
| 86 | + except ValueError: |
| 87 | + raise SubstackRequestException("Invalid Response: %s" % response.text) |
| 88 | + |
| 89 | + def get_publication_users(self): |
| 90 | + """ |
| 91 | +
|
| 92 | + :return: |
| 93 | + """ |
| 94 | + response = self._session.get(f"{self.publication_url}/publication/users") |
| 95 | + |
| 96 | + return Api._handle_response(response=response) |
| 97 | + |
| 98 | + def get_posts(self) -> dict: |
| 99 | + """ |
| 100 | +
|
| 101 | + :return: |
| 102 | + """ |
| 103 | + response = self._session.get(f"{self.base_url}/reader/posts") |
| 104 | + |
| 105 | + return Api._handle_response(response=response) |
| 106 | + |
| 107 | + def get_drafts(self, filter: str = None, offset: int = None, limit: int = None): |
| 108 | + response = self._session.get( |
| 109 | + f"{self.publication_url}/drafts", |
| 110 | + params={"filter": filter, "offset": offset, "limit": limit}, |
| 111 | + ) |
| 112 | + return Api._handle_response(response=response) |
| 113 | + |
| 114 | + def post_draft( |
| 115 | + self, |
| 116 | + draft_bylines: list, |
| 117 | + title: str = None, |
| 118 | + subtitle: str = None, |
| 119 | + body: str = None, |
| 120 | + ) -> dict: |
| 121 | + """ |
| 122 | +
|
| 123 | + Args: |
| 124 | + draft_bylines: |
| 125 | + title: |
| 126 | + subtitle: |
| 127 | + body: |
| 128 | +
|
| 129 | + Returns: |
| 130 | +
|
| 131 | + """ |
| 132 | + response = self._session.post( |
| 133 | + f"{self.publication_url}/drafts", |
| 134 | + json={ |
| 135 | + "draft_bylines": draft_bylines, |
| 136 | + "draft_title": title, |
| 137 | + "draft_subtitle": subtitle, |
| 138 | + "draft_body": body, |
| 139 | + }, |
| 140 | + ) |
| 141 | + return Api._handle_response(response=response) |
| 142 | + |
| 143 | + def put_draft( |
| 144 | + self, |
| 145 | + draft: str, |
| 146 | + title: str = None, |
| 147 | + subtitle: str = None, |
| 148 | + body: str = None, |
| 149 | + cover_image: str = None, |
| 150 | + ) -> dict: |
| 151 | + """ |
| 152 | +
|
| 153 | + Args: |
| 154 | + draft: draft id |
| 155 | + title: |
| 156 | + subtitle: |
| 157 | + body: |
| 158 | + cover_image: |
| 159 | +
|
| 160 | + Returns: |
| 161 | +
|
| 162 | + """ |
| 163 | + |
| 164 | + response = self._session.put( |
| 165 | + f"{self.publication_url}/drafts/{draft}", |
| 166 | + json={ |
| 167 | + "draft_title": title, |
| 168 | + "draft_subtitle": subtitle, |
| 169 | + "draft_body": body, |
| 170 | + "cover_image": cover_image, |
| 171 | + }, |
| 172 | + ) |
| 173 | + return Api._handle_response(response=response) |
| 174 | + |
| 175 | + def prepublish_draft(self, draft: str) -> dict: |
| 176 | + """ |
| 177 | +
|
| 178 | + Args: |
| 179 | + draft: draft id |
| 180 | +
|
| 181 | + Returns: |
| 182 | +
|
| 183 | + """ |
| 184 | + |
| 185 | + response = self._session.get( |
| 186 | + f"{self.publication_url}/drafts/{draft}/prepublish" |
| 187 | + ) |
| 188 | + return Api._handle_response(response=response) |
| 189 | + |
| 190 | + def publish_draft( |
| 191 | + self, draft: str, send: bool = True, share_automatically: bool = False |
| 192 | + ) -> dict: |
| 193 | + """ |
| 194 | +
|
| 195 | + Args: |
| 196 | + draft: draft id |
| 197 | + send: |
| 198 | + share_automatically: |
| 199 | +
|
| 200 | + Returns: |
| 201 | +
|
| 202 | + """ |
| 203 | + response = self._session.post( |
| 204 | + f"{self.publication_url}/drafts/{draft}/publish", |
| 205 | + json={"send": send, "share_automatically": share_automatically}, |
| 206 | + ) |
| 207 | + return Api._handle_response(response=response) |
0 commit comments