Advertisement
Guest User

Untitled

a guest
Feb 8th, 2022
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. SAM yaml:
  2. AWSTemplateFormatVersion: '2010-09-09'
  3. Transform: 'AWS::Serverless-2016-10-31'
  4. Description: An AWS Serverless Specification template describing your function.
  5. Resources:
  6. teslaordercheck:
  7. Type: 'AWS::Serverless::Function'
  8. Properties:
  9. Handler: lambda_function.lambda_handler
  10. Runtime: python3.9
  11. CodeUri: .
  12. Description: ''
  13. MemorySize: 128
  14. Timeout: 15
  15. Role: >-
  16. arn:aws:iam::<redacted>:role/service-role/tesla-order-check-role
  17. Events:
  18. Schedule1:
  19. Type: Schedule
  20. Properties:
  21. Schedule: cron(0/15 * ? * * *)
  22. Layers:
  23. - >-
  24. arn:aws:lambda:ap-southeast-2:<redacted>:layer:AWSLambda-Python-AWS-SDK:4
  25.  
  26. python source:
  27. import json
  28. import boto3
  29. import re
  30. from botocore.vendored import requests
  31.  
  32. def stock_check():
  33. found = False
  34. notification = ""
  35. url_template = "https://www.tesla.com/inventory/api/v1/inventory-results?query=%7B%22query%22%3A%7B%22model%22%3A%22{}%22%2C%22condition%22%3A%22{}%22%2C%22options%22%3A%7B%22FleetSalesRegions%22%3A%5B%22Victoria%22%5D%7D%2C%22arrangeby%22%3A%22Price%22%2C%22order%22%3A%22asc%22%2C%22market%22%3A%22AU%22%2C%22language%22%3A%22en%22%2C%22super_region%22%3A%22north%20america%22%7D%7D"
  36. for model in ["m3", "ms", "mx"]:
  37. for condition in ["new", "used"]:
  38. response = requests.get( url_template.format(model, condition))
  39. json_response = response.json()
  40. cars_count = json_response['total_matches_found']
  41. if (cars_count != 0):
  42. found = True
  43. notification += "{} {} {} cars: {}\n".format(cars_count, model, condition, url_template.format(model, condition))
  44. if found:
  45. client = boto3.client('sns')
  46. response = client.publish (
  47. TargetArn = "arn:aws:sns:ap-southeast-2:<REDACTED>:tesla-update",
  48. Message = json.dumps({'default': notification}),
  49. MessageStructure = 'json'
  50. )
  51.  
  52. def account_check():
  53. changes_found = False
  54. data_found = False
  55. notification = ""
  56. reservation = "<PASTE YOUR RNxxxx HERE>"
  57. url_template = "https://www.tesla.com/en_AU/teslaaccount/product-finalize?rn={}"
  58. headers = {'Referer': 'https://www.tesla.com/en_au/teslaaccount',
  59. 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:95.0) Gecko/20100101 Firefox/95.0',
  60. 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
  61. 'Accept-Language': 'en-US,en;q=0.5',
  62. 'Accept-Encoding': 'gzip, deflate, br',
  63. 'DNT': '1',
  64. 'Connection': 'keep-alive',
  65. 'Upgrade-Insecure-Requests': '1', 'Sec-Fetch-Dest': 'document', 'Sec-Fetch-Mode': 'navigate', 'Sec-Fetch-Site': 'same-origin', 'Sec-Fetch-User': '?1','Sec-GPC': '1','Cache-Control': 'max-age=0',
  66. 'Cookie': '<PASTE COOKIES FROM YOUR BROWSER HERE>'
  67. }
  68. response = requests.get(url_template.format(reservation), headers = headers)
  69.  
  70. text_response = response.text
  71. for line in text_response.split("\n"):
  72. if "Tesla.ProductF" in line:
  73. re.search("Tesla.ProductF = ({.*})\)\(\)\s*$",line.strip()).group(1)
  74. json_response = json.loads(re.search("Tesla.ProductF = ({.*)}\)\(\)\s*$",line.strip()).group(1))
  75. del json_response['Data']['Insurance']['form']
  76. data_found = True
  77. dynamo = boto3.resource('dynamodb').Table('tesla_checks')
  78. last_values = dynamo.get_item( Key = {'id': "1"})
  79. if ("Item" in last_values):
  80. if (json.loads(last_values['Item']['jsonvalue']) != json_response):
  81. changes_found = True
  82. notification += 'Changes found.'
  83. print ("Changes found")
  84. dynamo.update_item(Key = {'id': "1"},
  85. UpdateExpression="set jsonvalue=:r",
  86. ExpressionAttributeValues = {':r': json.dumps(json_response)},
  87. ReturnValues="UPDATED_NEW"
  88. )
  89. else:
  90. print ("New record")
  91. dynamo.put_item(Item = {'id': "1", 'jsonvalue': json.dumps(json_response)})
  92.  
  93.  
  94. if data_found == False:
  95. client = boto3.client('sns')
  96. response = client.publish (
  97. TargetArn = "arn:aws:sns:ap-southeast-2:<redacted>:tesla-update",
  98. Message = json.dumps({'default': 'Tesla check - no data found'}),
  99. MessageStructure = 'json'
  100. )
  101.  
  102. if changes_found:
  103. client = boto3.client('sns')
  104. response = client.publish (
  105. TargetArn = "arn:aws:sns:ap-southeast-2:<redacted>:tesla-update",
  106. Message = json.dumps({'default': notification}),
  107. MessageStructure = 'json'
  108. )
  109.  
  110. def lambda_handler(event, context):
  111. stock_check()
  112. account_check()
  113. return {
  114. 'statusCode': 200,
  115. 'body': "{}"
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement