2016-12-18 13:12:14 +00:00
|
|
|
import json
|
|
|
|
|
2016-11-06 04:24:43 +00:00
|
|
|
|
|
|
|
def album(a, minimal=False):
|
|
|
|
j = {
|
|
|
|
'id': a.id,
|
|
|
|
'description': a.description,
|
|
|
|
'title': a.title,
|
|
|
|
}
|
|
|
|
if not minimal:
|
|
|
|
j['photos'] = [photo(p) for p in a.photos()]
|
2017-05-13 22:44:03 +00:00
|
|
|
parent = a.parent()
|
|
|
|
if parent is not None:
|
|
|
|
j['parent'] = album(parent, minimal=True)
|
|
|
|
else:
|
|
|
|
j['parent'] = None
|
2016-11-06 04:24:43 +00:00
|
|
|
j['sub_albums'] = [child.id for child in a.children()]
|
|
|
|
|
|
|
|
return j
|
|
|
|
|
2017-06-13 03:15:02 +00:00
|
|
|
def bookmark(b):
|
|
|
|
j = {
|
|
|
|
'id': b.id,
|
|
|
|
'url': b.url,
|
|
|
|
'title': b.title,
|
|
|
|
}
|
|
|
|
return j
|
|
|
|
|
2017-03-17 08:13:42 +00:00
|
|
|
def exception(e):
|
|
|
|
j = {
|
|
|
|
'error_type': e.error_type,
|
|
|
|
'error_message': e.error_message,
|
|
|
|
}
|
|
|
|
return j
|
|
|
|
|
2016-11-06 04:24:43 +00:00
|
|
|
def photo(p, include_albums=True, include_tags=True):
|
|
|
|
tags = p.tags()
|
|
|
|
tags.sort(key=lambda x: x.name)
|
|
|
|
j = {
|
|
|
|
'id': p.id,
|
|
|
|
'extension': p.extension,
|
|
|
|
'width': p.width,
|
|
|
|
'height': p.height,
|
|
|
|
'ratio': p.ratio,
|
|
|
|
'area': p.area,
|
|
|
|
'bytes': p.bytes,
|
2017-03-10 23:07:34 +00:00
|
|
|
'duration_str': p.duration_string,
|
2016-11-06 04:24:43 +00:00
|
|
|
'duration': p.duration,
|
2016-11-07 02:00:30 +00:00
|
|
|
'bytes_str': p.bytestring(),
|
2016-11-06 04:24:43 +00:00
|
|
|
'has_thumbnail': bool(p.thumbnail),
|
|
|
|
'created': p.created,
|
|
|
|
'filename': p.basename,
|
2016-12-24 03:49:51 +00:00
|
|
|
'mimetype': p.mimetype,
|
2016-11-06 04:24:43 +00:00
|
|
|
}
|
|
|
|
if include_albums:
|
|
|
|
j['albums'] = [album(a, minimal=True) for a in p.albums()]
|
|
|
|
|
|
|
|
if include_tags:
|
|
|
|
j['tags'] = [tag(t) for t in tags]
|
|
|
|
|
|
|
|
return j
|
|
|
|
|
2017-03-05 03:59:16 +00:00
|
|
|
def tag(t, include_synonyms=False):
|
2016-11-06 04:24:43 +00:00
|
|
|
j = {
|
|
|
|
'id': t.id,
|
|
|
|
'name': t.name,
|
2017-05-13 00:31:17 +00:00
|
|
|
'description': t.description,
|
2016-11-06 04:24:43 +00:00
|
|
|
'qualified_name': t.qualified_name(),
|
|
|
|
}
|
2017-03-05 03:59:16 +00:00
|
|
|
if include_synonyms:
|
|
|
|
j['synonyms'] = list(t.synonyms())
|
2016-12-16 11:27:39 +00:00
|
|
|
return j
|
2017-02-05 02:30:02 +00:00
|
|
|
|
|
|
|
def user(u):
|
|
|
|
j = {
|
|
|
|
'id': u.id,
|
|
|
|
'username': u.username,
|
|
|
|
'created': u.created,
|
|
|
|
}
|
|
|
|
return j
|