diff --git a/.gitignore b/.gitignore index 7199a87..18d49a5 100644 --- a/.gitignore +++ b/.gitignore @@ -137,4 +137,3 @@ dmypy.json # Cython debug symbols cython_debug/ .vscode/ -config.json \ No newline at end of file diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..743f5d5 --- /dev/null +++ b/Procfile @@ -0,0 +1,2 @@ +web: gunicorn app:app +bot: python bot/bot.py \ No newline at end of file diff --git a/api/api.py b/api/api.py index 408ec7d..671708e 100644 --- a/api/api.py +++ b/api/api.py @@ -1,7 +1,6 @@ from flask import Flask from flask import jsonify from src.routes import ping_blueprint, scheduler_blueprint, status_blueprint - app = Flask(__name__) app.register_blueprint(scheduler_blueprint) app.register_blueprint(ping_blueprint) diff --git a/api/src/services/scheduleService.py b/api/src/services/scheduleService.py index ae2c527..36645f1 100644 --- a/api/src/services/scheduleService.py +++ b/api/src/services/scheduleService.py @@ -1,8 +1,83 @@ +import requests +from bs4 import BeautifulSoup +from datetime import date + + +def date_decode(date): + day = "%s/%s/%s" % (date[6:8], date[4:6], date[0:4]) + hour = "%02d:%s" % (int(date[9:11]) - 3, date[11:13]) + return [day, hour] + + +def get_events(site): + page = requests.get(site) + soup = BeautifulSoup(page.content, "lxml") + + events = [] + courses = soup.find_all("a", attrs={"class": "event-link"}) + + for name in courses: + getcourseurl = ( + lambda name: "https://calendar.google.com/calendar/" + name["href"] + ) + page = BeautifulSoup(requests.get(getcourseurl(name)).content, "lxml") + event = {"course": page.title.text} + + for time in page.find_all("time"): + event[time.attrs["itemprop"]] = date_decode(time["datetime"]) + + events.append(event) + return events + + +def SpecificEvent(events, day): + new_event = [event for event in events if event["startDate"][0] == day] + return new_event + + +def SiteUrl(): + curr_year = date.today().year + curr_month = date.today().month + + startdate = "%d%02d01" % (curr_year, curr_month) + enddate = "%d%02d01" % (curr_year, curr_month + 1) + + if curr_month == 12: + enddate = str(curr_year) + "01" + "01" + return "%s/%s" % (startdate, enddate) + + def scheduleService(): - LCC1 = [] - LCC2 = [ - { "course" : "Lab. de Prog2", "startTime": "10:00", "endTime": "12:00" }, - { "course" : "Prog1", "startTime": "08:00", "endTime": "10:00" } - ] - LCC3 = [] - return {"LCC1": LCC1, "LCC2": LCC2, "LCC3": LCC3} \ No newline at end of file + sites = { + "LCC1": "https://calendar.google.com/calendar/u/0/htmlembed?src=computacao.ufcg.edu.br_9u9fch5o55dbgrschdeduq348c@group.calendar.google.com&dates=" + + SiteUrl(), + "LCC2": "https://calendar.google.com/calendar/u/0/htmlembed?src=computacao.ufcg.edu.br_80qc5chl59nmv2268aef8hp528@group.calendar.google.com&dates=" + + SiteUrl(), + "LCC3": "https://calendar.google.com/calendar/u/0/htmlembed?src=computacao.ufcg.edu.br_noalttgqttm3c5pm94k3ttbj1k@group.calendar.google.com&dates=" + + SiteUrl(), + } + + dicEvents = {} + for lcc in sites: + site = sites[lcc] + + events = get_events(site) + today = date.today() + curr_day = today.strftime("%d/%m/%Y") + new_event = SpecificEvent(events, curr_day) + dicEvents[lcc] = new_event + + dicEvents2 = {} + for lcc in dicEvents: + listaDeAulas = dicEvents[lcc] + novaListaDeAulas = [] + for aula in listaDeAulas: + aulaNova = { + "course": aula["course"], + "startTime": aula["startDate"][1], + "endTime": aula["endDate"][1], + } + novaListaDeAulas.append(aulaNova) + dicEvents2[lcc] = novaListaDeAulas + + return dicEvents2 diff --git a/api/src/services/statusService.py b/api/src/services/statusService.py index 49ed0d6..9bfdf44 100644 --- a/api/src/services/statusService.py +++ b/api/src/services/statusService.py @@ -1,14 +1,15 @@ import paramiko + def statusByHost(host): - status = True - try: - ssh = paramiko.SSHClient() - ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) - ssh.connect(hostname=host, port=23456) - ssh.close() - except paramiko.AuthenticationException: - pass - except Exception as e: - status = False - return status \ No newline at end of file + status = True + try: + ssh = paramiko.SSHClient() + ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) + ssh.connect(hostname=host, port=23456) + ssh.close() + except paramiko.AuthenticationException: + pass + except Exception as e: + status = False + return status diff --git a/app.py b/app.py new file mode 100644 index 0000000..d39b051 --- /dev/null +++ b/app.py @@ -0,0 +1,10 @@ +from flask import Flask +from flask import jsonify +from api.src.routes import ping_blueprint, scheduler_blueprint, status_blueprint +app = Flask(__name__) +app.register_blueprint(scheduler_blueprint) +app.register_blueprint(ping_blueprint) +app.register_blueprint(status_blueprint) + +if __name__ == "__main__": + app.run() \ No newline at end of file diff --git a/bot/bot.py b/bot/bot.py index 9fb85ad..9b5e3d8 100644 --- a/bot/bot.py +++ b/bot/bot.py @@ -5,24 +5,28 @@ client = discord.Client() -with open('config.json') as f: +with open("config.json") as f: CONFIG = json.load(f) + +def _getArgs(mensagem): + return mensagem.split(" ")[1:] + + @client.event async def on_ready(): print("Efetuamos o login como {0.user}".format(client)) + @client.event async def on_message(message): if message.author == client.user: return - if message.content.lower().startswith(CONFIG['PREFIX'] + 'horarios'): + if message.content.lower().startswith(CONFIG["PREFIX"] + "horarios"): msg = message.content embed = horarios(msg, _getArgs(msg)) await message.channel.send(embed=embed) -client.run(CONFIG['TOKEN']) -def _getArgs(mensagem): - return mensagem.split(' ')[1:] \ No newline at end of file +client.run(CONFIG["TOKEN"]) diff --git a/bot/config.json b/bot/config.json new file mode 100644 index 0000000..c5f2c43 --- /dev/null +++ b/bot/config.json @@ -0,0 +1,4 @@ +{ + "TOKEN": "", + "PREFIX": "!" +} \ No newline at end of file diff --git a/bot/src/services/scheduleService.py b/bot/src/services/scheduleService.py index c060db7..de45e47 100644 --- a/bot/src/services/scheduleService.py +++ b/bot/src/services/scheduleService.py @@ -1,29 +1,34 @@ import requests import discord + def horarios(mensagem, args): - instrucao = '!horarios' + instrucao = "!horarios" + + req_schedule = requests.get("https://lcc-bot-mvp.herokuapp.com/schedule") + dic = req_schedule.json() - req_schedule = requests.get('http://127.0.0.1:5000/schedule') - dic = req_schedule.json() + embed = discord.Embed(title="LCC Bot", descripition="Aulas do dia:") + shouldCheckLcc = lambda lcc: lcc.lower() in args or len(args) == 0 - embed = discord.Embed(title="LCC Bot", descripition="Aulas do dia:") - shouldCheckLcc = lambda lcc : lcc.lower() in args or len(args) == 0 + for lcc in dic: + if shouldCheckLcc(lcc): + aulas = _getAulasByLcc(lcc, dic) + embed.add_field(name=lcc.upper(), value=(aulas), inline=False) - for lcc in dic: - if shouldCheckLcc(lcc): - aulas = _getAulasByLcc(lcc, dic) - embed.add_field(name = lcc.upper(), value=(aulas), inline= False) + embed.set_footer(text="Guardians-DSC") + return embed - embed.set_footer(text="Guardians-DSC") - return embed def _getAulasByLcc(lcc, dic): - aulas = '' + aulas = "" if len(dic[lcc]) == 0: aulas += " - Hoje não haverá aula" else: - aulas = '' + aulas = "" for aula in dic[lcc]: - aulas += " - Hoje tem aula de %s ás %s horas.\n\n" % (aula['course'], aula['startTime']) + aulas += " - Hoje tem aula de %s ás %s horas.\n\n" % ( + aula["course"], + aula["startTime"], + ) return aulas \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..05c3ff0 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,7 @@ +Flask +beautifulsoup4 +discord.py +requests +paramiko +gunicorn +lxml diff --git a/runtime.txt b/runtime.txt new file mode 100644 index 0000000..6919bf9 --- /dev/null +++ b/runtime.txt @@ -0,0 +1 @@ +python-3.7.6