comparison link.py @ 30:e86642cf7393

style and requirements.txt cleanup
author drewp@bigasterisk.com
date Sun, 12 Jul 2020 13:33:54 -0700
parents 7c82ffbca5d0
children f3a15a724483
comparison
equal deleted inserted replaced
29:890584020372 30:e86642cf7393
1 import urllib.parse, urllib.request, urllib.parse, urllib.error 1 import urllib.parse
2 import urllib.request
3 import urllib.parse
4 import urllib.error
2 from dateutil.tz import tzlocal 5 from dateutil.tz import tzlocal
6
3 7
4 class NotFound(ValueError): 8 class NotFound(ValueError):
5 pass 9 pass
6 10
11
7 class Links(object): 12 class Links(object):
8 def __init__(self, db): 13 def __init__(self, db):
9 self.coll = db['links'] 14 self.coll = db['links']
10 15
11 def insertOrUpdate(self, doc): 16 def insertOrUpdate(self, doc):
12 if not doc['href']: 17 if not doc['href']:
13 raise ValueError("no link") 18 raise ValueError("no link")
14 self.extract(doc) 19 self.extract(doc)
15 self.coll.update({'href':doc['href']}, doc, upsert=True, safe=True) 20 self.coll.update({'href': doc['href']}, doc, upsert=True, safe=True)
16 21
17 def extract(self, doc): 22 def extract(self, doc):
18 forUsers = [] 23 forUsers = []
19 tags = [] 24 tags = []
20 for t in doc.get('tag', '').split(' '): 25 for t in doc.get('tag', '').split(' '):
23 else: 28 else:
24 tags.append(t) 29 tags.append(t)
25 doc['extracted'] = dict(tags=tags, forUsers=forUsers) 30 doc['extracted'] = dict(tags=tags, forUsers=forUsers)
26 31
27 def find(self, uri, user): 32 def find(self, uri, user):
28 docs = list(self.coll.find({'href': uri, 'user' : user})) 33 docs = list(self.coll.find({'href': uri, 'user': user}))
29 if len(docs) == 0: 34 if len(docs) == 0:
30 raise NotFound("not found") 35 raise NotFound("not found")
31 elif len(docs) > 1: 36 elif len(docs) > 1:
32 raise ValueError("%s docs found for href %s" % (len(docs), uri)) 37 raise ValueError("%s docs found for href %s" % (len(docs), uri))
33 else: 38 else:
34 return docs[0] 39 return docs[0]
35 40
36 def filter(self, user, startTime): 41 def filter(self, user, startTime):
37 return self.coll.find({'user' : user, 't': {'$gte': startTime}}) 42 return self.coll.find({'user': user, 't': {'$gte': startTime}})
38 43
39 def forDisplay(self, doc): 44 def forDisplay(self, doc):
40 """return a mustache-ready dict for this db doc""" 45 """return a mustache-ready dict for this db doc"""
41 out = doc.copy() 46 out = doc.copy()
42 del out['_id'] 47 del out['_id']
43 out['t'] = out['t'].astimezone(tzlocal()).isoformat() 48 out['t'] = out['t'].astimezone(tzlocal()).isoformat()
44 if not out['description'].strip(): 49 if not out['description'].strip():
45 out['displayDescription'] = out['href'] 50 out['displayDescription'] = out['href']
46 else: 51 else:
47 out['displayDescription'] = out['description'] 52 out['displayDescription'] = out['description']
48 53
49 out['tagWords'] = [{'word' : w} for w in out['tag'].split(None)] 54 out['tagWords'] = [{'word': w} for w in out['tag'].split(None)]
50 out['domain'] = urllib.parse.urlparse(out['href']).netloc 55 out['domain'] = urllib.parse.urlparse(out['href']).netloc
51 out['editLink'] = 'addLink?' + urllib.parse.urlencode([('url', out['href'])]) 56 out['editLink'] = 'addLink?' + urllib.parse.urlencode(
52 out['shareWith'] = [{'label' : uri} for uri in doc.get('shareWith', [])] 57 [('url', out['href'])])
58 out['shareWith'] = [{'label': uri} for uri in doc.get('shareWith', [])]
53 return out 59 return out
54 60
55 def fromPostdata(self, data, user, t): 61 def fromPostdata(self, data, user, t):
56 if not user or not data.href: 62 if not user or not data.href:
57 raise ValueError("incomplete") 63 raise ValueError("incomplete")
65 tag=data.tag, 71 tag=data.tag,
66 t=t, 72 t=t,
67 ) 73 )
68 74
69 def asDeliciousAddParams(self): 75 def asDeliciousAddParams(self):
70 return dict(url=self['href'], 76 return dict(
71 description=self['description'], 77 url=self['href'],
72 extended=self['extended'], 78 description=self['description'],
73 tags=','.join(self['tag'].split(' ')), 79 extended=self['extended'],
74 dt=self['t'], 80 tags=','.join(self['tag'].split(' ')),
75 replace='yes', 81 dt=self['t'],
82 replace='yes',
76 ) 83 )