Skip to content

Commit

Permalink
Merge pull request #45 from Augugrumi/from_free_time
Browse files Browse the repository at this point in the history
Add from free functionality
  • Loading branch information
Polpetta authored Apr 5, 2018
2 parents 6ac0f0c + 1eead61 commit 221dc63
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 16 deletions.
33 changes: 33 additions & 0 deletions torrearchimedebot/bot/handlers/free_from_handler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This file is part of TorreArchimedeBot.
#
# TorreArchimedeBot is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# TorreArchimedeBot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with TorreArchimedeBot. If not, see <http://www.gnu.org/licenses/>.

from .abs_handler import AbsHandler
from .parsing import *
from .utility import *


class FreeFromHandler(AbsHandler):

def __init__(self, message):
self._message = message

def handleMessage(self):
# "\freefrom " is 10 characters
string_time = parse_freefrom_time(self._message[10:])
if not type(string_time) is datetime.time:
return string_time
response = freeFrom(string_time)
return "Rooms that are free from " + string_time.strftime('%H:%M') \
+ " are:\n" + response
2 changes: 1 addition & 1 deletion torrearchimedebot/bot/handlers/info_handler.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def __init__(self):
self.github = "https://github.com/Augugrumi/TorreArchimedeBot"
self.url_donation = "http://paypal.me/DavidePolonio"
self.author = "Augugrumi Team"
self.version = "0.3.8"
self.version = "0.3.9"

def handleMessage(self):

Expand Down
38 changes: 30 additions & 8 deletions torrearchimedebot/bot/handlers/parsing.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,19 @@ def now(self):
toReturn = [t] + toReturn
return toReturn

def scheduleAt(self, time_to_check):
toReturn = ''
t = ''
for time in self.schedule:
print(time)
if (time_in_range(time, time_to_check)):
print("si")
toReturn = self.schedule[time]
t = time
if (toReturn != ''):
toReturn = [t] + toReturn
return toReturn

def __str__(self):
return json.dumps(self, default=lambda o: o.__dict__, indent=4)

Expand Down Expand Up @@ -160,18 +173,15 @@ def nowSchedule():
return roomActivities


def nowFree():
def free_room_parser(strnow = "", hour_min = None):
schedule = ''
roomActivities = ''
nextActivities = []
scheduleAccess = ScheduleAccess()
rooms = retrieve_rooms()
tz = pytz.timezone('Europe/Rome')
now = datetime.datetime.now(tz).time()
strnow = now.strftime('%H:%M')
for room in rooms:
schedule = scheduleAccess.getScheduleForRoom(room)
roomScheduleNow = schedule.now()
roomScheduleNow = schedule.scheduleAt(hour_min) if hour_min else schedule.now()
if (roomScheduleNow == ''):
roomActivities += room
nextActivities = [
Expand All @@ -182,8 +192,8 @@ def nowFree():
else:
roomActivities += 'tomorrow'
roomActivities += '\n'
elif any(x in roomScheduleNow for x in ["da confermare",
"Aula riservata al Dip.to Matematica"]):
elif any(x in roomScheduleNow for x in ["da confermare",
"Aula riservata al Dip.to Matematica"]):
roomActivities += ("⚠ Possible empty: " + room)
nextActivities = [
k for k in schedule.schedule if k >= (strnow + '-' + strnow)]
Expand All @@ -199,6 +209,18 @@ def nowFree():
return "No room is free"


def freeFrom(hour_min):
strnow = hour_min.strftime('%H:%M')
return free_room_parser(strnow, hour_min)


def nowFree():
tz = pytz.timezone('Europe/Rome')
now = datetime.datetime.now(tz).time()
strnow = now.strftime('%H:%M')
return free_room_parser(strnow)


class ScheduleAccess:
allSchedules = {}

Expand Down Expand Up @@ -227,7 +249,7 @@ def lookupFromServer():
def startUpdater():
ScheduleUpdater.lookupFromServer()
if (time(20, 00) <= actual_time() <= time(23, 59)) or \
(time(2, 00) <= actual_time() <= time(5, 59)):
(time(2, 00) <= actual_time() <= time(5, 59)):
threading.Timer(14400, startUpdater).start()
else:
threading.Timer(3600, startUpdater).start()
39 changes: 32 additions & 7 deletions torrearchimedebot/bot/handlers/utility.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,19 @@

import datetime
import pytz
import re
from datetime import time


def time_in_range(interval):
def actual_time():
tz = pytz.timezone('Europe/Rome')
return datetime.datetime.now(tz).time()


def time_in_range(interval='12:00-12:00', time_to_check=actual_time()):
"""Return true if the interval include actual time"""
[start, end] = string_interval_to_time(interval)
return (start <= actual_time() <= end)
return (start <= time_to_check <= end)


def before_now(interval):
Expand All @@ -29,11 +36,6 @@ def before_now(interval):
return actual_time() > end


def actual_time():
tz = pytz.timezone('Europe/Rome')
return datetime.datetime.now(tz).time()


def string_interval_to_time(interval):
"""Return 2 time retrieved from the string interval"""
[timeStart, timeEnd] = interval.split('-')
Expand Down Expand Up @@ -64,3 +66,26 @@ def retrieve_rooms():
'P200'
]
return rooms


def parse_freefrom_time(string_time):
lst = re.findall(r"[\w']+", string_time)
if not lst:
return "Time not valid"
hour = lst[0]
minutes = lst[1] if len(lst) > 1 else "00"
if len(hour) >= 4:
tmp = hour
hour = tmp[0:2]
minutes = tmp[2:4]

try:
hour = int(hour)
minutes = int(minutes)
except Exception:
return "Time not valid"
print(str(hour) + ":" + str(minutes))
if not (0 <= hour <= 23 and 0 <= minutes <= 59):
return "Time not valid"
hour_min = time(hour, minutes)
return hour_min
10 changes: 10 additions & 0 deletions torrearchimedebot/bot/telegram_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from bot.handlers.room_handler import RoomHandler
from bot.handlers.now_handler import NowHandler
from bot.handlers.free_handler import FreeHandler
from bot.handlers.free_from_handler import FreeFromHandler
from bot.handlers.info_handler import InfoHandler
from bot.handlers.utility import *
from bot.handlers.parsing import startUpdater
Expand All @@ -47,6 +48,10 @@ def __init__(self):
freeHandler = CommandHandler('free', self.free)
self._dispatcher.add_handler(freeHandler)

#Free from a certain time Handler
freeFromHandler = CommandHandler('freefrom', self.freefrom)
self._dispatcher.add_handler(freeFromHandler)

#Info handler
infoHandler = CommandHandler('info', self.info)
self._dispatcher.add_handler(infoHandler)
Expand Down Expand Up @@ -99,6 +104,11 @@ def free(self, bot, update):
handler = FreeHandler()
bot.send_message(chat_id=update.message.chat_id, text=handler.handleMessage())

def freefrom(self, bot, update):
self.commonOperation(bot, update)
handler = FreeFromHandler(update.message.text)
bot.send_message(chat_id=update.message.chat_id, text=handler.handleMessage())

def info(self, bot, update):
self.commonOperation(bot, update)
handler = InfoHandler()
Expand Down

0 comments on commit 221dc63

Please sign in to comment.