-
Notifications
You must be signed in to change notification settings - Fork 0
/
handler.py
76 lines (63 loc) · 2.38 KB
/
handler.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
import json
import os
from datetime import datetime
from decimal import Decimal
import scraper
import database
import notification
import sentry_sdk
from sentry_sdk.integrations.aws_lambda import AwsLambdaIntegration
sentry_key = os.environ.get("SENTRY_KEY")
sentry_project = os.environ.get("SENTRY_PROJECT")
sentry_sdk.init(
dsn="https://{}@sentry.io/{}".format(sentry_key, sentry_project),
integrations=[AwsLambdaIntegration()]
)
def scrape_megabus(event, context):
route_info = json.loads(event['Records'][0]['Sns']['Message'])
date = route_info["date"]
origin = route_info["origin"]
destination = route_info["destination"]
journeys_prices = scraper.get_prices(date, origin, destination)
# boto3 float to decimal hack
journeys_prices_dump = json.dumps(journeys_prices)
journeys_prices = json.loads(journeys_prices_dump, parse_float=Decimal)
for journey_id, prices in journeys_prices.items():
journey = database.get_journey(journey_id)
if journey != None:
price_by_numtix = journey.get("price_by_numtix")
price_by_numtix[datetime.utcnow().isoformat()] = prices
database.update_journey(journey_id, price_by_numtix)
else:
price_by_numtix = {}
price_by_numtix[datetime.utcnow().isoformat()] = prices
database.put_journey({
"journey_id": journey_id,
"price_by_numtix": price_by_numtix,
"date": date,
"origin": origin,
"destination": destination})
return "Success"
def trigger_scrape_megabus(event, context):
origin = 56 # Edinburgh
destination = 34 # London
dates = scraper.travel_dates(origin, destination)
if dates != None:
for date in dates:
notification.send_scrape_params(json.dumps({
"origin": origin,
"destination": destination,
"date": date
}))
return "Success"
def get_journey_data(event, context):
journey_id = event['queryStringParameters']['journey_id']
journey = database.get_journey(journey_id)
journey_prices = journey["price_by_numtix"]
journey["price_by_numtix"] = {num: [str(price) for price in price_list] for (
num, price_list) in journey_prices.items()}
response = {
"statusCode": 200,
"body": json.dumps(journey)
}
return response