@@ -14,7 +14,14 @@ class Api:
1414
1515 """
1616
17- def __init__ (self , email : str , password : str , base_url : str | None = None , debug : bool = False ):
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+ ):
1825 """
1926
2027 To create an instance of the substack.Api class:
@@ -29,26 +36,32 @@ def __init__(self, email: str, password: str, base_url: str | None = None, debug
2936 Defaults to https://substack.com/api/v1.
3037 """
3138 self .base_url = base_url or "https://substack.com/api/v1"
39+ self .publication_url = publication_url
3240
3341 if debug :
3442 logging .basicConfig ()
3543 logging .getLogger ().setLevel (logging .DEBUG )
3644
3745 self ._init_session (email , password )
3846
39- def login (self , email : str , password : str ):
47+ def login (self , email : str , password : str ) -> dict :
4048 """
4149
4250 Args:
4351 email:
4452 password:
4553 """
4654
47- response = self ._session .post (f"{ self .base_url } /login" , json = {"captcha_response" : None ,
48- "email" : email ,
49- "for_pub" : "" ,
50- "password" : password ,
51- "redirect" : "/" })
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+ )
5265 return Api ._handle_response (response = response )
5366
5467 def _init_session (self , email , password ):
@@ -71,4 +84,124 @@ def _handle_response(response: requests.Response):
7184 try :
7285 return response .json ()
7386 except ValueError :
74- raise SubstackRequestException ('Invalid Response: %s' % response .text )
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:
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:
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:
197+ send:
198+ share_automatically:
199+
200+ Returns:
201+
202+ """
203+ response = requests .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