diff --git a/.gitignore b/.gitignore index 6f667ed..7018eb4 100644 --- a/.gitignore +++ b/.gitignore @@ -105,3 +105,6 @@ venv.bak/ # direnv .envrc + +# ideas +.vscode diff --git a/HOW TO RUN FLASK APP.md b/HOW TO RUN FLASK APP.md new file mode 100644 index 0000000..6e0ea33 --- /dev/null +++ b/HOW TO RUN FLASK APP.md @@ -0,0 +1,11 @@ +# HOW TO RUN FLASK APP +```sh +export FLASK_APP=eriwan_podcast.py +export FLASK_DEBUG=1 + +flask db init +flask db migrate +flask db upgrade + +flask run +``` diff --git a/app.db b/app.db new file mode 100644 index 0000000..c8c52df Binary files /dev/null and b/app.db differ diff --git a/app/__init__.py b/app/__init__.py new file mode 100644 index 0000000..d049118 --- /dev/null +++ b/app/__init__.py @@ -0,0 +1,43 @@ +# Creates the application object as an instance of class Flask + + +from flask import Flask +from flask_migrate import Migrate +from flask_sqlalchemy import SQLAlchemy + +from config import Config + + +app = Flask(__name__) + +# using Config class from ./config.py +app.config.from_object(Config) + +# database +db = SQLAlchemy(app) +migrate = Migrate(app, db) + +# The routes module is imported at the bottom and not at the top of the script +# as it is always done. The bottom import is a workaround to circular imports, +# a common problem with Flask applications. +from app import models +import atexit +from app.parser import parse_anekdot +from apscheduler.schedulers.background import BackgroundScheduler + +def scheduler_parser(): + ''' + Starts the parser. + ''' + parse_anekdot() + +# Scheduler settings and start. +# Variables locate in config.Config + +scheduler = BackgroundScheduler() +scheduler.add_job(func=scheduler_parser, trigger="interval", hours=Config.PARSE_TIME_HOURS) +scheduler.start() + +# Shut down the scheduler when exiting the app +atexit.register(lambda: scheduler.shutdown()) + diff --git a/app/forms.py b/app/forms.py new file mode 100644 index 0000000..8dd6df3 --- /dev/null +++ b/app/forms.py @@ -0,0 +1 @@ +# Here will be Flask Web Forms diff --git a/app/models.py b/app/models.py new file mode 100644 index 0000000..1b318cf --- /dev/null +++ b/app/models.py @@ -0,0 +1,77 @@ +import os +from werkzeug.security import generate_password_hash, check_password_hash + +from app import app, db + + +class User(db.Model): + id = db.Column(db.Integer, primary_key=True) + username = db.Column(db.String(64), index=True, unique=True, nullable=False) + email = db.Column(db.String(120), index=True, unique=True, nullable=False) + password_hash = db.Column(db.String(128), nullable=False) + is_admin = db.Column(db.Boolean, default=False, nullable=False) + + def __repr__(self): + return f'' + + def set_password(self, password): + self.password_hash = generate_password_hash(password) + + def check_password(self, password): + return check_password_hash(self.password_hash, password) + + +class Episode(db.Model): + id = db.Column(db.Integer, primary_key=True) + name = db.Column(db.String(255), nullable=False) + user_id = db.Column(db.Integer, db.ForeignKey('user.id')) + + def __repr__(self): + return f', name: {self.name}' + + def get_file_path(self): + ''' + Return wrapped in jingles file path + ''' + static_path = os.path.join(app.config.get('STATIC_ROOT'), 'episodes') + file_path = f'{static_path}/{self.id}.mp3' + if os.path.exists(file_path): + return file_path + + # todo: add to celery task + def generate_wrapped_file(self, upload_file): + ''' + Return generate file with name of episode prefix from upload_file + ''' + pass + + +class Joke(db.Model): + id = db.Column(db.Integer, primary_key=True) + joke_text = db.Column(db.Text, nullable=False) + user_id = db.Column(db.Integer, db.ForeignKey('user.id')) + + def __repr__(self): + return f', joke_text: {self.joke_text}' + + def get_file_path(self): + ''' + Return wrapped in jingles file path + ''' + static_path = os.path.join(app.config.get('STATIC_ROOT'), 'jokes') + file_path = f'{static_path}/{self.id}.mp3' + if os.path.exists(file_path): + return file_path + + def generate_base_file(self): + ''' + Return generate base audio file from joke_text + ''' + pass + + # todo: add to celery task + def generate_wrapped_file(self, upload_file): + ''' + Return generate wrapped in jingles file from upload_file + ''' + pass diff --git a/app/parser.py b/app/parser.py new file mode 100644 index 0000000..464f5c4 --- /dev/null +++ b/app/parser.py @@ -0,0 +1,29 @@ +# Parsing jokes from anekdotitut.ru and adds +# them to the database. +import urllib.request +from urllib.parse import quote +from urllib.parse import unquote +from bs4 import BeautifulSoup +import re +from app.models import Joke +from app import db + + +def parse_anekdot(): + ''' + Simple func for collecting jokes from anekdotitut.ru + and add them to DB. + ''' + jokes_out = [] + for i in range(1, 10): + url = url = 'https://anekdotitut.ru/pro_armyanskoe_radio' + str( + i) + '.php' + html_doc = urllib.request.urlopen(url) + soup_doc = BeautifulSoup(html_doc, 'html.parser') + jokes = soup_doc.body(class_='noselect', id=re.compile('anekdot\d+')) + for joke in jokes: + # Check for entry in DB. + if not bool(Joke.query.filter_by(joke_text = joke.text).first()): + j = Joke(joke_text = joke.text, user_id = 999) + db.session.add(j) + db.session.commit() \ No newline at end of file diff --git a/app/templates/index.html b/app/templates/index.html new file mode 100644 index 0000000..7789fb0 --- /dev/null +++ b/app/templates/index.html @@ -0,0 +1 @@ +{{ feed_blank }} \ No newline at end of file diff --git a/app/tests.py b/app/tests.py new file mode 100644 index 0000000..74813ec --- /dev/null +++ b/app/tests.py @@ -0,0 +1 @@ +# Here will be tests diff --git a/config.py b/config.py new file mode 100644 index 0000000..12e51ca --- /dev/null +++ b/config.py @@ -0,0 +1,18 @@ +# Config Classes + +import os +basedir = os.path.abspath(os.path.dirname(__file__)) + + +class Config(object): + SECRET_KEY = os.environ.get('SECRET_KEY') or 'Wo7GhuD2OWIv' + SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \ + 'sqlite:///' + os.path.join(basedir, 'app.db') + SQLALCHEMY_TRACK_MODIFICATIONS = False + + ADMINS = ['your-email@example.com'] + + STATIC_ROOT = '/static/' + + # Time period for parser + PARSE_TIME_HOURS = 40 diff --git a/eriwan_podcast.py b/eriwan_podcast.py new file mode 100644 index 0000000..2741a3e --- /dev/null +++ b/eriwan_podcast.py @@ -0,0 +1,2 @@ +# The top-level that defines the Flask application instance +from app import app diff --git a/migrations/README b/migrations/README new file mode 100644 index 0000000..98e4f9c --- /dev/null +++ b/migrations/README @@ -0,0 +1 @@ +Generic single-database configuration. \ No newline at end of file diff --git a/migrations/alembic.ini b/migrations/alembic.ini new file mode 100644 index 0000000..f8ed480 --- /dev/null +++ b/migrations/alembic.ini @@ -0,0 +1,45 @@ +# A generic, single database configuration. + +[alembic] +# template used to generate migration files +# file_template = %%(rev)s_%%(slug)s + +# set to 'true' to run the environment during +# the 'revision' command, regardless of autogenerate +# revision_environment = false + + +# Logging configuration +[loggers] +keys = root,sqlalchemy,alembic + +[handlers] +keys = console + +[formatters] +keys = generic + +[logger_root] +level = WARN +handlers = console +qualname = + +[logger_sqlalchemy] +level = WARN +handlers = +qualname = sqlalchemy.engine + +[logger_alembic] +level = INFO +handlers = +qualname = alembic + +[handler_console] +class = StreamHandler +args = (sys.stderr,) +level = NOTSET +formatter = generic + +[formatter_generic] +format = %(levelname)-5.5s [%(name)s] %(message)s +datefmt = %H:%M:%S diff --git a/migrations/env.py b/migrations/env.py new file mode 100644 index 0000000..79b8174 --- /dev/null +++ b/migrations/env.py @@ -0,0 +1,96 @@ +from __future__ import with_statement + +import logging +from logging.config import fileConfig + +from sqlalchemy import engine_from_config +from sqlalchemy import pool + +from alembic import context + +# this is the Alembic Config object, which provides +# access to the values within the .ini file in use. +config = context.config + +# Interpret the config file for Python logging. +# This line sets up loggers basically. +fileConfig(config.config_file_name) +logger = logging.getLogger('alembic.env') + +# add your model's MetaData object here +# for 'autogenerate' support +# from myapp import mymodel +# target_metadata = mymodel.Base.metadata +from flask import current_app +config.set_main_option( + 'sqlalchemy.url', current_app.config.get( + 'SQLALCHEMY_DATABASE_URI').replace('%', '%%')) +target_metadata = current_app.extensions['migrate'].db.metadata + +# other values from the config, defined by the needs of env.py, +# can be acquired: +# my_important_option = config.get_main_option("my_important_option") +# ... etc. + + +def run_migrations_offline(): + """Run migrations in 'offline' mode. + + This configures the context with just a URL + and not an Engine, though an Engine is acceptable + here as well. By skipping the Engine creation + we don't even need a DBAPI to be available. + + Calls to context.execute() here emit the given string to the + script output. + + """ + url = config.get_main_option("sqlalchemy.url") + context.configure( + url=url, target_metadata=target_metadata, literal_binds=True + ) + + with context.begin_transaction(): + context.run_migrations() + + +def run_migrations_online(): + """Run migrations in 'online' mode. + + In this scenario we need to create an Engine + and associate a connection with the context. + + """ + + # this callback is used to prevent an auto-migration from being generated + # when there are no changes to the schema + # reference: http://alembic.zzzcomputing.com/en/latest/cookbook.html + def process_revision_directives(context, revision, directives): + if getattr(config.cmd_opts, 'autogenerate', False): + script = directives[0] + if script.upgrade_ops.is_empty(): + directives[:] = [] + logger.info('No changes in schema detected.') + + connectable = engine_from_config( + config.get_section(config.config_ini_section), + prefix='sqlalchemy.', + poolclass=pool.NullPool, + ) + + with connectable.connect() as connection: + context.configure( + connection=connection, + target_metadata=target_metadata, + process_revision_directives=process_revision_directives, + **current_app.extensions['migrate'].configure_args + ) + + with context.begin_transaction(): + context.run_migrations() + + +if context.is_offline_mode(): + run_migrations_offline() +else: + run_migrations_online() diff --git a/migrations/script.py.mako b/migrations/script.py.mako new file mode 100644 index 0000000..2c01563 --- /dev/null +++ b/migrations/script.py.mako @@ -0,0 +1,24 @@ +"""${message} + +Revision ID: ${up_revision} +Revises: ${down_revision | comma,n} +Create Date: ${create_date} + +""" +from alembic import op +import sqlalchemy as sa +${imports if imports else ""} + +# revision identifiers, used by Alembic. +revision = ${repr(up_revision)} +down_revision = ${repr(down_revision)} +branch_labels = ${repr(branch_labels)} +depends_on = ${repr(depends_on)} + + +def upgrade(): + ${upgrades if upgrades else "pass"} + + +def downgrade(): + ${downgrades if downgrades else "pass"} diff --git a/migrations/versions/6301de4834d1_.py b/migrations/versions/6301de4834d1_.py new file mode 100644 index 0000000..e87730e --- /dev/null +++ b/migrations/versions/6301de4834d1_.py @@ -0,0 +1,55 @@ +"""empty message + +Revision ID: 6301de4834d1 +Revises: +Create Date: 2019-08-17 15:06:22.058512 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '6301de4834d1' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('user', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('username', sa.String(length=64), nullable=False), + sa.Column('email', sa.String(length=120), nullable=False), + sa.Column('password_hash', sa.String(length=128), nullable=False), + sa.Column('is_admin', sa.Boolean(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_user_email'), 'user', ['email'], unique=True) + op.create_index(op.f('ix_user_username'), 'user', ['username'], unique=True) + op.create_table('episode', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('name', sa.String(length=255), nullable=False), + sa.Column('user_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['user_id'], ['user.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_table('joke', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('joke_text', sa.Text(), nullable=False), + sa.Column('user_id', sa.Integer(), nullable=True), + sa.ForeignKeyConstraint(['user_id'], ['user.id'], ), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('joke') + op.drop_table('episode') + op.drop_index(op.f('ix_user_username'), table_name='user') + op.drop_index(op.f('ix_user_email'), table_name='user') + op.drop_table('user') + # ### end Alembic commands ### diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..04c0f32 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,23 @@ +alembic==1.0.11 +Click==7.0 +dominate==2.4.0 +Flask==1.1.1 +Flask-Bootstrap==3.3.7.1 +Flask-Login==0.4.1 +Flask-Migrate==2.5.2 +Flask-SQLAlchemy==2.4.0 +Flask-WTF==0.14.2 +itsdangerous==1.1.0 +Jinja2==2.10.1 +Mako==1.1.0 +MarkupSafe==1.1.1 +pydub==0.23.1 +python-dateutil==2.8.0 +python-editor==1.0.4 +six==1.12.0 +SQLAlchemy==1.3.7 +visitor==0.1.3 +Werkzeug==0.15.5 +WTForms==2.2.1 +beautifulsoup4==4.7.1 +APScheduler==3.6.1