├── core ├── __init__.py ├── management │ ├── __init__.py │ └── commands │ │ ├── __init__.py │ │ ├── runwatcher.py │ │ ├── runservice.py │ │ └── setup.py ├── migrations │ ├── __init__.py │ ├── 0005_delete_heartbeat.py │ ├── 0007_remove_screenshot_binary_image.py │ ├── 0006_screenshot_binary_image.py │ ├── 0004_heartbeat.py │ ├── 0003_remove_screenshot_bounding_boxes_and_more.py │ ├── 0002_remove_screenshot_for_processing_and_more.py │ └── 0001_initial.py ├── apps.py ├── serializer.py ├── urls.py ├── views.py ├── admin.py ├── watchdog.py ├── models.py ├── image_utils.py ├── nudity.py └── profanity.py ├── monitor ├── __init__.py ├── management │ ├── __init__.py │ └── commands │ │ ├── __init__.py │ │ └── runmonitor.py ├── migrations │ └── __init__.py ├── admin.py ├── apps.py ├── afk.py ├── monitor_windows.py └── window.py ├── openchaver ├── __init__.py ├── wsgi.py ├── decorators.py ├── urls.py ├── dirs.py ├── utils.py ├── const.py └── settings.py ├── bin └── nssm.exe ├── README.md ├── manage.py ├── requirements.txt ├── .gitignore └── LICENSE /core/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /monitor/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /core/management/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /core/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /monitor/management/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /monitor/migrations/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /core/management/commands/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /monitor/management/commands/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /openchaver/__init__.py: -------------------------------------------------------------------------------- 1 | PROGRAM_NAME = "OpenChaver" -------------------------------------------------------------------------------- /bin/nssm.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/dickermoshe/OpenChaver/HEAD/bin/nssm.exe -------------------------------------------------------------------------------- /monitor/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | -------------------------------------------------------------------------------- /core/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class CoreConfig(AppConfig): 5 | default_auto_field = 'django.db.models.BigAutoField' 6 | name = 'core' 7 | -------------------------------------------------------------------------------- /monitor/apps.py: -------------------------------------------------------------------------------- 1 | from django.apps import AppConfig 2 | 3 | 4 | class MonitorConfig(AppConfig): 5 | default_auto_field = "django.db.models.BigAutoField" 6 | name = "monitor" 7 | -------------------------------------------------------------------------------- /core/serializer.py: -------------------------------------------------------------------------------- 1 | from rest_framework import serializers 2 | 3 | from .models import Screenshot 4 | 5 | class ScreenshotSerializer(serializers.ModelSerializer): 6 | class Meta: 7 | model = Screenshot 8 | fields = '__all__' -------------------------------------------------------------------------------- /core/urls.py: -------------------------------------------------------------------------------- 1 | from django.urls import path 2 | 3 | from rest_framework import routers 4 | from .views import ScreenshotViewSet 5 | 6 | router = routers.DefaultRouter() 7 | router.register(r'screenshots', ScreenshotViewSet) 8 | 9 | urlpatterns = router.urls 10 | -------------------------------------------------------------------------------- /core/views.py: -------------------------------------------------------------------------------- 1 | from rest_framework.viewsets import ModelViewSet 2 | 3 | from .models import Screenshot 4 | from .serializer import ScreenshotSerializer 5 | 6 | class ScreenshotViewSet(ModelViewSet): 7 | queryset = Screenshot.objects.all() 8 | serializer_class = ScreenshotSerializer 9 | 10 | 11 | -------------------------------------------------------------------------------- /core/migrations/0005_delete_heartbeat.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.3 on 2022-11-04 04:12 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ("core", "0004_heartbeat"), 10 | ] 11 | 12 | operations = [ 13 | migrations.DeleteModel( 14 | name="Heartbeat", 15 | ), 16 | ] 17 | -------------------------------------------------------------------------------- /core/management/commands/runwatcher.py: -------------------------------------------------------------------------------- 1 | import logging 2 | from django.core.management.base import BaseCommand 3 | from core.watchdog import keep_service_alive 4 | 5 | logger = logging.getLogger(__name__) 6 | 7 | 8 | 9 | class Command(BaseCommand): 10 | help = "Keep the service alive" 11 | 12 | def handle(self, *args, **options): 13 | 14 | keep_service_alive() 15 | 16 | 17 | -------------------------------------------------------------------------------- /core/migrations/0007_remove_screenshot_binary_image.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.3 on 2022-11-06 02:01 2 | 3 | from django.db import migrations 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('core', '0006_screenshot_binary_image'), 10 | ] 11 | 12 | operations = [ 13 | migrations.RemoveField( 14 | model_name='screenshot', 15 | name='binary_image', 16 | ), 17 | ] 18 | -------------------------------------------------------------------------------- /openchaver/wsgi.py: -------------------------------------------------------------------------------- 1 | """ 2 | WSGI config for openchaver 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/4.1/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', 'openchaver.settings') 15 | 16 | application = get_wsgi_application() 17 | -------------------------------------------------------------------------------- /core/migrations/0006_screenshot_binary_image.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.3 on 2022-11-06 01:55 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ('core', '0005_delete_heartbeat'), 10 | ] 11 | 12 | operations = [ 13 | migrations.AddField( 14 | model_name='screenshot', 15 | name='binary_image', 16 | field=models.BinaryField(blank=True, null=True), 17 | ), 18 | ] 19 | -------------------------------------------------------------------------------- /monitor/management/commands/runmonitor.py: -------------------------------------------------------------------------------- 1 | # Custom command to run the monitor 2 | # Path: monitor\management\commands\monitor.py 3 | 4 | from django.core.management.base import BaseCommand 5 | from monitor.monitor_windows import run_monitor 6 | from openchaver.utils import thread_runner 7 | 8 | class Command(BaseCommand): 9 | help = "Run the monitor" 10 | 11 | def handle(self, *args, **options): 12 | services = { 13 | # Monitor 14 | "Monitor": { 15 | "target": run_monitor, 16 | "args": (), 17 | "kwargs": {}, 18 | "daemon": True, 19 | }, 20 | } 21 | thread_runner(services) -------------------------------------------------------------------------------- /core/migrations/0004_heartbeat.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.3 on 2022-11-03 13:39 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ("core", "0003_remove_screenshot_bounding_boxes_and_more"), 10 | ] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name="Heartbeat", 15 | fields=[ 16 | ( 17 | "username", 18 | models.CharField(max_length=255, primary_key=True, serialize=False), 19 | ), 20 | ("timestamp", models.DateTimeField(auto_now=True)), 21 | ], 22 | ), 23 | ] 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OpenChaver 2 | 3 | 4 | 5 | ``` 6 | Python 3.10 7 | pip install -r requirements.txt 8 | python manage.py setup 9 | 10 | Login to http://127.0.0.1:61313/admin 11 | User: admin 12 | Password: pass 13 | 14 | ``` 15 | 16 | TODO: 17 | 18 | - [ ] Create the uninstallation script that will uninstall the application. 19 | - [ ] Create UI 20 | - [ ] Create Taskbar Icon 21 | - [ ] Create the installation script that will install the application. 22 | - [ ] Create the update script that will update the application. 23 | - [ ] Create the documentation for the application. 24 | - [ ] Create the tests for the application. 25 | 26 | As you can see, there is a lot to do. If you want to help, please contact me. 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /manage.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | """Django's command-line utility for administrative tasks.""" 3 | import os 4 | import sys 5 | 6 | 7 | def main(): 8 | """Run administrative tasks.""" 9 | os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'openchaver.settings') 10 | try: 11 | from django.core.management import execute_from_command_line 12 | except ImportError as exc: 13 | raise ImportError( 14 | "Couldn't import Django. Are you sure it's installed and " 15 | "available on your PYTHONPATH environment variable? Did you " 16 | "forget to activate a virtual environment?" 17 | ) from exc 18 | execute_from_command_line(sys.argv) 19 | 20 | 21 | if __name__ == '__main__': 22 | main() 23 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | appdirs==1.4.4 2 | asgiref==3.5.2 3 | attrs==22.1.0 4 | certifi==2022.9.24 5 | charset-normalizer==2.1.1 6 | coloredlogs==15.0.1 7 | concurrent-log-handler==0.9.20 8 | Django==4.1.3 9 | djangorestframework==3.14.0 10 | drf-spectacular==0.24.2 11 | drf-spectacular-sidecar==2022.11.1 12 | flatbuffers==22.10.26 13 | humanfriendly==10.0 14 | idna==3.4 15 | inflection==0.5.1 16 | jsonschema==4.17.0 17 | mpmath==1.2.1 18 | mss==7.0.1 19 | numpy==1.23.4 20 | onnxruntime==1.13.1 21 | opencv-python==4.6.0.66 22 | packaging==21.3 23 | Pillow==9.3.0 24 | portalocker==2.6.0 25 | protobuf==4.21.9 26 | psutil==5.9.3 27 | pyparsing==3.0.9 28 | pyreadline3==3.4.1 29 | pyrsistent==0.19.2 30 | pytz==2022.6 31 | pywin32==304 32 | PyYAML==6.0 33 | requests==2.28.1 34 | sqlparse==0.4.3 35 | sympy==1.11.1 36 | tzdata==2022.6 37 | uritemplate==4.1.1 38 | urllib3==1.26.12 -------------------------------------------------------------------------------- /core/migrations/0003_remove_screenshot_bounding_boxes_and_more.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.3 on 2022-11-03 00:46 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ("core", "0002_remove_screenshot_for_processing_and_more"), 10 | ] 11 | 12 | operations = [ 13 | migrations.RemoveField( 14 | model_name="screenshot", 15 | name="bounding_boxes", 16 | ), 17 | migrations.AlterField( 18 | model_name="screenshot", 19 | name="is_nsfw", 20 | field=models.BooleanField(blank=True, null=True), 21 | ), 22 | migrations.AlterField( 23 | model_name="screenshot", 24 | name="is_profane", 25 | field=models.BooleanField(blank=True, null=True), 26 | ), 27 | ] 28 | -------------------------------------------------------------------------------- /core/admin.py: -------------------------------------------------------------------------------- 1 | from django.contrib import admin 2 | 3 | # Register your models here. 4 | from .models import Screenshot 5 | # IMport the html template 6 | from django.utils.html import format_html 7 | 8 | class ScreenshotAdmin(admin.ModelAdmin): 9 | list_display = ('title', 'excutable_name', 'screenshot_type','is_nsfw', 'is_profane', 'timestamp') 10 | search_fields = ('title', 'excutable_name') 11 | list_filter = ('screenshot_type', 'is_nsfw', 'is_profane', 'timestamp') 12 | 13 | # Show the image in the admin from the base64 string 14 | readonly_fields = ('image',) 15 | 16 | def image(self, obj): 17 | 18 | if obj.base64_image: 19 | return format_html('', obj.base64_image) 20 | return None 21 | 22 | 23 | 24 | admin.site.register(Screenshot, ScreenshotAdmin) 25 | -------------------------------------------------------------------------------- /openchaver/decorators.py: -------------------------------------------------------------------------------- 1 | import functools 2 | import logging 3 | import time 4 | 5 | 6 | def restart_on_exception(f, delay=1, exception=Exception): # pragma: no cover 7 | @functools.wraps(f) 8 | def g(*args, **kwargs): 9 | while True: 10 | try: 11 | f(*args, **kwargs) 12 | except exception: 13 | logging.exception(f"{f.__name__} crashed due to exception, restarting.") 14 | time.sleep( 15 | delay 16 | ) # To prevent extremely fast restarts in case of bad state. 17 | 18 | return g 19 | 20 | 21 | def handle_error(func): 22 | def __inner(*args, **kwargs): 23 | # Set the attribute to the function name 24 | __inner.__name__ = func.__name__ 25 | 26 | try: 27 | return func(*args, **kwargs) 28 | except: # noqa: E722 29 | logging.exception(f"Exception in {func.__name__}") 30 | raise 31 | 32 | return __inner 33 | -------------------------------------------------------------------------------- /monitor/afk.py: -------------------------------------------------------------------------------- 1 | import time 2 | 3 | import ctypes 4 | from ctypes import Structure, POINTER, WINFUNCTYPE, windll # type: ignore 5 | from ctypes.wintypes import BOOL, UINT, DWORD # type: ignore 6 | 7 | 8 | class LastInputInfo(Structure): 9 | _fields_ = [ 10 | ("cbSize", UINT), 11 | ("dwTime", DWORD) 12 | ] 13 | 14 | 15 | def _getLastInputTick() -> int: 16 | prototype = WINFUNCTYPE(BOOL, POINTER(LastInputInfo)) 17 | paramflags = ((1, "lastinputinfo"), ) 18 | c_GetLastInputInfo = prototype(("GetLastInputInfo", ctypes.windll.user32), paramflags) # type: ignore 19 | 20 | l = LastInputInfo() 21 | l.cbSize = ctypes.sizeof(LastInputInfo) 22 | assert 0 != c_GetLastInputInfo(l) 23 | return l.dwTime 24 | 25 | 26 | def _getTickCount() -> int: 27 | prototype = WINFUNCTYPE(DWORD) 28 | paramflags = () 29 | c_GetTickCount = prototype(("GetTickCount", ctypes.windll.kernel32), paramflags) # type: ignore 30 | return c_GetTickCount() 31 | 32 | 33 | def seconds_since_last_input(): 34 | seconds_since_input = (_getTickCount() - _getLastInputTick()) / 1000 35 | return seconds_since_input -------------------------------------------------------------------------------- /openchaver/urls.py: -------------------------------------------------------------------------------- 1 | """openchaver URL Configuration 2 | 3 | The `urlpatterns` list routes URLs to views. For more information please see: 4 | https://docs.djangoproject.com/en/4.1/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.contrib import admin 17 | from django.urls import path, include 18 | from drf_spectacular.views import SpectacularAPIView, SpectacularRedocView, SpectacularSwaggerView 19 | 20 | urlpatterns = [ 21 | path('admin/', admin.site.urls), 22 | path('api/', include('core.urls')), 23 | path('api/schema/', SpectacularAPIView.as_view(), name='schema'), 24 | path('docs/', SpectacularSwaggerView.as_view(url_name='schema'), name='swagger-ui'), 25 | ] 26 | -------------------------------------------------------------------------------- /core/migrations/0002_remove_screenshot_for_processing_and_more.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.3 on 2022-11-03 00:24 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | dependencies = [ 9 | ("core", "0001_initial"), 10 | ] 11 | 12 | operations = [ 13 | migrations.RemoveField( 14 | model_name="screenshot", 15 | name="for_processing", 16 | ), 17 | migrations.AddField( 18 | model_name="screenshot", 19 | name="screenshot_type", 20 | field=models.TextField( 21 | choices=[ 22 | ("META", "META"), 23 | ("IMAGE", "IMAGE"), 24 | ("NSFW", "NSFW"), 25 | ("NSFW_IMAGE", "NSFW_IMAGE"), 26 | ("NSFW_META", "NSFW_META"), 27 | ], 28 | default="META", 29 | ), 30 | ), 31 | migrations.AlterField( 32 | model_name="screenshot", 33 | name="base64_image", 34 | field=models.TextField(blank=True, null=True), 35 | ), 36 | ] 37 | -------------------------------------------------------------------------------- /core/management/commands/runservice.py: -------------------------------------------------------------------------------- 1 | import logging 2 | from django.core.management.base import BaseCommand 3 | from django.core.management import call_command 4 | from core.watchdog import keep_monitor_alive, keep_watcher_alive 5 | from openchaver.utils import thread_runner 6 | from openchaver.const import PORT 7 | 8 | logger = logging.getLogger(__name__) 9 | 10 | 11 | 12 | class Command(BaseCommand): 13 | help = "Run the main service" 14 | 15 | def handle(self, *args, **options): 16 | # Start the server on a separate thread 17 | services = { 18 | # Server 19 | "Server": { 20 | "target": call_command, 21 | "args": ('runserver',str(PORT),'--noreload'), 22 | "kwargs": {}, 23 | "daemon": True, 24 | }, 25 | # Keep keep_monitor_alive alive 26 | "keep_monitor_alive": { 27 | "target": keep_monitor_alive, 28 | "args": (), 29 | "kwargs": {}, 30 | "daemon": True, 31 | }, 32 | # Keep keep_watcher_alive alive 33 | "keep_watcher_alive": { 34 | "target": keep_watcher_alive, 35 | "args": (), 36 | "kwargs": {}, 37 | "daemon": True, 38 | }, 39 | 40 | } 41 | thread_runner(services) 42 | 43 | 44 | -------------------------------------------------------------------------------- /openchaver/dirs.py: -------------------------------------------------------------------------------- 1 | from .__init__ import PROGRAM_NAME 2 | 3 | import os 4 | from pathlib import Path 5 | from typing import Optional 6 | import appdirs 7 | from .utils import is_frozen 8 | 9 | 10 | def get_data_dir(module_name: Optional[str] = None) -> Path: 11 | data_dir = appdirs.user_data_dir("openchaver") 12 | path = os.path.join(data_dir, module_name) if module_name else data_dir 13 | 14 | if not os.path.exists(path): 15 | os.makedirs(path) 16 | 17 | return Path(path) 18 | 19 | 20 | def get_cache_dir(module_name: Optional[str] = None) -> Path: 21 | cache_dir = appdirs.user_cache_dir("openchaver") 22 | path = os.path.join(cache_dir, module_name) if module_name else cache_dir 23 | 24 | if not os.path.exists(path): 25 | os.makedirs(path) 26 | 27 | return Path(path) 28 | 29 | 30 | def get_config_dir(module_name: Optional[str] = None) -> Path: 31 | config_dir = appdirs.user_config_dir("openchaver") 32 | path = os.path.join(config_dir, module_name) if module_name else config_dir 33 | 34 | if not os.path.exists(path): 35 | os.makedirs(path) 36 | 37 | return Path(path) 38 | 39 | 40 | def get_install_dir() -> Path: 41 | # Dirs 42 | if not is_frozen(): 43 | return Path(__file__).parent.parent # Root of the project 44 | elif os.name == "nt": 45 | return Path(os.path.expandvars("%ProgramFiles(x86)%")) / PROGRAM_NAME 46 | 47 | -------------------------------------------------------------------------------- /openchaver/utils.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | import logging 3 | 4 | def is_frozen(): 5 | """Check if the program is frozen by PyInstaller or Nuitka""" 6 | import sys 7 | pyinstaller = getattr(sys, 'frozen', False) 8 | nuitka = "__compiled__" in globals() 9 | logging.debug(f"Pyinstaller: {pyinstaller}, Nuitka: {nuitka}") 10 | return pyinstaller or nuitka 11 | 12 | def delete_old_logs(log_location:Path,keep:list[Path] = []): 13 | for f in log_location.glob("*.log"): 14 | if f not in keep: 15 | f.unlink() 16 | 17 | def thread_runner(threads, die_event=None): 18 | # Create threads and start them 19 | import threading as th 20 | import time 21 | 22 | for k in threads.keys(): 23 | threads[k]["thread"] = th.Thread( 24 | target=threads[k]["target"], 25 | args=threads[k]["args"], 26 | kwargs=threads[k]["kwargs"], 27 | daemon=threads[k]["daemon"], 28 | ) 29 | 30 | # Start threads 31 | for k in threads.keys(): 32 | threads[k]["thread"].start() 33 | 34 | # Print threads ids 35 | for k in threads.keys(): 36 | logging.info(f"{k}: {threads[k]['thread'].ident}") 37 | 38 | # Loop -> Restart threads if they die and sleep for 5 seconds 39 | while True: 40 | for k in threads.keys(): 41 | if not threads[k]["thread"].is_alive(): 42 | logging.error(f'Thread "{k}" is dead, restarting...') 43 | threads[k]["thread"] = th.Thread( 44 | target=threads[k]["target"], 45 | args=threads[k]["args"], 46 | kwargs=threads[k]["kwargs"], 47 | daemon=threads[k]["daemon"], 48 | ) 49 | threads[k]["thread"].start() 50 | 51 | if die_event and die_event.is_set(): 52 | break 53 | 54 | time.sleep(5) -------------------------------------------------------------------------------- /core/migrations/0001_initial.py: -------------------------------------------------------------------------------- 1 | # Generated by Django 4.1.3 on 2022-11-02 19:33 2 | 3 | from django.db import migrations, models 4 | 5 | 6 | class Migration(migrations.Migration): 7 | 8 | initial = True 9 | 10 | dependencies = [] 11 | 12 | operations = [ 13 | migrations.CreateModel( 14 | name="Screenshot", 15 | fields=[ 16 | ( 17 | "id", 18 | models.BigAutoField( 19 | auto_created=True, 20 | primary_key=True, 21 | serialize=False, 22 | verbose_name="ID", 23 | ), 24 | ), 25 | ("title", models.TextField()), 26 | ("excutable_name", models.TextField()), 27 | ("base64_image", models.TextField()), 28 | ( 29 | "for_processing", 30 | models.BooleanField( 31 | default=True, help_text="Run NSFW detection on this screenshot" 32 | ), 33 | ), 34 | ("is_nsfw", models.BooleanField(default=False)), 35 | ("is_profane", models.BooleanField(default=False)), 36 | ( 37 | "bounding_boxes", 38 | models.JSONField( 39 | blank=True, 40 | default=list, 41 | help_text="Bounding boxes for the images in the screenshot", 42 | null=True, 43 | ), 44 | ), 45 | ( 46 | "nsfw_detection", 47 | models.JSONField( 48 | blank=True, 49 | default=list, 50 | help_text="NSFW detection results", 51 | null=True, 52 | ), 53 | ), 54 | ("timestamp", models.DateTimeField(auto_now_add=True)), 55 | ], 56 | ), 57 | ] 58 | -------------------------------------------------------------------------------- /openchaver/const.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | import sys 3 | from .dirs import get_data_dir, get_config_dir, get_install_dir 4 | from .utils import delete_old_logs, is_frozen 5 | import os 6 | import logging 7 | 8 | logger = logging.getLogger(__name__) 9 | os.environ['NO_PROXY'] = 'localhost' 10 | 11 | # Process Info 12 | BASE_EXE = Path(sys.executable) 13 | TESTING = not is_frozen() 14 | SERVICE_NAME = "OpenChaver Service (TESTING)" if TESTING else "OpenChaver Service" 15 | WATCHER_NAME = "OpenChaver Watcher (TESTING)" if TESTING else "OpenChaver Watcher" 16 | 17 | 18 | # Dirs 19 | INSTALL_DIR = get_install_dir() 20 | DATA_DIR = get_data_dir() 21 | CONFIG_DIR = get_config_dir() 22 | 23 | WATCHER_LOGS = INSTALL_DIR / "watcher.log" 24 | SERVICE_LOGS = INSTALL_DIR / "service.log" 25 | 26 | # Delete old logs 27 | delete_old_logs(INSTALL_DIR,keep = [WATCHER_LOGS,SERVICE_LOGS]) 28 | 29 | # Commands 30 | MIGRATE_ARGS = ["migrate"] # Migrates the database 31 | SERVICE_ARGS = ["runservice"] # Runs the main service in the service manager 32 | WATCHER_ARGS = ["runwatcher"] # Runs the watcher service in the service manager 33 | MONITOR_ARGS = ["runmonitor"] # Runs the monitor client in the user session 34 | 35 | if TESTING: 36 | MIGRATE_ARGS = [str(INSTALL_DIR / 'manage.py')] + MIGRATE_ARGS 37 | SERVICE_ARGS = [str(INSTALL_DIR / 'manage.py')] + SERVICE_ARGS 38 | WATCHER_ARGS = [str(INSTALL_DIR / 'manage.py')] + WATCHER_ARGS 39 | MONITOR_ARGS = [str(INSTALL_DIR / 'manage.py')] + MONITOR_ARGS 40 | 41 | MIGRATE_COMMAND = [str(BASE_EXE)] + MIGRATE_ARGS 42 | SERVICE_COMMAND = [str(BASE_EXE)] + SERVICE_ARGS 43 | WATCHER_COMMAND = [str(BASE_EXE)] + WATCHER_ARGS 44 | MONITOR_COMMAND = [str(BASE_EXE)] + MONITOR_ARGS 45 | 46 | PORT = 61313 47 | 48 | # Log all variables to the service log 49 | logger.info("BASE_EXE: %s", BASE_EXE) 50 | logger.info("TESTING: %s", TESTING) 51 | logger.info("SERVICE_NAME: %s", SERVICE_NAME) 52 | logger.info("WATCHER_NAME: %s", WATCHER_NAME) 53 | logger.info("INSTALL_DIR: %s", INSTALL_DIR) 54 | logger.info("DATA_DIR: %s", DATA_DIR) 55 | logger.info("CONFIG_DIR: %s", CONFIG_DIR) 56 | logger.info("WATCHER_LOGS: %s", WATCHER_LOGS) 57 | logger.info("SERVICE_LOGS: %s", SERVICE_LOGS) 58 | logger.info("MIGRATE_ARGS: %s", MIGRATE_ARGS) 59 | logger.info("SERVICE_ARGS: %s", SERVICE_ARGS) 60 | logger.info("WATCHER_ARGS: %s", WATCHER_ARGS) 61 | logger.info("MONITOR_ARGS: %s", MONITOR_ARGS) 62 | logger.info("MIGRATE_COMMAND: %s", MIGRATE_COMMAND) 63 | logger.info("SERVICE_COMMAND: %s", SERVICE_COMMAND) 64 | logger.info("WATCHER_COMMAND: %s", WATCHER_COMMAND) 65 | logger.info("MONITOR_COMMAND: %s", MONITOR_COMMAND) 66 | logger.info("PORT: %s", PORT) 67 | 68 | 69 | 70 | 71 | -------------------------------------------------------------------------------- /core/watchdog.py: -------------------------------------------------------------------------------- 1 | import os 2 | from openchaver.decorators import handle_error, restart_on_exception 3 | import logging 4 | import time 5 | import subprocess 6 | from openchaver.const import MONITOR_COMMAND, WATCHER_NAME, SERVICE_NAME 7 | 8 | def start_service_if_stopped(service_name: str): 9 | """Keep the service alive""" 10 | import os 11 | if os.name == 'nt': 12 | # Check if the service is running 13 | import win32serviceutil 14 | import win32service 15 | if win32serviceutil.QueryServiceStatus( 16 | service_name)[1] != win32service.SERVICE_RUNNING: # noqa E501 17 | win32serviceutil.StartService(service_name) 18 | 19 | logger = logging.getLogger(__name__) 20 | if os.name == 'nt': 21 | 22 | import psutil 23 | import win32ts 24 | import win32process 25 | import win32con 26 | 27 | def monitor_is_running(username: str) -> bool: 28 | """Check if the monitor is on""" 29 | for proc in psutil.process_iter(): 30 | try: 31 | u = proc.username().split('\\')[-1] 32 | if u == username: 33 | cmd = proc.cmdline() 34 | if cmd[1:] == MONITOR_COMMAND[1:]: 35 | return True 36 | except: # noqa: E722 37 | pass 38 | logger.info("Monitor is not running for user %s", username) 39 | return False 40 | 41 | @handle_error 42 | def keep_monitor_alive(interval: int = 5): 43 | """Return a list of logged in users""" 44 | while True: 45 | for session in win32ts.WTSEnumerateSessions( 46 | win32ts.WTS_CURRENT_SERVER_HANDLE): 47 | id = session['SessionId'] 48 | 49 | if id == 0: 50 | continue 51 | 52 | username = win32ts.WTSQuerySessionInformation( 53 | win32ts.WTS_CURRENT_SERVER_HANDLE, id, win32ts.WTSUserName) 54 | 55 | if monitor_is_running(username): 56 | continue 57 | 58 | # Get the token of the logged in user 59 | token = win32ts.WTSQueryUserToken(id) 60 | command = subprocess.list2cmdline(MONITOR_COMMAND) 61 | win32process.CreateProcessAsUser(token, None, command, 62 | None, None, False, 63 | win32con.CREATE_NO_WINDOW, 64 | None, None, 65 | win32process.STARTUPINFO()) 66 | logger.info(f"Started monitor for {username}") 67 | time.sleep(interval) 68 | 69 | @restart_on_exception 70 | @handle_error 71 | def keep_watcher_alive(): 72 | """This function Keeps the OpenChaver Watcher running""" 73 | 74 | logger.info("Starting the OpenChaver Watcher") 75 | while True: 76 | start_service_if_stopped(WATCHER_NAME) 77 | time.sleep(10) 78 | 79 | @restart_on_exception 80 | @handle_error 81 | def keep_service_alive(): 82 | """This function Keeps the OpenChaver Service running""" 83 | 84 | logger.info("Starting the OpenChaver Service") 85 | while True: 86 | start_service_if_stopped(SERVICE_NAME) 87 | time.sleep(10) 88 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[co] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | develop-eggs/ 12 | dist/ 13 | downloads/ 14 | eggs/ 15 | .eggs/ 16 | lib/ 17 | lib64/ 18 | parts/ 19 | sdist/ 20 | var/ 21 | wheels/ 22 | share/python-wheels/ 23 | *.egg-info/ 24 | .installed.cfg 25 | *.egg 26 | MANIFEST 27 | 28 | # PyInstaller 29 | # Usually these files are written by a python script from a template 30 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 31 | *.manifest 32 | 33 | 34 | # Installer logs 35 | pip-log.txt 36 | pip-delete-this-directory.txt 37 | 38 | # Unit test / coverage reports 39 | htmlcov/ 40 | .tox/ 41 | .nox/ 42 | .coverage 43 | .coverage.* 44 | .cache 45 | nosetests.xml 46 | coverage.xml 47 | *.cover 48 | *.py,cover 49 | .hypothesis/ 50 | .pytest_cache/ 51 | cover/ 52 | 53 | # Translations 54 | *.mo 55 | *.pot 56 | 57 | # Django stuff: 58 | *.log 59 | local_settings.py 60 | db.sqlite3 61 | db.sqlite3-journal 62 | 63 | # Flask stuff: 64 | instance/ 65 | .webassets-cache 66 | 67 | # Scrapy stuff: 68 | .scrapy 69 | 70 | # Sphinx documentation 71 | docs/_build/ 72 | 73 | # PyBuilder 74 | .pybuilder/ 75 | target/ 76 | 77 | # Jupyter Notebook 78 | .ipynb_checkpoints 79 | 80 | # IPython 81 | profile_default/ 82 | ipython_config.py 83 | 84 | # pyenv 85 | # For a library or package, you might want to ignore these files since the code is 86 | # intended to run in multiple environments; otherwise, check them in: 87 | # .python-version 88 | 89 | # pipenv 90 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 91 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 92 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 93 | # install all needed dependencies. 94 | #Pipfile.lock 95 | 96 | # poetry 97 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 98 | # This is especially recommended for binary packages to ensure reproducibility, and is more 99 | # commonly ignored for libraries. 100 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 101 | #poetry.lock 102 | 103 | # pdm 104 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 105 | #pdm.lock 106 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 107 | # in version control. 108 | # https://pdm.fming.dev/#use-with-ide 109 | .pdm.toml 110 | 111 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 112 | __pypackages__/ 113 | 114 | # Celery stuff 115 | celerybeat-schedule 116 | celerybeat.pid 117 | 118 | # SageMath parsed files 119 | *.sage.py 120 | 121 | # Environments 122 | .env 123 | .venv 124 | env/ 125 | venv/ 126 | ENV/ 127 | env.bak/ 128 | venv.bak/ 129 | 130 | # Spyder project settings 131 | .spyderproject 132 | .spyproject 133 | 134 | # Rope project settings 135 | .ropeproject 136 | 137 | # mkdocs documentation 138 | /site 139 | 140 | # mypy 141 | .mypy_cache/ 142 | .dmypy.json 143 | dmypy.json 144 | 145 | # Pyre type checker 146 | .pyre/ 147 | 148 | # pytype static type analyzer 149 | .pytype/ 150 | 151 | # Cython debug symbols 152 | cython_debug/ 153 | 154 | # PyCharm 155 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 156 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 157 | # and can be added to the global gitignore or merged into this file. For a more nuclear 158 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 159 | #.idea/ 160 | 161 | # PyInstaller 162 | output/ 163 | 164 | # Images 165 | /media/ 166 | 167 | # Static files 168 | /staticfiles/ 169 | 170 | # Images 171 | *.png 172 | *.jpg 173 | *.jpeg 174 | 175 | # Databases 176 | *.db 177 | *.db-shm 178 | *.db-wal 179 | 180 | # Utils 181 | _utils/ 182 | .hello 183 | 184 | 185 | # VSCode 186 | .vscode/ 187 | 188 | # Build 189 | *.build/ 190 | 191 | # onefile-build 192 | *.onefile-build/ 193 | 194 | temp_data/ 195 | system_data/ 196 | 197 | *.onnx 198 | 199 | # Rotating log files 200 | *.log.* -------------------------------------------------------------------------------- /core/models.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | from django.db import models 4 | import django.dispatch 5 | 6 | from .profanity import is_profane 7 | from .image_utils import decode_base64_to_numpy, get_bounding_boxes # noqa E501 8 | from .nudity import Detector, Classifier 9 | 10 | logger = logging.getLogger(__name__) 11 | 12 | class Screenshot(models.Model): 13 | title = models.TextField() 14 | excutable_name = models.TextField() 15 | base64_image = models.TextField(null=True, blank=True) 16 | 17 | TYPES = ( 18 | ("META", "META"), # Archive the meta data. No image 19 | ('IMAGE', 'IMAGE'), # Archive the image and meta data. Don't process the image for NSFW 20 | ('NSFW', 'NSFW'), # Process the image for NSFW. Archive the image and meta data if NSFW 21 | ('NSFW_IMAGE', 'NSFW_IMAGE'), # Process the image for NSFW. Archive the image and meta data NSFW always 22 | ('NSFW_META', 'NSFW_META'), # Process the image for NSFW. Archive the meta data if NSFW. Don't archive the image 23 | ) 24 | 25 | screenshot_type = models.TextField(choices=TYPES, default="META",) 26 | is_nsfw = models.BooleanField(null=True, blank=True) 27 | is_profane = models.BooleanField(null=True, blank=True) 28 | nsfw_detection = models.JSONField(default=list, blank=True, null=True, help_text="NSFW detection results") 29 | 30 | timestamp = models.DateTimeField(auto_now_add=True,) 31 | 32 | def __str__(self): 33 | return self.title 34 | 35 | @property 36 | def image(self): 37 | """Return the base64 string as a OpenCV image""" 38 | if self.base64_image: 39 | return decode_base64_to_numpy(self.base64_image) 40 | 41 | 42 | 43 | def run_nsfw_detection(self): 44 | if self.base64_image is None: 45 | self.is_nsfw = False 46 | self.save() 47 | return 48 | 49 | if self.is_nsfw is not None: 50 | self.is_nsfw = False 51 | self.save() 52 | return 53 | 54 | 55 | image = self.image 56 | sub_images = [] 57 | 58 | for x, y, w, h in self.create_bounding_boxes(): 59 | sub_images.append(image[y:y + h, x:x + w]) 60 | 61 | classifier = Classifier() 62 | for i in sub_images: 63 | if classifier.is_nsfw(i): 64 | # Run Detector on the images 65 | detector = Detector() 66 | detector_results = detector.is_nsfw(i) 67 | if detector_results['is_nsfw']: 68 | self.is_nsfw = True 69 | self.nsfw_detection = detector_results 70 | break 71 | else: 72 | self.is_nsfw = False 73 | 74 | self.save() 75 | logger.info(f"NSFW detection complete for {self.title} - {self.is_nsfw}") 76 | 77 | if not self.is_nsfw: 78 | if self.screenshot_type == "NSFW": 79 | self.delete() 80 | elif self.screenshot_type == "NSFW_META": 81 | self.base64_image = None 82 | self.save() 83 | 84 | def run_profanity_detection(self): 85 | if self.is_profane is None: 86 | self.is_profane = is_profane(self.title) 87 | self.save() 88 | 89 | def create_bounding_boxes(self) -> list: 90 | """Create bounding boxes for the images in the screenshot""" 91 | if self.base64_image is None: 92 | return [] 93 | return get_bounding_boxes(self.image) 94 | 95 | 96 | @django.dispatch.receiver(models.signals.post_save, sender=Screenshot) 97 | def post_process(sender, instance: Screenshot, **kwargs): 98 | instance.run_profanity_detection() 99 | if instance.is_nsfw is None: 100 | instance.run_nsfw_detection() 101 | 102 | # Check if the instanve has been deleted 103 | if Screenshot.objects.filter(pk=instance.pk).exists(): 104 | return 105 | 106 | # If the image is not NSFW and the latest screenshot has the same title 107 | # then delete the this one 108 | if not instance.is_nsfw: 109 | latest_screenshot = Screenshot.objects.exclude(pk=instance.pk).order_by('-timestamp').first() 110 | if latest_screenshot and latest_screenshot.title == instance.title: 111 | instance.delete() 112 | return 113 | 114 | 115 | -------------------------------------------------------------------------------- /monitor/monitor_windows.py: -------------------------------------------------------------------------------- 1 | import time 2 | import requests 3 | import logging 4 | from openchaver.decorators import handle_error 5 | from .afk import seconds_since_last_input 6 | from .window import Window, UnstableWindow, NoWindowFound 7 | from openchaver.const import PORT 8 | logger = logging.getLogger(__name__) 9 | 10 | 11 | class WindowMonitor: 12 | def __init__( 13 | self, 14 | sleep_interval=1, 15 | meta_interval=1, 16 | image_interval=300, 17 | nsfw_interval=10, 18 | stable=5, 19 | away=60, 20 | ) -> None: 21 | self.sleep_interval = sleep_interval 22 | self.meta_interval = meta_interval 23 | self.image_interval = image_interval 24 | self.nsfw_interval = nsfw_interval 25 | self.stable = stable 26 | self.away = away 27 | self.window = Window.get_active_window() 28 | self.meta_timer = time.time() 29 | self.image_timer = time.time() 30 | self.nsfw_timer = time.time() 31 | 32 | @handle_error 33 | def upload_screenshot(self, window: Window, screenshot_type="META"): 34 | """Upload the screenshot to the server""" 35 | data = { 36 | "title": window.title, 37 | "excutable_name": window.exec_name, 38 | "base64_image": window.take_screenshot() 39 | if screenshot_type in ["IMAGE", "NSFW", "NSFW_IMAGE", "NSFW_META"] 40 | else None, 41 | "screenshot_type": screenshot_type, 42 | } 43 | response = requests.post( 44 | f"http://localhost:{PORT}/api/screenshots/", json=data 45 | ) 46 | response.raise_for_status() 47 | 48 | 49 | 50 | def screenshoot( 51 | self, 52 | ) -> None: 53 | """Take a screenshot of the window""" 54 | meta = False 55 | image = False 56 | nsfw = False 57 | 58 | # Get the active window 59 | try: 60 | window = Window.get_active_window( 61 | invalid_title=self.window.title, stable=self.stable 62 | ) 63 | # If meta interval has passed 64 | if time.time() - self.meta_timer > self.meta_interval: 65 | meta = True 66 | self.meta_timer = time.time() 67 | 68 | # If image interval has passed 69 | if time.time() - self.image_timer > self.image_interval: 70 | image = True 71 | self.image_timer = time.time() 72 | 73 | # If nsfw interval has passed 74 | if time.time() - self.nsfw_timer > self.nsfw_interval: 75 | nsfw = True 76 | self.nsfw_timer = time.time() 77 | 78 | if image and nsfw: # Keep image - scan nsfw 79 | self.upload_screenshot(window, screenshot_type="NSFW_IMAGE") 80 | 81 | elif meta and image: # Keep image - dont scan nsfw 82 | self.upload_screenshot(window, screenshot_type="IMAGE") 83 | 84 | elif meta and nsfw: # Dont keep image - scan nsfw 85 | self.upload_screenshot(window, screenshot_type="NSFW_META") 86 | 87 | elif meta: # Dont keep image - dont scan nsfw 88 | self.upload_screenshot(window, screenshot_type="META") 89 | 90 | elif image: # Keep image - dont scan nsfw 91 | self.upload_screenshot(window, screenshot_type="IMAGE") 92 | 93 | elif nsfw: # Dont keep image - scan nsfw 94 | self.upload_screenshot(window, screenshot_type="NSFW") 95 | 96 | except (UnstableWindow, NoWindowFound): 97 | pass 98 | except: 99 | logger.exception("Error in Screenshooter") 100 | 101 | def is_afk(self) -> bool: 102 | """Check if the user is afk""" 103 | return seconds_since_last_input() > self.away 104 | 105 | @handle_error 106 | def run( 107 | self, 108 | ): 109 | """ 110 | Run the monitor 111 | """ 112 | 113 | while True: 114 | time.sleep(self.sleep_interval / 2) 115 | 116 | # Screenshoot if not afk 117 | if not self.is_afk(): 118 | self.screenshoot() 119 | 120 | time.sleep(self.sleep_interval / 2) 121 | 122 | def run_monitor(): 123 | """Run the monitor""" 124 | monitor = WindowMonitor() 125 | monitor.run() -------------------------------------------------------------------------------- /core/management/commands/setup.py: -------------------------------------------------------------------------------- 1 | # Custom command to run the monitor 2 | # Path: monitor\management\commands\monitor.py 3 | from pathlib import Path 4 | import subprocess 5 | import psutil 6 | import logging 7 | import shutil 8 | import os 9 | 10 | from django.core.management.base import BaseCommand 11 | 12 | from openchaver.const import ( 13 | INSTALL_DIR, 14 | MIGRATE_COMMAND, 15 | TESTING, 16 | BASE_EXE, 17 | SERVICE_NAME, 18 | WATCHER_NAME, 19 | SERVICE_LOGS, 20 | WATCHER_LOGS, 21 | MIGRATE_COMMAND, 22 | SERVICE_ARGS, 23 | WATCHER_ARGS, 24 | ) 25 | 26 | logger = logging.getLogger(__name__) 27 | 28 | 29 | def create_service(name: str, exe: str | Path, args: str, log_file: Path): 30 | """ 31 | This function creates a service using nssm.exe 32 | name: The name of the service 33 | exe: The executable to run 34 | args: The arguments to pass to the executable 35 | log_file: The log file to write to 36 | """ 37 | # Migrate the database 38 | logger.info("Migrating the database") 39 | subprocess.run(MIGRATE_COMMAND) 40 | 41 | # Make Superuser 42 | if TESTING: 43 | from django.contrib.auth.models import User 44 | try: 45 | User.objects.create_superuser('admin', 'admin@example.com', 'pass') 46 | except Exception as e: 47 | logger.exception(e) 48 | 49 | 50 | if os.name == "nt": 51 | logger.info(f"Creating the OpenChaver Service: {name}") 52 | 53 | nssm_path = ( 54 | Path("C:\\nssm.exe") if TESTING else INSTALL_DIR / "nssm.exe" 55 | ) # noqa: E501 56 | 57 | if TESTING and not nssm_path.exists(): 58 | shutil.copyfile((INSTALL_DIR / "bin" / "nssm.exe"), nssm_path) 59 | 60 | # Edit the service if it exists 61 | if name in [i.name() for i in psutil.win_service_iter()]: 62 | # Stop the service 63 | logger.info("Stopping the OpenChaver Service") 64 | subprocess.run([str(nssm_path), "stop", name]) 65 | 66 | # Set the service executable 67 | logger.info("Updating the OpenChaver Service") 68 | subprocess.run([str(nssm_path), "set", name, "Application", str(exe)]) 69 | 70 | # Set the service arguments 71 | subprocess.run([str(nssm_path), "set", name, "AppParameters", str(args)]) 72 | else: 73 | # Create the OpenChaver Service 74 | logger.info(f"Creating {name}") 75 | subprocess.run([str(nssm_path), "install", name, str(exe), str(args)]) 76 | 77 | # Auto Start the OpenChaver Service 78 | logger.info(f"Auto Starting {name}") 79 | subprocess.run([str(nssm_path), "set", name, "Start", "SERVICE_AUTO_START"]) 80 | 81 | # Set the OpenChaver Service to run restart on failure 82 | subprocess.run([str(nssm_path), "set", name, "AppExit", "Default", "Restart"]) 83 | 84 | # Set logging 85 | subprocess.run([str(nssm_path), "set", name, "AppStderr", str(log_file)]) 86 | 87 | # Set Log Rotation 88 | subprocess.run([str(nssm_path), "set", name, "AppRotateFiles", "1"]) 89 | subprocess.run( 90 | [str(nssm_path), "set", name, "AppRotateBytes", str(1024 * 1024 * 10)] 91 | ) 92 | 93 | subprocess.run([str(nssm_path), "set", name, "AppRotateOnline", "1"]) 94 | subprocess.run([str(nssm_path), "set", name, "AppRotateFiles", "5"]) 95 | 96 | # Start the OpenChaver Service 97 | logger.info(f"Starting {name}") 98 | subprocess.run([str(nssm_path), "start", name]) 99 | 100 | # Start the OpenChaver Service 101 | logger.info(f"Starting the OpenChaver Service") 102 | subprocess.run([str(nssm_path), "start", name]) 103 | 104 | logger.info(f"OpenChaver Service Created: {name}") 105 | 106 | # Start the OpenChaver Service 107 | subprocess.run([str(nssm_path), "start", name]) 108 | 109 | 110 | class Command(BaseCommand): 111 | help = "Run the monitor" 112 | 113 | def handle(self, *args, **options): 114 | 115 | """ 116 | This script creates the following if they dont exist: 117 | 1. The OpenChaver Service 118 | 2. The OpenChaver Auto Start Link in the Communal Startup Foler 119 | Start the following if not started: 120 | 1. The OpenChaver Service 121 | 2. The OpenChaver Auto Start Link in the Communal Startup Foler 122 | """ 123 | SERVICE_ARGS_STR = subprocess.list2cmdline(SERVICE_ARGS) 124 | WATCHER_ARGS_STR = subprocess.list2cmdline(WATCHER_ARGS) 125 | create_service(SERVICE_NAME, BASE_EXE, SERVICE_ARGS_STR, SERVICE_LOGS) 126 | create_service(WATCHER_NAME, BASE_EXE, WATCHER_ARGS_STR, WATCHER_LOGS) 127 | -------------------------------------------------------------------------------- /openchaver/settings.py: -------------------------------------------------------------------------------- 1 | """ 2 | Django settings for openchaver project. 3 | 4 | Generated by 'django-admin startproject' using Django 4.1.3. 5 | 6 | For more information on this file, see 7 | https://docs.djangoproject.com/en/4.1/topics/settings/ 8 | 9 | For the full list of settings and their values, see 10 | https://docs.djangoproject.com/en/4.1/ref/settings/ 11 | """ 12 | 13 | from pathlib import Path 14 | from .dirs import get_config_dir, get_install_dir 15 | 16 | # Build paths inside the project like this: BASE_DIR / 'subdir'. 17 | BASE_DIR = Path(__file__).resolve().parent.parent 18 | 19 | 20 | # Quick-start development settings - unsuitable for production 21 | # See https://docs.djangoproject.com/en/4.1/howto/deployment/checklist/ 22 | 23 | # SECURITY WARNING: keep the secret key used in production secret! 24 | SECRET_KEY = "Does not matter - Runs Locally" 25 | 26 | # SECURITY WARNING: don't run with debug turned on in production! 27 | DEBUG = True 28 | 29 | ALLOWED_HOSTS = [] 30 | 31 | 32 | # Application definition 33 | 34 | INSTALLED_APPS = [ 35 | "django.contrib.admin", 36 | "django.contrib.auth", 37 | "django.contrib.contenttypes", 38 | "django.contrib.sessions", 39 | "django.contrib.messages", 40 | "django.contrib.staticfiles", 41 | 'rest_framework', 42 | 'core', 43 | 'monitor', 44 | 'drf_spectacular', 45 | 'drf_spectacular_sidecar', 46 | ] 47 | 48 | MIDDLEWARE = [ 49 | "django.middleware.security.SecurityMiddleware", 50 | "django.contrib.sessions.middleware.SessionMiddleware", 51 | "django.middleware.common.CommonMiddleware", 52 | "django.middleware.csrf.CsrfViewMiddleware", 53 | "django.contrib.auth.middleware.AuthenticationMiddleware", 54 | "django.contrib.messages.middleware.MessageMiddleware", 55 | "django.middleware.clickjacking.XFrameOptionsMiddleware", 56 | ] 57 | 58 | ROOT_URLCONF = "openchaver.urls" 59 | 60 | TEMPLATES = [ 61 | { 62 | "BACKEND": "django.template.backends.django.DjangoTemplates", 63 | "DIRS": [], 64 | "APP_DIRS": True, 65 | "OPTIONS": { 66 | "context_processors": [ 67 | "django.template.context_processors.debug", 68 | "django.template.context_processors.request", 69 | "django.contrib.auth.context_processors.auth", 70 | "django.contrib.messages.context_processors.messages", 71 | ], 72 | }, 73 | }, 74 | ] 75 | 76 | WSGI_APPLICATION = "openchaver.wsgi.application" 77 | 78 | 79 | # Database 80 | # https://docs.djangoproject.com/en/4.1/ref/settings/#databases 81 | 82 | DATABASES = { 83 | "default": { 84 | "ENGINE": "django.db.backends.sqlite3", 85 | "NAME": get_install_dir() / "db.sqlite3", 86 | } 87 | } 88 | 89 | 90 | # Password validation 91 | # https://docs.djangoproject.com/en/4.1/ref/settings/#auth-password-validators 92 | 93 | AUTH_PASSWORD_VALIDATORS = [ 94 | { 95 | "NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator", 96 | }, 97 | { 98 | "NAME": "django.contrib.auth.password_validation.MinimumLengthValidator", 99 | }, 100 | { 101 | "NAME": "django.contrib.auth.password_validation.CommonPasswordValidator", 102 | }, 103 | { 104 | "NAME": "django.contrib.auth.password_validation.NumericPasswordValidator", 105 | }, 106 | ] 107 | 108 | 109 | # Internationalization 110 | # https://docs.djangoproject.com/en/4.1/topics/i18n/ 111 | 112 | LANGUAGE_CODE = "en-us" 113 | 114 | TIME_ZONE = "UTC" 115 | 116 | USE_I18N = True 117 | 118 | USE_TZ = True 119 | 120 | 121 | # Static files (CSS, JavaScript, Images) 122 | # https://docs.djangoproject.com/en/4.1/howto/static-files/ 123 | 124 | STATIC_URL = "static/" 125 | STATIC_ROOT = get_install_dir() / "static" 126 | 127 | # Default primary key field type 128 | # https://docs.djangoproject.com/en/4.1/ref/settings/#default-auto-field 129 | 130 | DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" 131 | 132 | # Logging 133 | LOGGING = { 134 | "version": 1, 135 | "disable_existing_loggers": False, 136 | "handlers": { 137 | "console": { 138 | "level": "DEBUG", 139 | "class": "logging.StreamHandler", 140 | }, 141 | "file": { 142 | "level": "DEBUG" if DEBUG else "INFO", 143 | "class": "concurrent_log_handler.ConcurrentRotatingFileHandler", 144 | "filename": get_config_dir() / "openchaver.log", 145 | "formatter": "verbose", 146 | "maxBytes": 1024 * 1024 * 5, # 5 MB 147 | "backupCount": 5, 148 | "encoding": "utf8", 149 | "use_gzip": True, 150 | }, 151 | }, 152 | "formatters": { 153 | "verbose": {"format": "%(asctime)s [%(levelname)s] %(name)s: %(message)s"}, 154 | }, 155 | "loggers": { 156 | "django": { 157 | "handlers": ["console"], 158 | "level": "INFO", 159 | }, 160 | 'core': { 161 | 'handlers': ['console', 'file'], 162 | 'level': 'DEBUG', 163 | }, 164 | 'monitor': { 165 | 'handlers': ['console', 'file'], 166 | 'level': 'DEBUG', 167 | }, 168 | 'openchaver': { 169 | 'handlers': ['console', 'file'], 170 | 'level': 'DEBUG', 171 | }, 172 | }, 173 | } 174 | 175 | REST_FRAMEWORK = { 176 | 'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', 177 | } 178 | 179 | SPECTACULAR_SETTINGS = { 180 | 'TITLE': 'Your Project API', 181 | 'DESCRIPTION': 'Your project description', 182 | 'VERSION': '1.0.0', 183 | 'SERVE_INCLUDE_SCHEMA': False, 184 | 'SWAGGER_UI_DIST': 'SIDECAR', 185 | 'SWAGGER_UI_FAVICON_HREF': 'SIDECAR', 186 | 'REDOC_DIST': 'SIDECAR', 187 | } 188 | 189 | -------------------------------------------------------------------------------- /monitor/window.py: -------------------------------------------------------------------------------- 1 | import logging 2 | import os 3 | import psutil 4 | import time 5 | import numpy as np 6 | import cv2 as cv 7 | import mss 8 | 9 | from core.image_utils import encode_numpy_to_base64 10 | 11 | # Logger 12 | logger = logging.getLogger(__name__) 13 | 14 | 15 | # Exceptions 16 | class NoWindowFound(Exception): 17 | def __init__(self, title=None, message="No Active Window Found"): 18 | self.message = message 19 | self.current_title = title 20 | super().__init__(self.message) 21 | 22 | pass 23 | 24 | 25 | class UnstableWindow(Exception): 26 | def __init__(self, title=None, message="Window is Unstable"): 27 | self.message = message 28 | self.current_title = title 29 | super().__init__(self.message) 30 | 31 | pass 32 | 33 | 34 | class WindowDestroyed(Exception): 35 | """Window has been destroyed""" 36 | 37 | pass 38 | 39 | 40 | class WindowBase: 41 | def __init__(self) -> None: 42 | self.nsfw_detections = None 43 | self.is_nsfw = False 44 | 45 | def __str__(self): 46 | return self.title 47 | 48 | def __repr__(self): 49 | return self.title 50 | 51 | def take_screenshot(self) -> str: 52 | """Get a screenshot of the window""" 53 | 54 | # Get the coordinates of the window 55 | coordinates = self.get_coordinates() 56 | 57 | # Get the image 58 | with mss.mss() as sct: 59 | image = sct.grab(coordinates) 60 | image = np.array(image)[:, :, :3] # Remove alpha channel 61 | 62 | # Scale image to self.DEFAULT_DPI DPI 63 | if self.dpi != self.DEFAULT_DPI: 64 | logger.debug(f"Scaling image to {self.DEFAULT_DPI} DPI") 65 | scale = self.dpi / self.DEFAULT_DPI 66 | image = cv.resize(image, None, fx=scale, fy=scale) 67 | 68 | return encode_numpy_to_base64(image) 69 | 70 | def stable_check(self) -> None: 71 | """Check if the window is stable""" 72 | try: 73 | window_2 = self.__class__.get_active_window(recursive=True) 74 | if window_2.title != self.title: 75 | raise UnstableWindow(window_2.title) 76 | except UnstableWindow: 77 | raise 78 | except: # noqa: E722 79 | raise UnstableWindow 80 | 81 | 82 | if os.name == "nt": 83 | import ctypes 84 | import win32ui 85 | import win32process as wproc 86 | 87 | 88 | class Window(WindowBase): 89 | DEFAULT_DPI = 96 90 | user32 = ctypes.windll.user32 91 | 92 | def __init__(self, hwnd): 93 | super().__init__() 94 | 95 | self.hwnd = hwnd 96 | self.title = "Unknown Title" 97 | self.exec_name = "Unknown Executable" 98 | self.pid = -1 99 | 100 | # Get the window title 101 | try: 102 | self.title = self.hwnd.GetWindowText() 103 | except: # noqa: E722 104 | logger.exception("Unable to get window title") 105 | 106 | # Get the window process ID and executable path 107 | try: 108 | self.pid = wproc.GetWindowThreadProcessId(self.hwnd.GetSafeHwnd())[1] 109 | self.exec_name = psutil.Process(self.pid).name() 110 | except: # noqa: E722 111 | logger.exception("Unable to get window pid | exec_name") 112 | 113 | # Get the window DPI 114 | try: 115 | self.dpi = self.user32.GetDpiForWindow(self.hwnd.GetSafeHwnd()) 116 | except: # noqa: E722 117 | logger.exception( 118 | f"Unable to get window DPI - defaulting to {self.DEFAULT_DPI}" # noqa: E501 119 | ) 120 | self.dpi = self.DEFAULT_DPI 121 | 122 | def get_coordinates(self): 123 | """Get the coordinates of the window""" 124 | 125 | # The following code is used to calculate the coordinates of 126 | # the window with the border monitor pixels removed 127 | try: 128 | client_rect = self.hwnd.GetClientRect() 129 | logger.debug(f"Client rect: {client_rect}") 130 | 131 | window_rect = self.hwnd.GetWindowRect() 132 | logger.debug(f"Window rect: {window_rect}") 133 | client_width = client_rect[2] - client_rect[0] 134 | window_width = window_rect[2] - window_rect[0] 135 | border = (window_width - client_width) // 2 136 | 137 | coordinates = ( 138 | window_rect[0] + border, 139 | window_rect[1] + border, 140 | window_rect[2] - border, 141 | window_rect[3] - border, 142 | ) 143 | logger.debug(f"Calculated coordinates: {coordinates}") 144 | 145 | return coordinates 146 | except: # noqa: E722 147 | logger.exception("Unable to get window coordinates") 148 | raise WindowDestroyed 149 | 150 | @classmethod 151 | def get_active_window( 152 | cls, 153 | invalid_title: str | None = None, 154 | stable: bool | int = False, 155 | recursive: bool = False, 156 | ): 157 | """Get the active window""" 158 | try: 159 | # Get the active window 160 | hwnd = win32ui.GetForegroundWindow() 161 | window = cls(hwnd) 162 | 163 | # Check for an invalid title 164 | if window.title == invalid_title: 165 | raise NoWindowFound(window.title) 166 | 167 | if not recursive: 168 | logger.debug(f"Active window: {window.title}") 169 | 170 | # Check if the window is stable 171 | for _ in range(int(stable)): 172 | window.stable_check() 173 | time.sleep(1) 174 | 175 | return window 176 | 177 | except UnstableWindow: 178 | raise 179 | except NoWindowFound: 180 | raise 181 | except: # noqa: E722 182 | raise NoWindowFound 183 | 184 | else: 185 | print("Unsupported OS") 186 | exit(1) 187 | -------------------------------------------------------------------------------- /core/image_utils.py: -------------------------------------------------------------------------------- 1 | import logging 2 | 3 | import cv2 as cv 4 | import base64 5 | import numpy as np 6 | 7 | logger = logging.getLogger(__name__) 8 | 9 | 10 | def encode_numpy_to_base64(img: np.ndarray) -> str: 11 | """ 12 | Encode a numpy array to base64. 13 | """ 14 | 15 | return base64.b64encode(cv.imencode(".png", img)[1]).decode() 16 | 17 | 18 | def decode_base64_to_numpy(str: str) -> np.ndarray: 19 | """ 20 | Decode a base64 string to a numpy array. 21 | """ 22 | 23 | return cv.imdecode(np.frombuffer(base64.b64decode(str), np.uint8), -1) 24 | 25 | 26 | def color_in_image(img: np.ndarray) -> bool: 27 | """Check if the image has color""" 28 | return ( 29 | np.count_nonzero(img[:, :, 0] - img[:, :, 1]) > 0 30 | or np.count_nonzero(img[:, :, 1] - img[:, :, 2]) > 0 31 | ) 32 | 33 | 34 | def deblot_image(mask: np.ndarray, min_size: float): 35 | """Remove small blobs from an image.""" 36 | import cv2 as cv 37 | 38 | ( 39 | nb_blobs, 40 | im_with_separated_blobs, 41 | stats, 42 | _, 43 | ) = cv.connectedComponentsWithStats( # noqa E501 44 | mask 45 | ) 46 | sizes = stats[:, -1] 47 | sizes = sizes[1:] 48 | nb_blobs -= 1 49 | im_result = np.zeros((mask.shape)) 50 | for blob in range(nb_blobs): 51 | if sizes[blob] >= min_size: 52 | im_result[im_with_separated_blobs == blob + 1] = 255 53 | mask = im_result.astype(np.uint8) 54 | return mask 55 | 56 | 57 | def count_skin_pixels(image: np.ndarray): 58 | """Count the number of pixels in the image which are skin colored""" 59 | import cv2 as cv 60 | 61 | lower = np.array([0, 48, 80], dtype="uint8") 62 | upper = np.array([20, 255, 255], dtype="uint8") 63 | converted = cv.cvtColor(image, cv.COLOR_BGR2HSV) 64 | skin_mask = cv.inRange(converted, lower, upper) 65 | kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (11, 11)) 66 | skin_mask = cv.erode(skin_mask, kernel, iterations=2) 67 | skin_mask = cv.dilate(skin_mask, kernel, iterations=2) 68 | skin_mask = deblot_image(skin_mask, 250) 69 | return np.sum(skin_mask) 70 | 71 | 72 | def contains_skin(img: np.ndarray, thresh=1.5) -> bool: 73 | """Check if the image contains skin beyond a certain threshold""" 74 | logger.debug("checking if image contains skin") 75 | 76 | # Return True if the image is completely black and white 77 | color = color_in_image(img) 78 | logger.debug(f"B&W: {not color}") 79 | if not color: 80 | return True 81 | 82 | skin_pixel_count = count_skin_pixels(img) 83 | skin_ratio = skin_pixel_count / (img.shape[0] * img.shape[1]) 84 | logger.debug(f"Skin ratio: {skin_ratio}") 85 | return skin_ratio > thresh 86 | 87 | 88 | def get_bounding_boxes(image: np.ndarray) -> list: 89 | # Check if there are skin pixels in the image 90 | # This is done to remove images that are definitely not NSFW 91 | if not contains_skin(image, thresh=0.5): 92 | logger.debug("Image does not contain skin. Skipping...") 93 | return [] 94 | 95 | # Remove all parts of the image that are 96 | # very similar to their neighbors 97 | gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY) 98 | shift_r = np.roll(gray, 1, axis=1) 99 | shift_l = np.roll(gray, 1 * -1, axis=1) 100 | shift_u = np.roll(gray, 1, axis=0) 101 | shift_d = np.roll(gray, 1 * -1, axis=0) 102 | diff_r = np.absolute(gray - shift_r) 103 | diff_l = np.absolute(gray - shift_l) 104 | diff_u = np.absolute(gray - shift_u) 105 | diff_d = np.absolute(gray - shift_d) 106 | diff = diff_r * diff_l * diff_u * diff_d 107 | _, mask = cv.threshold(diff, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU) 108 | 109 | # Kernel for morphological operations 110 | # Relative to the size of the image 111 | kernel_size = int(image.shape[0] * 0.005) 112 | kernel = cv.getStructuringElement(cv.MORPH_ELLIPSE, (kernel_size, kernel_size)) 113 | 114 | # Morphological operations on the mask 115 | mask = cv.morphologyEx(mask, cv.MORPH_CLOSE, kernel, iterations=1) 116 | 117 | # Deblot the mask 118 | min_size = 0.0025 * mask.shape[0] * mask.shape[1] 119 | mask = deblot_image(mask, min_size=min_size) 120 | 121 | # Morphological operations on the mask 122 | mask = cv.morphologyEx(mask, cv.MORPH_CLOSE, kernel, iterations=1) 123 | 124 | # Apply the mask to the image 125 | masked_image = cv.bitwise_and(image, image, mask=mask) 126 | 127 | # Detect individual images 128 | max_aspect_ratio = 3 129 | gray = cv.cvtColor(masked_image, cv.COLOR_BGR2GRAY) 130 | contours, _ = cv.findContours(gray, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE) 131 | contours = [c for c in contours if cv.contourArea(c) > min_size] 132 | bounding_boxes = [] 133 | for cnt in contours: 134 | x, y, w, h = cv.boundingRect(cnt) 135 | # If the images aspect ratio is very narrow or very wide, skip it 136 | if w / h > max_aspect_ratio or w / h < max_aspect_ratio * 0.1: 137 | continue 138 | bounding_boxes.append((x, y, w, h)) 139 | 140 | filtered_bounding_boxes = [] # Images with a skin ratio above 5 141 | 142 | for x, y, w, h in bounding_boxes: 143 | sub_image = image[y : y + h, x : x + w] 144 | if contains_skin(sub_image, thresh=5): 145 | filtered_bounding_boxes.append((x, y, w, h)) 146 | 147 | logger.debug(f"Found {len(filtered_bounding_boxes)} images") 148 | 149 | return filtered_bounding_boxes 150 | 151 | def match_size(images: list[np.ndarray]) -> list[np.ndarray]: 152 | """ 153 | Resize images to the size of the largest 154 | image by adding black borders 155 | """ 156 | max_width = max([img.shape[1] for img in images]) 157 | max_height = max([img.shape[0] for img in images]) 158 | resized_images = [] 159 | for img in images: 160 | if img.shape[1] < max_width or img.shape[0] < max_height: 161 | resized_images.append( 162 | cv.copyMakeBorder( 163 | img, 164 | 0, 165 | max_height - img.shape[0], 166 | 0, 167 | max_width - img.shape[1], 168 | cv.BORDER_CONSTANT, 169 | value=[0, 0, 0], 170 | )) 171 | else: 172 | resized_images.append(img) 173 | return resized_images 174 | 175 | def compute_resize_scale(image_shape, min_side=800, max_side=1333): 176 | """Compute the scale to resize an image to a given size""" 177 | (rows, cols, _) = image_shape 178 | smallest_side = min(rows, cols) 179 | scale = min_side / smallest_side 180 | largest_side = max(rows, cols) 181 | if largest_side * scale > max_side: 182 | scale = max_side / largest_side 183 | return scale 184 | 185 | def resize_image(img, min_side=800, max_side=1333): 186 | """Resize an image""" 187 | import cv2 as cv 188 | scale = compute_resize_scale(img.shape, 189 | min_side=min_side, 190 | max_side=max_side) 191 | img = cv.resize(img, None, fx=scale, fy=scale) 192 | return img, scale -------------------------------------------------------------------------------- /core/nudity.py: -------------------------------------------------------------------------------- 1 | import logging 2 | from pathlib import Path 3 | 4 | import numpy as np 5 | from PIL import Image 6 | import cv2 as cv 7 | 8 | from openchaver.dirs import get_data_dir 9 | from .image_utils import match_size, resize_image 10 | 11 | 12 | model_dir = get_data_dir() / "models" 13 | model_dir.mkdir(parents=True, exist_ok=True) 14 | 15 | DETECTION_MODEL_URL = 'https://pub-43a5d92b0b0b4908a9aec2a745986a23.r2.dev/detector_v2_default_checkpoint.onnx' # noqa: E501 16 | DETECTION_MODEL_SHA256_HASH = "D4BE1C504BE61851D9745E6DA8FA09455EB39B8856626DD6B5CA413C9E8B1578" # noqa: E501 17 | DETECTION_MODEL_PATH = model_dir / 'detect.onnx' 18 | 19 | CLASSIFICATION_MODEL_URL = 'https://pub-43a5d92b0b0b4908a9aec2a745986a23.r2.dev/open-nsfw.onnx' # noqa: E501 20 | CLASSIFICATION_MODEL_SHA256_HASH = "864BB37BF8863564B87EB330AB8C785A79A773F4E7C43CB96DB52ED8611305FA" # noqa: E501 21 | CLASSIFICATION_MODEL_PATH = model_dir / 'classify.onnx' 22 | 23 | logger = logging.getLogger(__name__) 24 | 25 | def test_model(model_path: Path) -> bool: 26 | """Test if a model can be loaded""" 27 | import onnxruntime 28 | try: 29 | onnxruntime.InferenceSession(str(model_path), 30 | providers=["CPUExecutionProvider"]) 31 | return True 32 | except: # noqa E722 33 | logger.error("Failed to load model") 34 | return False 35 | 36 | def chech_hash(path: Path, hash: str) -> bool: 37 | """Check the hash of a file""" 38 | import hashlib 39 | if not path.exists(): 40 | return False 41 | with open(path, "rb") as f: 42 | file_hash = hashlib.sha256() 43 | while chunk := f.read(8192): 44 | file_hash.update(chunk) 45 | return file_hash.hexdigest().upper() == hash 46 | 47 | def download_model(url, path: Path, hash=None): 48 | """Download the model""" 49 | import requests 50 | 51 | logger.info("Downloading model...") 52 | try: 53 | # Download 54 | response = requests.get(url, stream=True, verify=False) 55 | with open(path, "wb") as handle: 56 | for data in response.iter_content(chunk_size=8192): 57 | handle.write(data) 58 | 59 | # Check hash 60 | if hash: 61 | if not chech_hash(path, hash): 62 | raise Exception("Hash mismatch") 63 | 64 | # Test Load model 65 | if not test_model(path): 66 | raise Exception("Failed to load model") 67 | 68 | except: # noqa E722 69 | logger.error("Failed to download model") 70 | path.unlink(missing_ok=True) 71 | raise 72 | 73 | class Detector: 74 | 75 | def __init__(self): 76 | import onnxruntime 77 | model_file = DETECTION_MODEL_PATH 78 | logger.debug(f"Loading detection model from {model_file}") 79 | 80 | if not model_file.exists(): 81 | logger.debug( 82 | f"Downloading detection model from {DETECTION_MODEL_URL}") 83 | download_model(DETECTION_MODEL_URL, model_file, 84 | DETECTION_MODEL_SHA256_HASH) 85 | 86 | self.detection_model = onnxruntime.InferenceSession( 87 | str(model_file), providers=["CPUExecutionProvider"]) 88 | 89 | self.classes = [ 90 | "EXPOSED_ANUS", 91 | "EXPOSED_ARMPITS", 92 | "COVERED_BELLY", 93 | "EXPOSED_BELLY", 94 | "COVERED_BUTTOCKS", 95 | "EXPOSED_BUTTOCKS", 96 | "FACE_F", 97 | "FACE_M", 98 | "COVERED_FEET", 99 | "EXPOSED_FEET", 100 | "COVERED_BREAST_F", 101 | "EXPOSED_BREAST_F", 102 | "COVERED_GENITALIA_F", 103 | "EXPOSED_GENITALIA_F", 104 | "EXPOSED_BREAST_M", 105 | "EXPOSED_GENITALIA_M", 106 | ] 107 | 108 | def detect( 109 | self, 110 | images: list[np.ndarray], 111 | min_prob=None, 112 | fast=True, 113 | batch_size=5, 114 | ) -> list[dict]: 115 | """Detect objects in an image.""" 116 | 117 | # Function to preprocess the image 118 | def preprocess_image( 119 | image_path, 120 | min_side, 121 | max_side, 122 | ): 123 | image = np.ascontiguousarray( 124 | Image.fromarray(image_path))[:, :, ::-1] 125 | image = image.astype(np.float32) 126 | image -= [103.939, 116.779, 123.68] 127 | image, scale = resize_image(image, 128 | min_side=min_side, 129 | max_side=max_side) 130 | return image, scale 131 | 132 | # Match size 133 | images = match_size(images) 134 | 135 | # Preprocess images 136 | preprocessed_images = [ 137 | preprocess_image(img, 138 | min_side=480 if fast else 800, 139 | max_side=800 if fast else 1333) for img in images 140 | ] 141 | # Show images 142 | 143 | min_prob = 0.5 if fast else 0.6 144 | scale = preprocessed_images[0][1] 145 | preprocessed_images = [p[0] for p in preprocessed_images] 146 | results = [] 147 | 148 | while len(preprocessed_images): 149 | batch = preprocessed_images[:batch_size] 150 | preprocessed_images = preprocessed_images[batch_size:] 151 | outputs = self.detection_model.run( 152 | [s_i.name for s_i in self.detection_model.get_outputs()], 153 | {self.detection_model.get_inputs()[0].name: np.asarray(batch)}, 154 | ) 155 | 156 | labels = [op for op in outputs if op.dtype == "int32"][0] 157 | scores = [ 158 | op for op in outputs if isinstance(op[0][0], np.float32) 159 | ][0] # type: ignore 160 | boxes = [op for op in outputs 161 | if isinstance(op[0][0], np.ndarray)][0] 162 | boxes /= scale 163 | 164 | for frame_boxes, frame_scores, frame_labels in zip( 165 | boxes, 166 | scores, 167 | labels, 168 | ): 169 | frame_result = {"detections": [], 'is_nsfw': False} 170 | for box, score, label in zip(frame_boxes, frame_scores, 171 | frame_labels): 172 | if score < min_prob: 173 | continue 174 | box = box.astype(int).tolist() 175 | label = self.classes[label] 176 | detection = { 177 | "box": [int(c) for c in box], 178 | "score": float(score), 179 | "label": label, 180 | } 181 | frame_result["detections"].append(detection) 182 | 183 | is_nsfw = self._eval_detection(frame_result["detections"]) 184 | frame_result["is_nsfw"] = is_nsfw 185 | results.append(frame_result) 186 | 187 | return results 188 | 189 | def _eval_detection(self, result, threshold=0.6) -> bool: 190 | nsfw_labels = [ 191 | "EXPOSED_ANUS", 192 | "EXPOSED_BUTTOCKS", 193 | "EXPOSED_BREAST_F", 194 | "EXPOSED_GENITALIA_F", 195 | "EXPOSED_GENITALIA_M", 196 | ] 197 | for detection in result: 198 | if detection[ 199 | "label"] in nsfw_labels and detection["score"] > threshold: 200 | return True 201 | return False 202 | 203 | def is_nsfw(self, img: np.ndarray) -> dict: 204 | """Detect objects in an image.""" 205 | return self.detect([img])[0] 206 | 207 | class Classifier: 208 | 209 | def __init__(self): 210 | model_file = CLASSIFICATION_MODEL_PATH 211 | logger.info(f"Loading classification model from {model_file}") 212 | 213 | if not model_file.exists(): 214 | logger.info( 215 | f"Downloading classification model from {CLASSIFICATION_MODEL_URL}" # noqa: E501 216 | ) 217 | download_model(CLASSIFICATION_MODEL_URL, model_file, 218 | CLASSIFICATION_MODEL_SHA256_HASH) 219 | 220 | self.lite_model = cv.dnn.readNet(str(model_file)) 221 | 222 | def classify(self, images: list[np.ndarray], threshold=0.6): 223 | """Classify an image.""" 224 | 225 | # Preprocess images 226 | # Copied from 227 | # https://pypi.org/project/opennsfw-standalone/ 228 | preprocessed_images = [] 229 | for image in images: 230 | 231 | image = cv.resize(image, (224, 224), interpolation=cv.INTER_LINEAR) 232 | 233 | image_jpeg_data = image.astype(np.float32, copy=False) 234 | 235 | image_jpeg_data -= np.array([104, 117, 123], dtype=np.float32) 236 | 237 | image_jpeg_data = np.expand_dims(image_jpeg_data, axis=0) 238 | 239 | preprocessed_images.append(image_jpeg_data) 240 | 241 | # Run model 242 | for image in preprocessed_images: 243 | self.lite_model.setInput(image) 244 | result = self.lite_model.forward() 245 | yield result[0][1] > threshold 246 | 247 | def is_nsfw(self, image: np.ndarray, threshold=0.6) -> bool: 248 | """Classify an image.""" 249 | return next(self.classify([image], threshold)) -------------------------------------------------------------------------------- /core/profanity.py: -------------------------------------------------------------------------------- 1 | import re 2 | 3 | BAD_WORDS = [ 4 | "2 girls 1 cup", 5 | "2g1c", 6 | "4r5e", 7 | "5h1t", 8 | "5hit", 9 | "a55", 10 | "a_s_s", 11 | "acrotomophilia", 12 | "alabama hot pocket", 13 | "alaskan pipeline", 14 | "anal", 15 | "anilingus", 16 | "anus", 17 | "apeshit", 18 | "ar5e", 19 | "arrse", 20 | "arse", 21 | "arsehole", 22 | "ass", 23 | "ass-fucker", 24 | "ass-hat", 25 | "ass-pirate", 26 | "assbag", 27 | "assbandit", 28 | "assbanger", 29 | "assbite", 30 | "assclown", 31 | "asscock", 32 | "asscracker", 33 | "asses", 34 | "assface", 35 | "assfucker", 36 | "assfukka", 37 | "assgoblin", 38 | "asshat", 39 | "asshead", 40 | "asshole", 41 | "assholes", 42 | "asshopper", 43 | "assjacker", 44 | "asslick", 45 | "asslicker", 46 | "assmonkey", 47 | "assmunch", 48 | "assmuncher", 49 | "asspirate", 50 | "assshole", 51 | "asssucker", 52 | "asswad", 53 | "asswhole", 54 | "asswipe", 55 | "auto erotic", 56 | "autoerotic", 57 | "b!tch", 58 | "b00bs", 59 | "b17ch", 60 | "b1tch", 61 | "babeland", 62 | "baby batter", 63 | "baby juice", 64 | "ball gag", 65 | "ball gravy", 66 | "ball kicking", 67 | "ball licking", 68 | "ball sack", 69 | "ball sucking", 70 | "ballbag", 71 | "balls", 72 | "ballsack", 73 | "bampot", 74 | "bangbros", 75 | "bareback", 76 | "barely legal", 77 | "barenaked", 78 | "bastard", 79 | "bastardo", 80 | "bastinado", 81 | "bbw", 82 | "bdsm", 83 | "beaner", 84 | "beaners", 85 | "beastial", 86 | "beastiality", 87 | "beastility", 88 | "beaver cleaver", 89 | "beaver lips", 90 | "bellend", 91 | "bestial", 92 | "bestiality", 93 | "bi+ch", 94 | "biatch", 95 | "big black", 96 | "big breasts", 97 | "big knockers", 98 | "big tits", 99 | "bimbos", 100 | "birdlock", 101 | "bitch", 102 | "bitcher", 103 | "bitchers", 104 | "bitches", 105 | "bitchin", 106 | "bitching", 107 | "black cock", 108 | "blonde action", 109 | "blonde on blonde action", 110 | "bloody", 111 | "blow job", 112 | "blow your load", 113 | "blowjob", 114 | "blowjobs", 115 | "blue waffle", 116 | "blumpkin", 117 | "boiolas", 118 | "bollock", 119 | "bollocks", 120 | "bollok", 121 | "bollox", 122 | "bondage", 123 | "boner", 124 | "boob", 125 | "boobie", 126 | "boobs", 127 | "booobs", 128 | "boooobs", 129 | "booooobs", 130 | "booooooobs", 131 | "booty call", 132 | "breasts", 133 | "brown showers", 134 | "brunette action", 135 | "buceta", 136 | "bugger", 137 | "bukkake", 138 | "bulldyke", 139 | "bullet vibe", 140 | "bullshit", 141 | "bum", 142 | "bung hole", 143 | "bunghole", 144 | "bunny fucker", 145 | "busty", 146 | "butt", 147 | "butt-pirate", 148 | "buttcheeks", 149 | "butthole", 150 | "buttmunch", 151 | "buttplug", 152 | "c0ck", 153 | "c0cksucker", 154 | "camel toe", 155 | "camgirl", 156 | "camslut", 157 | "camwhore", 158 | "carpet muncher", 159 | "carpetmuncher", 160 | "cawk", 161 | "chinc", 162 | "chink", 163 | "choad", 164 | "chocolate rosebuds", 165 | "chode", 166 | "cipa", 167 | "circlejerk", 168 | "cl1t", 169 | "cleveland steamer", 170 | "clit", 171 | "clitface", 172 | "clitoris", 173 | "clits", 174 | "clover clamps", 175 | "clusterfuck", 176 | "cnut", 177 | "cock", 178 | "cock-sucker", 179 | "cockbite", 180 | "cockburger", 181 | "cockface", 182 | "cockhead", 183 | "cockjockey", 184 | "cockknoker", 185 | "cockmaster", 186 | "cockmongler", 187 | "cockmongruel", 188 | "cockmonkey", 189 | "cockmunch", 190 | "cockmuncher", 191 | "cocknose", 192 | "cocknugget", 193 | "cocks", 194 | "cockshit", 195 | "cocksmith", 196 | "cocksmoker", 197 | "cocksuck", 198 | "cocksuck ", 199 | "cocksucked", 200 | "cocksucked ", 201 | "cocksucker", 202 | "cocksucking", 203 | "cocksucks ", 204 | "cocksuka", 205 | "cocksukka", 206 | "cok", 207 | "cokmuncher", 208 | "coksucka", 209 | "coochie", 210 | "coochy", 211 | "coon", 212 | "coons", 213 | "cooter", 214 | "coprolagnia", 215 | "coprophilia", 216 | "cornhole", 217 | "cox", 218 | "crap", 219 | "creampie", 220 | "cum", 221 | "cumbubble", 222 | "cumdumpster", 223 | "cumguzzler", 224 | "cumjockey", 225 | "cummer", 226 | "cumming", 227 | "cums", 228 | "cumshot", 229 | "cumslut", 230 | "cumtart", 231 | "cunilingus", 232 | "cunillingus", 233 | "cunnie", 234 | "cunnilingus", 235 | "cunt", 236 | "cuntface", 237 | "cunthole", 238 | "cuntlick", 239 | "cuntlick ", 240 | "cuntlicker", 241 | "cuntlicker ", 242 | "cuntlicking", 243 | "cuntlicking ", 244 | "cuntrag", 245 | "cunts", 246 | "cyalis", 247 | "cyberfuc", 248 | "cyberfuck ", 249 | "cyberfucked ", 250 | "cyberfucker", 251 | "cyberfuckers", 252 | "cyberfucking ", 253 | "d1ck", 254 | "dammit", 255 | "damn", 256 | "darkie", 257 | "date rape", 258 | "daterape", 259 | "deep throat", 260 | "deepthroat", 261 | "dendrophilia", 262 | "dick", 263 | "dickbag", 264 | "dickbeater", 265 | "dickface", 266 | "dickhead", 267 | "dickhole", 268 | "dickjuice", 269 | "dickmilk", 270 | "dickmonger", 271 | "dickslap", 272 | "dicksucker", 273 | "dickwad", 274 | "dickweasel", 275 | "dickweed", 276 | "dickwod", 277 | "dike", 278 | "dildo", 279 | "dildos", 280 | "dingleberries", 281 | "dingleberry", 282 | "dink", 283 | "dinks", 284 | "dipshit", 285 | "dirsa", 286 | "dirty pillows", 287 | "dirty sanchez", 288 | "dlck", 289 | "dog style", 290 | "dog-fucker", 291 | "doggie style", 292 | "doggiestyle", 293 | "doggin", 294 | "dogging", 295 | "doggy style", 296 | "doggystyle", 297 | "dolcett", 298 | "domination", 299 | "dominatrix", 300 | "dommes", 301 | "donkey punch", 302 | "donkeyribber", 303 | "doochbag", 304 | "dookie", 305 | "doosh", 306 | "double dong", 307 | "double penetration", 308 | "douche", 309 | "douchebag", 310 | "dp action", 311 | "dry hump", 312 | "duche", 313 | "dumbshit", 314 | "dumshit", 315 | "dvda", 316 | "dyke", 317 | "eat my ass", 318 | "ecchi", 319 | "ejaculate", 320 | "ejaculated", 321 | "ejaculates ", 322 | "ejaculating ", 323 | "ejaculatings", 324 | "ejaculation", 325 | "ejakulate", 326 | "erotic", 327 | "erotism", 328 | "escort", 329 | "eunuch", 330 | "f u c k", 331 | "f u c k e r", 332 | "f4nny", 333 | "f_u_c_k", 334 | "fag", 335 | "fagbag", 336 | "fagg", 337 | "fagging", 338 | "faggit", 339 | "faggitt", 340 | "faggot", 341 | "faggs", 342 | "fagot", 343 | "fagots", 344 | "fags", 345 | "fagtard", 346 | "fanny", 347 | "fannyflaps", 348 | "fannyfucker", 349 | "fanyy", 350 | "fart", 351 | "farted", 352 | "farting", 353 | "farty", 354 | "fatass", 355 | "fcuk", 356 | "fcuker", 357 | "fcuking", 358 | "fecal", 359 | "feck", 360 | "fecker", 361 | "felatio", 362 | "felch", 363 | "felching", 364 | "fellate", 365 | "fellatio", 366 | "feltch", 367 | "female squirting", 368 | "femdom", 369 | "figging", 370 | "fingerbang", 371 | "fingerfuck ", 372 | "fingerfucked ", 373 | "fingerfucker ", 374 | "fingerfuckers", 375 | "fingerfucking ", 376 | "fingerfucks ", 377 | "fingering", 378 | "fistfuck", 379 | "fistfucked ", 380 | "fistfucker ", 381 | "fistfuckers ", 382 | "fistfucking ", 383 | "fistfuckings ", 384 | "fistfucks ", 385 | "fisting", 386 | "flamer", 387 | "flange", 388 | "fook", 389 | "fooker", 390 | "foot fetish", 391 | "footjob", 392 | "frotting", 393 | "fuck", 394 | "fuck buttons", 395 | "fucka", 396 | "fucked", 397 | "fucker", 398 | "fuckers", 399 | "fuckhead", 400 | "fuckheads", 401 | "fuckin", 402 | "fucking", 403 | "fuckings", 404 | "fuckingshitmotherfucker", 405 | "fuckme ", 406 | "fucks", 407 | "fucktards", 408 | "fuckwhit", 409 | "fuckwit", 410 | "fudge packer", 411 | "fudgepacker", 412 | "fuk", 413 | "fuker", 414 | "fukker", 415 | "fukkin", 416 | "fuks", 417 | "fukwhit", 418 | "fukwit", 419 | "futanari", 420 | "fux", 421 | "fux0r", 422 | "g-spot", 423 | "gang bang", 424 | "gangbang", 425 | "gangbanged", 426 | "gangbanged ", 427 | "gangbangs ", 428 | "gay sex", 429 | "gayass", 430 | "gaybob", 431 | "gaydo", 432 | "gaylord", 433 | "gaysex", 434 | "gaytard", 435 | "gaywad", 436 | "genitals", 437 | "giant cock", 438 | "girl on", 439 | "girl on top", 440 | "girls gone wild", 441 | "goatcx", 442 | "goatse", 443 | "god damn", 444 | "god-dam", 445 | "god-damned", 446 | "goddamn", 447 | "goddamned", 448 | "gokkun", 449 | "golden shower", 450 | "goo girl", 451 | "gooch", 452 | "goodpoop", 453 | "gook", 454 | "goregasm", 455 | "gringo", 456 | "grope", 457 | "group sex", 458 | "guido", 459 | "guro", 460 | "hand job", 461 | "handjob", 462 | "hard core", 463 | "hardcore", 464 | "hardcoresex ", 465 | "heeb", 466 | "hell", 467 | "hentai", 468 | "heshe", 469 | "ho", 470 | "hoar", 471 | "hoare", 472 | "hoe", 473 | "hoer", 474 | "homo", 475 | "homoerotic", 476 | "honkey", 477 | "honky", 478 | "hooker", 479 | "hore", 480 | "horniest", 481 | "horny", 482 | "hot carl", 483 | "hot chick", 484 | "hotsex", 485 | "how to kill", 486 | "how to murder", 487 | "huge fat", 488 | "humping", 489 | "incest", 490 | "intercourse", 491 | "jack off", 492 | "jack-off ", 493 | "jackass", 494 | "jackoff", 495 | "jail bait", 496 | "jailbait", 497 | "jap", 498 | "jelly donut", 499 | "jerk off", 500 | "jerk-off ", 501 | "jigaboo", 502 | "jiggaboo", 503 | "jiggerboo", 504 | "jism", 505 | "jiz", 506 | "jiz ", 507 | "jizm", 508 | "jizm ", 509 | "jizz", 510 | "juggs", 511 | "kawk", 512 | "kike", 513 | "kinbaku", 514 | "kinkster", 515 | "kinky", 516 | "kiunt", 517 | "knob", 518 | "knobbing", 519 | "knobead", 520 | "knobed", 521 | "knobend", 522 | "knobhead", 523 | "knobjocky", 524 | "knobjokey", 525 | "kock", 526 | "kondum", 527 | "kondums", 528 | "kooch", 529 | "kootch", 530 | "kum", 531 | "kumer", 532 | "kummer", 533 | "kumming", 534 | "kums", 535 | "kunilingus", 536 | "kunt", 537 | "kyke", 538 | "l3i+ch", 539 | "l3itch", 540 | "labia", 541 | "leather restraint", 542 | "leather straight jacket", 543 | "lemon party", 544 | "lesbo", 545 | "lezzie", 546 | "lmfao", 547 | "lolita", 548 | "lovemaking", 549 | "lust", 550 | "lusting", 551 | "m0f0", 552 | "m0fo", 553 | "m45terbate", 554 | "ma5terb8", 555 | "ma5terbate", 556 | "make me come", 557 | "male squirting", 558 | "masochist", 559 | "master-bate", 560 | "masterb8", 561 | "masterbat*", 562 | "masterbat3", 563 | "masterbate", 564 | "masterbation", 565 | "masterbations", 566 | "masturbate", 567 | "menage a trois", 568 | "milf", 569 | "minge", 570 | "missionary position", 571 | "mo-fo", 572 | "mof0", 573 | "mofo", 574 | "mothafuck", 575 | "mothafucka", 576 | "mothafuckas", 577 | "mothafuckaz", 578 | "mothafucked ", 579 | "mothafucker", 580 | "mothafuckers", 581 | "mothafuckin", 582 | "mothafucking ", 583 | "mothafuckings", 584 | "mothafucks", 585 | "mother fucker", 586 | "motherfuck", 587 | "motherfucked", 588 | "motherfucker", 589 | "motherfuckers", 590 | "motherfuckin", 591 | "motherfucking", 592 | "motherfuckings", 593 | "motherfuckka", 594 | "motherfucks", 595 | "mound of venus", 596 | "mr hands", 597 | "muff", 598 | "muff diver", 599 | "muffdiver", 600 | "muffdiving", 601 | "mutha", 602 | "muthafecker", 603 | "muthafuckker", 604 | "muther", 605 | "mutherfucker", 606 | "n1gga", 607 | "n1gger", 608 | "nambla", 609 | "nawashi", 610 | "nazi", 611 | "negro", 612 | "neonazi", 613 | "nig nog", 614 | "nigg3r", 615 | "nigg4h", 616 | "nigga", 617 | "niggah", 618 | "niggas", 619 | "niggaz", 620 | "nigger", 621 | "niggers ", 622 | "niglet", 623 | "nimphomania", 624 | "nipple", 625 | "nipples", 626 | "nob", 627 | "nob jokey", 628 | "nobhead", 629 | "nobjocky", 630 | "nobjokey", 631 | "nsfw images", 632 | "nude", 633 | "nudity", 634 | "numbnuts", 635 | "nutsack", 636 | "nympho", 637 | "nymphomania", 638 | "octopussy", 639 | "omorashi", 640 | "one cup two girls", 641 | "one guy one jar", 642 | "orgasim", 643 | "orgasim ", 644 | "orgasims ", 645 | "orgasm", 646 | "orgasms ", 647 | "orgy", 648 | "p0rn", 649 | "paedophile", 650 | "paki", 651 | "panooch", 652 | "panties", 653 | "panty", 654 | "pawn", 655 | "pecker", 656 | "pe", 657 | "phuking", 658 | "phukked", 659 | "phukking", 660 | "phuks", 661 | "phuq", 662 | "piece of shit", 663 | "pigfucker", 664 | "pimpis", 665 | "pis", 666 | "pises", 667 | "pisin", 668 | "pising", 669 | "pisof", 670 | "piss", 671 | "piss pig", 672 | "pissed", 673 | "pisser", 674 | "pissers", 675 | "pisses ", 676 | "pissflap", 677 | "pissflaps", 678 | "pissin", 679 | "pissin ", 680 | "pissing", 681 | "pissoff", 682 | "pissoff ", 683 | "pisspig", 684 | "playboy", 685 | "pleasure chest", 686 | "pole smoker", 687 | "polesmoker", 688 | "pollock", 689 | "ponyplay", 690 | "poo", 691 | "poof", 692 | "poon", 693 | "poonani", 694 | "poonany", 695 | "poontang", 696 | "poop", 697 | "poop chute", 698 | "poopchute", 699 | "porn", 700 | "porno", 701 | "pornography", 702 | "pornos", 703 | "prick", 704 | "pricks ", 705 | "prince albert piercing", 706 | "pron", 707 | "pthc", 708 | "pube", 709 | "pubes", 710 | "punanny", 711 | "punany", 712 | "punta", 713 | "pusies", 714 | "pusse", 715 | "pussi", 716 | "pussies", 717 | "pussy", 718 | "pussylicking", 719 | "pussys ", 720 | "pusy", 721 | "puto", 722 | "queaf", 723 | "queef", 724 | "queerbait", 725 | "queerhole", 726 | "quim", 727 | "raghead", 728 | "raging boner", 729 | "rape", 730 | "raping", 731 | "rapist", 732 | "rectum", 733 | "renob", 734 | "retard", 735 | "reverse cowgirl", 736 | "rimjaw", 737 | "rimjob", 738 | "rimming", 739 | "rosy palm", 740 | "rosy palm and her 5 sisters", 741 | "ruski", 742 | "rusty trombone", 743 | "s hit", 744 | "s&m", 745 | "s.o.b.", 746 | "s_h_i_t", 747 | "sadism", 748 | "sadist", 749 | "santorum", 750 | "scat", 751 | "schlong", 752 | "scissoring", 753 | "screwing", 754 | "scroat", 755 | "scrote", 756 | "scrotum", 757 | "semen", 758 | "sex", 759 | "sexo", 760 | "sexy", 761 | "sh!+", 762 | "sh!t", 763 | "sh1t", 764 | "shag", 765 | "shagger", 766 | "shaggin", 767 | "shagging", 768 | "shaved beaver", 769 | "shaved pussy", 770 | "shemale", 771 | "shi+", 772 | "shibari", 773 | "shit", 774 | "shit-ass", 775 | "shit-bag", 776 | "shit-bagger", 777 | "shit-brain", 778 | "shit-breath", 779 | "shit-cunt", 780 | "shit-dick", 781 | "shit-eating", 782 | "shit-face", 783 | "shit-faced", 784 | "shit-fit", 785 | "shit-head", 786 | "shit-heel", 787 | "shit-hole", 788 | "shit-house", 789 | "shit-load", 790 | "shit-pot", 791 | "shit-spitter", 792 | "shit-stain", 793 | "shitass", 794 | "shitbag", 795 | "shitbagger", 796 | "shitblimp", 797 | "shitbrain", 798 | "shitbreath", 799 | "shitcunt", 800 | "shitdick", 801 | "shite", 802 | "shiteating", 803 | "shited", 804 | "shitey", 805 | "shitface", 806 | "shitfaced", 807 | "shitfit", 808 | "shitfuck", 809 | "shitfull", 810 | "shithead", 811 | "shitheel", 812 | "shithole", 813 | "shithouse", 814 | "shiting", 815 | "shitings", 816 | "shitload", 817 | "shitpot", 818 | "shits", 819 | "shitspitter", 820 | "shitstain", 821 | "shitted", 822 | "shitter", 823 | "shitters ", 824 | "shittiest", 825 | "shitting", 826 | "shittings", 827 | "shitty", 828 | "shitty ", 829 | "shity", 830 | "shiz", 831 | "shiznit", 832 | "shota", 833 | "shrimping", 834 | "skank", 835 | "skeet", 836 | "slanteye", 837 | "slut", 838 | "slutbag", 839 | "sluts", 840 | "smeg", 841 | "smegma", 842 | "smut", 843 | "snatch", 844 | "snowballing", 845 | "sodomize", 846 | "sodomy", 847 | "son-of-a-bitch", 848 | "spac", 849 | "spic", 850 | "spick", 851 | "splooge", 852 | "splooge moose", 853 | "spooge", 854 | "spread legs", 855 | "spunk", 856 | "strap on", 857 | "strapon", 858 | "strappado", 859 | "strip club", 860 | "style doggy", 861 | "suck", 862 | "sucks", 863 | "suicide girls", 864 | "sultry women", 865 | "swastika", 866 | "swinger", 867 | "t1tt1e5", 868 | "t1tties", 869 | "tainted love", 870 | "tard", 871 | "taste my", 872 | "tea bagging", 873 | "teets", 874 | "teez", 875 | "testical", 876 | "testicle", 877 | "threesome", 878 | "throating", 879 | "thundercunt", 880 | "tied up", 881 | "tight white", 882 | "tit", 883 | "titfuck", 884 | "tits", 885 | "titt", 886 | "tittie5", 887 | "tittiefucker", 888 | "titties", 889 | "titty", 890 | "tittyfuck", 891 | "tittywank", 892 | "titwank", 893 | "tongue in a", 894 | "topless", 895 | "tosser", 896 | "towelhead", 897 | "tranny", 898 | "tribadism", 899 | "tub girl", 900 | "tubgirl", 901 | "turd", 902 | "tushy", 903 | "tw4t", 904 | "twat", 905 | "twathead", 906 | "twatlips", 907 | "twatty", 908 | "twink", 909 | "twinkie", 910 | "two girls one cup", 911 | "twunt", 912 | "twunter", 913 | "undressing", 914 | "upskirt", 915 | "urethra play", 916 | "urophilia", 917 | "v14gra", 918 | "v1gra", 919 | "va-j-j", 920 | "vag", 921 | "vagina", 922 | "venus mound", 923 | "viagra", 924 | "vibrator", 925 | "violet wand", 926 | "vjayjay", 927 | "vorarephilia", 928 | "voyeur", 929 | "vulva", 930 | "w00se", 931 | "wang", 932 | "wank", 933 | "wanker", 934 | "wanky", 935 | "wet dream", 936 | "wetback", 937 | "white power", 938 | "whoar", 939 | "whore", 940 | "willies", 941 | "willy", 942 | "wrapping men", 943 | "wrinkled starfish", 944 | "xrated", 945 | "xx", 946 | "xxx", 947 | "yaoi", 948 | "yellow showers", 949 | "yiffy", 950 | "zoophilia", 951 | ] 952 | 953 | def is_profane(s: str) -> bool: 954 | if re.compile( 955 | r"\b" + r"\b|\b".join(BAD_WORDS) + r"\b", 956 | re.IGNORECASE, 957 | ).search(s): 958 | return True 959 | else: 960 | return False -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | Preamble 9 | 10 | The GNU General Public License is a free, copyleft license for 11 | software and other kinds of works. 12 | 13 | The licenses for most software and other practical works are designed 14 | to take away your freedom to share and change the works. By contrast, 15 | the GNU General Public License is intended to guarantee your freedom to 16 | share and change all versions of a program--to make sure it remains free 17 | software for all its users. We, the Free Software Foundation, use the 18 | GNU General Public License for most of our software; it applies also to 19 | any other work released this way by its authors. You can apply it to 20 | your programs, too. 21 | 22 | When we speak of free software, we are referring to freedom, not 23 | price. Our General Public Licenses are designed to make sure that you 24 | have the freedom to distribute copies of free software (and charge for 25 | them if you wish), that you receive source code or can get it if you 26 | want it, that you can change the software or use pieces of it in new 27 | free programs, and that you know you can do these things. 28 | 29 | To protect your rights, we need to prevent others from denying you 30 | these rights or asking you to surrender the rights. Therefore, you have 31 | certain responsibilities if you distribute copies of the software, or if 32 | you modify it: responsibilities to respect the freedom of others. 33 | 34 | For example, if you distribute copies of such a program, whether 35 | gratis or for a fee, you must pass on to the recipients the same 36 | freedoms that you received. You must make sure that they, too, receive 37 | or can get the source code. And you must show them these terms so they 38 | know their rights. 39 | 40 | Developers that use the GNU GPL protect your rights with two steps: 41 | (1) assert copyright on the software, and (2) offer you this License 42 | giving you legal permission to copy, distribute and/or modify it. 43 | 44 | For the developers' and authors' protection, the GPL clearly explains 45 | that there is no warranty for this free software. For both users' and 46 | authors' sake, the GPL requires that modified versions be marked as 47 | changed, so that their problems will not be attributed erroneously to 48 | authors of previous versions. 49 | 50 | Some devices are designed to deny users access to install or run 51 | modified versions of the software inside them, although the manufacturer 52 | can do so. This is fundamentally incompatible with the aim of 53 | protecting users' freedom to change the software. The systematic 54 | pattern of such abuse occurs in the area of products for individuals to 55 | use, which is precisely where it is most unacceptable. Therefore, we 56 | have designed this version of the GPL to prohibit the practice for those 57 | products. If such problems arise substantially in other domains, we 58 | stand ready to extend this provision to those domains in future versions 59 | of the GPL, as needed to protect the freedom of users. 60 | 61 | Finally, every program is threatened constantly by software patents. 62 | States should not allow patents to restrict development and use of 63 | software on general-purpose computers, but in those that do, we wish to 64 | avoid the special danger that patents applied to a free program could 65 | make it effectively proprietary. To prevent this, the GPL assures that 66 | patents cannot be used to render the program non-free. 67 | 68 | The precise terms and conditions for copying, distribution and 69 | modification follow. 70 | 71 | TERMS AND CONDITIONS 72 | 73 | 0. Definitions. 74 | 75 | "This License" refers to version 3 of the GNU General Public License. 76 | 77 | "Copyright" also means copyright-like laws that apply to other kinds of 78 | works, such as semiconductor masks. 79 | 80 | "The Program" refers to any copyrightable work licensed under this 81 | License. Each licensee is addressed as "you". "Licensees" and 82 | "recipients" may be individuals or organizations. 83 | 84 | To "modify" a work means to copy from or adapt all or part of the work 85 | in a fashion requiring copyright permission, other than the making of an 86 | exact copy. The resulting work is called a "modified version" of the 87 | earlier work or a work "based on" the earlier work. 88 | 89 | A "covered work" means either the unmodified Program or a work based 90 | on the Program. 91 | 92 | To "propagate" a work means to do anything with it that, without 93 | permission, would make you directly or secondarily liable for 94 | infringement under applicable copyright law, except executing it on a 95 | computer or modifying a private copy. Propagation includes copying, 96 | distribution (with or without modification), making available to the 97 | public, and in some countries other activities as well. 98 | 99 | To "convey" a work means any kind of propagation that enables other 100 | parties to make or receive copies. Mere interaction with a user through 101 | a computer network, with no transfer of a copy, is not conveying. 102 | 103 | An interactive user interface displays "Appropriate Legal Notices" 104 | to the extent that it includes a convenient and prominently visible 105 | feature that (1) displays an appropriate copyright notice, and (2) 106 | tells the user that there is no warranty for the work (except to the 107 | extent that warranties are provided), that licensees may convey the 108 | work under this License, and how to view a copy of this License. If 109 | the interface presents a list of user commands or options, such as a 110 | menu, a prominent item in the list meets this criterion. 111 | 112 | 1. Source Code. 113 | 114 | The "source code" for a work means the preferred form of the work 115 | for making modifications to it. "Object code" means any non-source 116 | form of a work. 117 | 118 | A "Standard Interface" means an interface that either is an official 119 | standard defined by a recognized standards body, or, in the case of 120 | interfaces specified for a particular programming language, one that 121 | is widely used among developers working in that language. 122 | 123 | The "System Libraries" of an executable work include anything, other 124 | than the work as a whole, that (a) is included in the normal form of 125 | packaging a Major Component, but which is not part of that Major 126 | Component, and (b) serves only to enable use of the work with that 127 | Major Component, or to implement a Standard Interface for which an 128 | implementation is available to the public in source code form. A 129 | "Major Component", in this context, means a major essential component 130 | (kernel, window system, and so on) of the specific operating system 131 | (if any) on which the executable work runs, or a compiler used to 132 | produce the work, or an object code interpreter used to run it. 133 | 134 | The "Corresponding Source" for a work in object code form means all 135 | the source code needed to generate, install, and (for an executable 136 | work) run the object code and to modify the work, including scripts to 137 | control those activities. However, it does not include the work's 138 | System Libraries, or general-purpose tools or generally available free 139 | programs which are used unmodified in performing those activities but 140 | which are not part of the work. For example, Corresponding Source 141 | includes interface definition files associated with source files for 142 | the work, and the source code for shared libraries and dynamically 143 | linked subprograms that the work is specifically designed to require, 144 | such as by intimate data communication or control flow between those 145 | subprograms and other parts of the work. 146 | 147 | The Corresponding Source need not include anything that users 148 | can regenerate automatically from other parts of the Corresponding 149 | Source. 150 | 151 | The Corresponding Source for a work in source code form is that 152 | same work. 153 | 154 | 2. Basic Permissions. 155 | 156 | All rights granted under this License are granted for the term of 157 | copyright on the Program, and are irrevocable provided the stated 158 | conditions are met. This License explicitly affirms your unlimited 159 | permission to run the unmodified Program. The output from running a 160 | covered work is covered by this License only if the output, given its 161 | content, constitutes a covered work. This License acknowledges your 162 | rights of fair use or other equivalent, as provided by copyright law. 163 | 164 | You may make, run and propagate covered works that you do not 165 | convey, without conditions so long as your license otherwise remains 166 | in force. You may convey covered works to others for the sole purpose 167 | of having them make modifications exclusively for you, or provide you 168 | with facilities for running those works, provided that you comply with 169 | the terms of this License in conveying all material for which you do 170 | not control copyright. Those thus making or running the covered works 171 | for you must do so exclusively on your behalf, under your direction 172 | and control, on terms that prohibit them from making any copies of 173 | your copyrighted material outside their relationship with you. 174 | 175 | Conveying under any other circumstances is permitted solely under 176 | the conditions stated below. Sublicensing is not allowed; section 10 177 | makes it unnecessary. 178 | 179 | 3. Protecting Users' Legal Rights From Anti-Circumvention Law. 180 | 181 | No covered work shall be deemed part of an effective technological 182 | measure under any applicable law fulfilling obligations under article 183 | 11 of the WIPO copyright treaty adopted on 20 December 1996, or 184 | similar laws prohibiting or restricting circumvention of such 185 | measures. 186 | 187 | When you convey a covered work, you waive any legal power to forbid 188 | circumvention of technological measures to the extent such circumvention 189 | is effected by exercising rights under this License with respect to 190 | the covered work, and you disclaim any intention to limit operation or 191 | modification of the work as a means of enforcing, against the work's 192 | users, your or third parties' legal rights to forbid circumvention of 193 | technological measures. 194 | 195 | 4. Conveying Verbatim Copies. 196 | 197 | You may convey verbatim copies of the Program's source code as you 198 | receive it, in any medium, provided that you conspicuously and 199 | appropriately publish on each copy an appropriate copyright notice; 200 | keep intact all notices stating that this License and any 201 | non-permissive terms added in accord with section 7 apply to the code; 202 | keep intact all notices of the absence of any warranty; and give all 203 | recipients a copy of this License along with the Program. 204 | 205 | You may charge any price or no price for each copy that you convey, 206 | and you may offer support or warranty protection for a fee. 207 | 208 | 5. Conveying Modified Source Versions. 209 | 210 | You may convey a work based on the Program, or the modifications to 211 | produce it from the Program, in the form of source code under the 212 | terms of section 4, provided that you also meet all of these conditions: 213 | 214 | a) The work must carry prominent notices stating that you modified 215 | it, and giving a relevant date. 216 | 217 | b) The work must carry prominent notices stating that it is 218 | released under this License and any conditions added under section 219 | 7. This requirement modifies the requirement in section 4 to 220 | "keep intact all notices". 221 | 222 | c) You must license the entire work, as a whole, under this 223 | License to anyone who comes into possession of a copy. This 224 | License will therefore apply, along with any applicable section 7 225 | additional terms, to the whole of the work, and all its parts, 226 | regardless of how they are packaged. This License gives no 227 | permission to license the work in any other way, but it does not 228 | invalidate such permission if you have separately received it. 229 | 230 | d) If the work has interactive user interfaces, each must display 231 | Appropriate Legal Notices; however, if the Program has interactive 232 | interfaces that do not display Appropriate Legal Notices, your 233 | work need not make them do so. 234 | 235 | A compilation of a covered work with other separate and independent 236 | works, which are not by their nature extensions of the covered work, 237 | and which are not combined with it such as to form a larger program, 238 | in or on a volume of a storage or distribution medium, is called an 239 | "aggregate" if the compilation and its resulting copyright are not 240 | used to limit the access or legal rights of the compilation's users 241 | beyond what the individual works permit. Inclusion of a covered work 242 | in an aggregate does not cause this License to apply to the other 243 | parts of the aggregate. 244 | 245 | 6. Conveying Non-Source Forms. 246 | 247 | You may convey a covered work in object code form under the terms 248 | of sections 4 and 5, provided that you also convey the 249 | machine-readable Corresponding Source under the terms of this License, 250 | in one of these ways: 251 | 252 | a) Convey the object code in, or embodied in, a physical product 253 | (including a physical distribution medium), accompanied by the 254 | Corresponding Source fixed on a durable physical medium 255 | customarily used for software interchange. 256 | 257 | b) Convey the object code in, or embodied in, a physical product 258 | (including a physical distribution medium), accompanied by a 259 | written offer, valid for at least three years and valid for as 260 | long as you offer spare parts or customer support for that product 261 | model, to give anyone who possesses the object code either (1) a 262 | copy of the Corresponding Source for all the software in the 263 | product that is covered by this License, on a durable physical 264 | medium customarily used for software interchange, for a price no 265 | more than your reasonable cost of physically performing this 266 | conveying of source, or (2) access to copy the 267 | Corresponding Source from a network server at no charge. 268 | 269 | c) Convey individual copies of the object code with a copy of the 270 | written offer to provide the Corresponding Source. This 271 | alternative is allowed only occasionally and noncommercially, and 272 | only if you received the object code with such an offer, in accord 273 | with subsection 6b. 274 | 275 | d) Convey the object code by offering access from a designated 276 | place (gratis or for a charge), and offer equivalent access to the 277 | Corresponding Source in the same way through the same place at no 278 | further charge. You need not require recipients to copy the 279 | Corresponding Source along with the object code. If the place to 280 | copy the object code is a network server, the Corresponding Source 281 | may be on a different server (operated by you or a third party) 282 | that supports equivalent copying facilities, provided you maintain 283 | clear directions next to the object code saying where to find the 284 | Corresponding Source. Regardless of what server hosts the 285 | Corresponding Source, you remain obligated to ensure that it is 286 | available for as long as needed to satisfy these requirements. 287 | 288 | e) Convey the object code using peer-to-peer transmission, provided 289 | you inform other peers where the object code and Corresponding 290 | Source of the work are being offered to the general public at no 291 | charge under subsection 6d. 292 | 293 | A separable portion of the object code, whose source code is excluded 294 | from the Corresponding Source as a System Library, need not be 295 | included in conveying the object code work. 296 | 297 | A "User Product" is either (1) a "consumer product", which means any 298 | tangible personal property which is normally used for personal, family, 299 | or household purposes, or (2) anything designed or sold for incorporation 300 | into a dwelling. In determining whether a product is a consumer product, 301 | doubtful cases shall be resolved in favor of coverage. For a particular 302 | product received by a particular user, "normally used" refers to a 303 | typical or common use of that class of product, regardless of the status 304 | of the particular user or of the way in which the particular user 305 | actually uses, or expects or is expected to use, the product. A product 306 | is a consumer product regardless of whether the product has substantial 307 | commercial, industrial or non-consumer uses, unless such uses represent 308 | the only significant mode of use of the product. 309 | 310 | "Installation Information" for a User Product means any methods, 311 | procedures, authorization keys, or other information required to install 312 | and execute modified versions of a covered work in that User Product from 313 | a modified version of its Corresponding Source. The information must 314 | suffice to ensure that the continued functioning of the modified object 315 | code is in no case prevented or interfered with solely because 316 | modification has been made. 317 | 318 | If you convey an object code work under this section in, or with, or 319 | specifically for use in, a User Product, and the conveying occurs as 320 | part of a transaction in which the right of possession and use of the 321 | User Product is transferred to the recipient in perpetuity or for a 322 | fixed term (regardless of how the transaction is characterized), the 323 | Corresponding Source conveyed under this section must be accompanied 324 | by the Installation Information. But this requirement does not apply 325 | if neither you nor any third party retains the ability to install 326 | modified object code on the User Product (for example, the work has 327 | been installed in ROM). 328 | 329 | The requirement to provide Installation Information does not include a 330 | requirement to continue to provide support service, warranty, or updates 331 | for a work that has been modified or installed by the recipient, or for 332 | the User Product in which it has been modified or installed. Access to a 333 | network may be denied when the modification itself materially and 334 | adversely affects the operation of the network or violates the rules and 335 | protocols for communication across the network. 336 | 337 | Corresponding Source conveyed, and Installation Information provided, 338 | in accord with this section must be in a format that is publicly 339 | documented (and with an implementation available to the public in 340 | source code form), and must require no special password or key for 341 | unpacking, reading or copying. 342 | 343 | 7. Additional Terms. 344 | 345 | "Additional permissions" are terms that supplement the terms of this 346 | License by making exceptions from one or more of its conditions. 347 | Additional permissions that are applicable to the entire Program shall 348 | be treated as though they were included in this License, to the extent 349 | that they are valid under applicable law. If additional permissions 350 | apply only to part of the Program, that part may be used separately 351 | under those permissions, but the entire Program remains governed by 352 | this License without regard to the additional permissions. 353 | 354 | When you convey a copy of a covered work, you may at your option 355 | remove any additional permissions from that copy, or from any part of 356 | it. (Additional permissions may be written to require their own 357 | removal in certain cases when you modify the work.) You may place 358 | additional permissions on material, added by you to a covered work, 359 | for which you have or can give appropriate copyright permission. 360 | 361 | Notwithstanding any other provision of this License, for material you 362 | add to a covered work, you may (if authorized by the copyright holders of 363 | that material) supplement the terms of this License with terms: 364 | 365 | a) Disclaiming warranty or limiting liability differently from the 366 | terms of sections 15 and 16 of this License; or 367 | 368 | b) Requiring preservation of specified reasonable legal notices or 369 | author attributions in that material or in the Appropriate Legal 370 | Notices displayed by works containing it; or 371 | 372 | c) Prohibiting misrepresentation of the origin of that material, or 373 | requiring that modified versions of such material be marked in 374 | reasonable ways as different from the original version; or 375 | 376 | d) Limiting the use for publicity purposes of names of licensors or 377 | authors of the material; or 378 | 379 | e) Declining to grant rights under trademark law for use of some 380 | trade names, trademarks, or service marks; or 381 | 382 | f) Requiring indemnification of licensors and authors of that 383 | material by anyone who conveys the material (or modified versions of 384 | it) with contractual assumptions of liability to the recipient, for 385 | any liability that these contractual assumptions directly impose on 386 | those licensors and authors. 387 | 388 | All other non-permissive additional terms are considered "further 389 | restrictions" within the meaning of section 10. If the Program as you 390 | received it, or any part of it, contains a notice stating that it is 391 | governed by this License along with a term that is a further 392 | restriction, you may remove that term. If a license document contains 393 | a further restriction but permits relicensing or conveying under this 394 | License, you may add to a covered work material governed by the terms 395 | of that license document, provided that the further restriction does 396 | not survive such relicensing or conveying. 397 | 398 | If you add terms to a covered work in accord with this section, you 399 | must place, in the relevant source files, a statement of the 400 | additional terms that apply to those files, or a notice indicating 401 | where to find the applicable terms. 402 | 403 | Additional terms, permissive or non-permissive, may be stated in the 404 | form of a separately written license, or stated as exceptions; 405 | the above requirements apply either way. 406 | 407 | 8. Termination. 408 | 409 | You may not propagate or modify a covered work except as expressly 410 | provided under this License. Any attempt otherwise to propagate or 411 | modify it is void, and will automatically terminate your rights under 412 | this License (including any patent licenses granted under the third 413 | paragraph of section 11). 414 | 415 | However, if you cease all violation of this License, then your 416 | license from a particular copyright holder is reinstated (a) 417 | provisionally, unless and until the copyright holder explicitly and 418 | finally terminates your license, and (b) permanently, if the copyright 419 | holder fails to notify you of the violation by some reasonable means 420 | prior to 60 days after the cessation. 421 | 422 | Moreover, your license from a particular copyright holder is 423 | reinstated permanently if the copyright holder notifies you of the 424 | violation by some reasonable means, this is the first time you have 425 | received notice of violation of this License (for any work) from that 426 | copyright holder, and you cure the violation prior to 30 days after 427 | your receipt of the notice. 428 | 429 | Termination of your rights under this section does not terminate the 430 | licenses of parties who have received copies or rights from you under 431 | this License. If your rights have been terminated and not permanently 432 | reinstated, you do not qualify to receive new licenses for the same 433 | material under section 10. 434 | 435 | 9. Acceptance Not Required for Having Copies. 436 | 437 | You are not required to accept this License in order to receive or 438 | run a copy of the Program. Ancillary propagation of a covered work 439 | occurring solely as a consequence of using peer-to-peer transmission 440 | to receive a copy likewise does not require acceptance. However, 441 | nothing other than this License grants you permission to propagate or 442 | modify any covered work. These actions infringe copyright if you do 443 | not accept this License. Therefore, by modifying or propagating a 444 | covered work, you indicate your acceptance of this License to do so. 445 | 446 | 10. Automatic Licensing of Downstream Recipients. 447 | 448 | Each time you convey a covered work, the recipient automatically 449 | receives a license from the original licensors, to run, modify and 450 | propagate that work, subject to this License. You are not responsible 451 | for enforcing compliance by third parties with this License. 452 | 453 | An "entity transaction" is a transaction transferring control of an 454 | organization, or substantially all assets of one, or subdividing an 455 | organization, or merging organizations. If propagation of a covered 456 | work results from an entity transaction, each party to that 457 | transaction who receives a copy of the work also receives whatever 458 | licenses to the work the party's predecessor in interest had or could 459 | give under the previous paragraph, plus a right to possession of the 460 | Corresponding Source of the work from the predecessor in interest, if 461 | the predecessor has it or can get it with reasonable efforts. 462 | 463 | You may not impose any further restrictions on the exercise of the 464 | rights granted or affirmed under this License. For example, you may 465 | not impose a license fee, royalty, or other charge for exercise of 466 | rights granted under this License, and you may not initiate litigation 467 | (including a cross-claim or counterclaim in a lawsuit) alleging that 468 | any patent claim is infringed by making, using, selling, offering for 469 | sale, or importing the Program or any portion of it. 470 | 471 | 11. Patents. 472 | 473 | A "contributor" is a copyright holder who authorizes use under this 474 | License of the Program or a work on which the Program is based. The 475 | work thus licensed is called the contributor's "contributor version". 476 | 477 | A contributor's "essential patent claims" are all patent claims 478 | owned or controlled by the contributor, whether already acquired or 479 | hereafter acquired, that would be infringed by some manner, permitted 480 | by this License, of making, using, or selling its contributor version, 481 | but do not include claims that would be infringed only as a 482 | consequence of further modification of the contributor version. For 483 | purposes of this definition, "control" includes the right to grant 484 | patent sublicenses in a manner consistent with the requirements of 485 | this License. 486 | 487 | Each contributor grants you a non-exclusive, worldwide, royalty-free 488 | patent license under the contributor's essential patent claims, to 489 | make, use, sell, offer for sale, import and otherwise run, modify and 490 | propagate the contents of its contributor version. 491 | 492 | In the following three paragraphs, a "patent license" is any express 493 | agreement or commitment, however denominated, not to enforce a patent 494 | (such as an express permission to practice a patent or covenant not to 495 | sue for patent infringement). To "grant" such a patent license to a 496 | party means to make such an agreement or commitment not to enforce a 497 | patent against the party. 498 | 499 | If you convey a covered work, knowingly relying on a patent license, 500 | and the Corresponding Source of the work is not available for anyone 501 | to copy, free of charge and under the terms of this License, through a 502 | publicly available network server or other readily accessible means, 503 | then you must either (1) cause the Corresponding Source to be so 504 | available, or (2) arrange to deprive yourself of the benefit of the 505 | patent license for this particular work, or (3) arrange, in a manner 506 | consistent with the requirements of this License, to extend the patent 507 | license to downstream recipients. "Knowingly relying" means you have 508 | actual knowledge that, but for the patent license, your conveying the 509 | covered work in a country, or your recipient's use of the covered work 510 | in a country, would infringe one or more identifiable patents in that 511 | country that you have reason to believe are valid. 512 | 513 | If, pursuant to or in connection with a single transaction or 514 | arrangement, you convey, or propagate by procuring conveyance of, a 515 | covered work, and grant a patent license to some of the parties 516 | receiving the covered work authorizing them to use, propagate, modify 517 | or convey a specific copy of the covered work, then the patent license 518 | you grant is automatically extended to all recipients of the covered 519 | work and works based on it. 520 | 521 | A patent license is "discriminatory" if it does not include within 522 | the scope of its coverage, prohibits the exercise of, or is 523 | conditioned on the non-exercise of one or more of the rights that are 524 | specifically granted under this License. You may not convey a covered 525 | work if you are a party to an arrangement with a third party that is 526 | in the business of distributing software, under which you make payment 527 | to the third party based on the extent of your activity of conveying 528 | the work, and under which the third party grants, to any of the 529 | parties who would receive the covered work from you, a discriminatory 530 | patent license (a) in connection with copies of the covered work 531 | conveyed by you (or copies made from those copies), or (b) primarily 532 | for and in connection with specific products or compilations that 533 | contain the covered work, unless you entered into that arrangement, 534 | or that patent license was granted, prior to 28 March 2007. 535 | 536 | Nothing in this License shall be construed as excluding or limiting 537 | any implied license or other defenses to infringement that may 538 | otherwise be available to you under applicable patent law. 539 | 540 | 12. No Surrender of Others' Freedom. 541 | 542 | If conditions are imposed on you (whether by court order, agreement or 543 | otherwise) that contradict the conditions of this License, they do not 544 | excuse you from the conditions of this License. If you cannot convey a 545 | covered work so as to satisfy simultaneously your obligations under this 546 | License and any other pertinent obligations, then as a consequence you may 547 | not convey it at all. For example, if you agree to terms that obligate you 548 | to collect a royalty for further conveying from those to whom you convey 549 | the Program, the only way you could satisfy both those terms and this 550 | License would be to refrain entirely from conveying the Program. 551 | 552 | 13. Use with the GNU Affero General Public License. 553 | 554 | Notwithstanding any other provision of this License, you have 555 | permission to link or combine any covered work with a work licensed 556 | under version 3 of the GNU Affero General Public License into a single 557 | combined work, and to convey the resulting work. The terms of this 558 | License will continue to apply to the part which is the covered work, 559 | but the special requirements of the GNU Affero General Public License, 560 | section 13, concerning interaction through a network will apply to the 561 | combination as such. 562 | 563 | 14. Revised Versions of this License. 564 | 565 | The Free Software Foundation may publish revised and/or new versions of 566 | the GNU General Public License from time to time. Such new versions will 567 | be similar in spirit to the present version, but may differ in detail to 568 | address new problems or concerns. 569 | 570 | Each version is given a distinguishing version number. If the 571 | Program specifies that a certain numbered version of the GNU General 572 | Public License "or any later version" applies to it, you have the 573 | option of following the terms and conditions either of that numbered 574 | version or of any later version published by the Free Software 575 | Foundation. If the Program does not specify a version number of the 576 | GNU General Public License, you may choose any version ever published 577 | by the Free Software Foundation. 578 | 579 | If the Program specifies that a proxy can decide which future 580 | versions of the GNU General Public License can be used, that proxy's 581 | public statement of acceptance of a version permanently authorizes you 582 | to choose that version for the Program. 583 | 584 | Later license versions may give you additional or different 585 | permissions. However, no additional obligations are imposed on any 586 | author or copyright holder as a result of your choosing to follow a 587 | later version. 588 | 589 | 15. Disclaimer of Warranty. 590 | 591 | THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY 592 | APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT 593 | HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY 594 | OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, 595 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 596 | PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM 597 | IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF 598 | ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 599 | 600 | 16. Limitation of Liability. 601 | 602 | IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 603 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS 604 | THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY 605 | GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE 606 | USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF 607 | DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD 608 | PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS), 609 | EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF 610 | SUCH DAMAGES. 611 | 612 | 17. Interpretation of Sections 15 and 16. 613 | 614 | If the disclaimer of warranty and limitation of liability provided 615 | above cannot be given local legal effect according to their terms, 616 | reviewing courts shall apply local law that most closely approximates 617 | an absolute waiver of all civil liability in connection with the 618 | Program, unless a warranty or assumption of liability accompanies a 619 | copy of the Program in return for a fee. 620 | 621 | END OF TERMS AND CONDITIONS 622 | 623 | How to Apply These Terms to Your New Programs 624 | 625 | If you develop a new program, and you want it to be of the greatest 626 | possible use to the public, the best way to achieve this is to make it 627 | free software which everyone can redistribute and change under these terms. 628 | 629 | To do so, attach the following notices to the program. It is safest 630 | to attach them to the start of each source file to most effectively 631 | state the exclusion of warranty; and each file should have at least 632 | the "copyright" line and a pointer to where the full notice is found. 633 | 634 | 635 | Copyright (C) 2022 636 | 637 | This program is free software: you can redistribute it and/or modify 638 | it under the terms of the GNU General Public License as published by 639 | the Free Software Foundation, either version 3 of the License, or 640 | (at your option) any later version. 641 | 642 | This program is distributed in the hope that it will be useful, 643 | but WITHOUT ANY WARRANTY; without even the implied warranty of 644 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 645 | GNU General Public License for more details. 646 | 647 | You should have received a copy of the GNU General Public License 648 | along with this program. If not, see . 649 | 650 | Also add information on how to contact you by electronic and paper mail. 651 | 652 | If the program does terminal interaction, make it output a short 653 | notice like this when it starts in an interactive mode: 654 | 655 | Copyright (C) 2022 656 | This program comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 657 | This is free software, and you are welcome to redistribute it 658 | under certain conditions; type `show c' for details. 659 | 660 | The hypothetical commands `show w' and `show c' should show the appropriate 661 | parts of the General Public License. Of course, your program's commands 662 | might be different; for a GUI interface, you would use an "about box". 663 | 664 | You should also get your employer (if you work as a programmer) or school, 665 | if any, to sign a "copyright disclaimer" for the program, if necessary. 666 | For more information on this, and how to apply and follow the GNU GPL, see 667 | . 668 | 669 | The GNU General Public License does not permit incorporating your program 670 | into proprietary programs. If your program is a subroutine library, you 671 | may consider it more useful to permit linking proprietary applications with 672 | the library. If this is what you want to do, use the GNU Lesser General 673 | Public License instead of this License. But first, please read 674 | . 675 | --------------------------------------------------------------------------------