aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBobby <[email protected]>2023-03-26 03:02:56 -0400
committerBobby <[email protected]>2023-03-26 03:02:56 -0400
commitb8284d8322063b1e936b84bcab26a5f97d3b6dd9 (patch)
treecedcb49358ce1ab55cb172c6d872ecfee0e55f66
parent8b9dea3fca9c7b91a4f3aa698696f28d3f3063e7 (diff)
downloadthatcomputerscientist-b8284d8322063b1e936b84bcab26a5f97d3b6dd9.tar.xz
thatcomputerscientist-b8284d8322063b1e936b84bcab26a5f97d3b6dd9.zip
Improvement: Take Screenshot only on push and serve cached screenshot
-rw-r--r--.gitignore2
-rw-r--r--ignis/views.py40
-rw-r--r--thatcomputerscientist/urls.py30
3 files changed, 45 insertions, 27 deletions
diff --git a/.gitignore b/.gitignore
index 4ce23d5a..34977871 100644
--- a/.gitignore
+++ b/.gitignore
@@ -134,3 +134,5 @@ dmypy.json
images/
staticfiles/
+
+siteshot.png
diff --git a/ignis/views.py b/ignis/views.py
index cdfef4dd..a6f02c95 100644
--- a/ignis/views.py
+++ b/ignis/views.py
@@ -12,7 +12,6 @@ from users.tokens import CaptchaTokenGenerator
from django.http import HttpResponse
from django.views.decorators.cache import never_cache
-from selenium import webdriver
# from .github import get_cover
# Create your views here.
@@ -167,29 +166,16 @@ def captcha_image(request, captcha_string):
@never_cache
def get_screenshot(request):
- # Configure Selenium WebDriver with headless Chrome options
- options = webdriver.FirefoxOptions()
- options.headless = True
- driver = webdriver.Firefox(options=options)
- driver.set_window_size(1280, 1280)
-
- url = 'https://www.thatcomputerscientist.com'
-
- # Wait until the page is loaded
- driver.get(url)
-
- screenshot = driver.get_screenshot_as_png()
- screenshot = Image.open(BytesIO(screenshot))
-
- # Close the browser
- driver.quit()
-
- # Convert the screenshot to a data URI
- output = BytesIO()
- screenshot.save(output, format='PNG')
-
- response = HttpResponse(output.getvalue(), content_type='image/png')
- response['Cache-Control'] = 'no-cache, no-store, must-revalidate'
- response['Pragma'] = 'no-cache'
- response['Expires'] = '0'
- return response
+ # open file called 'siteshot.png' in the root directory
+ with open('siteshot.png', 'rb') as f:
+ image = f.read()
+ # convert to png
+ image = Image.open(BytesIO(image))
+ output = BytesIO()
+ image.save(output, format='PNG')
+
+ response = HttpResponse(output.getvalue(), content_type='image/png')
+ response['Cache-Control'] = 'no-cache, no-store, must-revalidate'
+ response['Pragma'] = 'no-cache'
+ response['Expires'] = '0'
+ return response
diff --git a/thatcomputerscientist/urls.py b/thatcomputerscientist/urls.py
index 373a4358..f3e97bfb 100644
--- a/thatcomputerscientist/urls.py
+++ b/thatcomputerscientist/urls.py
@@ -20,6 +20,11 @@ from django.conf.urls.static import static
from django.contrib.sitemaps.views import sitemap
from .sitemaps import PostSitemap, CategorySitemap, TagSitemap, StaticViewSitemap, GithubSitemap
+from PIL import Image
+from io import BytesIO
+from selenium import webdriver
+import time
+
sitemaps = {
'posts': PostSitemap,
'categories': CategorySitemap,
@@ -41,3 +46,28 @@ urlpatterns = [
]
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
+
+
+def get_screenshot():
+ # Configure Selenium WebDriver with headless Chrome options
+ options = webdriver.FirefoxOptions()
+ options.headless = True
+ driver = webdriver.Firefox(options=options)
+ driver.set_window_size(1280, 1280)
+
+ url = 'https://www.thatcomputerscientist.com'
+
+ # Wait until the page is loaded
+ driver.get(url)
+ time.sleep(5)
+
+ screenshot = driver.get_screenshot_as_png()
+ screenshot = Image.open(BytesIO(screenshot))
+
+ # Close the browser
+ driver.quit()
+
+ # Save as 'siteshot.png'
+ screenshot.save('siteshot.png')
+
+get_screenshot()