forked from chrislauinger/swa-scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
userFlights_db.py
145 lines (121 loc) · 4.66 KB
/
userFlights_db.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
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
from scraper import *
import swa.settings as settings
from datetime import datetime, timedelta
from helpers import *
from fares_db import getFaresForFlight
from user_db import getUser
import boto3
from boto3.dynamodb.conditions import Key, Attr
TABLE_NAME = 'userFlights'
REGION = 'us-west-2'
AWS_URL = 'https://dynamodb.us-west-2.amazonaws.com'
class UserFlight():
def __init__(self, item):
self.itemDict = item
self.username = item['username']
self.origin = item['origin']
self.date = fromMsEpoch(int(item['date']))
self.destination = item['destination']
self.flightNumber = item['flight_number']
self.cost = item['cost']
self.usingPoints = False
if (self.cost > 1000):
self.usingPoints = True
self.flightKey = item['flight_key']
self.route = self.origin + "_" + self.destination
self.fareHistory = []
self.maxDrop = 0
if ('max_drop' in item):
self.maxDrop = float(item['max_drop'])
def __str__(self):
return "%s paid %s for %s -> %s (#%s) on %s" % (self.username, costString(self.cost, self.usingPoints), self.origin, self.destination, self.flightNumber, self.date.strftime("%m/%d/%Y"))
def basicStr(self):
return "%s -> %s on %s" % (self.origin, self.destination, self.date.strftime("%m/%d/%Y"))
def addFares(self, fares):
for fare in fares:
self.fareHistory.append(fare)
def checkRefund(self):
#returns False for no Refund a number for the diff
if (len(self.fareHistory) == 0):
return False
latestFare = self.fareHistory[-1]
currentCost = latestFare.points if self.usingPoints else latestFare.price
if (currentCost < self.cost):
diff = self.cost - currentCost
return diff
else:
return False
def dynamoResponseToObjects(items):
flights = []
for item in items:
flights.append(UserFlight(item))
return flights
def putUserFlight(flight):
flight.itemDict['max_drop'] = flight.maxDrop
table = boto3.resource('dynamodb', region_name=REGION, endpoint_url=AWS_URL).Table(TABLE_NAME)
table.put_item(Item= flight.itemDict)
def getUserFlights(username):
table = boto3.resource('dynamodb', region_name=REGION, endpoint_url=AWS_URL).Table(TABLE_NAME)
response = table.query(KeyConditionExpression=Key('username').eq(username))
items = response['Items']
while(response.has_key('LastEvaluatedKey')):
response = table.query(KeyConditionExpression=Key('username').eq(username), ExclusiveStartKey = response['LastEvaluatedKey'])
items = items + response['Items']
return dynamoResponseToObjects(items)
def removeOldFlights():
table = boto3.resource('dynamodb', region_name=REGION, endpoint_url=AWS_URL).Table(TABLE_NAME)
def getAllFlights():
table = boto3.resource('dynamodb', region_name=REGION, endpoint_url=AWS_URL).Table(TABLE_NAME)
response = table.scan()
items = response['Items']
while(response.has_key('LastEvaluatedKey')):
response = table.scane(ExclusiveStartKey = response['LastEvaluatedKey'])
items = items + response['Items']
return dynamoResponseToObjects(items)
def removeScrapeDups(flights):
cleanList = []
keyList = []
for flight in flights:
key = flight.route + flight.date.strftime("%m/%d/%Y")
if key not in keyList:
cleanList.append(flight)
keyList.append(key)
diff = len(flights) - len(cleanList)
return cleanList
def checkForRefunds():
flights = getAllFlights()
#flights = getUserFlights('scottblackburn')
for flight in flights:
fares = getFaresForFlight(flight)
flight.addFares(fares)
refund = flight.checkRefund()
print(flight)
if refund:
if refund > (flight.maxDrop + 1): #+1 for rounding error
refundStr = "Refund Found! Re-book on southwest.com for a refund of %s\n%s\nYou will receive another email if the price drops lower" % (diffCostString(refund, flight.usingPoints), str(flight))
sendEmail(getUser(flight.username).email, 'Southwest Refund Found: ' + flight.basicStr(), refundStr)
flight.maxDrop = refund
putUserFlight(flight)
else:
print(str(flight) + '-- Refund found, but not a large enough drop')
def countUserFlights():
table = boto3.resource('dynamodb', region_name=REGION, endpoint_url=AWS_URL).Table(TABLE_NAME)
return table.item_count
def printFlightsWithoutFares():
flights = getAllFlights()
for flight in flights:
fares = getFaresForFlight(flight)
if (len(fares) == 0):
print(flight)
def minPointsFlight():
minPoints = 5000
flights = getAllFlights()
for flight in flights:
fares = getFaresForFlight(flight)
for fare in fares:
if (fare.points < minPoints):
minPoints = fare.points
print(minPoints)
if __name__ == '__main__':
print(str(printFlightsWithoutFares()))
#minPointsFlight()