Skip to content

Commit

Permalink
Merge pull request #25 from stepanskryabin/develop
Browse files Browse the repository at this point in the history
Релиз версии 0.6
  • Loading branch information
stepanskryabin authored Jan 15, 2022
2 parents 4be10c9 + ccd01fb commit 662ebd3
Show file tree
Hide file tree
Showing 47 changed files with 2,155 additions and 16,812 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,7 @@ chromedriver
/log
/database.db
ngrok.exe
win_cron.tab
/.idea
/tests/database.db
/tests/log/
1 change: 0 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ selenium = "==4.0.0"
beautifulsoup4 = "==4.10.0"
lxml = "==4.6.3"
flask = "==2.0.2"
sqlobject = "==3.9.1"
loguru = "==0.5.3"
python-crontab = "==2.6.0"

Expand Down
199 changes: 88 additions & 111 deletions Pipfile.lock

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
VERSION=0.6-develop
13 changes: 13 additions & 0 deletions controller/config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import configparser

config = configparser.ConfigParser()
config.read('config.ini', encoding="utf-8-sig")

USER = config['USER']
PARAMETERS = config['PARAMETERS']
OTHER = config['OTHER']
LOGGING = config['LOGGING']
DNEVNIK_RU = config['DNEVNIK_RU']
TRIMESTER = config['TRIMESTER']
DB = config["DATABASE"]
SCHEDULES = config["SCHEDULES"]
87 changes: 76 additions & 11 deletions controller/convtime.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
from datetime import timedelta, date
from collections import namedtuple
from datetime import timedelta
from datetime import date

from loguru import logger

from controller.config import DNEVNIK_RU
from controller.config import TRIMESTER


