2525
2626class InoreaderClient (object ):
2727 # paths
28- TOKEN_PATH = ' /oauth2/token'
29- USER_INFO_PATH = ' user-info'
30- TAG_LIST_PATH = ' tag/list'
31- SUBSCRIPTION_LIST_PATH = ' subscription/list'
32- STREAM_CONTENTS_PATH = ' stream/contents/'
33- EDIT_TAG_PATH = ' edit-tag'
34- EDIT_SUBSCRIPTION_PATH = ' subscription/edit'
28+ TOKEN_PATH = " /oauth2/token"
29+ USER_INFO_PATH = " user-info"
30+ TAG_LIST_PATH = " tag/list"
31+ SUBSCRIPTION_LIST_PATH = " subscription/list"
32+ STREAM_CONTENTS_PATH = " stream/contents/"
33+ EDIT_TAG_PATH = " edit-tag"
34+ EDIT_SUBSCRIPTION_PATH = " subscription/edit"
3535
3636 # tags
37- GENERAL_TAG_TEMPLATE = ' user/-/label/{}'
38- READ_TAG = ' user/-/state/com.google/read'
39- STARRED_TAG = ' user/-/state/com.google/starred'
40- LIKED_TAG = ' user/-/state/com.google/like'
41- BROADCAST_TAG = ' user/-/state/com.google/broadcast'
37+ GENERAL_TAG_TEMPLATE = " user/-/label/{}"
38+ READ_TAG = " user/-/state/com.google/read"
39+ STARRED_TAG = " user/-/state/com.google/starred"
40+ LIKED_TAG = " user/-/state/com.google/like"
41+ BROADCAST_TAG = " user/-/state/com.google/broadcast"
4242
4343 def __init__ (
4444 self , app_id , app_key , access_token , refresh_token , expires_at , config_manager = None
@@ -51,9 +51,9 @@ def __init__(
5151 self .session = requests .Session ()
5252 self .session .headers .update (
5353 {
54- ' AppId' : self .app_id ,
55- ' AppKey' : self .app_key ,
56- ' Authorization' : ' Bearer {}' .format (self .access_token ),
54+ " AppId" : self .app_id ,
55+ " AppKey" : self .app_key ,
56+ " Authorization" : " Bearer {}" .format (self .access_token ),
5757 }
5858 )
5959 self .config_manager = config_manager
@@ -76,16 +76,16 @@ def parse_response(response, json_data=True):
7676 def refresh_access_token (self ):
7777 url = urljoin (BASE_URL , self .TOKEN_PATH )
7878 payload = {
79- ' client_id' : self .app_id ,
80- ' client_secret' : self .app_key ,
81- ' grant_type' : ' refresh_token' ,
82- ' refresh_token' : self .refresh_token ,
79+ " client_id" : self .app_id ,
80+ " client_secret" : self .app_key ,
81+ " grant_type" : " refresh_token" ,
82+ " refresh_token" : self .refresh_token ,
8383 }
8484 response = self .parse_response (requests .post (url , json = payload , proxies = self .proxies ))
85- self .access_token = response [' access_token' ]
86- self .refresh_token = response [' refresh_token' ]
87- self .expires_at = datetime .now ().timestamp () + response [' expires_in' ]
88- self .session .headers [' Authorization' ] = ' Bearer {}' .format (self .access_token )
85+ self .access_token = response [" access_token" ]
86+ self .refresh_token = response [" refresh_token" ]
87+ self .expires_at = datetime .now ().timestamp () + response [" expires_in" ]
88+ self .session .headers [" Authorization" ] = " Bearer {}" .format (self .access_token )
8989
9090 if self .config_manager :
9191 self .config_manager .access_token = self .access_token
@@ -103,47 +103,47 @@ def get_folders(self):
103103 self .check_token ()
104104
105105 url = urljoin (BASE_URL , self .TAG_LIST_PATH )
106- params = {' types' : 1 , ' counts' : 1 }
106+ params = {" types" : 1 , " counts" : 1 }
107107 response = self .parse_response (self .session .post (url , params = params , proxies = self .proxies ))
108108
109109 folders = []
110- for item in response [' tags' ]:
111- if item .get (' type' ) != ' folder' :
110+ for item in response [" tags" ]:
111+ if item .get (" type" ) != " folder" :
112112 continue
113113
114- folder_name = item ['id' ].split ('/' )[- 1 ]
115- folders .append ({' name' : folder_name , ' unread_count' : item [' unread_count' ]})
114+ folder_name = item ["id" ].split ("/" )[- 1 ]
115+ folders .append ({" name" : folder_name , " unread_count" : item [" unread_count" ]})
116116
117- folders .sort (key = itemgetter (' name' ))
117+ folders .sort (key = itemgetter (" name" ))
118118 return folders
119119
120120 def get_tags (self ):
121121 self .check_token ()
122122
123123 url = urljoin (BASE_URL , self .TAG_LIST_PATH )
124- params = {' types' : 1 , ' counts' : 1 }
124+ params = {" types" : 1 , " counts" : 1 }
125125 response = self .parse_response (self .session .post (url , params = params , proxies = self .proxies ))
126126
127127 tags = []
128- for item in response [' tags' ]:
129- if item .get (' type' ) != ' tag' :
128+ for item in response [" tags" ]:
129+ if item .get (" type" ) != " tag" :
130130 continue
131131
132- folder_name = item ['id' ].split ('/' )[- 1 ]
133- tags .append ({' name' : folder_name , ' unread_count' : item [' unread_count' ]})
132+ folder_name = item ["id" ].split ("/" )[- 1 ]
133+ tags .append ({" name" : folder_name , " unread_count" : item [" unread_count" ]})
134134
135- tags .sort (key = itemgetter (' name' ))
135+ tags .sort (key = itemgetter (" name" ))
136136 return tags
137137
138138 def get_subscription_list (self ):
139139 self .check_token ()
140140
141141 url = urljoin (BASE_URL , self .SUBSCRIPTION_LIST_PATH )
142142 response = self .parse_response (self .session .get (url , proxies = self .proxies ))
143- for item in response [' subscriptions' ]:
143+ for item in response [" subscriptions" ]:
144144 yield Subscription .from_json (item )
145145
146- def get_stream_contents (self , stream_id , c = '' , limit = None ):
146+ def get_stream_contents (self , stream_id , c = "" , limit = None ):
147147 fetched_count = 0
148148 stop = False
149149 while not stop :
@@ -161,16 +161,16 @@ def get_stream_contents(self, stream_id, c='', limit=None):
161161 if c is None :
162162 break
163163
164- def __get_stream_contents (self , stream_id , continuation = '' ):
164+ def __get_stream_contents (self , stream_id , continuation = "" ):
165165 self .check_token ()
166166
167167 url = urljoin (BASE_URL , self .STREAM_CONTENTS_PATH + quote_plus (stream_id ))
168- params = {'n' : 50 , 'r' : '' , 'c' : continuation , ' output' : ' json' } # default 20, max 1000
168+ params = {"n" : 50 , "r" : "" , "c" : continuation , " output" : " json" } # default 20, max 1000
169169 response = self .parse_response (self .session .post (url , params = params , proxies = self .proxies ))
170- if ' continuation' in response :
171- return response [' items' ], response [' continuation' ]
170+ if " continuation" in response :
171+ return response [" items" ], response [" continuation" ]
172172 else :
173- return response [' items' ], None
173+ return response [" items" ], None
174174
175175 def fetch_articles (self , folder = None , tags = None , unread = True , starred = False , limit = None ):
176176 self .check_token ()
@@ -179,20 +179,20 @@ def fetch_articles(self, folder=None, tags=None, unread=True, starred=False, lim
179179 if folder :
180180 url = urljoin (url , quote_plus (self .GENERAL_TAG_TEMPLATE .format (folder )))
181181
182- params = {'c' : str (uuid4 ())}
182+ params = {"c" : str (uuid4 ())}
183183 if unread :
184- params ['xt' ] = self .READ_TAG
184+ params ["xt" ] = self .READ_TAG
185185
186186 if starred :
187- params ['it' ] = self .STARRED_TAG
187+ params ["it" ] = self .STARRED_TAG
188188
189189 fetched_count = 0
190190 response = self .parse_response (self .session .post (url , params = params , proxies = self .proxies ))
191- for data in response [' items' ]:
191+ for data in response [" items" ]:
192192 categories = {
193- category .split ('/' )[- 1 ]
194- for category in data .get (' categories' , [])
195- if category .find (' label' ) > 0
193+ category .split ("/" )[- 1 ]
194+ for category in data .get (" categories" , [])
195+ if category .find (" label" ) > 0
196196 }
197197 if tags and not categories .issuperset (set (tags )):
198198 continue
@@ -202,17 +202,17 @@ def fetch_articles(self, folder=None, tags=None, unread=True, starred=False, lim
202202 if limit and fetched_count >= limit :
203203 break
204204
205- continuation = response .get (' continuation' )
205+ continuation = response .get (" continuation" )
206206 while continuation and (not limit or fetched_count < limit ):
207- params ['c' ] = continuation
207+ params ["c" ] = continuation
208208 response = self .parse_response (
209209 self .session .post (url , params = params , proxies = self .proxies )
210210 )
211- for data in response [' items' ]:
211+ for data in response [" items" ]:
212212 categories = {
213- category .split ('/' )[- 1 ]
214- for category in data .get (' categories' , [])
215- if category .find (' label' ) > 0
213+ category .split ("/" )[- 1 ]
214+ for category in data .get (" categories" , [])
215+ if category .find (" label" ) > 0
216216 }
217217 if tags and not categories .issuperset (set (tags )):
218218 continue
@@ -221,7 +221,7 @@ def fetch_articles(self, folder=None, tags=None, unread=True, starred=False, lim
221221 if limit and fetched_count >= limit :
222222 break
223223
224- continuation = response .get (' continuation' )
224+ continuation = response .get (" continuation" )
225225
226226 def fetch_unread (self , folder = None , tags = None , limit = None ):
227227 for article in self .fetch_articles (folder = folder , tags = tags , unread = True ):
@@ -237,7 +237,7 @@ def add_general_label(self, articles, label):
237237 url = urljoin (BASE_URL , self .EDIT_TAG_PATH )
238238 for start in range (0 , len (articles ), 10 ):
239239 end = min (start + 10 , len (articles ))
240- params = {'a' : label , 'i' : [articles [idx ].id for idx in range (start , end )]}
240+ params = {"a" : label , "i" : [articles [idx ].id for idx in range (start , end )]}
241241 self .parse_response (
242242 self .session .post (url , params = params , proxies = self .proxies ), json_data = False
243243 )
@@ -248,7 +248,7 @@ def remove_general_label(self, articles, label):
248248 url = urljoin (BASE_URL , self .EDIT_TAG_PATH )
249249 for start in range (0 , len (articles ), 10 ):
250250 end = min (start + 10 , len (articles ))
251- params = {'r' : label , 'i' : [articles [idx ].id for idx in range (start , end )]}
251+ params = {"r" : label , "i" : [articles [idx ].id for idx in range (start , end )]}
252252 self .parse_response (
253253 self .session .post (url , params = params , proxies = self .proxies ), json_data = False
254254 )
@@ -285,16 +285,16 @@ def edit_subscription(self, stream_id, action, title=None, add_folder=None, remo
285285 url = urljoin (BASE_URL , self .EDIT_SUBSCRIPTION_PATH )
286286 # https://us.inoreader.com/developers/edit-subscription
287287 # The documentation looks a bit outdated, `follow`/`unfollow` don't work
288- action = {' follow' : ' subscribe' , ' unfollow' : ' unsubscribe' }.get (action ) or action
289- params = {'ac' : action , 's' : stream_id }
288+ action = {" follow" : " subscribe" , " unfollow" : " unsubscribe" }.get (action ) or action
289+ params = {"ac" : action , "s" : stream_id }
290290 if title :
291- params ['t' ] = title
291+ params ["t" ] = title
292292
293293 if add_folder :
294- params ['a' ] = add_folder
294+ params ["a" ] = add_folder
295295
296296 if remove_folder :
297- params ['r' ] = remove_folder
297+ params ["r" ] = remove_folder
298298
299299 r = self .session .post (url , params = params , proxies = self .proxies )
300300 response = self .parse_response (
0 commit comments