import pickle, json, urllib2 import urllib2, base64 from django.core.management.base import BaseCommand, CommandError from django.db.models import Q from django.conf import settings from city.models import Hotel, City 'https://distribution-xml.booking.com/json/bookings.getHotelPhotos?' 'https://distribution-xml.booking.com/json/bookings.getHotels?' HOTEL_URL = 'https://distribution-xml.booking.com/json/bookings.getHotels?' HOTEL_PHOTO_URL = 'https://distribution-xml.booking.com/json/bookings.getHotelPhotos?' username = 'expomap' password = '33xp00m33p' langs = [code for code, name in settings.LANGUAGES] def create_hotels(hotels, city): dj_hotels = [] dj_hotels_translation = [] tr_model = Hotel._meta.translations_model for hotel in hotels: h = Hotel(id=hotel['hotel_id'], url=hotel['url'], city=city, ranking=hotel['ranking'], hotel_class=hotel['class'], latitude=hotel['location']['latitude'], longitude=hotel['location']['longitude']) dj_hotels.append(h) for lang in langs: tr = tr_model(name=hotel['name'], address=hotel['address'], master_id=hotel['hotel_id'], language_code=lang) dj_hotels_translation.append(tr) Hotel.objects.bulk_create(dj_hotels) #print dj_hotels tr_model.objects.bulk_create(dj_hotels_translation) print('city: %s'%str(city)) def main(): cities = City.objects.select_related('place_expositions', 'place_conferences').\ filter(Q(place_expositions__city__isnull=False) | Q(place_conferences__city__isnull=False)).distinct() for city in cities: url = HOTEL_URL + 'city_ids=%s'%city.id offset = 0 rows = 500 flag = True while(flag): new_url = url + '&offset=%s&rows=%s'%(offset, rows) 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_hotels = response.read() hotels = json.loads(json_hotels) create_hotels(hotels, city) offset +=rows if not hotels: flag = False class Command(BaseCommand): def handle(self, *args, **options): main() """ request = urllib2.Request(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_hotels = response.read() hotels = json.loads(json_hotels) print(hotels) """