Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- import mechanize
- import json
- import math
- import urllib
- br = mechanize.Browser()
- br.set_handle_robots(False) # ignore robots
- br.set_handle_refresh(False) # can sometimes hang without this
- br.addheaders = [('User-agent', 'Firefox')]
- def dataLayerGet( responseText ):
- inData=False
- result = {}
- for line in responseText.split('\n'):
- if -1 != line.find( 'var dataLayer = {' ):
- inData=True
- continue
- if inData and -1 != line.find('};'):
- inData=False
- continue
- if inData:
- if len( line.split('"') ) == 5:
- nothing,key,nothing2,value,nothing3 = line.split('"')
- result[key] = value
- return result
- def codesGet( responseText ):
- results = []
- for line in responseText.split('\n'):
- if -1 != line.find('<a title') and -1 != line.find('data-marsha'):
- results.append( [ line.split('"')[1], line.split('"')[7] ] )
- return results
- def roomRatesGet( code, fromDate, toDate ):
- fromDateFormatted = fromDate[1] + '/' + fromDate[2] + '/' + fromDate[0][2:]
- toDateFormatted = toDate[1] + '/' + toDate[2] + '/' + toDate[0][2:]
- 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')
- responseText = response.read()
- results = []
- prices = []
- descs = []
- for price in responseText.split('data-totalpricebeforetax="')[1:]:
- prices.append( price[:price.find('"')] )
- for desc in responseText.split('description t-description">')[1:]:
- descs.append( desc[:desc.find('</h3>')].rstrip().lstrip() )
- for index in range( len( prices ) ):
- results.append( [ descs[index], prices[index] ] )
- return results
- def searchResults( city, fromDate, toDate ):
- fromDateFormatted = fromDate[1] + '%2F' + fromDate[2] + '%2F' + fromDate[0]
- toDateFormatted = toDate[1] + '%2F' + toDate[2] + '%2F' + toDate[0]
- 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='
- responseText = br.open(url).read()
- dataLayer = dataLayerGet( responseText )
- codes = codesGet( responseText )
- pageCount = int( math.ceil( float(dataLayer['numberOfRecords']) / float( dataLayer['recordsPerPage'] ) ) )
- for pageNo in range( 2, pageCount + 1 ):
- responseText = br.open('https://www.marriott.com/search/refineSearch.mi?page=%d' % pageNo).read()
- codes += codesGet( responseText )
- result = []
- for code in codes:
- result.append( [ code, roomRatesGet( code[1], fromDate, toDate ) ] )
- return result
- print json.dumps( searchResults( 'calgary', ['2015', '12', '23' ], ['2015', '12', '24' ] ), indent=8 )
- #https://www.marriott.com/reservation/availabilitySearch.mi
- #propertyCode=YEGEC
- #isSearch=true
- #isRateCalendar=false
- #flexibleDateSearchRateDisplay=false
- #flexibleDateLowestRateMonth=
- #flexibleDateLowestRateDate=
- #fromDate=12/23/15
- #toDate=12/24/15
- #clusterCode=
- #corporateCode=
- #groupCode=
- #numberOfRooms=1
- #numberOfGuests=1
- #incentiveType_Number=
- #incentiveType=false
- #marriottRewardsNumber=
- #useRewardsPoints=false
- #https://www.marriott.com/search/submitSearch.mi
- #airportCode=
- #autoSuggestItemType=Unmatched
- #clickToSearch=false
- #clusterCode=none
- #corporateCode=
- #destinationAddress.city=
- #destinationAddress.cityPopulation=
- #destinationAddress.cityPopulationDensity=
- #destinationAddress.country=
- #destinationAddress.destination=calgary
- #destinationAddress.latitude=
- #destinationAddress.longitude=
- #destinationAddress.stateProvince=
- #for-hotels-nearme=Near
- #fromDate=12/23/2015
- #fromToDate=Wed, Dec 23, 2015
- #fromToDate_submit=12/23/2015
- #groupCode=
- #guestCount=1
- #guestCountBox=1
- #lengthOfStay=1
- #poiCity=
- #poiName=
- #recordsPerPage=20
- #roomCount=1
- #roomCountBox=1
- #searchRadius=
- #searchType=InCity
- #singleSearch=true
- #singleSearchAutoSuggest=false
- #toDate=12/24/2015
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement