forked from bobuk/addmeto.cc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kippter
executable file
·232 lines (208 loc) · 8.5 KB
/
kippter
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#!/usr/bin/env python3
# mostly based on https://github.com/thomasbiddle/Kippt-Projects
import os
import json
from operator import itemgetter
import json, ast, requests, sys
from urllib.parse import quote_plus
class user:
def __init__(self, username, apitoken):
self.username = username
self.apitoken = apitoken
self.header = {'X-Kippt-Username': username,
'X-Kippt-API-Token': apitoken,
'X-Kippt-Client': 'Kippt-Python-Wrapper,[email protected],https://github.com/thomasbiddle/Kippt-Projects',
'content-type': 'application/vnd.kippt.20120609+json'}
def checkAuth(self):
r = requests.get('https://kippt.com/api/account/', headers=self.header)
if r.status_code is 200: return True
else: return False
def getLists(self, limit = 0, offset = 0):
url = 'https://kippt.com/api/lists?limit=' + str(limit) + '&offset=' + str(offset)
r = requests.get(url, headers=self.header)
if r.status_code is 200: return r.json()['meta'], r.json()['objects']
else: return False, False
def getList(self, id):
r = requests.get('https://kippt.com/api/lists/' + str(id), headers=self.header)
if r.status_code is 200: return r.json
else: return False
def getListCollab(self, id):
r = requests.get('https://kippt.com/api/lists/' + str(id) + '/collaborators', headers=self.header)
if r.status_code is 200: return r.json
else: return False
def getClips(self, listID = None, limit = 0, offset = 0):
params = { "limit": str(limit),
"offset": str(offset) }
url = 'https://kippt.com/api/clips?limit={limit}&offset={offset}'.format(**params)
if not listID is None: url = url + '&list=' + str(listID)
r = requests.get(url, headers=self.header)
if r.status_code is 200: return r.json()['meta'], r.json()['objects']
else: return False, False
def getClip(self, id):
r = requests.get('https://kippt.com/api/clips/' + str(id), headers=self.header)
if r.status_code is 200: return r.json()
else: return False
def search(self, query, limit = 0, offset = 0):
query = quote_plus(query)
params = { "query": query,
"limit": str(limit),
"offset": str(offset)
}
r = requests.get('https://kippt.com/api/search/clips/?q={query}&limit={limit}&offset={offset}'.format(**params),
headers=self.header)
if r.status_code is 200: return r.json()['meta'], r.json()['objects']
else: return False, False
# Create a list.
# Examples:
# user.createList('My New List!')
#
# Will return data on success and False on failure.
def createList(self, name):
clipdata = {'title': name}
r = requests.post('https://kippt.com/api/lists/', data=json.dumps(clipdata), headers=self.header)
if r.status_code is 201: return r.json()
else: return False
# Add a clip.
# Examples:
# user.addClip('www.kippt.com')
# user.addClip('www.kippt.com',title="My Title!")
# user.addClip('www.kippt.com',1234,starred="true",notes='My Notes!')
#
# Will return data on success and False on failure.
def addClip(self, url, listID=0, title = None, starred = None, notes = None, ):
clipdata = {'url': url, 'list': '/api/lists/' + str(listID)}
if not title is None: clipdata['title'] = title
if not starred is None: clipdata['is_starred'] = starred
if not notes is None: clipdata['notes'] = notes
r = requests.post('https://kippt.com/api/clips/', data=json.dumps(clipdata), headers=self.header)
if r.status_code is 201: return r.json()
else: return False
# Create a list.
# Examples:
# newList = user.createList('Programming')
# print newList
def createList(self, name):
clipdata = {'title': name}
r = requests.post('https://kippt.com/api/lists/', data=json.dumps(clipdata), headers=self.header)
if r.status_code is 201: return r.json()
else: return False
# Delete a clip ( Only clip owners can modify or delete clips, not collaborators! )
# Examples:
# user.deleteClip(2028643)
#
# Will return True on success, and False on failure
def deleteClip(self, id):
r = requests.delete('https://kippt.com/api/clips/' + str(id), headers=self.header)
if r.status_code is 204: return True
else: return False
# Delete a List
# Examples:
# user.deleteList(54433)
#
# Will return True on success, and False on failure
def deleteList(self, id):
r = requests.delete('https://kippt.com/api/lists/' + str(id), headers=self.header)
if r.status_code is 204: return True
else: return False
# Update a Clip ( Only clip owners can modify or delete clips, not collaborators! )
# Example:
# pyRespUpdateClip(id=2027593, list_uri='/api/lists/55284/')
#
# Returns True on success, False on failure.
def updateClip(self, id, title = None, notes = None, listID = None, starred = None):
clipdata = {}
if not title is None: clipdata['title'] = title
if not notes is None: clipdata['notes'] = notes
if not starred is None: clipdata['is_starred'] = starred
if not listID is None: clipdata['list'] = '/api/lists/' + str(listID)
r = requests.put('https://kippt.com/api/clips/' + str(id), data=json.dumps(clipdata), headers=self.header)
# import pdb; pdb.set_trace();
if r.status_code is 200: return True
else: return False
# Updating List ( With Python Requests )
# Example:
# client.updateList(55284, title="New Title!")
#
# Returns True on success, False on failure.
def updateList(self, id, title = None):
clipdata = {}
if not title is None: clipdata['title'] = title
r = requests.put('https://kippt.com/api/lists/' + str(id), data=json.dumps(clipdata), headers=self.header)
if r.status_code is 200: return True
else: return False
__cfg_singleton = None
def config():
global __cfg_singleton
if not __cfg_singleton:
config_path = os.path.expanduser(
os.path.join(
'~/Yandex.Disk.localized/',
'System/', 'etc/'
'kippt.conf'
)
)
__cfg_singleton = json.loads(open(config_path, 'r').read())
return __cfg_singleton
def get_list(k, slug='inbox'):
[header, lists] = k.getLists()
inbox = lists[0]['id']
for li in lists:
if li['slug'] == slug:
inbox = li['id']
return inbox
def get_clips(k, slug = 'inbox'):
inbox = get_list(k, slug)
[header, clips] = k.getClips(listID = inbox, limit=100)
return clips
def move_clip(k, clip, listID):
clip_id = int(clip)
return k.updateClip(clip, listID=listID)
def show(k):
res = []
clips = get_clips(k, 'inbox')
for clip in clips:
if 'notes' not in clip or clip['notes'] == None:
note = ""
else:
if '{' in clip['notes']:
before, middle = clip['notes'].split('{', 1)
middle, other = middle.split('}', 1)
note = "* " + before + '[' + middle + '](' + clip['url'] + ')' + other
else:
note = "* {notes} [ссылка]({url})".format_map(clip)
ts = int(clip['updated'])
if '#tech' in note:
ts = ts - 500000000
note = note.replace('#tech', '')
if '#last' in note:
ts = ts - 1000000000
note = note.replace('#last', '')
res.append((ts, note))
res = sorted(res, key = itemgetter(0), reverse=True)
print('\n'.join([x[1] for x in res]))
def flush(k):
published = get_list(k, 'published')
for clip in get_clips(k, 'inbox'):
if move_clip(k, clip['id'], published):
print ('>>> ' + clip['title'])
else:
print ('??? ' + clip['title'])
def unflush(k):
inbox = get_list(k, 'inbox')
for clip in get_clips(k, 'published'):
if move_clip(k, clip['id'], inbox):
print ('<<< ' + clip['title'])
else:
print ('??? ' + clip['title'])
def main(args):
k = user(config()['user'], config()['token'])
if not k.checkAuth():
return 'No auth!'
if not args or args[0] == 'show':
show(k)
elif args[0] == 'flush':
flush(k)
elif args[0] == 'unflush':
unflush(k)
if __name__ == '__main__':
main(sys.argv[1:])