Skip to content

Commit

Permalink
ref: big moving
Browse files Browse the repository at this point in the history
  • Loading branch information
uburuntu committed Apr 1, 2018
1 parent 0cfb484 commit 487ee42
Show file tree
Hide file tree
Showing 18 changed files with 461 additions and 444 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,4 @@ gen/
.idea

# 3rd Party
commands/yolo/
utils/yolo/
11 changes: 6 additions & 5 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@

import config
import tokens
from commands import admin_tools, birthday, chai, donate, playroom, stats
from managers import my_acs, my_data
from utils import action_log, bold, bot_admin_command, botan, chai_user_command, command_with_delay, commands_handler, \
cut_long_text, dump_messages, global_lock, is_command, link_user, message_dump_lock, my_bot, my_bot_name, scheduler, \
subs_notify, user_action_log, check_outdated_callback
from utils import admin_tools, birthday, chai, donate, playroom, stats
from utils.acs_manager import my_acs
from utils.common_utils import action_log, bold, bot_admin_command, botan, chai_user_command, command_with_delay, \
commands_handler, cut_long_text, dump_messages, global_lock, is_command, link_user, message_dump_lock, my_bot, \
my_bot_name, scheduler, subs_notify, user_action_log, check_outdated_callback
from utils.data_manager import my_data


@my_bot.message_handler(func=commands_handler(['/start']))
Expand Down
427 changes: 0 additions & 427 deletions managers.py

This file was deleted.

1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
apscheduler>=3.3.1
beautifulsoup4>=4.6.0
opencv-python>=3.4.0
pyTelegramBotAPI>=3.2.0
requests>=2.7.0
File renamed without changes.
File renamed without changes.
158 changes: 158 additions & 0 deletions utils/acs_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# _*_ coding: utf-8 _*_
from calendar import monthrange
from datetime import datetime, timedelta

import requests
from telebot import types

import config
import tokens
from utils.common_utils import is_non_zero_file, my_bot, bold
from utils.data_manager import my_data


class AcsManager:
def __init__(self):
self.acs_url = 'https://corp.rfdyn.ru/index.php/acs-tabel-intermediadate/index-text'
self.in_url = 'https://corp.rfdyn.ru/index.php/site/now-in-office-text'
self.in_office = set()
self.in_office_old = set()

self.asc_unaccessible_error = 'Сервер СКД сейчас недоступен :('

self.keyboard = types.InlineKeyboardMarkup()
self.keyboard.add(types.InlineKeyboardButton(text="🔄", callback_data="in_office_update"))

@staticmethod
def time_format(time):
return time.strftime('%Y-%m-%d')

@staticmethod
def reply_format(text, start_date, end_date):
if is_non_zero_file(config.FileLocation.acs_answer):
with open(config.FileLocation.acs_answer, 'r', encoding='utf-8') as file:
split = text.split()
nice = file.read()
return nice.format(split[-5], split[-4], start_date, end_date, split[-2], split[-1])
return text

@staticmethod
def state_format(text):
if is_non_zero_file(config.FileLocation.acs_state_answer):
with open(config.FileLocation.acs_state_answer, 'r', encoding='utf-8') as file:
split = text.split()
nice = file.read()
if len(split) > 17:
return nice.format(split[-6], split[-5], 'В офисе' if split[18] == 'Вход' else 'Не в офисе',
split[-2])
return '🌴 Не в офисе сегодня 🌴'
return text

def year_time(self, message):
today = datetime.today()
year_start = today.replace(day=1, month=1)
year_end = today.replace(day=31, month=12)
self._make_time_request(message, year_start, year_end)

def month_time(self, message):
today = datetime.today()
month_start = today.replace(day=1)
month_end = today.replace(day=monthrange(today.year, today.month)[1])
self._make_time_request(message, month_start, month_end)

def week_time(self, message):
today = datetime.today()
week_start = today - timedelta(days=today.weekday())
week_end = week_start + timedelta(days=6)
self._make_time_request(message, week_start, week_end)

def day_time(self, message):
today = datetime.today()
self._make_time_request(message, today, today)

def _make_time_request(self, message, start_date, end_date):
payload = (('AcsTabelIntermediadateSearch[staff_id]', my_data.get_user_name(message.from_user.id)),
('AcsTabelIntermediadateSearch[date_pass_first]', self.time_format(start_date)),
('AcsTabelIntermediadateSearch[date_pass_last]', self.time_format(end_date)),
('AcsTabelIntermediadateSearch[summary_table]', '1'))

response = requests.get(self.acs_url, auth=(tokens.auth_login, tokens.auth_pswd), params=payload)
answer = self.reply_format(response.text,
self.time_format(start_date),
self.time_format(end_date)) if response.ok else self.asc_unaccessible_error
my_bot.reply_to(message, answer, parse_mode="HTML")

