You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

67 lines
1.8 KiB

import urllib2
import base64
import json
from django.core.management.base import BaseCommand
from django.conf import settings
from city.models import Hotel
URL = 'https://distribution-xml.booking.com/json/bookings.getRooms?'
username = settings.BOOKING_USERNAME
password = settings.BOOKING_PASSWORD
def get_prices(rooms):
min_price = None
max_price = None
for room in rooms:
try:
cur_min = float(room.get('min_price'))
except TypeError:
cur_min = None
try:
cur_max = float(room.get('max_price'))
except TypeError:
cur_max = None
if cur_min > min_price:
min_price = cur_min
if cur_max > max_price:
max_price = cur_max
return min_price, max_price
def handle_hotels(hotel_id):
hotel_ids_str = hotel_id
new_url = URL + 'hotel_ids=%s'%hotel_ids_str
request = urllib2.Request(new_url)
base64string = base64.encodestring('%s:%s' % (username, password)).replace('\n', '')
request.add_header("Authorization", "Basic %s" % base64string)
try:
response = urllib2.urlopen(request)
code = response.getcode()
except urllib2.HTTPError, e:
code = e.code
except urllib2.URLError, e:
code = e.code
json_rooms = response.read()
rooms = json.loads(json_rooms)
min_price, max_price = get_prices(rooms)
Hotel.objects.filter(id=hotel_id).update(min_price=min_price, max_price=max_price)
print('success')
def main():
for hotel in Hotel.objects.all():
try:
handle_hotels(str(hotel.id))
except:
continue
class Command(BaseCommand):
def handle(self, *args, **options):
# update hotels prices
# runs one time in a week
main()