Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,3 @@ dmypy.json
# Cython debug symbols
cython_debug/
.vscode/
config.json
2 changes: 2 additions & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
web: gunicorn app:app
bot: python bot/bot.py
1 change: 0 additions & 1 deletion api/api.py
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
89 changes: 82 additions & 7 deletions api/src/services/scheduleService.py
Original file line number Diff line number Diff line change
@@ -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}
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
23 changes: 12 additions & 11 deletions api/src/services/statusService.py
Original file line number Diff line number Diff line change
@@ -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
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
10 changes: 10 additions & 0 deletions app.py
Original file line number Diff line number Diff line change
@@ -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()
14 changes: 9 additions & 5 deletions bot/bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]
client.run(CONFIG["TOKEN"])
4 changes: 4 additions & 0 deletions bot/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"TOKEN": "",
"PREFIX": "!"
}
33 changes: 19 additions & 14 deletions bot/src/services/scheduleService.py
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Flask
beautifulsoup4
discord.py
requests
paramiko
gunicorn
lxml
1 change: 1 addition & 0 deletions runtime.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python-3.7.6