-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmegabus.py
More file actions
151 lines (131 loc) · 5.48 KB
/
megabus.py
File metadata and controls
151 lines (131 loc) · 5.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import urllib
import smtplib
import time
import sqlite3 as sqlite
from bs4 import BeautifulSoup
print 'MegaBus Ticket Checker v1.0\n'
mailSender = "email" #Gmail account being used to send email
mailSenderPassword = "pass" #Gmail account's password
cityCodes = { #Cities and their respective code
'Austin':320,
'austin':320,
'Dallas':317,
'dallas':317,
'Houston':318,
'houston':318
}
monthCodes = { #months and their respective number
1:'January',
2:'February',
3:'March',
4:'April',
5:'May',
6:'June',
7:'July',
8:'August',
9:'September',
10:'October',
11:'November',
12:'December',
}
def city_error_check(city): #checks if city is in cityCodes (only required for user input, not SQL DB control)
try:
cityCodes[city]
return city
except KeyError:
print "City misspelled or not in database. Please try again."
city = city_error_check(raw_input("Re-enter city: "))
return city
def send_email(data, message, mailReceiver): #mailReceiver is email address of recipient
print "Accessing Gmail servers to send email to {}".format(mailReceiver)
#login to gmail servers
gmailserver = smtplib.SMTP("smtp.gmail.com", 587)
gmailserver.ehlo()
gmailserver.starttls()
gmailserver.ehlo()
gmailserver.login(mailSender, mailSenderPassword)
#prepare message as a string
header = "Megabus Trip Details for {} {}, {} from {} to {}".format(monthCodes[parameters[0]], parameters[1], parameters[2], parameters[4], parameters[3])
for each_trip in data:
message = message + "\n\n" + each_trip[0] + "\n" + each_trip[1] + "\n" + each_trip[2]
#send message and quit server
gmailserver.sendmail(mailSender, mailReceiver, header + "\n\n\n" + message)
gmailserver.quit()
print "Email sent to {}\n".format(mailReceiver)
def megabus_day_data(Month, Day, Year, destination, departure): #return trip details as list [departure, arrival, cost] for given parameters
global parameters
parameters = (Month, Day, Year, destination, departure)
print "Accessing Megabus site for data"
customURL = 'http://us.megabus.com/JourneyResults.aspx?originCode={}&destinationCode={}&outboundDepartureDate={}%2f{}%2f{}&inboundDepartureDate=&passengerCount=1&transportType=0&concessionCount=0&nusCount=0&outboundWheelchairSeated=0&outboundOtherDisabilityCount=0&inboundWheelchairSeated=0&inboundOtherDisabilityCount=0&outboundPcaCount=0&inboundPcaCount=0&promotionCode=&withReturn=0' .format(cityCodes[departure], cityCodes[destination], Month, Day, Year)
if "No journeys have been found for the date selected" in urllib.urlopen(customURL).read():
return False
dayData = BeautifulSoup(urllib.urlopen(customURL).read()).find_all("ul", class_="journey standard")
#extract all relavent information into list
data = []
for tag in dayData:
tempdata = []
departure = ""
for string in tag.find("li", class_="two").p.stripped_strings:
departure = departure + " " + string
tempdata.append(departure[1:].encode('utf-8'))
arrive = ""
for string in tag.find("li", class_="two").find("p", class_="arrive").stripped_strings:
arrive = arrive + " " + string
tempdata.append(arrive[1:].encode('utf-8'))
for string in tag.find("li", class_="five").p.stripped_strings:
cost = string
tempdata.append(cost.encode('utf-8'))
data.append(tempdata)
return data
def config_data(instance): #return user details for instance given
print "Accessing SQL Database for User {}".format(instance)
configdb = sqlite.connect('config.db')
with configdb:
cursor = configdb.cursor()
cursor.execute('SELECT * FROM users WHERE id=?', (instance,))
return cursor.fetchone()
def config_to_day_data(instance): #returns ([depart, arrive, cost], email, cost)
userDetails = config_data(instance)
if userDetails == None:
return None
if not userDetails[8] == "yes":
return "Inactive"
return megabus_day_data(userDetails[5], userDetails[6], userDetails[7], userDetails[3], userDetails[4]), userDetails[2], userDetails[9]
def save_cost_data(allData, instance):
configdb = sqlite.connect('config.db')
with configdb:
cursor = configdb.cursor()
cursor.execute('UPDATE users SET Cost=? WHERE Id=?', (cost_template(allData), instance))
def cost_template(allData):
temp = "("
for trip in allData:
temp = temp + trip[2] + ","
temp = temp + ")"
return temp
#--------------------Start Main Program--------------------#
while True:
instance = 0
while True:
instance = instance + 1
instanceData = config_to_day_data(instance)
if instanceData == None: #checks if user exists
print "User {} does not exist.".format(instance)
print "Completed all user requests.\n"
break
if instanceData == "Inactive": #checks if user is still active
print "User with ID {} is inactive".format(instance)
continue
if not instanceData[0]:
print "User {} request not for sale yet.\n".format(instance)
continue
if instanceData[2] == "0": #checks if user has previous cost data
send_email(instanceData[0], "Welcome to Megabus Checker. Please make sure to unblock aj.test.python@gmail.com if you wish to receive emails from this service.", instanceData[1])
save_cost_data(instanceData[0], instance)
else:
if instanceData[2] == cost_template(instanceData[0]):
print "User {} cost information did not change. Email not sent.\n".format(instance)
else:
print "User {} cost information changed.".format(instance)
send_email(instanceData[0], "Ticket prices for your trip date have changed. Here are the updated prices.", instanceData[1])
print time.strftime('%c') + ": Standby mode. Safe to edit SQL Database for next 3 hours."
time.sleep(10800)