import requests from datetime import datetime import pytz r = requests.get("https://apps.studentaffairs.cmu.edu/dining/conceptinfo/?page=listConcepts") text = r.text text = text.splitlines() recording = False lines = [] for line in text: line = line.strip() if (len(line) > 0 and line[0].isalnum()) or '<div class="status">' in line: if recording: lines.append(line) if "Off Campus" in line: recording = True i = 0 #Some words that should be ignored because they are not necessary ignored = ["Location:", "Closed today", "Open today", "Order Online", "Today's Hours:", "4700 Fifth Avenue<br />", "Pittsburgh, PA 15213<br />"] while i < len(lines): if lines[i] in ignored: lines.pop(i) else: i+=1 def getNames(lines): names = [] for line in lines: #Only the restaurant names have all capital letters and don't start with a number if line.upper() == line and line[0].isalpha(): names.append(line) return names def updateRestaurantStatus(lines, names, restaurantDict): curr = names[0] for line in lines: if "Closed" in line: restaurantDict[curr][0] = "Closed" elif "Open" in line: restaurantDict[curr][0] = "Open" elif line in names: curr = line #Manually overrides the entry for Stephanie's Market since it's not up-to-date restaurantDict["STEPHANIE'S - MARKET C"][0] = "Closed" def updateRestaurantTimes(lines, names, restaurantDict): curr = names[0] prevName = None for line in lines: if line[0].isdigit(): if curr == prevName: restaurantDict[curr][1] = [restaurantDict[curr][1], line] else: restaurantDict[curr][1] = line prevName = curr elif line in names: curr = line #Manually overrides the entry for Stephanie's Market since it's not up-to-date restaurantDict["STEPHANIE'S - MARKET C"][1] = "" def convertAllTimes(restaurantDict): for restaurant in restaurantDict: times = restaurantDict[restaurant][1] if type(times) == str and len(times) > 0: #print(convertTimeRange(times)) restaurantDict[restaurant][1] = convertTimeRange(times) elif type(times) == list: restaurantDict[restaurant][1] = [convertTimeRange(times[0]), convertTimeRange(times[1])] def convertTimeRange(timeString): times = timeString.split() return [convertTime(times[0:2]), convertTime(times[3:])] def convertTime(time): number = time[0] amPm = time[1] #converts to a 24 hour clock if amPm == "PM" and number[:2] != "12": time = number.split(":") return f"{int(time[0]) + 12}:{time[1]}" elif amPm == "AM" and number[:2] == "12": time = number.split(":") return f"0:{time[1]}" return number def createRestaurantDict(lines): getNames(lines) restaurantDict = {} for name in names: restaurantDict[name] = ["", ""] return restaurantDict def getOpenList(restaurantDict, time): openList = [] for restaurant in restaurantDict: if restaurantDict[restaurant][0] == "Open": openList.append(restaurant) getTimeToClosing(restaurant, restaurantDict, time) return openList def getTimeToClosing(restaurant, restaurantDict, time): #Notifications only display within 2 hours of closing cutoffTime = 2 time = getDecimalTime(time) if type(restaurantDict[restaurant][1][0]) == str: closingTime = getDecimalTime(restaurantDict[restaurant][1][1]) else: closingTime = getDecimalTime(restaurantDict[restaurant][1][0][1]) if time > closingTime: closingTime = getDecimalTime(restaurantDict[restaurant][1][1][1]) timeToClosing = closingTime - time if timeToClosing > cutoffTime: restaurantDict[restaurant].append("") return hours = int(timeToClosing) minutes = round((timeToClosing - hours) * 60) output = "" output += "Closes in" if hours == 1: output += " 1 hour" elif hours > 1: output += f" {hours} hours" if minutes == 1: output += " 1 minute" else: output += f" {minutes} minutes" restaurantDict[restaurant].append(output) def getTimeToOpening(restaurant, restaurantDict, time): if restaurantDict[restaurant][1] == "": return "" #Notifications only display within 2 hours of closing cutoffTime = 2 time = getDecimalTime(time) if type(restaurantDict[restaurant][1][0]) == str: openingTime = getDecimalTime(restaurantDict[restaurant][1][0]) else: openingTime = getDecimalTime(restaurantDict[restaurant][1][0][0]) if time > openingTime: openingTime = getDecimalTime(restaurantDict[restaurant][1][1][0]) timeToOpening = openingTime - time if timeToOpening < 0: timeToOpening += 24 if timeToOpening > cutoffTime: return "" hours = int(timeToOpening) minutes = round((timeToOpening - hours) * 60) output = "" output += "Opens in" if hours == 1: output += " 1 hour" elif hours > 1: output += f" {hours} hours" if minutes == 1: output += " 1 minute" else: output += f" {minutes} minutes" return output def getMaxLen(names): maxLen = 0 for name in names: length = len(fixName(name)) if length > maxLen: maxLen = length return maxLen def printOpenRestaurants(restaurantDict, time): print("Open Restaurants:") print() openList = getOpenList(restaurantDict, time) #maxLen = getMaxLen(openList) #startingIndex = maxLen + 4 for name in openList: length = len(name) print(fixName(name)) timeToClosing = restaurantDict[name][2] if timeToClosing != "": print(" " * 4 + timeToClosing) print() #print(fixName(name), " " * (startingIndex - length), restaurantDict[name][2]) def printOpeningRestaurants(restaurantDict, time): print("Opening Soon:") print() openList = getOpenList(restaurantDict, time) for restaurant in restaurantDict: if restaurant not in openList: timeToOpening = getTimeToOpening(restaurant, restaurantDict, time) if timeToOpening != "": print(fixName(restaurant)) print(" " * 4 + timeToOpening) print() def fixName(name): names = {"AU BON PAIN AT SKIBO CAFÉ": "AU BON PAIN", "ROHR COMMONS - TAZZA D'ORO": "TAZZA D'ORO", "ROHR COMMONS - TEPPER EATERY": "TEPPER EATERY", "ROHR COMMONS - WILD BLUE SUSHI": "WILD BLUE SUSHI", "SCHATZ DINING ROOM": "SCHATZ"} newName = names.get(name, None) if newName == None: return name return newName def getDecimalTime(time): time = time.split(":") hours = int(time[0]) minutes = int(time[1]) return hours + minutes/60 def getTime(): tz = pytz.timezone('US/Eastern') time = datetime.now(tz) hours = time.hour minutes = time.minute if minutes < 10: return f"{hours}:0{minutes}" else: return f"{hours}:{minutes}" time = getTime() print("Current Time:", time) print() names = getNames(lines) restaurantDict = createRestaurantDict(names) updateRestaurantStatus(lines, names, restaurantDict) updateRestaurantTimes(lines, names, restaurantDict) convertAllTimes(restaurantDict) printOpenRestaurants(restaurantDict, time) print() printOpeningRestaurants(restaurantDict, time) #print() #print(restaurantDict)
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question