So now the Discord bot send a message when it find a subject call Lingua Italiana - I try to fix the api for school time table

This commit is contained in:
Stefano Assenzo 2023-01-25 15:44:38 +00:00 committed by GitHub
parent bf041c0bbb
commit f3a75fadc0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 174 additions and 143 deletions

View File

@ -11,9 +11,7 @@ load_dotenv() # Load .env file
DISCORD_TOKEN = os.getenv('DISCORD_TOKEN') # Discord token
PASSWORD_MONGODB = os.getenv('PASSWORD_MONGODB') # Password for MongoDB
URL_MONGODB = os.getenv('URL_MONGODB') # URL for MongoDB
mongo_url = "mongodb+srv://elci:" + \
urllib.parse.quote_plus(PASSWORD_MONGODB) + \
URL_MONGODB # URL for MongoDB (with password)
mongo_url = "mongodb+srv://elci:" + urllib.parse.quote_plus(PASSWORD_MONGODB) + URL_MONGODB # URL for MongoDB (with password)
client = pymongo.MongoClient(mongo_url) # Connect to MongoDB
database = client["website-class"] # Database name
# Collection school time table current
@ -21,10 +19,13 @@ collection = database["school-time-table"]
bot = discord.Bot()
@bot.event
async def on_ready():
print('We have logged in as {0.user}'.format(bot))
bot.loop.create_task(orario())
@tasks.loop(seconds=1)
async def orario(ctx):
async def orario():
documents = collection.find()
# Iterate through the documents
@ -32,14 +33,8 @@ async def orario(ctx):
for day in document['School Subject']:
for i, subject in enumerate(document['School Subject'][day]):
if subject['Subject'] == "CALF1 LINGUA ITALIANA":
print(f"Subject found: {subject['Subject']} at index: {i}")
# Send a message on channel #general with the subject found and the index of the subject
channel = bot.get_channel(1063753802638954519).send("bot is online")
await ctx.send(f"Subject found: {subject['Subject']} at index: {i}")
channel = bot.get_channel(1063753802638954519)
#await ctx.send(f"Subject found: {subject['Subject']} at index: {i}")
await channel.send(f"Hours school: {i}, Subject: {subject['Subject']}, Teacher: {subject['Teacher']}, Room: {subject['Room']}")
@bot.command()
async def testpy(ctx):
bot.loop.create_task(orario(ctx))
await ctx.send("testpy")
bot.run(DISCORD_TOKEN)

View File

