Skip to content

Commit

Permalink
first commit w/ the basis for the captcha image / audio generation an…
Browse files Browse the repository at this point in the history
…d docker
  • Loading branch information
smokinggoats committed Sep 16, 2019
1 parent 866e5e4 commit 81b5ea3
Show file tree
Hide file tree
Showing 12 changed files with 290 additions and 0 deletions.
132 changes: 132 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
.DS_STORE
.vscode
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
pip-wheel-metadata/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM python:alpine

WORKDIR /app

COPY requirements.txt /app/
COPY ./ /app

RUN apk add alpine-sdk make gcc g++
RUN make docker_build

RUN pip3 install -r requirements.txt
RUN pip3 install dist/capytcha-*.whl

RUN rm -R capytcha

EXPOSE 8080

CMD ["capytcha-serve"]
43 changes: 43 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
.PHONY: deps install clean build_clean dist_clean dist docker

ENV=.env
PYTHON_VERSION=3
PYTHON=python${PYTHON_VERSION}
SITE_PACKAGES=${ENV}/lib/${PYTHON}/site-packages
IN_ENV=. ${ENV}/bin/activate;
PACKAGE_VERSION=$(shell cat VERSION)

default: ${ENV} deps

${ENV}:
@echo "Creating Python environment..." >&2
@${PYTHON} -m venv ${ENV}
@echo "Updating pip..." >&2
@${IN_ENV} ${PYTHON} -m pip install -U pip setuptools

${SITE_PACKAGES}: ${ENV}
@${IN_ENV} ${PYTHON} -m pip install -r requirements.txt

${SITE_PACKAGES}/planets: ${ENV} install

deps: ${SITE_PACKAGES}

install: default
@${IN_ENV} ${PYTHON} -m pip install -e .

wheel: ${ENV}
@${IN_ENV} ${PYTHON} -m pip install -U wheel
@${IN_ENV} ${PYTHON} setup.py bdist_wheel

dist: wheel

dist_clean:
@rm -rf dist

build_clean:
@rm -rf build

clean: build_clean dist_clean
@rm -rf ${ENV} dist build __pycache__ *.egg-info

docker_build: build_clean dist_clean wheel
1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.0.1
Empty file added capytcha/__init__.py
Empty file.
Empty file added capytcha/app.py
Empty file.
58 changes: 58 additions & 0 deletions capytcha/capytcha.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# ref: https://www.dev2qa.com/how-to-generate-random-captcha-in-python/
import random

from captcha.audio import AudioCaptcha
from captcha.image import ImageCaptcha

from capytcha.utils import char_choices, numbers_list


def create_random_text(captcha_string_size=10):
captcha_string_list = []
captcha_string = ''
for i in range(captcha_string_size):
captcha_string = f'{captcha_string}{random.choice(char_choices)}'
return captcha_string


def create_random_number(captcha_string_size=10):
captcha_string_list = []
captcha_string = ''
for i in range(captcha_string_size):
captcha_string = f'{captcha_string}{random.choice(number_list)}'
return captcha_string


def create_image_captcha(captcha_text):
image_captcha = ImageCaptcha(width=400, height=150)
image_data = image_captcha.generate_image(captcha_text)
image_captcha.create_noise_curve(image_data, image_data.getcolors())
image_captcha.create_noise_dots(image_data, image_data.getcolors())

# image_file = "./captcha_"+captcha_text + ".png"
# image_captcha.write(captcha_text, image_file)

return image_data


def create_audio_captcha():
# Create the audio captcha with the specified voice wav file library folder.
# Each captcha char should has it's own directory under the specified folder ( such as ./voices),
# for example ./voices/a/a.wav will be played when the character is a.
# If you do not specify your own voice file library folder, the default built-in voice library which has only digital voice file will be used.
# audio_captcha = AudioCaptcha(voicedir='./voices')
# Create an audio captcha which use digital voice file only.
audio_captcha = AudioCaptcha()
captcha_text = create_random_number()
audio_data = audio_captcha.generate(captcha_text)

# audio_file = "./captcha_"+captcha_text+'.wav'
# audio_captcha.write(captcha_text, audio_file)

return audio_data


if __name__ == '__main__':
captcha_text = create_random_text()
create_image_captcha(captcha_text)
create_audio_captcha()
7 changes: 7 additions & 0 deletions capytcha/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
numbers_list = list(range(0, 10))

lower_letters_list = list(map(lambda i: chr(i), range(97, 123)))

upper_letters_list = list(map(lambda i: chr(i), range(65, 91)))

char_choices = lower_letters_list + upper_letters_list + numbers_list
9 changes: 9 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
version: "3.2"

services:
app:
build: .
volumes:
- ./:/app
ports:
- "8080:8080"
Empty file added requirements.txt
Empty file.
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[bdist_wheel]
universal = 1
20 changes: 20 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from setuptools import setup


def _get_version():
with open('VERSION') as fd:
return fd.read().strip()


setup(
name='capytcha',
version=_get_version(),
packages=['capytcha'],
package_dir={'': './'},
py_modules=['capytcha.app'],
entry_points={
'console_scripts': [
'capytcha-serve=capytcha.app:main',
]
}
)

0 comments on commit 81b5ea3

Please sign in to comment.