def in_office_now_text(self, user_id=0):
in_office_txt = self._make_in_office_request()
for alert_user in my_data.data.get(str(user_id), {}).get('alert_users', []):
in_office_txt = in_office_txt.replace(alert_user, bold(alert_user))
return '👥 ' + in_office_txt

def in_office_now(self, message):
text = self.in_office_now_text(message.from_user.id)
my_bot.reply_to(message, text, reply_markup=self.keyboard, parse_mode="HTML")

def in_office_update(self, call):
message = call.message
text = self.in_office_now_text(message.chat.id)
my_bot.edit_message_text(chat_id=message.chat.id, message_id=message.message_id, text=text, parse_mode="HTML")
my_bot.edit_message_reply_markup(chat_id=message.chat.id, message_id=message.message_id,
reply_markup=self.keyboard)
my_bot.answer_callback_query(callback_query_id=call.id, show_alert=False, text="✅ Данные обновлены")

def in_office_alert(self):
def need_alert():
if my_data.get_user_settings(user_id)['alert_about_users'] == 'on':
return True
if my_data.get_user_settings(user_id)['alert_about_users'] == 'when_in_office':
return self.is_user_in_office(user_id)

self.in_office = set(self._make_in_office_request().split('\n'))
if len(self.in_office) == 1:
return
if len(self.in_office_old) == 0:
self.in_office_old = self.in_office
return
come = self.in_office - self.in_office_old
gone = self.in_office_old - self.in_office

for user_id, user in my_data.data.items():
if need_alert():
for alert_user in user.get('alert_users', []):
if alert_user in come:
my_bot.send_message(user_id, '👨🏻‍💻️ {} сейчас в офисе!'.format(alert_user))
if alert_user in gone:
my_bot.send_message(user_id, '🙇🏻 {} теперь не в офисе!'.format(alert_user))
self.in_office_old = self.in_office

def _make_in_office_request(self):
response = requests.get(self.in_url, auth=(tokens.auth_login, tokens.auth_pswd))
return response.text if response.ok else ''

def user_state(self, message):
today = datetime.today()

payload = (('AcsTabelIntermediadateSearch[staff_id]', my_data.get_user_name(message.from_user.id)),
('AcsTabelIntermediadateSearch[date_pass_first]', self.time_format(today)),
('AcsTabelIntermediadateSearch[date_pass_last]', self.time_format(today)))

response = requests.get(self.acs_url, auth=(tokens.auth_login, tokens.auth_pswd), params=payload)
answer = self.state_format(response.text) if response.ok else self.asc_unaccessible_error
my_bot.reply_to(message, answer, parse_mode="HTML")

def is_user_in_office(self, user_id):
today = datetime.today()

payload = (('AcsTabelIntermediadateSearch[staff_id]', my_data.get_user_name(user_id)),
('AcsTabelIntermediadateSearch[date_pass_first]', self.time_format(today)),
('AcsTabelIntermediadateSearch[date_pass_last]', self.time_format(today)))

response = requests.get(self.acs_url, auth=(tokens.auth_login, tokens.auth_pswd), params=payload)
if response.ok:
split = response.text.split()
if len(split) > 17:
return split[18] == 'Вход'
return True


my_acs = AcsManager()
4 changes: 2 additions & 2 deletions commands/admin_tools.py → utils/admin_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import os

import config
from utils import my_bot, user_action_log, value_to_file
from utils.common_utils import my_bot, user_action_log, value_to_file


def kill_bot(message):
Expand All @@ -24,4 +24,4 @@ def update_bot(message):

my_bot.reply_to(message, "Ух, ухожу на обновление...")
user_action_log(message, "remotely ran update script.")
os.execl('/bin/bash', 'bash', 'bot_update.sh')
os.execl('/bin/bash', 'bash', 'utils/__bot_update.sh')
5 changes: 3 additions & 2 deletions commands/birthday.py → utils/birthday.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import requests

import tokens
from managers import my_acs, my_data
from utils import action_log, my_bot, subs_notify
from utils.acs_manager import my_acs
from utils.common_utils import action_log, my_bot, subs_notify
from utils.data_manager import my_data

happy_emoji = ['🔥', '✨', '🎂', '🍰', '🎉', '🎊', '🎁', '🎈']

Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion commands/chai.py → utils/chai.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from telebot import types

import config
from utils import link, my_bot, subs_notify, user_name, link_user
from utils.common_utils import link, my_bot, subs_notify, user_name, link_user


def chai(message):
Expand Down
2 changes: 1 addition & 1 deletion utils.py → utils/common_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import config
import tokens
from botan import Botan
from utils.botan import Botan

# Инициализация бота
my_bot = telebot.TeleBot(tokens.bot, threaded=False)
Expand Down
Loading

0 comments on commit 487ee42

Please sign in to comment.