@ -42,204 +42,252 @@ def update_time_school():
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
],
"Tuesday": [
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
],
"Wednesday": [
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
],
"Thursday": [
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
],
"Friday": [
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
],
"Saturday": [
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
{
"Subject": "null",
"Teacher": "null",
"Room": "null",
},
],
}
@ -249,10 +297,14 @@ def update_time_school():
x = collection_archive.insert_one(mydict) # Add collection on collection (archive-school-time-table)
check_repeat = 0
check_repeat_teacher = 0
check_repeat_room = 0
dont_repeat = 0
dont_repeat_teacher = 0
dont_repeat_room = 0
gagaga_teacher = 0
gagaga_room = 0
number_teacher = 1
number_room = 1
number = 1
current_day = None
day_counter = 0
@ -270,6 +322,7 @@ def update_time_school():
day = str(ws.cell(row=i, column=3).value) # Get day from excel file
school_subject = ws.cell(row=i, column=column).value # Get school subject from excel file
teacher = ws.cell(row=i, column=column+1).value
room = ws.cell(row=i, column=column+2)
if dont_repeat == 9:
check_repeat += 1 # 13
if check_repeat == 5:
@ -409,7 +462,7 @@ def update_time_school():
collection_archive.update_one(
{ "_id": ObjectId(array_document_archive_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga)+ ".Subject": school_subject,
"School Subject." + array_test[0] + "." + str(gagaga)+ ".Teacher": teacher,
}
}
)
@ -439,7 +492,7 @@ def update_time_school():
collection_archive.update_one(
{ "_id": ObjectId(array_document_archive_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga)+ ".Subject": school_subject,
"School Subject." + array_test[0] + "." + str(gagaga)+ ".Teacher": teacher,
}
}
)
@ -451,4 +504,78 @@ def update_time_school():
if gagaga_teacher == 8:
gagaga_teacher = 0
#Search room school
if dont_repeat_room == 9:
check_repeat_room += 1
if check_repeat_room == 5:
check_repeat_room = 0
dont_repeat_room = 0
#number = 1
else:
if day == "None":
if room == 0:
number_room += 1
gagaga_room += 1
dont_repeat_room += 1
if number_room == 9:
number_room = 1
if gagaga_room == 9:
gagaga_room = 0
else:
find_document_school_time_table = list(collection.find({}, {"Date": long_date}))
find_document_archive_school_time_table = list(collection_archive.find({}, {"Date": long_date}))
array_document_school_time_table = find_document_school_time_table[0]["_id"]
array_document_archive_school_time_table = find_document_archive_school_time_table[0]["_id"]
collection.update_one(
{ "_id": ObjectId(array_document_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga_teacher) + ".Room": room,
}
}
)
collection_archive.update_one(
{ "_id": ObjectId(array_document_archive_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga)+ ".Room": room,
}
}
)
number_room += 1
gagaga_room += 1
dont_repeat_room += 1
if number_room == 9:
number_room = 1
if gagaga_room == 8:
gagaga_room = 0
else:
datetime_obj = datetime.datetime.strptime(day, "%Y-%m-%d %H:%M:%S").strftime("%d %m %Y")
convert_date_to_day = datetime.datetime.strptime(datetime_obj, '%d %m %Y').strftime('%A')
array_test = []
array_test.append(convert_date_to_day)
find_document_school_time_table = list(collection.find({}, {"Date": long_date}))
find_document_archive_school_time_table = list(collection_archive.find({}, {"Date": long_date}))
array_document_school_time_table = find_document_school_time_table[0]["_id"]
array_document_archive_school_time_table = find_document_archive_school_time_table[0]["_id"]
collection.update_one(
{ "_id": ObjectId(array_document_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga_teacher)+ ".Room": room,
}
}
)
collection_archive.update_one(
{ "_id": ObjectId(array_document_archive_school_time_table)},
{ "$set": {
"School Subject." + array_test[0] + "." + str(gagaga)+ ".Room": room,
}
}
)
number_room += 1
gagaga_room += 1
dont_repeat_room += 1
if number_room == 9:
number_room = 1
if gagaga_room == 8:
gagaga_room = 0
update_time_school()

View File

@ -0,0 +1,36 @@
from flask import Flask, render_template, request, redirect, session, url_for, jsonify
from dotenv import load_dotenv
from pymongo import MongoClient
import requests
import os
import urllib
import pymongo
import datetime
import requests
import time
app = Flask(__name__)
load_dotenv() #Load .env file
PASSWORD_MONGODB = os.getenv('PASSWORD_MONGODB') #Password for MongoDB
URL_MONGODB = os.getenv('URL_MONGODB') #URL for MongoDB
mongo_url = "mongodb+srv://elci:gWB3EL%25W%405%5EA%40PGvvYRt@stefano-cluster.iphin.mongodb.net/website-class"
print(mongo_url) #URL for MongoDB (with password)
client = MongoClient(mongo_url) #Connect to MongoDB
database = client["website-class"] #Database name
collection = database["school-time-table"]
db = client.get_database()
@app.route("/", methods=["GET"])
def get_subjects():
schedule_collection = db.schedule
# Utilizza il metodo find() per recuperare tutti i documenti della collezione
# e li converte in una lista
schedule = list(schedule_collection.find())
# Restituisce la risposta come una stringa JSON
return jsonify(schedule)
if __name__ == '__main__':
app.run()

View File

