This commit is contained in:
USA Hexor
2018-06-04 08:16:11 +00:00
5 changed files with 162 additions and 2 deletions

View File

@ -1,2 +1,13 @@
# fesmoo_perdoliq
dirty hack for kids
It is a Telegram bot which is using OpenPerdoliqlib for resolving tests on fesmu university website.
# python3 only
reqs:
pip install requests
pip install beautifulsoup4
pip install python-telegram-bot

View File

@ -143,5 +143,7 @@ def do_action(update):
if __name__ == '__main__':
main()
try:
main()
except:
pass

94
bot.py Executable file
View File

@ -0,0 +1,94 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Simple Bot to reply to Telegram messages.
This is built on the API wrapper, see echobot2.py to see the same example built
on the telegram.ext bot framework.
This program is dedicated to the public domain under the CC0 license.
"""
import os
import logging
import threading
import telegram
from telegram.error import NetworkError, Unauthorized
from time import sleep
try:
import tg_settings
except:
print("You have to create tg_settings.py. See tg_settings.py.example.")
os.exit(1)
try:
if os.environ["DRYRUN"]:
from dryrun import Perdoliq
except:
from main import Perdoliq
update_id = None
def main():
"""Run the bot."""
global update_id
# Telegram Bot Authorization Token
TOKEN = tg_settings.TG_TOKEN
bot = telegram.Bot(TOKEN)
# get the first pending update_id,
# this is so we can skip over it in case
# we get an "Unauthorized" exception.
try:
update_id = bot.get_updates()[0].update_id
except IndexError:
update_id = None
logging.basicConfig(level=logging.DEBUG)
while True:
try:
handle_update(bot)
except NetworkError:
sleep(1)
except Unauthorized:
# The user has removed or blocked the bot.
update_id += 1
def perdoliq(username, password, subj, test, acc, is_delayed):
try:
app = Perdoliq(username, password)
app.auth()
app.resolve(subj, test, acc, is_delayed=int(is_delayed))
except Exception as e:
return "Exception: " + str(e)
def list_test(username, password):
try:
app = Perdoliq(username, password)
app.auth()
return (app.get_tests())
except Exception as e:
return "Exception: " + str(e)
def handle_update(bot):
"""Echo the message the user sent."""
global update_id
# Request updates after the last update_id
for update in bot.get_updates(offset=update_id, timeout=10):
update_id = update.update_id + 1
if update.message:
# if there is and update, process it in threads
t = threading.Thread(target=do_action, args=(update,))
t.start()
def do_action(update):
try:
s = update.message.text.split()
except:
return 1
# if s[0] == '/resolve':
# return 0
if __name__ == '__main__':
main()

41
database.py Normal file
View File

@ -0,0 +1,41 @@
import datetime as dt
class DataBase:
def __init__(self, basefile, scheme):
import sqlite3
self.scheme = ''
try:
self.conn = sqlite3.connect(basefile, check_same_thread=False)
except:
print('Could not connect to DataBase.')
return None
with open(scheme, 'r') as scheme_sql:
sql = scheme_sql.read()
self.scheme = sql
if self.conn is not None:
try:
cursor = self.conn.cursor()
cursor.executescript(sql)
except:
print('Could not create scheme.')
else:
print("Error! cannot create the database connection.")
print('DB created.')
def execute(self, sql):
cursor = self.conn.cursor()
cursor.execute(sql)
self.conn.commit()
return cursor.fetchall()
def update_user(self, user_name, user_id):
date = int(dt.datetime.now().strftime("%s"))
sql = """INSERT OR IGNORE INTO
users('user_id', 'user_name', 'date')
VALUES ('%s','%s','%s')""" % (
user_id,
user_name,
date
)
self.execute(sql)

12
scheme.sql Normal file
View File

@ -0,0 +1,12 @@
BEGIN TRANSACTION;
-- DROP TABLE IF EXISTS `user`;
CREATE TABLE IF NOT EXISTS `user` (
`id` INTEGER PRIMARY KEY AUTOINCREMENT,
`user_id` TEXT NOT NULL,
`user_name` TEXT NOT NULL,
`site_name` TEXT,
`site_user` TEXT,
`site_pass` TEXT,
`date` INTEGER NOT NULL
);
COMMIT;