Mercurial > hg > index.fcgi > www > www-1
comparison pics3/pics_flask_app.py @ 140:ae6b4032a912
myrss2: FEEDS: Add jabberwocking.com, remove danielmiessler.com
author | paulo |
---|---|
date | Mon, 03 Jul 2023 04:40:23 +0000 |
parents | 06f97e38e1b2 |
children |
comparison
equal
deleted
inserted
replaced
2:65b08637cfbc | 3:c071875ef152 |
---|---|
1 import csv | 1 import csv |
2 import datetime | 2 import datetime |
3 import random | 3 import random |
4 import os | 4 import os |
5 import time | |
5 | 6 |
6 import flask | 7 import flask |
7 import google.cloud.storage | 8 import google.cloud.storage |
8 from html3.html3 import HTML | 9 from html3.html3 import HTML |
9 | 10 |
29 lineterminator = '\n' | 30 lineterminator = '\n' |
30 | 31 |
31 PICSDIALECT = PicsDialect() | 32 PICSDIALECT = PicsDialect() |
32 | 33 |
33 | 34 |
35 class BlobCache(object): | |
36 def __init__(self): | |
37 self._bloblist_cache = {} | |
38 self._bloblist_ttl = 60*15 # 15 minutes | |
39 self._public_url_cache = {} | |
40 | |
41 def get_list_blobs(self, prefix): | |
42 if (prefix not in self._bloblist_cache | |
43 or time.time() > (self._bloblist_cache[prefix][1] + self._bloblist_ttl)): | |
44 self._bloblist_cache[prefix] = ([i for i in GCS_CLIENT.list_blobs(GCS_BUCKET, prefix=prefix)], | |
45 time.time()) | |
46 return self._bloblist_cache[prefix][0] | |
47 | |
48 def cache_public_url(self, blob): | |
49 if blob.name not in self._public_url_cache: | |
50 self._public_url_cache[blob.name] = blob.public_url | |
51 | |
52 def get_public_url(self, blob_name): | |
53 return self._public_url_cache[blob_name] | |
54 | |
55 BLOBCACHE = BlobCache() | |
56 | |
57 | |
34 def _parse_dt(dts): | 58 def _parse_dt(dts): |
35 return datetime.datetime.strptime(dts, "%Y%m%d") | 59 return datetime.datetime.strptime(dts, "%Y%m%d") |
36 | 60 |
37 | 61 |
38 def _format_dt(dt): | 62 def _format_dt(dt): |
46 def _get_images(d): | 70 def _get_images(d): |
47 exts = (".jpg", ".webm") | 71 exts = (".jpg", ".webm") |
48 thumb_dir = f"pics/{d}/thumbs" | 72 thumb_dir = f"pics/{d}/thumbs" |
49 browse_dir = f"pics/{d}/browse" | 73 browse_dir = f"pics/{d}/browse" |
50 | 74 |
51 thumb_fns = [i.name for i in GCS_CLIENT.list_blobs(GCS_BUCKET, prefix=thumb_dir) if i.name.endswith(exts)] | 75 thumb_fns = [] |
76 for i in BLOBCACHE.get_list_blobs(thumb_dir): | |
77 if i.name.endswith(exts): | |
78 thumb_fns.append(i.name) | |
79 BLOBCACHE.cache_public_url(i) | |
52 thumb_fns = sorted(thumb_fns, key=_numeric_pad_basename) | 80 thumb_fns = sorted(thumb_fns, key=_numeric_pad_basename) |
53 | 81 |
54 browse_contents = set(i.name for i in GCS_CLIENT.list_blobs(GCS_BUCKET, prefix=browse_dir) if i.name.endswith(exts)) | 82 browse_contents = set() |
83 for i in BLOBCACHE.get_list_blobs(browse_dir): | |
84 if i.name.endswith(exts): | |
85 browse_contents.add(i.name) | |
86 BLOBCACHE.cache_public_url(i) | |
55 browse_fns = [] | 87 browse_fns = [] |
56 for i in thumb_fns: | 88 for i in thumb_fns: |
57 i_basename = os.path.splitext(os.path.basename(i))[0] | 89 i_basename = os.path.splitext(os.path.basename(i))[0] |
58 try: | 90 try: |
59 for j in exts: | 91 for j in exts: |
81 | 113 |
82 return (root, header, body) | 114 return (root, header, body) |
83 | 115 |
84 | 116 |
85 def _go_thumbnail_links_to_browse_imgs_html_body(body, d, t, b, a_args={}, img_args={}, lazyload=False): | 117 def _go_thumbnail_links_to_browse_imgs_html_body(body, d, t, b, a_args={}, img_args={}, lazyload=False): |
86 thumb_img_url = GCS_BUCKET.get_blob(t).public_url | 118 thumb_img_url = BLOBCACHE.get_public_url(t) |
87 browse_url = flask.url_for("browse", d=d, img=os.path.basename(b)) | 119 browse_url = flask.url_for("browse", d=d, img=os.path.basename(b)) |
88 | 120 |
89 a = body.a(href=browse_url, **a_args) | 121 a = body.a(href=browse_url, **a_args) |
90 if lazyload: | 122 if lazyload: |
91 img_args = dict(img_args) | 123 img_args = dict(img_args) |
96 | 128 |
97 body.text(" ") | 129 body.text(" ") |
98 | 130 |
99 | 131 |
100 def _go_thumbnail_links_to_thumbs_html_body(body, d, t, a_args={}, img_args={}): | 132 def _go_thumbnail_links_to_thumbs_html_body(body, d, t, a_args={}, img_args={}): |
101 thumb_img_url = GCS_BUCKET.get_blob(t).public_url | 133 thumb_img_url = BLOBCACHE.get_public_url(t) |
102 thumbs_url_args = {"from": os.path.basename(t), "_anchor": "selected"} | 134 thumbs_url_args = {"from": os.path.basename(t), "_anchor": "selected"} |
103 thumbs_url = flask.url_for("thumbs", d=d, **thumbs_url_args) | 135 thumbs_url = flask.url_for("thumbs", d=d, **thumbs_url_args) |
104 | 136 |
105 a = body.a(href=thumbs_url, **a_args) | 137 a = body.a(href=thumbs_url, **a_args) |
106 a.img(src=thumb_img_url, **img_args) | 138 a.img(src=thumb_img_url, **img_args) |
206 | 238 |
207 | 239 |
208 @app.route("/<d>") | 240 @app.route("/<d>") |
209 def thumbs(d): | 241 def thumbs(d): |
210 args = flask.request.args | 242 args = flask.request.args |
211 print(f"args = {args}") | |
212 | 243 |
213 from_img = args.get("from") | 244 from_img = args.get("from") |
214 | 245 |
215 imgs = list(_get_images(d)) | 246 imgs = list(_get_images(d)) |
216 (root, header, body) = _get_standard_html_doc(d) | 247 (root, header, body) = _get_standard_html_doc(d) |