|
| 1 | +# coding: utf-8 |
| 2 | +from __future__ import print_function, unicode_literals |
| 3 | + |
| 4 | +from uuid import uuid4 |
| 5 | +from operator import itemgetter |
| 6 | +try: # python2 |
| 7 | + from urlparse import urljoin |
| 8 | + from urllib import quote_plus |
| 9 | +except ImportError: # python3 |
| 10 | + from urllib.parse import urljoin, quote_plus |
| 11 | + |
| 12 | +import requests |
| 13 | + |
| 14 | +from .consts import BASE_URL, LOGIN_URL |
| 15 | +from .exception import NotLoginError, APIError |
| 16 | +from .article import Article |
| 17 | + |
| 18 | + |
| 19 | +class InoreaderClient(object): |
| 20 | + |
| 21 | + def __init__(self, app_id, app_key, auth_token=None): |
| 22 | + self.app_id = app_id |
| 23 | + self.app_key = app_key |
| 24 | + self.auth_token = auth_token |
| 25 | + self.session = requests.Session() |
| 26 | + self.session.headers.update({ |
| 27 | + 'AppId': self.app_id, |
| 28 | + 'AppKey': self.app_key, |
| 29 | + 'Authorization': 'GoogleLogin auth={}'.format(self.auth_token) |
| 30 | + }) |
| 31 | + self.userid = None if not self.auth_token else self.userinfo()['userId'] |
| 32 | + |
| 33 | + def userinfo(self): |
| 34 | + if not self.auth_token: |
| 35 | + raise NotLoginError |
| 36 | + |
| 37 | + url = urljoin(BASE_URL, 'user-info') |
| 38 | + resp = self.session.post(url) |
| 39 | + if resp.status_code != 200: |
| 40 | + raise APIError(resp.text) |
| 41 | + |
| 42 | + return resp.json() |
| 43 | + |
| 44 | + def login(self, username, password): |
| 45 | + resp = self.session.get(LOGIN_URL, params={'Email': username, 'Passwd': password}) |
| 46 | + if resp.status_code != 200: |
| 47 | + return False |
| 48 | + |
| 49 | + for line in resp.text.split('\n'): |
| 50 | + if line.startswith('Auth'): |
| 51 | + self.auth_token = line.replace('Auth=', '').strip() |
| 52 | + |
| 53 | + return bool(self.auth_token) |
| 54 | + |
| 55 | + def get_folders(self): |
| 56 | + if not self.auth_token: |
| 57 | + raise NotLoginError |
| 58 | + |
| 59 | + url = urljoin(BASE_URL, 'tag/list') |
| 60 | + params = {'types': 1, 'counts': 1} |
| 61 | + resp = self.session.post(url, params=params) |
| 62 | + if resp.status_code != 200: |
| 63 | + raise APIError(resp.text) |
| 64 | + |
| 65 | + folders = [] |
| 66 | + for item in resp.json()['tags']: |
| 67 | + if item.get('type') != 'folder': |
| 68 | + continue |
| 69 | + |
| 70 | + folder_name = item['id'].split('/')[-1] |
| 71 | + folders.append({'name': folder_name, 'unread_count': item['unread_count']}) |
| 72 | + |
| 73 | + folders.sort(key=itemgetter('name')) |
| 74 | + return folders |
| 75 | + |
| 76 | + def get_tags(self): |
| 77 | + if not self.auth_token: |
| 78 | + raise NotLoginError |
| 79 | + |
| 80 | + url = urljoin(BASE_URL, 'tag/list') |
| 81 | + params = {'types': 1, 'counts': 1} |
| 82 | + resp = self.session.post(url, params=params) |
| 83 | + if resp.status_code != 200: |
| 84 | + raise APIError(resp.text) |
| 85 | + |
| 86 | + tags = [] |
| 87 | + for item in resp.json()['tags']: |
| 88 | + if item.get('type') != 'tag': |
| 89 | + continue |
| 90 | + |
| 91 | + folder_name = item['id'].split('/')[-1] |
| 92 | + tags.append({'name': folder_name, 'unread_count': item['unread_count']}) |
| 93 | + |
| 94 | + tags.sort(key=itemgetter('name')) |
| 95 | + return tags |
| 96 | + |
| 97 | + def fetch_unread(self, folder=None, tags=None): |
| 98 | + if not self.auth_token: |
| 99 | + raise NotLoginError |
| 100 | + |
| 101 | + url = urljoin(BASE_URL, 'stream/contents/') |
| 102 | + if folder: |
| 103 | + url = urljoin( |
| 104 | + url, |
| 105 | + quote_plus('user/{}/label/{}'.format(self.userid, folder)) |
| 106 | + ) |
| 107 | + params = { |
| 108 | + 'xt': 'user/{}/state/com.google/read'.format(self.userid), |
| 109 | + 'c': str(uuid4()) |
| 110 | + } |
| 111 | + |
| 112 | + resp = self.session.post(url, params=params) |
| 113 | + if resp.status_code != 200: |
| 114 | + raise APIError(resp.text) |
| 115 | + |
| 116 | + for data in resp.json()['items']: |
| 117 | + categories = set([ |
| 118 | + category.split('/')[-1] for category in data.get('categories', []) |
| 119 | + if category.find('label') > 0 |
| 120 | + ]) |
| 121 | + if tags and not categories.issuperset(set(tags)): |
| 122 | + continue |
| 123 | + yield Article.from_json(data) |
| 124 | + |
| 125 | + continuation = resp.json().get('continuation') |
| 126 | + while continuation: |
| 127 | + params['c'] = continuation |
| 128 | + resp = self.session.post(url, params=params) |
| 129 | + if resp.status_code != 200: |
| 130 | + raise APIError(resp.text) |
| 131 | + for data in resp.json()['items']: |
| 132 | + categories = set([ |
| 133 | + category.split('/')[-1] for category in data.get('categories', []) |
| 134 | + if category.find('label') > 0 |
| 135 | + ]) |
| 136 | + if tags and not categories.issuperset(set(tags)): |
| 137 | + continue |
| 138 | + yield Article.from_json(data) |
| 139 | + continuation = resp.json().get('continuation') |
0 commit comments