Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # coding: cp1251
- import requests
- class Aeroflot(object): # Поиск билетов на сайте Аэрофлота
- def __init__(self):
- self.search_url = 'https://www.aeroflot.ru/sb/booking/api/app/search/v2'
- self.code_origin = None
- self.code_destination = None
- self.date = None
- self.page = None
- self.city_to = None
- self.city_from = None
- def _input_info(self): # Ввод данных от пользователя
- self.city_from = raw_input('City origin: ')
- self.city_to = raw_input('City destination: ')
- self.date = raw_input('Date ( yyyy-mm-dd): ')
- def _check_input(self): # Поиск введенных городов в библиотеке, определение запроса
- with open('Library.txt', 'r') as libra:
- for line in libra.readlines():
- if self.city_from in line:
- self.code_origin = line[:3]
- elif self.city_to in line:
- self.code_destination = line[:3]
- if self.code_destination and self.code_origin:
- print 'Cities found'
- self._give_a_page()
- self._extract_data()
- break
- else:
- print 'Cities not found'
- def _give_a_page(self): # Устанавливает успешность запроса
- json_req = {"routes":
- [{"origin": self.code_origin, "destination": self.code_destination, "departure": self.date}],
- "cabin": "econom", "award": False, "country": "ru", "adults": 1, "children": 0, "infants": 0,
- "combined": False, "lang": "ru"}
- respond = requests.post(self.search_url, json=json_req)
- if respond.status_code == 200:
- self.page = respond.json()
- print 'Success request'
- else:
- print 'Invalid request'
- self.page = None
- def _extract_data(self): # Извлекает информацию и записывает в файл
- if self.page is not None:
- self.document.write('\n\nFlights from %s to %s %s\n'
- 'Code Airport\Time\Price\n\n' % (self.code_origin, self.code_destination, self.date))
- try:
- for num in self.page['data']['itineraries'][0]:
- if len(num['legs'][0]['segments']) != 1:
- self.document.write('Complex flight\n')
- continue
- else:
- rescode = num['legs'][0]['segments'][0]['operating_flight_number'] # Номер рейса
- reschr = num['legs'][0]['segments'][0]['airline_code'] # Код рейса
- resfrom = num['legs'][0]['segments'][0]['origin']['airport_name'] # Аэропорт вылета
- resto = num['legs'][0]['segments'][0]['destination']['airport_name'] # Аэропорт прилета
- restravel = num['time_name'] # Время в пути
- restfrom = num['legs'][0]['segments'][0]['departure_name'] # Время вылета
- restto = num['legs'][0]['segments'][0]['arrival_name'] # Время прилета
- price = num['prices'][0]['total_amount'] # Цена, самая низкая из доступных
- city_name_to = num['legs'][0]['segments'][0]['destination']['city_name'] # Город прилета
- city_name_from = num['legs'][0]['segments'][0]['origin']['city_name'] # Город вылета
- print price, reschr + rescode, resto
- self.document.write(('%-10s %s/%-20s %-25s %-15s %s/%-20s %-25s %s\n' %
- (reschr + rescode, city_name_from, resfrom, restfrom, restravel,
- resto, city_name_to, restto, price)).encode('cp1251'))
- except IndexError:
- print 'Flights not found'
- self.document.write('\n\nFlights not found!\n\n')
- def search(self): # Порядок действий
- with open('ResultAero.txt', 'w') as self.document:
- self._input_info()
- self._check_input()
- if __name__ == '__main__':
- first = Aeroflot()
- first.search()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement