├── app ├── __init__.py ├── migrations │ └── __init__.py ├── templatetags │ ├── __init__.py │ └── app_extras.py ├── models.py ├── admin.py ├── tests.py ├── apps.py ├── urls.py └── views.py ├── screenshot_generator ├── __init__.py ├── wsgi.py ├── urls.py └── settings.py ├── requirements.txt ├── AUTHORS ├── static ├── img │ └── python_screenshot_generator.png └── css │ └── styles.css ├── README.md ├── .gitignore ├── manage.py └── templates └── home.html /app/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /app/templatetags/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /screenshot_generator/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Django==2.2.2 2 | selenium==3.141.0 3 | -------------------------------------------------------------------------------- /AUTHORS: -------------------------------------------------------------------------------- 1 | Ronny Yabar 2 | 3 | http://ronnyml.com 4 | -------------------------------------------------------------------------------- /app/models.py: -------------------------------------------------------------------------------- 1 | from django.db import models 2 | 3 | # Create your models here. 4 | -------------------------------------------------------------------------------- /app/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /app/tests.py: -------------------------------------------------------------------------------- 1 | from django.test import TestCase 2 | 3 | # Create your tests here. 4 | -------------------------------------------------------------------------------- /app/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class AppConfig(AppConfig): 5 | name = 'app' 6 | -------------------------------------------------------------------------------- /static/img/python_screenshot_generator.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ronnyml/python-screenshot-generator/HEAD/static/img/python_screenshot_generator.png -------------------------------------------------------------------------------- /app/urls.py: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | 3 | from django.urls import path 4 | from app import views 5 | 6 | urlpatterns = [ 7 | path('', views.get_screenshot, name='get_screenshot'), 8 | ] -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Python Screenshot Generator 2 | -------- 3 | 4 | App to generate a screenshot from websites built with Python/Django and Selenium. 5 | 6 | ![Python Screenshot Generator](/static/img/python_screenshot_generator.png) -------------------------------------------------------------------------------- /app/templatetags/app_extras.py: -------------------------------------------------------------------------------- 1 | from django import template 2 | import base64 3 | 4 | register = template.Library() 5 | 6 | @register.filter() 7 | def decode_image(encoded_image): 8 | return "data:image/png;base64,%s" % encoded_image.decode("utf8") 9 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Files 2 | .DS_Store 3 | db.sqlite3 4 | *.pyc 5 | *.json 6 | *.patch 7 | .project 8 | .pydevproject 9 | 10 | # Django 11 | local_settings.py 12 | 13 | # Directories 14 | logs/ 15 | tmp/ 16 | media/ 17 | __pycache__/ 18 | env/ 19 | 20 | # Linux 21 | *~ 22 | 23 | # KDE 24 | .directory -------------------------------------------------------------------------------- /screenshot_generator/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for screenshot_generator project. 3 | 4 | It exposes the WSGI callable as a module-level variable named ``application``. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/ 8 | """ 9 | 10 | import os 11 | 12 | from django.core.wsgi import get_wsgi_application 13 | 14 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "screenshot_generator.settings") 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/python3 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'screenshot_generator.settings') 9 | try: 10 | from django.core.management import execute_from_command_line 11 | except ImportError as exc: 12 | raise ImportError( 13 | "Couldn't import Django. Are you sure it's installed and " 14 | "available on your PYTHONPATH environment variable? Did you " 15 | "forget to activate a virtual environment?" 16 | ) from exc 17 | execute_from_command_line(sys.argv) 18 | 19 | 20 | if __name__ == '__main__': 21 | main() 22 | -------------------------------------------------------------------------------- /static/css/styles.css: -------------------------------------------------------------------------------- 1 | body { 2 | background-color: #ECF0F1; 3 | color: #333333; 4 | font-family: Arial, Helvetica, sans-serif; 5 | margin: 0 auto; 6 | padding: 6px; 7 | } 8 | 9 | a { 10 | color: #246099; 11 | } 12 | 13 | div { 14 | text-align: center 15 | } 16 | 17 | h1 { 18 | font-size: 48px; 19 | margin-bottom: 12px; 20 | } 21 | 22 | input, button { 23 | margin: 12px 0; 24 | border: 2px solid #246099; 25 | } 26 | 27 | input { 28 | color: #4D4D4D; 29 | padding: 14px; 30 | } 31 | 32 | button { 33 | color: #FFF; 34 | cursor: pointer; 35 | background-color: #246099; 36 | padding: 12px; 37 | font-size: 16px; 38 | } 39 | 40 | small { 41 | font-size: small; 42 | } 43 | -------------------------------------------------------------------------------- /screenshot_generator/urls.py: -------------------------------------------------------------------------------- 1 | """screenshot_generator URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/2.2/topics/http/urls/ 5 | Examples: 6 | Function views 7 | 1. Add an import: from my_app import views 8 | 2. Add a URL to urlpatterns: path('', views.home, name='home') 9 | Class-based views 10 | 1. Add an import: from other_app.views import Home 11 | 2. Add a URL to urlpatterns: path('', Home.as_view(), name='home') 12 | Including another URLconf 13 | 1. Import the include() function: from django.urls import include, path 14 | 2. Add a URL to urlpatterns: path('blog/', include('blog.urls')) 15 | """ 16 | from django.urls import include, path 17 | from django.conf import settings 18 | from django.conf.urls.static import static 19 | 20 | urlpatterns = [ 21 | path('', include('app.urls')), 22 | ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) 23 | -------------------------------------------------------------------------------- /templates/home.html: -------------------------------------------------------------------------------- 1 | {% load app_extras %} 2 | {% load static %} 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | Python Screenshot Generator 12 | 13 | 14 |
15 |

