-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathclient.py
108 lines (95 loc) · 4.14 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# -*- coding: UTF-8 -*-
import requests
import json
class FeedlyClient(object):
def __init__(self, **options):
self.client_id = options.get('client_id')
self.client_secret = options.get('client_secret')
self.sandbox = options.get('sandbox', True)
if self.sandbox:
default_service_host = 'sandbox.feedly.com'
else:
default_service_host = 'cloud.feedly.com'
self.service_host = options.get('service_host', default_service_host)
self.additional_headers = options.get('additional_headers', {})
self.token = options.get('token')
self.secret = options.get('secret')
def get_code_url(self, callback_url):
scope = 'https://cloud.feedly.com/subscriptions'
response_type = 'code'
request_url = '%s?client_id=%s&redirect_uri=%s&scope=%s&response_type=%s' % (
self._get_endpoint('v3/auth/auth'),
self.client_id,
callback_url,
scope,
response_type
)
return request_url
def get_access_token(self,redirect_uri,code):
params = dict(
client_id=self.client_id,
client_secret=self.client_secret,
grant_type='authorization_code',
redirect_uri=redirect_uri,
code=code
)
quest_url=self._get_endpoint('v3/auth/token')
res = requests.post(url=quest_url, params=params)
return res.json()
def refresh_access_token(self,refresh_token):
'''obtain a new access token by sending a refresh token to the feedly Authorization server'''
params = dict(
refresh_token=refresh_token,
client_id=self.client_id,
client_secret=self.client_secret,
grant_type='refresh_token',
)
quest_url=self._get_endpoint('v3/auth/token')
res = requests.post(url=quest_url, params=params)
return res.json()
def get_user_subscriptions(self,access_token):
'''return list of user subscriptions'''
headers = {'Authorization': 'OAuth '+access_token}
quest_url=self._get_endpoint('v3/subscriptions')
res = requests.get(url=quest_url, headers=headers)
return res.json()
def get_feed_content(self,access_token,streamId,unreadOnly,newerThan):
'''return contents of a feed'''
headers = {'Authorization': 'OAuth '+access_token}
quest_url=self._get_endpoint('v3/streams/contents')
params = dict(
streamId=streamId,
unreadOnly=unreadOnly,
newerThan=newerThan
)
res = requests.get(url=quest_url, params=params,headers=headers)
return res.json()
def mark_article_read(self, access_token, entryIds):
'''Mark one or multiple articles as read'''
headers = {'content-type': 'application/json',
'Authorization': 'OAuth ' + access_token
}
quest_url = self._get_endpoint('v3/markers')
params = dict(
action="markAsRead",
type="entries",
entryIds=entryIds,
)
res = requests.post(url=quest_url, data=json.dumps(params), headers=headers)
return res
def save_for_later(self, access_token, user_id, entryIds):
'''saved for later.entryIds is a list for entry id.'''
headers = {'content-type': 'application/json',
'Authorization': 'OAuth ' + access_token
}
request_url = self._get_endpoint('v3/tags') + '/user%2F' + user_id + '%2Ftag%2Fglobal.saved'
params = dict(
entryIds=entryIds
)
res = requests.put(url=request_url, data=json.dumps(params), headers=headers)
return res
def _get_endpoint(self, path=None):
url = "https://%s" % (self.service_host)
if path is not None:
url += "/%s" % path
return url