def filtred_by_week(year: int,
month: int,
day: int,
deep_day: int) -> tuple:
"""Функция выдаёт список двух дат принадлежащих разным неделям
deep_day: int) -> set:
"""Выводит список двух дат принадлежащих разным неделям
Args:
year (int): год начала
Expand All @@ -14,7 +21,7 @@ def filtred_by_week(year: int,
deep_day (int): глубина (дни) на которую загружается расписание
Returns:
tuple: список дат, которые принадлежат только одной неделе.
set: список дат, которые принадлежат только одной неделе.
На одну неделю - одна дата.
"""
start_date = date(year, month, day)
Expand All @@ -29,11 +36,12 @@ def filtred_by_week(year: int,
else:
d = new_week
result.append(d)
return tuple(result)
return set(result)


def date_on_week(today=None, week=None) -> tuple:
"""Функция выводит список дат с понедельника по воскресенье
def date_on_week(today=None,
week: int = None) -> tuple:
"""Выводит список дат с понедельника по воскресенье
используя для этого только текущую дату
Args:
Expand All @@ -47,20 +55,77 @@ def date_on_week(today=None, week=None) -> tuple:
TODAY = date.today()
else:
TODAY = today

if week is None:
WEEK = 1
else:
WEEK = week

Date = namedtuple("Date", ["name",
"date",
"str_date"])
WEEKDAY_NAME = ("ПН",
"ВТ",
"СР",
"ЧТ",
"ПТ",
"СБ",
"ВС")
WEEKDAY = date.weekday(TODAY)
a = [0,1,2,3,4,5,6]
a = [0,
1,
2,
3,
4,
5,
6]
FIRST_DAY = TODAY - timedelta(days=a.index(WEEKDAY))
result = []
if WEEK == 1:
for day in range(7):
result.append(FIRST_DAY + timedelta(day))
date_ = FIRST_DAY + timedelta(day)
result.append(Date(name=WEEKDAY_NAME[day],
date=date_,
str_date=str(date_)))
return tuple(result)
elif WEEK == 2:
for day in range(7):
result.append(FIRST_DAY + timedelta(day) + timedelta(7))
date_ = FIRST_DAY + timedelta(day) + timedelta(7)
result.append(Date(name=WEEKDAY_NAME[day],
date=date_,
str_date=str(date_)))
return tuple(result)


def convert_to_isodate(from_string: str) -> str:
"""Функция конвертирует найденный ID в дату
в ISO-формате
Args:
from_string (str): строка содержащая дату, например 'd20201101'
Returns:
[str]: возвращает дату в формате ISO, например '2020-11-01'
"""
var = from_string.lstrip('d')
return date(int(var[0:4]),
int(var[4:6]),
int(var[6:8])).isoformat()


def get_trimester(request_date: date) -> int:
number_of_trimester = (DNEVNIK_RU.getint('1trimester'),
DNEVNIK_RU.getint('2trimester'),
DNEVNIK_RU.getint('3trimester'))
first_trimester = date.fromisoformat(TRIMESTER.get('first'))
second_trimester = date.fromisoformat(TRIMESTER.get('second'))
third_trimester = date.fromisoformat(TRIMESTER.get('third'))
if request_date <= first_trimester:
return number_of_trimester[0]
elif first_trimester < request_date <= second_trimester:
return number_of_trimester[1]
elif second_trimester < request_date <= third_trimester:
return number_of_trimester[2]
else:
logger.error(' '.join("Ошибка в определении триместра.",
"Использован номер первого триместра"))
return number_of_trimester[1]
17 changes: 6 additions & 11 deletions controller/logger.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
import sys
import configparser

from loguru import logger

# ************** Read "config.ini" ********************
config = configparser.ConfigParser()
config.read('config.ini')
logging = config['LOGGING']
# ************** END **********************************
from controller.config import LOGGING


def add_logging(debug_status: int) -> None:
def add_logging(debug_status: int = 20) -> None:
"""Enables logging depending on start parameter uvicorn
Instead of print we use: #
logger.debug('debug message') #
Expand Down Expand Up @@ -41,14 +36,14 @@ def add_logging(debug_status: int) -> None:
if DEBUG:
# We connect the output to TTY, level DEBUG
logger.add(sys.stdout,
format=logging.get("debug"),
format=LOGGING.get("debug"),
level="DEBUG",
enqueue=True,
colorize=True)

# Connect the output to a file, level DEBUG
logger.add('log/debug.log',
format=logging.get("debug"),
format=LOGGING.get("debug"),
level="DEBUG",
enqueue=True,
colorize=True,
Expand All @@ -58,14 +53,14 @@ def add_logging(debug_status: int) -> None:
else:
# We connect the output to TTY, level INFO
logger.add(sys.stdout,
format=logging.get("info"),
format=LOGGING.get("info"),
level="INFO",
enqueue=True,
colorize=True)

# We connect the output to a file, level ERROR
logger.add('log/error.log',
format=logging.get("error"),
format=LOGGING.get("error"),
level="ERROR",
backtrace=True,
diagnose=True,
Expand Down
44 changes: 0 additions & 44 deletions controller/schedules.py

This file was deleted.

File renamed without changes.
11 changes: 6 additions & 5 deletions example_config.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@
# SQLite = sqlite:/:memory:
# PostgreSQL = postgres://postgres:[email protected]/database_name?debug=True
# MySQL = mysql://user:password@host/database
URI = sqlite:database.db
URI = database.db

[LOGGING]
# Level status:
# 50 - CRITICAL
# 40 - ERROR
# 30 - WARNING
# 25 - SUCCES
# 25 - SUCCESS
# 20 - INFO
# 10 - DEBUG
# 5 - TRACE
LEVEL = 20

# Log message formating
# Log message formatting
DEBUG = <b>{time:YYYY-MM-DD HH:mm:ss}</> | <g>{level}</> | {module} | {function} | line:{line: >3} | {message}
ERROR = <b>{time:YYYY-MM-DD HH:mm:ss}</> | <r>{level}</> | {module} | {function} | line:{line: >3} | {message}
INFO = <b>{time:YYYY-MM-DD HH:mm:ss}</> | <e>{level}</> | {message}

[DNEVNIK_RU]
LOGIN_URL = https://login.dnevnik.ru/login/esia/kir
LOGIN_URL = https://login.dnevnik.ru/
BASE_URL = https://dnevnik.ru/
SCHEDULES_URL = https://schools.dnevnik.ru/schedules/
1TRIMESTER = 1849680915715679022
2TRIMESTER = 1849680915715679023
Expand All @@ -49,4 +50,4 @@ THIRD = 2022-06-06
HOUR = 12

[OTHER]
BROWSER_DRIVER = chromedriver
BROWSER_DRIVER = chromedriver
31 changes: 7 additions & 24 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,31 @@
from time import process_time
import configparser

import click

from models import dbhandler
from controller import schedules
from db.dbhandler import DBHandler
from controller.config import DB

# ************** Read "config.ini" ********************
config = configparser.ConfigParser()
config.read('config.ini')
logging = config['LOGGING']
DB = config["DATABASE"]
SCHEDULES = config["SCHEDULES"]
# ************** END **********************************
db = DBHandler(DB.get('uri'))

db = dbhandler.DBHandler(DB.get('uri'))
sc = schedules.Schedule(SCHEDULES.get('hour'))

@click.command()
@click.option("--database",
type=click.Choice(["create", "delete"]),
help='Create or delete all table '
'with all data. Оperation, '
'with all data. Operation, '
'by default, creates all tables')
@click.option("--schedules",
type=click.Choice(["run", "delete"]),
help='Run or delete cron job '
'where started parser.py with period'
'Perion settings in config.ini')
def main(database, schedules):
def main(database,
schedules):
if database == "create":
start_time = process_time()
db.create()
click.echo(f'Table is createt at: '
click.echo(f'Table is created at: '
f'{process_time() - start_time} sec.')
elif database == "delete":
start_time = process_time()
db.delete()
click.echo(f'Table is deleted at: '
f'{process_time() - start_time} sec.')
elif schedules == "run":
click.echo(f"{sc.create()} and {sc.run()}")
elif schedules == "delete":
click.echo(f"{sc.delete()}")


if __name__ == "__main__":
Expand Down
40 changes: 0 additions & 40 deletions models/models.py

This file was deleted.

Loading

0 comments on commit 662ebd3

Please sign in to comment.