annotate lookup.py @ 4:409da49c148d

partway though add Ignore-this: 330d1f3393bb91ca56267d2138e3bb22
author drewp@bigasterisk.com
date Sun, 17 Feb 2013 21:12:53 -0800
parents 80b11112c9e0
children f8c4c7ce5f4a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
1 #!bin/python
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
2 """
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
3 serve some queries over bookmarks:
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
4
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
5 /user
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
6 /user/tag+tag+tag
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
7
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
8 """
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
9 import pymongo, bottle, time, urllib, datetime
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
10 from urllib2 import urlparse
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
11 from dateutil.tz import tzlocal
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
12 from bottle import static_file
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
13 from jadestache import Renderer
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
14 db = pymongo.Connection('bang', tz_aware=True)['href']
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
15
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
16 renderer = Renderer(search_dirs=['template'], debug=bottle.DEBUG)
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
17
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
18 @bottle.route('/static/<filename>')
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
19 def server_static(filename):
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
20 return static_file(filename, root='static')
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
21
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
22 def recentTags(user, tags=None):
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
23 out = {'links':[]}
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
24 t1 = time.time()
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
25 spec = {'user':user}
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
26 if tags:
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
27 spec['extracted.tags'] = {'$all' : tags}
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
28 for doc in db['links'].find(spec, sort=[('t', -1)], limit=50):
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
29 del doc['_id']
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
30 doc['t'] = doc['t'].astimezone(tzlocal()).isoformat()
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
31 if not doc['description'].strip():
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
32 doc['displayDescription'] = doc['href']
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
33 else:
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
34 doc['displayDescription'] = doc['description']
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
35
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
36 doc['tagWords'] = [{'word' : w} for w in doc['tag'].split(None)]
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
37 doc['domain'] = urlparse.urlparse(doc['href']).netloc
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
38
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
39 out['links'].append(doc)
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
40 out['stats'] = {'queryTimeMs' : round((time.time() - t1) * 1000, 2)}
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
41 return out
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
42
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
43 def renderWithTime(name, data):
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
44 t1 = time.time()
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
45 rendered = renderer.render_name(name, data)
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
46 dt = (time.time() - t1) * 1000
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
47 rendered = rendered.replace('TEMPLATETIME', "%.02f ms" % dt)
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
48 return rendered
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
49
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
50 def getUser():
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
51 return 'drewpca' # logged in user
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
52
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
53 @bottle.route('/addLink')
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
54 def addLink():
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
55 out = {'toRoot': '.'}
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
56 out['user'] = getUser()
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
57 out['withKnockout'] = True
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
58 return renderWithTime('add.jade', out)
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
59
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
60 @bottle.route('/addOverlay')
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
61 def addOverlay():
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
62 p = bottle.request.params
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
63
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
64 return ""
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
65
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
66
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
67 proposal check existing links, get page title (stuff that in db), get tags from us and other serviecs. maybe the deferred ones ater
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
68
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
69
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
70 @bottle.route('/<user>/')
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
71 def userSlash(user):
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
72 bottle.redirect("/%s" % urllib.quote(user))
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
73
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
74 @bottle.route('/<user>', method='GET')
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
75 def userAll(user):
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
76 data = recentTags(user, tags=None)
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
77
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
78 data['desc'] = "%s's recent links" % user
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
79 data['toRoot'] = "."
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
80 data['stats']['template'] = 'TEMPLATETIME'
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
81 return renderWithTime('links.jade', data)
4
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
82
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
83 @bottle.route('/<user>', method='POST')
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
84 def userAddLink(user):
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
85 p = bottle.request.params
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
86 doc = dict(
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
87 user=user,
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
88 description=p.description,
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
89 extended=p.extended,
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
90 href=p.href,
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
91 #private=p.private, == checked,
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
92 #shared ??
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
93 tag=p.tag,
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
94 t=datetime.datetime.now(tzlocal()),
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
95 )
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
96 db['links'].insert(doc, safe=True)
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
97
409da49c148d partway though add
drewp@bigasterisk.com
parents: 2
diff changeset
98 bottle.redirect(user)
2
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
99
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
100 @bottle.route('/<user>/<tags>')
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
101 def userLinks(user, tags):
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
102 tags = tags.split('+')
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
103 data = recentTags(user, tags)
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
104 data['desc'] = "%s's recent links tagged %s" % (user, tags)
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
105 data['toRoot'] = ".."
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
106
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
107 data['pageTags'] = [{"word":t} for t in tags]
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
108 data['stats']['template'] = 'TEMPLATETIME'
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
109 return renderWithTime('links.jade', data)
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
110
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
111 if __name__ == '__main__':
80b11112c9e0 web app for query urls like /user and /user/tag+tag
Drew Perttula <drewp@bigasterisk.com>
parents:
diff changeset
112 bottle.run(server='gunicorn', host='0.0.0.0', port=10002, workers=4)