forked from cboddy/flight-finder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flight_finder_script.py
115 lines (98 loc) · 3.86 KB
/
flight_finder_script.py
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
import urllib2, re, sys, smtplib
from ConfigParser import SafeConfigParser
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time, os
# import selenium.webdriver.chrome.service as service
"""Login to Gmail and send notification email with body (msg)."""
def sendGmailMessage(username,password,msg_price):
# print (username)
# print (password)
fromaddr = username
toaddrs = ['[email protected]']
msg = "\r\n".join([
"From: "+username,
"To: "+toaddrs[0],
"Subject: Flight to Chicago 4th of July!",
"",
msg_price
])
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()
server.ehlo()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
def checkOpodo(searchURL,maxPrice,username,password):
minPrice=10
price = selenium_test(searchURL)
print ("lowest price is",price)
# if price < minPrice: minPrice = price
if price < maxPrice:
message = "Time to check the flight company; the flight is available for "+str(price)+" kroner.\nFollow this link for the search results: "+searchURL+"\nGo, go, go!"
print message
sendGmailMessage(username,password,message)
return
print ('Min price for search was '+str(maxPrice)+'... better luck next time!')
return
def print_elm(element):
try:
print ("len price_elem inside:",len(element))
# print ("price_elem:",element)
for i in range(len(element)):
print ("element:",i,element[i])
print ("element:",i,element[i].text)
print ("element:",i,element[i].tag_name)
print ("element:",i,element[i].parent)
print ("element:",i,element[i].location)
print ("element:",i,element[i].size)
except:
print ("element:",element.text)
print ("element:",element.tag_name)
print ("element:",element.parent)
print ("element:",element.location)
print ("element:",element.size)
out = element.text
out = out.replace("kr", "")
out = out.replace(" ", "")
print (out)
# print ("element:",element)
return out
def selenium_test(searchURL):
options = webdriver.ChromeOptions()
outputdir = "/var/log"
options.binary_location = '/usr/lib/chromium-browser/chromium-browser'
log_path = "{}/chromedriver.log".format(outputdir)
service_args = ['--verbose']
print (log_path)
browser = webdriver.Chrome('/usr/lib/chromium-browser/chromedriver', \
chrome_options=options, \
service_args=service_args, \
service_log_path=log_path)
yahoo = browser.get(searchURL)
time.sleep(3)
price_elem = browser.find_element_by_class_name('MHNSJI-d-zb')
price = print_elm(price_elem)
browser.quit()
try:
return_price = int(price)
except:
return_price = price
return return_price
if __name__ == "__main__":
parser = SafeConfigParser()
config = os.path.normpath(os.path.join(os.path.realpath(__file__),"..","config.txt"))
print (config)
parser.read(config)
try:
gmailUser = parser.get('emailSettings','gmailUsername')
gmailPassword = parser.get('emailSettings','gmailPassword')
except Exception:
sys.exit('Could not find/parse email settings in config.txt')
try:
priceMax= parser.getfloat('searchSettings',"flightMaxPriceForEmail")
searchURL= parser.get('searchSettings',"searchURL",1)
except Exception:
sys.exit('Could not find/parse search settings in config.txt')
checkOpodo(searchURL,priceMax,gmailUser,gmailPassword)