aboutsummaryrefslogtreecommitdiffstats
path: root/services.py
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 /services.py
parent2a0d3c4ccb287282974856b5f20f67a078149cb8 (diff)
downloadboymoder.blog-25789243a832c60524bb28c815f48914697a3506.tar.gz
boymoder.blog-25789243a832c60524bb28c815f48914697a3506.zip
updated the random image to download it
Diffstat (limited to 'services.py')
-rw-r--r--services.py17
1 files changed, 17 insertions, 0 deletions
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)