aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorjwansek <eddie.atten.ea29@gmail.com>2021-03-06 23:28:49 +0000
committerjwansek <eddie.atten.ea29@gmail.com>2021-03-06 23:28:49 +0000
commit25789243a832c60524bb28c815f48914697a3506 (patch)
tree7f4297103bff1e879b6d91c8d75f35f7e66f4f86
parent2a0d3c4ccb287282974856b5f20f67a078149cb8 (diff)
downloadeda.gay-25789243a832c60524bb28c815f48914697a3506.tar.gz
eda.gay-25789243a832c60524bb28c815f48914697a3506.zip
updated the random image to download it
-rw-r--r--app.py10
-rw-r--r--services.py17
-rwxr-xr-xstatic/images/random.jpgbin0 -> 624236 bytes
-rw-r--r--templates/random.html2
4 files changed, 27 insertions, 2 deletions
diff --git a/app.py b/app.py
index a7784dd..c77d55f 100644
--- a/app.py
+++ b/app.py
@@ -6,6 +6,7 @@ import webbrowser
import datetime
import database
import services
+import urllib
import random
import parser
import flask
@@ -143,11 +144,18 @@ def serve_random():
except KeyError:
flask.abort(400)
+ sbi = services.get_random_image(tags)
+ req = urllib.request.Request(sbi.imurl)
+ mediaContent = urllib.request.urlopen(req).read()
+ with open(os.path.join("static", "images", "random.jpg"), "wb") as f:
+ f.write(mediaContent)
+
with database.Database() as db:
return flask.render_template(
"random.html",
**get_template_items("random image", db),
- sbi = services.get_random_image(tags)
+ sbi = sbi,
+ localimg = "/img/random.jpg?seed=%i" % random.randint(0, 9999)
)
@app.route("/api/<infoRequest>")
diff --git a/services.py b/services.py
index 0423a3e..8531c6b 100644
--- a/services.py
+++ b/services.py
@@ -14,6 +14,7 @@ import queue
import json
import time
import app
+import os
theLastId = 0
@@ -129,6 +130,22 @@ class SafebooruImage:
def remove_tag(self, tag):
return list(set(self.searchTags).difference(set([tag])))
+@dataclass
+class DownloadedImage:
+ imurl: str
+
+ def __enter__(self):
+ self.filename = os.path.join("static", "images", "random.jpg")
+
+ req = urllib.request.Request(self.imurl, headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_5_8) AppleWebKit/534.50.2 (KHTML, like Gecko) Version/5.0.6 Safari/533.22.3'})
+ mediaContent = urllib.request.urlopen(req).read()
+ with open(self.filename, "wb") as f:
+ f.write(mediaContent)
+ return self.filename
+
+ def __exit__(self, type, value, traceback):
+ os.remove(self.filename)
+
def get_num_pages(tags):
pages_url = "https://safebooru.org/index.php?page=post&s=list&tags=%s" % "+".join(tags)
tree = html.fromstring(requests.get(pages_url).content)
diff --git a/static/images/random.jpg b/static/images/random.jpg
new file mode 100755
index 0000000..5654cca
--- /dev/null
+++ b/static/images/random.jpg
Binary files differ
diff --git a/templates/random.html b/templates/random.html
index e36bb87..b6e7f64 100644
--- a/templates/random.html
+++ b/templates/random.html
@@ -15,7 +15,7 @@
</ul>
</aside>
<section id="randomImage">
- <a href={{sbi.source}}><img src={{sbi.imurl}}></a>
+ <a href={{sbi.source}}><img src={{localimg}}></a>
<h1><a href={{"/random?tags=" + "+".join(sbi.searchTags)}}>generate another</a></h1>
<h2><a href={{sbi.source}}>artist link</a></h2>
</section>