Python Screenshot Generator

16 |
17 | {% csrf_token %} 18 | 19 | 20 |
21 | Optional params w, h for screenshot size and save for saving the screenshot in your media dir.
Ex: https://nodejs.org/?w=800&h=600&save=true
22 | 23 | {% if screenshot %} 24 |

Screenshot for {{ domain }}

25 | {% if save %} 26 | 27 | {% else %} 28 | 29 | {% endif %} 30 | {% endif %} 31 |
32 | 33 | 34 | -------------------------------------------------------------------------------- /app/views.py: -------------------------------------------------------------------------------- 1 | import base64 2 | import os 3 | import urllib.parse as urlparse 4 | 5 | from django.shortcuts import render 6 | from django.http import HttpResponse 7 | from django.conf import settings 8 | 9 | from datetime import datetime 10 | from selenium import webdriver 11 | 12 | DRIVER = 'chromedriver' 13 | 14 | def get_screenshot(request): 15 | """ 16 | Take a screenshot and return a png file based on the url. 17 | """ 18 | width = 1024 19 | height = 768 20 | 21 | if request.method == 'POST' and 'url' in request.POST: 22 | url = request.POST.get('url', '') 23 | if url is not None and url != '': 24 | save = False 25 | base_url = '{0.scheme}://{0.netloc}/'.format(urlparse.urlsplit(url)) 26 | domain = urlparse.urlsplit(url)[1].split(':')[0] 27 | params = urlparse.parse_qs(urlparse.urlparse(url).query) 28 | if len(params) > 0: 29 | if 'w' in params: width = int(params['w'][0]) 30 | if 'h' in params: height = int(params['h'][0]) 31 | driver = webdriver.Chrome(DRIVER) 32 | driver.get(url) 33 | driver.set_window_size(width, height) 34 | 35 | if 'save' in params and params['save'][0] == 'true': 36 | save = True 37 | now = str(datetime.today().timestamp()) 38 | img_dir = settings.MEDIA_ROOT 39 | img_name = ''.join([now, '_image.png']) 40 | full_img_path = os.path.join(img_dir, img_name) 41 | if not os.path.exists(img_dir): 42 | os.makedirs(img_dir) 43 | driver.save_screenshot(full_img_path) 44 | screenshot = img_name 45 | else: 46 | screenshot_img = driver.get_screenshot_as_png() 47 | screenshot = base64.encodestring(screenshot_img) 48 | 49 | var_dict = { 50 | 'screenshot': screenshot, 51 | 'domain': domain, 52 | 'base_url': base_url, 53 | 'full_url': url, 54 | 'save': save 55 | } 56 | 57 | driver.quit() 58 | return render(request, 'home.html', var_dict) 59 | else: 60 | return render(request, 'home.html') 61 | -------------------------------------------------------------------------------- /screenshot_generator/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for testing project. 3 | 4 | Generated by 'django-admin startproject' using Django 2.2.2. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/2.2/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/2.2/ref/settings/ 11 | """ 12 | 13 | import os 14 | 15 | # Build paths inside the project like this: os.path.join(BASE_DIR, ...) 16 | BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) 17 | 18 | 19 | # Quick-start development settings - unsuitable for production 20 | # See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/ 21 | 22 | # SECURITY WARNING: keep the secret key used in production secret! 23 | SECRET_KEY = '$y81oo=b$st*ameryq#7w3m88436_(e6r)!h3r2-#cu5(_m0yl' 24 | 25 | # SECURITY WARNING: don't run with debug turned on in production! 26 | DEBUG = True 27 | 28 | ALLOWED_HOSTS = [] 29 | 30 | 31 | # Application definition 32 | 33 | INSTALLED_APPS = [ 34 | 'django.contrib.admin', 35 | 'django.contrib.auth', 36 | 'django.contrib.contenttypes', 37 | 'django.contrib.sessions', 38 | 'django.contrib.messages', 39 | 'django.contrib.staticfiles', 40 | 'app' 41 | ] 42 | 43 | MIDDLEWARE = [ 44 | 'django.middleware.security.SecurityMiddleware', 45 | 'django.contrib.sessions.middleware.SessionMiddleware', 46 | 'django.middleware.common.CommonMiddleware', 47 | 'django.middleware.csrf.CsrfViewMiddleware', 48 | 'django.contrib.auth.middleware.AuthenticationMiddleware', 49 | 'django.contrib.messages.middleware.MessageMiddleware', 50 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', 51 | ] 52 | 53 | ROOT_URLCONF = 'screenshot_generator.urls' 54 | 55 | TEMPLATES = [ 56 | { 57 | 'BACKEND': 'django.template.backends.django.DjangoTemplates', 58 | 'DIRS': [os.path.join(BASE_DIR, 'templates')], 59 | 'APP_DIRS': True, 60 | 'OPTIONS': { 61 | 'context_processors': [ 62 | 'django.template.context_processors.debug', 63 | 'django.template.context_processors.request', 64 | 'django.template.context_processors.media', 65 | 'django.contrib.auth.context_processors.auth', 66 | 'django.contrib.messages.context_processors.messages', 67 | ], 68 | }, 69 | }, 70 | ] 71 | 72 | WSGI_APPLICATION = 'screenshot_generator.wsgi.application' 73 | 74 | 75 | # Database 76 | # https://docs.djangoproject.com/en/2.2/ref/settings/#databases 77 | 78 | DATABASES = { 79 | 'default': { 80 | 'ENGINE': 'django.db.backends.sqlite3', 81 | 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), 82 | } 83 | } 84 | 85 | 86 | # Password validation 87 | # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators 88 | 89 | AUTH_PASSWORD_VALIDATORS = [ 90 | { 91 | 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator', 92 | }, 93 | { 94 | 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator', 95 | }, 96 | { 97 | 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator', 98 | }, 99 | { 100 | 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator', 101 | }, 102 | ] 103 | 104 | 105 | # Internationalization 106 | # https://docs.djangoproject.com/en/2.2/topics/i18n/ 107 | 108 | LANGUAGE_CODE = 'en-us' 109 | 110 | TIME_ZONE = 'UTC' 111 | 112 | USE_I18N = True 113 | 114 | USE_L10N = True 115 | 116 | USE_TZ = True 117 | 118 | 119 | # Static files (CSS, JavaScript, Images) 120 | # https://docs.djangoproject.com/en/2.2/howto/static-files/ 121 | 122 | STATIC_URL = '/static/' 123 | MEDIA_ROOT = os.path.join(BASE_DIR, 'media') 124 | MEDIA_URL = '/media/' 125 | 126 | STATICFILES_DIRS = ( 127 | ('', os.path.join(BASE_DIR, 'static')), 128 | ) --------------------------------------------------------------------------------