-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
156 lines (124 loc) · 4.02 KB
/
main.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
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
"""Manage a collection of bookmarked URLs."""
import cherrypy
class Controller:
"""Dispatch application requests based on HTTP verb."""
exposed = True
show_on_homepage = True
@staticmethod
def check_wayback_availability(url):
"""See if an archived copy of the URL is available."""
response = cherrypy.engine.publish(
"urlfetch:get",
"http://archive.org/wayback/available",
params={"url": url},
as_json=True
).pop() or {}
snapshots = response.get("archived_snapshots", {})
closest_snapshot = snapshots.get("closest", {})
return closest_snapshot
@cherrypy.tools.negotiable()
def GET(self, *args, **kwargs):
"""Display a list of recently bookmarked URLs, or URLs matching a
search.
"""
order = kwargs.get('order', 'rank')
query = kwargs.get('query')
page = int(kwargs.get('page', 1))
per_page = 20
offset = (page - 1) * per_page
if query:
query = query.strip()
if args == ('taglist',):
return self.taglist()
if kwargs.get('wayback'):
return {
"json": self.check_wayback_availability(kwargs.get('wayback'))
}
if query:
max_days = None
(bookmarks, count, query_plan) = cherrypy.engine.publish(
"bookmarks:search",
query,
order=order,
limit=per_page,
offset=offset,
).pop()
else:
max_days = 180
(bookmarks, count, query_plan) = cherrypy.engine.publish(
"bookmarks:recent",
limit=per_page,
offset=offset,
max_days=max_days
).pop()
start_index = offset + 1
end_index = offset + len(bookmarks)
total_pages = count // per_page + 1
next_page = page + 1
if next_page > total_pages:
next_page = None
previous_page = page - 1
if previous_page < 1:
previous_page = None
general_query = None
if query:
general_query = cherrypy.engine.publish(
"bookmarks:generalize_query",
query
).pop()
return {
"html": ("bookmarks.jinja.html", {
"bookmarks": bookmarks,
"max_days": max_days,
"count": count,
"query": query,
"general_query": general_query,
"query_plan": query_plan,
"order": order,
"next_page": next_page,
"previous_page": previous_page,
"total_pages": total_pages,
"page": page,
"start_index": start_index,
"end_index": end_index
})
}
@staticmethod
def POST(url, title=None, tags=None, comments=None, added=None):
"""Add a new bookmark, or update an existing one."""
result = cherrypy.engine.publish(
"scheduler:add",
2,
"bookmarks:add",
url,
title,
comments,
tags,
added
).pop()
if not result:
raise cherrypy.HTTPError(400)
cherrypy.response.status = 204
@staticmethod
def DELETE(url):
"""Discard a previously bookmarked URL."""
deleted_rows = cherrypy.engine.publish(
"bookmarks:remove",
url
).pop()
if not deleted_rows:
raise cherrypy.HTTPError(404, "Invalid url")
cherrypy.response.status = 204
@staticmethod
def taglist():
"""Render a list of all known bookmark tags using a dedicated
template.
"""
tags = cherrypy.engine.publish(
"bookmarks:tags:all"
).pop()
return {
"html": ("bookmarks-taglist.jinja.html", {
"tags": tags,
})
}