Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.88 KB | None | 0 0
  1. from datetime import datetime
  2. import asyncio
  3. import requests
  4. from bs4 import BeautifulSoup
  5. import aiohttp
  6. from aiohttp import ClientSession
  7. import async_timeout
  8. from contextlib import closing
  9.  
  10.  
  11. async def search(session: ClientSession, start: str, end: str, limit: int, attempt: int = 0):
  12.     params = {
  13.         'product': 'web',
  14.         'start_time': str(start),
  15.         'end_time': str(end),
  16.         'uid': '100004464721185',
  17.         'user_lat': "37.7876582",
  18.         'user_lng': "-122.39051549999999",
  19.         'lat': "37.7876582",
  20.         'lng': "-122.39051549999999",
  21.         'distance': 8,
  22.         'max_total_price': 400,
  23.         'transmission': 'AUTOMATIC',
  24.         'category':'suv_or_jeep,minivan,hatchback_or_wagon,cargo_van',
  25.         'properties': 'car_id,car_name,car_photo_v2,carkit_enabled,distance,latitude,longitude,make,model,postcode,price_daily,price_hourly,price_weekly,total_price,timezone,year,dedicated_parking',
  26.         'sort': 'best',
  27.         'page_sort': 'magic',
  28.         'page_size': limit
  29.     }
  30.     headers = {
  31.         'Accept':'application/json, text/javascript, */*; q=0.01',
  32.         'Accept-Encoding':'gzip, deflate, br',
  33.         'Accept-Language':'en-US,en;q=0.9,ko-KR;q=0.8,ko;q=0.7',
  34.         'Origin':'https://www.getaround.com',
  35.         'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
  36.         'Referer':'https://www.getaround.com/search?start_time=2018-02-16T19:30:00.000Z&end_time=2018-02-20T05:00:00.000Z&max_total_price=398&category=suv_or_jeep,minivan,cargo_van&transmission=automatic&viewport='
  37.     }
  38.     # async with async_timeout.timeout(10):
  39.     async with session.get('https://index.getaround.com/v1.0/search', params=params, headers=headers) as response:
  40.         assert response.status == 200
  41.         json = await response.json()
  42.         cars = json['cars']
  43.         return cars
  44.  
  45. async def lookup_features(session, car_name):
  46.     # async with async_timeout.timeout(10):
  47.     async with session.get("https://www.getaround.com/{}".format(car_name)) as response:
  48.         assert response.status == 200
  49.         body = await response.text()
  50.         parser = BeautifulSoup(body, 'html.parser')
  51.         features = parser.select('#content #description table tr td')
  52.         return {feature.text.strip() for feature in features}
  53.  
  54. # async def search_cars(start: str, end: str, limit: int):
  55.    
  56. async def lookup_and_print(session, car, start, end, attempts = 0):
  57.     try:
  58.         features = await lookup_features(session, car['car_name'])
  59.         if 'All-wheel drive' in features or '4-wheel drive' in features:
  60.             print('"{}", "{}", "{}", "{}", "{}", "https://www.getaround.com/{}?start_time={}&end_time={}&use=CARSHARE"'.format(
  61.                 car['car_name'], car['total_price'], car['distance'], car['year'], car['make'],
  62.                 car['car_name'], start, end))
  63.     except aiohttp.client_exceptions.ClientConnectorError as e:
  64.         if attempts > 3:
  65.             raise Exception(e)
  66.         return await lookup_and_print(session, car,start,end, attempts + 1)
  67.  
  68.        
  69. async def main():
  70.     session = aiohttp.ClientSession(loop=loop)
  71.     begin = '2019-03-23T01:00:00.000Z'
  72.     end = '2019-03-25T04:00:00.000Z'
  73.     cars = await search(session, begin, end, 500)
  74.     print('"car_name", "total_price", "distance", "year", "make"')
  75.     await asyncio.gather(*[lookup_and_print(session, car, begin, end) for car in cars])
  76.     # session.close()
  77.  
  78.  
  79. loop = asyncio.get_event_loop()
  80. loop.run_until_complete(main())
  81. loop.close()
  82.  
  83.  
  84. # for car in search_cars(begin, end, 500):
  85. #     car_name = car['car_name']
  86. #     features = car['features']
  87. #     is_4wd = 'All-wheel drive' in features or '4-wheel drive' in features
  88. #     if is_4wd:
  89. #         print('{}, {}, {}, {}, {}'.format(car['car_name'], car['total_price'], car['distance'], car['year'], car['make']))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement