1
0

initial commit v2 !!

This commit is contained in:
Akbar Rahman 2021-02-25 21:34:33 +00:00
parent e9decc0ee7
commit 6212d92fc9
Signed by: alvierahman90
GPG Key ID: 20609519444A1269
4 changed files with 169 additions and 0 deletions

3
.gitignore vendored
View File

@ -1,2 +1,5 @@
config.json
tasks.json
db.json
env
__pycache__

13
requirements.txt Normal file
View File

@ -0,0 +1,13 @@
APScheduler==3.6.3
certifi==2020.12.5
cffi==1.14.4
cryptography==3.3.1
fuzzywuzzy==0.18.0
pycparser==2.20
pydo.txt==1.1
python-Levenshtein==0.12.2
python-telegram-bot==13.2
pytz==2021.1
six==1.15.0
tornado==6.1
tzlocal==2.1

68
src/bot.py Normal file
View File

@ -0,0 +1,68 @@
from telegram.ext import Updater
from telegram.ext import CommandHandler
from telegram.ext import MessageHandler, Filters
import logging
import db
import pydo
from fuzzywuzzy import process as fuzzyprocess
from fuzzywuzzy import utils as fuzzyutils
logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
level=logging.INFO)
updater = Updater(token='671191771:AAFx_sGQGoHvjgwCBDROPsj3_VudgfFm2qg', use_context=True)
dispatcher = updater.dispatcher
def start(update, context):
context.bot.send_message(chat_id=update.effective_chat.id,
text="hiiiiiiiiii. what do you need to get done today?")
dispatcher.add_handler(CommandHandler('start', start))
def ls(update, context):
tasks = db.get_all_user_tasks(update.effective_user)
r = ""
for task in tasks:
if task.done is ('done' in update.message['text']) or ('all' in update.message['text']):
r+= f"{task.id} {str(task)}"
r+= '\n'
r = r if r != "" else "no tasks!"
context.bot.send_message(chat_id=update.effective_chat.id,
text=r)
dispatcher.add_handler(CommandHandler('ls', ls))
def do(update, context):
for arg in context.args:
if not arg.isnumeric():
task_ids = [fuzzy_get_task_id(update.effective_user, ' '.join(context.args))]
break
else:
task_ids = [int(x) for x in context.args]
for task_id in task_ids:
task = db.get_task(update.effective_user, task_id)
task.do()
db.update_task(update.effective_user, task)
context.bot.send_message(chat_id=update.effective_chat.id, text=str(task_ids))
def fuzzy_get_task_id(user, text):
task_strs = [str(task) for task in db.get_all_user_tasks(user)]
return task_strs.index(fuzzyprocess.extractOne(text, task_strs)[0])
dispatcher.add_handler(CommandHandler('do', do))
def new_task(update, context):
db.add_task(update.effective_user, pydo.Task(update.message.text))
context.bot.send_message(chat_id=update.effective_chat.id,
text=f"Created task: {update.message.text}")
dispatcher.add_handler(MessageHandler(Filters.text & (~Filters.command), new_task))
updater.start_polling()

85
src/db.py Normal file
View File

@ -0,0 +1,85 @@
# this is a temporary db to get things working
import json
import pydo
import telegram
DB_FILE="./db.json"
class DbKeys:
USER_TASKS = 'user_tasks'
def _create_db():
db = {
DbKeys.USER_TASKS : {}
}
with open(DB_FILE, "w+") as file:
json.dump(db, file)
def _get_db():
with open(DB_FILE) as file:
return json.load(file)
def _set_db(db):
with open(DB_FILE, "w") as file:
json.dump(db, file)
def get_all_user_tasks(user) -> "list of pydo.Task":
db = _get_db()
task_list = db[DbKeys.USER_TASKS][str(user.id)]
r = []
for id, task_str in enumerate(task_list):
task = pydo.Task(task_str)
task.id = id
r.append(task)
return r
def export_user_tasks(user: telegram.User) -> str:
db = _get_db()
print(db)
return '\n'.join(db[DbKeys.USER_TASKS][str(user.id)])
def get_task(user: telegram.User, task_id: int) -> pydo.Task:
for task in get_all_user_tasks(user):
if int(task.id) == int(task_id):
return task
def create_user(db, user: telegram.User):
if str(user.id) not in db[DbKeys.USER_TASKS].keys():
db[DbKeys.USER_TASKS][str(user.id)] = []
return db
def add_task(user: telegram.User, task: pydo.Task) -> pydo.Task:
db = _get_db()
db = create_user(db, user)
db[DbKeys.USER_TASKS][str(user.id)].append(str(task))
_set_db(db)
return task
def update_task(user: telegram.User, new_task: pydo.Task) -> pydo.Task:
db = _get_db()
db[DbKeys.USER_TASKS][str(user.id)][new_task.id] = str(new_task)
_set_db(db)
return new_task
def remove_task(user: telegram.User, task_id: int) -> int:
db = _get_db()
# instead of removing an item, set it's text value to nothing, so that all other tasks' ids
# don't change
db[DbKeys.USER_TASKS][str(user.id)][str(task_id)] = ""
_set_db(db)
def remove_task(user: telegram.User, task: pydo.Task) -> pydo.Task:
remove_task(user.id, task.id)
return task
try:
_get_db()
except:
_create_db()