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.
51 lines
1.6 KiB
51 lines
1.6 KiB
import urllib2, base64
|
|
import json
|
|
from django.core.management.base import BaseCommand, CommandError
|
|
from city.models import Hotel
|
|
|
|
HOTEL_URL = 'https://distribution-xml.booking.com/json/bookings.getHotels?'
|
|
username = 'expomap'
|
|
password = '33xp00m33p'
|
|
|
|
|
|
def handle_hotels(hotel_ids):
|
|
good_hotels = []
|
|
hotel_ids_str = ','.join(hotel_ids)
|
|
new_url = HOTEL_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_hotels = response.read()
|
|
hotels = json.loads(json_hotels)
|
|
for item in hotels:
|
|
id = item['hotel_id']
|
|
currency = item['currencycode']
|
|
Hotel.objects.filter(id=id).update(currency=currency)
|
|
good_hotels.append(id)
|
|
bad_hotels = list(set(hotel_ids) - set(good_hotels))
|
|
|
|
Hotel.objects.filter(id__in=bad_hotels).delete()
|
|
print('success update currency in %d hotels'%len(good_hotels))
|
|
|
|
|
|
def main():
|
|
hotels_todo = Hotel.objects.filter(currency__isnull=True)[:100].count()
|
|
while hotels_todo > 0:
|
|
hotel_ids = [str(item.id) for item in list(Hotel.objects.filter(currency__isnull=True)[:100])]
|
|
handle_hotels(hotel_ids)
|
|
hotels_todo =Hotel.objects.filter(currency__isnull=True)[:100].count()
|
|
|
|
|
|
|
|
|
|
class Command(BaseCommand):
|
|
def handle(self, *args, **options):
|
|
main()
|
|
|