Added Booking.com endpoints automation

This commit is contained in:
Fabio Manganiello 2018-01-11 19:31:44 +01:00
parent 959296b15a
commit b1f42c22ae
9 changed files with 74 additions and 5 deletions

View File

@ -6,6 +6,7 @@ from threading import Thread
from platypush.bus import Bus
from platypush.backend import Backend
from platypush.backend.http.request import HttpRequest
from platypush.message.request import Request
class HttpPollBackend(Backend):

View File

@ -39,6 +39,9 @@ class HttpRequest(object):
else:
raise RuntimeError('{} is neither a dictionary nor an HttpRequest')
if 'timeout' not in self.args.kwargs:
self.args.kwargs['timeout'] = self.timeout
self.request_args = {
'method': self.args.method, 'url': self.args.url, **self.args.kwargs
}
@ -53,8 +56,13 @@ class HttpRequest(object):
response = method(self.args.url, *self.args.args, **self.args.kwargs)
new_items = self.get_new_items(response)
if new_items and not is_first_call and self.bus:
if isinstance(new_items, HttpEvent):
event = new_items
new_items = event.args['response']
else:
event = HttpEvent(dict(self), new_items)
if new_items and not is_first_call and self.bus:
self.bus.post(event)
response.raise_for_status()

View File

@ -0,0 +1,47 @@
import datetime
import dateutil.parser
import json
from platypush.backend.http.request import JsonHttpRequest
from platypush.message.event.http.ota.booking import NewReservationEvent
class GetReservationUpdates(JsonHttpRequest):
def __init__(self, hotel_id, token, *args, **kwargs):
self.hotel_id = hotel_id
self.token = token
self.seen_entries = set()
self.last_update = None
args = {
'method': 'get',
'url': 'https://hub-api.booking.com/v1/hotels/{}/reservations'.format(self.hotel_id),
'headers': { 'X-Booking-Auth-Token': self.token },
'params': { 'updatedSince': datetime.date.today().isoformat() }
}
super().__init__(args=args, **kwargs)
def get_new_items(self, response):
response = response.json()
entries = []
for entry in response:
update_timestamp = dateutil.parser.parse(entry['updateDate'])
last_update_timestamp = dateutil.parser.parse(self.last_update['updateDate']) \
if self.last_update else None
if self.last_update is None \
or (update_timestamp > last_update_timestamp
and not (
entry['booker']['email'] == self.last_update['booker']['email']
and entry['status'] == self.last_update['status'])):
self.last_update = entry
entries.append(entry)
return NewReservationEvent(dict(self), entries)
# vim:sw=4:ts=4:et:

View File

@ -0,0 +1,9 @@
from platypush.message.event.http import HttpEvent
class NewReservationEvent(HttpEvent):
def __init__(self, request, response, *args, **kwargs):
super().__init__(request=request, response=response, *args, **kwargs)
# vim:sw=4:ts=4:et:

View File

@ -131,7 +131,6 @@ class LoopProcedure(Procedure):
for item in iterable:
context[self.iterator_name] = item
# print('**** context[{}]: {}, iterable type: {}'.format(self.iterator_name, item, type(iterable)))
response = super().execute(n_tries, **context)
return response

View File

@ -31,9 +31,13 @@ google-assistant-sdk[samples]
google-assistant-library
# Flic buttons support
git+https://github.com/50ButtonsEach/fliclib-linux-hci
# It doesn't come with a pip package nor a setup.py,
# follow the instructions on github to get the flicd daemon
# running, set up the buttons you want to use, and then
# enable backend.button.flic in your configuration.
# git+https://github.com/50ButtonsEach/fliclib-linux-hci
# text2speech plugin: mplayer package
# text2speech plugin: mplayer system package
# Video support on RaspberryPi: omxplayer package
# Video support on RaspberryPi: omxplayer system package

View File

@ -73,6 +73,7 @@ setup(
'Support for OMXPlayer plugin': ['omxplayer'],
'Support for YouTube in the OMXPlayer plugin': ['youtube-dl'],
'Support for Google Assistant': ['google-assistant-library'],
'Support for most of the HTTP poll backends': ['python-dateutil'],
# 'Support for Flic buttons': ['git+ssh://git@github.com/50ButtonsEach/fliclib-linux-hci']
},
)