@ -1,127 +0,0 @@
from flask import Flask, render_template, request, redirect, session, url_for, jsonify
from dotenv import load_dotenv
import requests
import os
import urllib
import pymongo
import datetime
import requests
import time
app = Flask(__name__)
load_dotenv() #Load .env file
PASSWORD_MONGODB = os.getenv('PASSWORD_MONGODB') #Password for MongoDB
URL_MONGODB = os.getenv('URL_MONGODB') #URL for MongoDB
mongo_url = "mongodb+srv://elci:" + urllib.parse.quote_plus(PASSWORD_MONGODB) + URL_MONGODB #URL for MongoDB (with password)
client = pymongo.MongoClient(mongo_url) #Connect to MongoDB
database = client["website-class"] #Database name
collection = database["school-time-table"]
@app.route('/', methods = ['GET', 'POST'])
def api():
current_time = datetime.datetime.now()
day = str(current_time.day)
month = str(current_time.month)
year = str(current_time.year)
hour = str(current_time.hour)
minute = str(current_time.minute)
long_date = day + "-" + month + "-" + year + " " + hour + ":" + minute
#Search school subject
collection_find_schoolsubject = list(collection.find({}, {"Date": long_date ,"School Subject": 1,}))
array_schoolsubject = collection_find_schoolsubject[0]['School Subject']
#Search teacher
collection_find_teacher = list(collection.find({}, {"Date": long_date ,"Teacher": 1,}))
array_teacher = collection_find_teacher[0]['Teacher']
test = {
"subject" : {
"monday": {
"Subject 1": array_schoolsubject[0],
"Subject 2": array_schoolsubject[1],
"Subject 3": array_schoolsubject[2],
"Subject 4": array_schoolsubject[3],
"Subject 5": array_schoolsubject[4],
"Subject 6": array_schoolsubject[5],
},
"tuesday": {
"Subject 1": array_schoolsubject[14],
"Subject 2": array_schoolsubject[15],
"Subject 3": array_schoolsubject[16],
"Subject 4": array_schoolsubject[17],
"Subject 5": array_schoolsubject[18],
"Subject 6": array_schoolsubject[19],
},
"wednesday": {
"Subject 1": array_schoolsubject[12],
"Subject 2": array_schoolsubject[13],
"Subject 3": array_schoolsubject[14],
"Subject 4": array_schoolsubject[15],
"Subject 5": array_schoolsubject[16],
"Subject 6": array_schoolsubject[17],
},
"thursday": {
"Subject 1": array_schoolsubject[28],
"Subject 2": array_schoolsubject[29],
"Subject 3": array_schoolsubject[30],
"Subject 4": array_schoolsubject[31],
"Subject 5": array_schoolsubject[32],
"Subject 6": array_schoolsubject[33],
},
"friday": {
"Subject 1": array_schoolsubject[42],
"Subject 2": array_schoolsubject[43],
"Subject 3": array_schoolsubject[44],
"Subject 4": array_schoolsubject[45],
"Subject 5": array_schoolsubject[46],
"Subject 6": array_schoolsubject[47],
},
},
"teacher": {
"monday": {
"Teacher 1": array_teacher[0],
"Teacher 2": array_teacher[1],
"Teacher 3": array_teacher[2],
"Teacher 4": array_teacher[3],
"Teacher 5": array_teacher[4],
"Teacher 6": array_teacher[5],
},
"tuesday": {
"Teacher 1": array_teacher[14],
"Teacher 2": array_teacher[15],
"Teacher 3": array_teacher[16],
"Teacher 4": array_teacher[17],
"Teacher 5": array_teacher[18],
"Teacher 6": array_teacher[19],
},
"wednesday": {
"Teacher 1": array_teacher[28],
"Teacher 2": array_teacher[29],
"Teacher 3": array_teacher[30],
"Teacher 4": array_teacher[31],
"Teacher 5": array_teacher[32],
"Teacher 6": array_teacher[33],
},
"thursday": {
"Teacher 1": array_teacher[42],
"Teacher 2": array_teacher[43],
"Teacher 3": array_teacher[44],
"Teacher 4": array_teacher[45],
"Teacher 5": array_teacher[46],
"Teacher 6": array_teacher[47],
},
"friday": {
"Teacher 1": array_teacher[56],
"Teacher 2": array_teacher[57],
"Teacher 3": array_teacher[58],
"Teacher 4": array_teacher[59],
"Teacher 5": array_teacher[60],
"Teacher 6": array_teacher[61],
},
},
}
return jsonify(test)
if __name__ == '__main__':
app.run()