-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstapaper_api_client.py
More file actions
37 lines (30 loc) · 1.43 KB
/
instapaper_api_client.py
File metadata and controls
37 lines (30 loc) · 1.43 KB
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
import requests
from requests_oauthlib import OAuth1
import urllib
class InstapaperApiClient(object):
BASE_URL = 'https://www.instapaper.com/api/1.1'
def __init__(self, consumer_key, consumer_secret):
self.consumer_key = consumer_key
self.consumer_secret = consumer_secret
self.app_consumer = OAuth1(consumer_key, client_secret=consumer_secret)
def authenticate_user(self, username, password):
params = {'x_auth_username': username, 'x_auth_password': password}
r = requests.post('%s/oauth/access_token' % self.BASE_URL, params=params, auth=self.app_consumer)
if r.status_code != 200:
print('Error authenticating, non-200 status code: %d' % r.status_code)
return None
response = urllib.parse.parse_qs(r.text)
oauth_token = response['oauth_token'][0]
oauth_token_secret = response['oauth_token_secret'][0]
return OAuth1(
self.consumer_key,
client_secret=self.consumer_secret,
resource_owner_key=oauth_token,
resource_owner_secret=oauth_token_secret)
def list_bookmarks(self, user_oauth):
params = {'format': 'json'}
r = requests.post('%s/bookmarks/list' % self.BASE_URL, params=params, auth=user_oauth)
if r.status_code != 200:
print('Error getting bookmarks, non-200 status code: %d' % r.status_code)
return None
return r.json()