-
Notifications
You must be signed in to change notification settings - Fork 9
/
pocket_actions.py
79 lines (67 loc) · 2.52 KB
/
pocket_actions.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
# encoding:utf-8
import time
from pprint import pprint
from pocket import Pocket
import pocket_client
import config
class PocketbotPocketSlave(object):
'''docstring for PocketbotPocketSlave'''
def __init__(self, app_key):
super(PocketbotPocketSlave, self).__init__()
self.pocket_key = app_key
self.access_token = None
def set_access_token(self, access_token):
self.access_token = access_token
self.pocket_instance = Pocket(self.pocket_key, self.access_token)
def get_request_token(self):
return Pocket.get_request_token(consumer_key=self.pocket_key,
redirect_uri=config.redirect_uri)
def get_auth_url(self, request_token):
return Pocket.get_auth_url(code=request_token,
redirect_uri=config.redirect_uri)
def get_access_token(self, request_token):
user_credentials = Pocket.get_credentials(consumer_key=self.pocket_key,
code=request_token)
return user_credentials['access_token']
async def add_url(self, url, tags=None):
if tags is None:
tags = list()
else:
tags = tags[:]
tags = ','.join(tags)
trials = 0
success = False
item = {}
item_id = None
while not success and trials < 5:
res = await pocket_client.add(self.pocket_key,
self.access_token,
url,
tags)
item_id, item = res
if item.get('response_code') is not None:
success = True
trials += 1
title = item.get('title', 'PockeError')
item_id = item_id or -1
lang = item.get('lang', 'en')
resolved_url = item.get('resolved_url', 'http://fillll.ru/pockebot.html')
timing = 0
if title is not None:
timing = (int(item.get('word_count', 0)) / 150) + 1
item = {
'timing': timing,
'title': title,
'url': url,
'resolved_url': resolved_url,
'lang': lang
}
# pprint({'a': item_id, 'b': item})
return item_id, item
def add_tags(self, items, tags=None):
if not isinstance(tags, list):
tags = ['pockebot']
tags = ','.join(tags)
# print(items, tags)
for item in items:
self.pocket_instance.tags_add(item, tags).commit()