aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ignis/views.py30
-rw-r--r--thatcomputerscientist/urls.py31
2 files changed, 32 insertions, 29 deletions
diff --git a/ignis/views.py b/ignis/views.py
index a6f02c95..965e6498 100644
--- a/ignis/views.py
+++ b/ignis/views.py
@@ -12,6 +12,11 @@ from users.tokens import CaptchaTokenGenerator
from django.http import HttpResponse
from django.views.decorators.cache import never_cache
+from PIL import Image
+from io import BytesIO
+from selenium import webdriver
+import time
+import os
# from .github import get_cover
# Create your views here.
@@ -166,6 +171,29 @@ def captcha_image(request, captcha_string):
@never_cache
def get_screenshot(request):
+ # check if screenshot exists
+ if not os.path.exists('siteshot.png'):
+ 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', 'PNG')
+
+
# open file called 'siteshot.png' in the root directory
with open('siteshot.png', 'rb') as f:
image = f.read()
@@ -173,7 +201,7 @@ def get_screenshot(request):
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'
diff --git a/thatcomputerscientist/urls.py b/thatcomputerscientist/urls.py
index aa6c3187..25fb077e 100644
--- a/thatcomputerscientist/urls.py
+++ b/thatcomputerscientist/urls.py
@@ -20,11 +20,6 @@ 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,
@@ -47,27 +42,7 @@ urlpatterns = [
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
+import os
-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()
+if os.path.exists('siteshot.png'):
+ os.remove('siteshot.png')