Skip to content

Commit b950961

Browse files
committed
Add more functions to client
- subscription list - get stream contents
1 parent 68300cf commit b950961

File tree

4 files changed

+81
-0
lines changed

4 files changed

+81
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,3 +62,4 @@ ENV/
6262

6363
# Spyder project settings
6464
.spyderproject
65+
.idea/*

inoreader/client.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from .consts import BASE_URL, LOGIN_URL
1515
from .exception import NotLoginError, APIError
1616
from .article import Article
17+
from .subscription import Subscription
1718

1819

1920
class InoreaderClient(object):
@@ -97,6 +98,44 @@ def get_tags(self):
9798
tags.sort(key=itemgetter('name'))
9899
return tags
99100

101+
def get_subscription_list(self):
102+
if not self.auth_token:
103+
raise NotLoginError
104+
105+
url = urljoin(BASE_URL, 'subscription/list')
106+
resp = self.session.get(url)
107+
if resp.status_code != 200:
108+
raise APIError(resp.text)
109+
110+
for item in resp.json()['subscriptions']:
111+
yield Subscription.from_json(item)
112+
113+
def get_stream_contents(self, stream_id):
114+
c = ''
115+
while True:
116+
articles, c = self.__get_stream_contents(stream_id, c)
117+
for a in articles:
118+
yield Article.from_json(a)
119+
if c is None:
120+
break
121+
122+
def __get_stream_contents(self, stream_id, continuation=''):
123+
if not self.auth_token:
124+
raise NotLoginError
125+
126+
url = urljoin(BASE_URL, 'stream/contents/' + quote_plus(stream_id))
127+
params = {
128+
'n': 50, # default 20, max 1000
129+
'r': '',
130+
'c': continuation,
131+
'output': 'json'
132+
}
133+
resp = self.session.post(url, params=params)
134+
if resp.status_code != 200:
135+
raise APIError(resp.text)
136+
137+
return resp.json()['items'], resp.json()['continuation']
138+
100139
def fetch_unread(self, folder=None, tags=None):
101140
if not self.auth_token:
102141
raise NotLoginError

inoreader/main.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,18 @@ def list_folders():
123123
print("{}\t{}".format(item['unread_count'], item['name']))
124124

125125

126+
def get_subscription_list():
127+
client = get_client()
128+
for item in client.get_subscription_list():
129+
print(item.__dict__)
130+
131+
132+
def get_stream_contents(stream_id):
133+
client = get_client()
134+
for item in client.get_stream_contents(stream_id):
135+
print(item.__dict__)
136+
137+
126138
def add_tags_list_parser(subparsers):
127139
subparsers.add_parser('list-tags', help="List all tags")
128140

inoreader/subscription.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/usr/bin/env python
2+
# -*- coding: utf-8 -*-
3+
4+
5+
class Subscription(object):
6+
def __init__(self, id, title, categories,
7+
sortid, firstitemmsec, url, htmlUrl, iconUrl):
8+
self.id = id
9+
self.title = title
10+
self.categories = categories
11+
self.sortid = sortid
12+
self.firstitemmsec = firstitemmsec
13+
self.url = url
14+
self.htmlUrl = htmlUrl
15+
self.iconUrl = iconUrl
16+
17+
@classmethod
18+
def from_json(cls, data):
19+
subscription_info = {
20+
'id': data['id'],
21+
'title': data['title'],
22+
'categories': [item for item in data['categories']],
23+
'sortid': data['sortid'],
24+
'firstitemmsec': data['firstitemmsec'],
25+
'url': data['url'],
26+
'htmlUrl': data['htmlUrl'],
27+
'iconUrl': data['iconUrl']
28+
}
29+
return cls(**subscription_info)

0 commit comments

Comments
 (0)