Skip to content

Commit

Permalink
added comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Ethan Friedman committed Dec 6, 2019
1 parent b41c83b commit 135a0fd
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 25 deletions.
21 changes: 16 additions & 5 deletions mainApp.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
import tkinter as tk
import os
#This runs the times.py and makes a GUI that updates every n milliseconds.
import tkinter as tk #imports GUI package
import os # allows you to run commands in command line from a line of code.

n = 1000# <-- change this value to change update time (ms)
#Update Function
def update():
#set output of times.py to var so I can call later
times = os.popen('python times.py').read()
#set times to a string
l.config(text=str(times))
root.after(1000, update)

#Main update function for GUI
root.after(n, update)
# define GUI properties and display it
root = tk.Tk()
#Label GUI
l = tk.Label(text='Train Times:')
#Pack info to be displayed
l.pack()
root.after(1000, update)
#update after defined number of milliseconds
root.after(n, update)
#start GUI
root.mainloop()
46 changes: 26 additions & 20 deletions times.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,47 @@
#Scraping Module
from bs4 import BeautifulSoup
#Allows you to get the webpage without visting the site by sending a get request for defind URl
import requests
#allows us to change json dictionary to python dictionary for parsing
import json
#URLs for two sites that are scraped
twothreeURL = 'http://traintimelb-367443097.us-east-1.elb.amazonaws.com/getTime/2/232?callback=angular.callbacks._0'
fourfiveURL = 'http://traintimelb-367443097.us-east-1.elb.amazonaws.com/getTime/4/423?callback=angular.callbacks._0'
def FourFive(trainURL):
r = requests.get(trainURL)
soup = BeautifulSoup(r.text,'html.parser')
#json1 since need to call an actual json funtion later on
json1 = soup.text
json1 = json1.split('(')
json1 = json1[1].split(')')
json1 = json1[0]
jsonDict = json.loads(json1)
#uptown and downtown train number + last stop + min away
def FourFive(trainURL): #Function that scrapes the 4/5 train site
r = requests.get(trainURL) # sets r so when you call it you send a GET request to specified URL
soup = BeautifulSoup(r.text,'html.parser') # scrapes the returned page as html, and only includes text
json1 = soup.text #json1 since need to call an actual json funtion later on
json1 = json1.split('(') # removes junk before dictionary
json1 = json1[1].split(')') #removes unk after dictionary
json1 = json1[0] # after split, things are placed in a list, so by doing this json1 is now just the dictionary without junk code.
jsonDict = json.loads(json1) # sets JSON dict as python dict so it can be parsed.

# Manhattan is uptown, Brooklyn is downtown. Both of these look into the dictionary and get the first two trains under the 'times' key.
# The direction1 key is for Manhattan and direction 2 is for brooklyn
# Inside the "times" value is the "last stop" "minutes away" and "route"
manhattan = jsonDict["direction1"]["times"][0:2]
brooklyn = jsonDict["direction2"]["times"][0:2]
#manhattan bound 4 5

# Prints direction1 value and the name, runs a loop that prints the upcoming trains with some text to help distinguish
print(jsonDict ["direction1"]["name"],"4/5:")
for i in range(0,2):
NextTrainsManhattan = ("There is a", manhattan[i]["route"], "train to", manhattan[i]["lastStation"],":", manhattan[i]["minutes"], "minutes away")
print(*NextTrainsManhattan)
print('---------')
#Brooklyn bound 4 5

#Brooklyn bound 4 5 # same a above loop but for Brooklyn bound trains
print(jsonDict["direction2"]["name"],("4/5:"))
for j in range(0,2):
NextTrainsBrooklyn = ("There is a", brooklyn[j]["route"], "train to", brooklyn[j]["lastStation"],":", brooklyn[j]["minutes"], "minutes away")
print(*NextTrainsBrooklyn)
def TwoThree(trainURL2):
r = requests.get(trainURL2)
soup = BeautifulSoup(r.text,'html.parser')
#json1 since need to call an actual json funtion later on
json1 = soup.text
json1 = json1.split('(')
json1 = json1[1].split(')')
json1 = json1[0]
jsonDict = json.loads(json1)
#uptown and downtown train number + last stop + min away
manhattan = jsonDict["direction1"]["times"][0:2]
brooklyn = jsonDict["direction2"]["times"][0:2]
#manhattan bound 2 3
Expand All @@ -48,8 +54,8 @@ def TwoThree(trainURL2):
print(jsonDict["direction2"]["name"],"2/3:")
for y in range(0,2):
NextTrainsBrooklyn = ("There is a", brooklyn[y]["route"], "train to", brooklyn[y]["lastStation"],":", brooklyn[y]["minutes"], "minutes away")
print(*NextTrainsBrooklyn)
def info(url):
print(*NextTrainsBrooklyn) # Everything in here is the same as above, just for 2/3 trains. Only thing that changed is the URL.
def info(url): #Information that displays at top of GUI (station and last time updated)
r = requests.get(url)
soup = BeautifulSoup(r.text,'html.parser')
json1 = soup.text
Expand All @@ -63,12 +69,12 @@ def info(url):
print('----------')
print(updateTime)
print('___________')
def mainText(): #updates times
def mainText(): #Runs all other functions to get info. This is run when the document is run from mainApp.py
#f = open('/times/')
info(twothreeURL)
print("Upcoming 2 and 3 Trains:\n_______________")
info(twothreeURL) # gets info
print("Upcoming 2 and 3 Trains:\n_______________") # Presents it nicely
TwoThree(twothreeURL)
print("_______________\nUpcoming 4 and 5 Trains:\n_______________")
FourFive(fourfiveURL)
print("")
mainText()
mainText() # runs everything

0 comments on commit 135a0fd

Please sign in to comment.