Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2015
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.78 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import mechanize
  4. import json
  5. import math
  6. import urllib
  7.  
  8. br = mechanize.Browser()
  9. br.set_handle_robots(False)   # ignore robots
  10. br.set_handle_refresh(False)  # can sometimes hang without this
  11. br.addheaders =             [('User-agent', 'Firefox')]
  12.  
  13. def dataLayerGet( responseText ):
  14.     inData=False
  15.     result = {}
  16.     for line in responseText.split('\n'):
  17.         if -1 != line.find( 'var dataLayer = {' ):
  18.             inData=True
  19.             continue
  20.         if inData and -1 != line.find('};'):
  21.             inData=False
  22.             continue
  23.         if inData:
  24.             if len( line.split('"') ) == 5:
  25.                 nothing,key,nothing2,value,nothing3 = line.split('"')
  26.                 result[key] = value
  27.     return result
  28.  
  29. def codesGet( responseText ):
  30.     results = []
  31.     for line in responseText.split('\n'):
  32.         if -1 != line.find('<a title') and -1 != line.find('data-marsha'):
  33.             results.append( [ line.split('"')[1], line.split('"')[7] ] )
  34.     return results
  35.  
  36. def roomRatesGet( code, fromDate, toDate ):
  37.     fromDateFormatted = fromDate[1] + '/' + fromDate[2] + '/' + fromDate[0][2:]
  38.     toDateFormatted = toDate[1] + '/' + toDate[2] + '/' + toDate[0][2:]
  39.  
  40.     response = br.open( 'https://www.marriott.com/reservation/availabilitySearch.mi?propertyCode=' + code + '&isSearch=true&isRateCalendar=false&flexibleDateSearchRateDisplay=false&flexibleDateLowestRateMonth=&flexibleDateLowestRateDate=&fromToDate_submit=' + fromDateFormatted + 'fromDate=' + fromDateFormatted + '&toDate=' + toDateFormatted + '&clusterCode=&corporateCode=&groupCode=&numberOfRooms=1&numberOfGuests=1&incentiveType_Number=&incentiveType=false&marriottRewardsNumber=&useRewardsPoints=false')
  41.     responseText = response.read()
  42.     results = []
  43.     prices = []
  44.     descs = []
  45.     for price in responseText.split('data-totalpricebeforetax="')[1:]:
  46.         prices.append( price[:price.find('"')] )
  47.     for desc in responseText.split('description t-description">')[1:]:
  48.         descs.append( desc[:desc.find('</h3>')].rstrip().lstrip() )
  49.     for index in range( len( prices ) ):
  50.         results.append( [ descs[index], prices[index] ] )
  51.     return results
  52.  
  53. def searchResults( city, fromDate, toDate ):
  54.     fromDateFormatted = fromDate[1] + '%2F' + fromDate[2] + '%2F' + fromDate[0]
  55.     toDateFormatted = toDate[1] + '%2F' + toDate[2] + '%2F' + toDate[0]
  56.     url='https://www.marriott.com/search/submitSearch.mi?searchType=InCity&groupCode=&searchRadius=&poiName=&poiCity=&recordsPerPage=20&for-hotels-nearme=Near&destinationAddress.destination=' + urllib.quote( city ) + '&singleSearch=true&singleSearchAutoSuggest=false&autoSuggestItemType=Unmatched&clickToSearch=false&destinationAddress.latitude=&destinationAddress.longitude=&destinationAddress.cityPopulation=&destinationAddress.cityPopulationDensity=&destinationAddress.city=&destinationAddress.stateProvince=&destinationAddress.country=&airportCode=&fromToDate=&fromToDate_submit=' + fromDateFormatted + '&fromDate=' + fromDateFormatted + '&toDate=' + toDateFormatted + '&lengthOfStay=1&roomCountBox=1&roomCount=1&guestCountBox=1&guestCount=1&clusterCode=none&corporateCode='
  57.     responseText = br.open(url).read()
  58.     dataLayer = dataLayerGet( responseText )
  59.     codes = codesGet( responseText )
  60.  
  61.     pageCount = int( math.ceil( float(dataLayer['numberOfRecords']) / float( dataLayer['recordsPerPage'] ) ) )
  62.  
  63.     for pageNo in range( 2, pageCount + 1 ):
  64.         responseText = br.open('https://www.marriott.com/search/refineSearch.mi?page=%d' % pageNo).read()
  65.         codes += codesGet( responseText )
  66.  
  67.     result = []
  68.     for code in codes:
  69.         result.append( [ code, roomRatesGet( code[1], fromDate, toDate ) ] )
  70.  
  71.     return result
  72.  
  73. print json.dumps( searchResults( 'calgary', ['2015', '12', '23' ], ['2015', '12', '24' ] ), indent=8 )
  74.  
  75. #https://www.marriott.com/reservation/availabilitySearch.mi
  76. #propertyCode=YEGEC
  77. #isSearch=true
  78. #isRateCalendar=false
  79. #flexibleDateSearchRateDisplay=false
  80. #flexibleDateLowestRateMonth=
  81. #flexibleDateLowestRateDate=
  82. #fromDate=12/23/15
  83. #toDate=12/24/15
  84. #clusterCode=
  85. #corporateCode=
  86. #groupCode=
  87. #numberOfRooms=1
  88. #numberOfGuests=1
  89. #incentiveType_Number=
  90. #incentiveType=false
  91. #marriottRewardsNumber=
  92. #useRewardsPoints=false
  93.  
  94. #https://www.marriott.com/search/submitSearch.mi
  95. #airportCode=
  96. #autoSuggestItemType=Unmatched
  97. #clickToSearch=false
  98. #clusterCode=none
  99. #corporateCode=
  100. #destinationAddress.city=
  101. #destinationAddress.cityPopulation=
  102. #destinationAddress.cityPopulationDensity=
  103. #destinationAddress.country=
  104. #destinationAddress.destination=calgary
  105. #destinationAddress.latitude=
  106. #destinationAddress.longitude=
  107. #destinationAddress.stateProvince=
  108. #for-hotels-nearme=Near
  109. #fromDate=12/23/2015
  110. #fromToDate=Wed, Dec 23, 2015
  111. #fromToDate_submit=12/23/2015
  112. #groupCode=
  113. #guestCount=1
  114. #guestCountBox=1
  115. #lengthOfStay=1
  116. #poiCity=
  117. #poiName=
  118. #recordsPerPage=20
  119. #roomCount=1
  120. #roomCountBox=1
  121. #searchRadius=
  122. #searchType=InCity
  123. #singleSearch=true
  124. #singleSearchAutoSuggest=false
  125. #toDate=12/24/2015
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement