def update_opdrachten(self): opdrachten = jotihuntApi.get_opdrachten().data if opdrachten and opdrachten[0] != self.lastOpdracht: opdracht = opdrachten[0].data message = 'Er is nieuws met de titel [{title}]({url})'.format( title=opdracht.titel, url=settings.Settings().base_opdracht_url + opdracht.ID) for chat_id in self._opdrachten: self.bot.sendMessage(chat_id, message, parse_mode=ParseMode.MARKDOWN) self.lastOpdracht = opdrachten[0]
def update_nieuws(self): nieuws = jotihuntApi.get_nieuws_lijst().data if nieuws and nieuws[0] != self.lastNieuws: item = nieuws[0].data message = 'Er is nieuws met de titel [{title}]({url})'.format( title=item.titel, url=settings.Settings().base_nieuws_url + item.ID) for chat_id in self._nieuws: self.bot.sendMessage(chat_id, message, parse_mode=ParseMode.MARKDOWN) self.lastNieuws = nieuws[0]
def update_hint(self): hints = jotihuntApi.get_hints().data if hints and hints[0] != self.lastHint: hint = hints[0].data message = 'Er is een hint met de titel [{title}]({url})' message = message.format(title=hint.titel, url=settings.Settings().base_hint_url + hint.ID) for chat_id in self._hints: self.bot.sendMessage(chat_id, message, parse_mode=ParseMode.MARKDOWN) self.lastHint = hints[0]
def update_vos_status(self): response = jotihuntApi.get_vossen() curr_status = response.data def send_update(chat_id, vos, new_status): if new_status is None: return m = self.bot.sendSticker(chat_id, status_plaatjes[vos][new_status][ 'file_id']) self.botan.track(m, 'vos_status_' + vos + '_' + new_status) send_cloudmessage(vos, new_status) def extract_status(vos): return curr_status[vos[0].lower()].status def send_a(): for chat_id in self._A: vos = 'a' send_update(chat_id, vos, extract_status(vos)) def send_b(): for chat_id in self._B: vos = 'b' send_update(chat_id, vos, extract_status(vos)) def send_c(): for chat_id in self._C: vos = 'c' send_update(chat_id, vos, extract_status(vos)) def send_d(): for chat_id in self._D: vos = 'd' send_update(chat_id, vos, extract_status(vos)) def send_e(): for chat_id in self._E: vos = 'e' send_update(chat_id, vos, extract_status(vos)) def send_f(): for chat_id in self._F: vos = 'f' send_update(chat_id, vos, extract_status(vos)) def send_x(): for chat_id in self._X: vos = 'x' send_update(chat_id, vos, extract_status(vos)) if self.lastStatus is None: send_a() send_b() send_c() send_d() send_e() send_f() send_x() self.lastStatus = curr_status else: for k, item in enumerate(curr_status): if item.team == 'Alpha' and item['status'] != \ extract_status('a')['status']: send_a() if item.team == 'Bravo' and item['status'] != \ extract_status('b')['status']: send_b() if item.team == 'Charlie' and item['status'] != \ extract_status('c')['status']: send_c() if item.team == 'Delta' and item['status'] != \ extract_status('d')['status']: send_d() if item.team == 'Echo' and item['status'] != \ extract_status('e')['status']: send_e() if item.team == 'Foxtrot' and item['status'] != \ extract_status('f')['status']: send_f() self.lastStatus = curr_status
def retriever(): return jotihuntApi.get_vossen().data[dg]
def __init__(self): self._reminders_lock = threading.Lock() self.mail = imaplib.IMAP4_SSL('imap.gmail.com') self.seenHunts = dict() self.bot = None self.botan = Botan(settings.Settings().botan_key) self.rp_api = RPApi.get_instance(settings.Settings().rp_username, settings.Settings().rp_pass) self.messages = [] # list of tuples (int, str, str, tuple, dict) def get_kwargs_vos(new_item): if new_item['icon'] == '3': new_item['soort'] = 'Spot' elif new_item['icon'] == '4': new_item['soort'] = 'Hunt' else: new_item['soort'] = 'Hint' new_item['team_upper'] = new_item['team'].upper() new_item['tijd'] = new_item['datetime'][11:] return new_item.data def get_kwargs_vos_status(new_item): new_item['dg'] = new_item['team'][0].lower() return new_item._data def send_location(vos, chat_ids): bot = get_updates().bot if bot is not None: for chat_id in chat_ids: bot.sendLocation(chat_id, latitude=vos['latitude'], longitude=vos['longitude']) def send_status_sticker(status, chat_ids): bot = get_updates().bot vos = status['dg'] new_status = status['status'] if bot is not None: for chat_id in chat_ids: bot.sendSticker(chat_id, status_plaatjes[vos][new_status]['file_id'] ) send_cloudmessage(vos, new_status) message_vos = 'er is een {soort} ingevoerd voor {team}.\n' \ ' extra info: {extra}\n ' \ 'opmerking/adres: {opmerking}\n' \ 'tijd: {tijd}\n' \ 'link: http://jotihunt2016.area348.nl/map.php?gebied=' \ '{team_upper}' message_vos_status = 'Er is een nieuwe status voor {team}' self.botan_id_vos = 'newLoc_{team}_{soort}' self.botan_id_vos_status = 'newStatus_{dg}_{status}' def get_retriever(dg): def retriever(): return self.rp_api.vos(dg) return retriever def get_retriever_status(dg): def retriever(): return jotihuntApi.get_vossen().data[dg] return retriever for dg in ['a', 'b', 'c', 'd', 'e', 'f', 'x']: retr1 = get_retriever(dg) updater_rp = SingleUpdater(retr1, get_kwargs_vos, message_vos, botan_id=self.botan_id_vos+'_'+dg, callback=send_location) retr = get_retriever_status(dg) updater_status = SingleUpdater(retr, get_kwargs_vos_status, message_vos_status, botan_id=self.botan_id_vos_status + '_'+dg, callback=send_status_sticker) if dg == 'a': self._A = SingleUpdateContainer([updater_rp, updater_status]) elif dg == 'b': self._B = SingleUpdateContainer([updater_rp, updater_status]) elif dg == 'c': self._C = SingleUpdateContainer([updater_rp, updater_status]) elif dg == 'd': self._D = SingleUpdateContainer([updater_rp, updater_status]) elif dg == 'e': self._E = SingleUpdateContainer([updater_rp, updater_status]) elif dg == 'f': self._F = SingleUpdateContainer([updater_rp, updater_status]) elif dg == 'x': self._X = SingleUpdateContainer([updater_rp, updater_status]) self._photos = set() # naam niet veranderen get_last_opdracht = lambda: jotihuntApi.get_opdrachten().data[0] get_last_nieuws = lambda: jotihuntApi.get_nieuws_lijst().data[0] get_last_hint = lambda: jotihuntApi.get_hints().data[0] self.botan_id_jotihunt = 'new_{botan_soort}' def get_jotihunt_kwargs(new_response): r = dict() item = new_response.data r['title'] = item.titel if isinstance(item, jotihuntApiBase.Opdracht): r['url'] = settings.Settings().base_opdracht_url + item.ID r['soort'] = 'een nieuwe opdracht' elif isinstance(item, jotihuntApiBase.Hint): r['url'] = settings.Settings().base_hint_url + item.ID r['soort'] = 'een nieuwe hint' elif isinstance(item, jotihuntApiBase.Nieuws): r['url'] = settings.Settings().base_nieuws_url + item.ID r['soort'] = 'nieuws' r['botan_soort'] = r['soort'].replace(' ', '_') return r jotihunt_message = 'Er is {soort} met de titel [{title}]({url})' def add_opdracht_reminder(opdracht, chat_ids): opdracht_id = opdracht.data.ID if opdracht_id not in self.reminders: self._reminders_lock.acquire() try: self.reminders[opdracht_id] = Reminder(opdracht, chat_ids) finally: self._reminders_lock.release() opdracht_updater = SingleUpdater(get_last_opdracht, get_jotihunt_kwargs, jotihunt_message, botan_id=self.botan_id_jotihunt, callback=add_opdracht_reminder) nieuws_updater = SingleUpdater(get_last_nieuws, get_jotihunt_kwargs, jotihunt_message, botan_id=self.botan_id_jotihunt) hint_updater = SingleUpdater(get_last_hint, get_jotihunt_kwargs, jotihunt_message, botan_id=self.botan_id_jotihunt) self.reminders = dict() self._opdrachten = SingleUpdateContainer([opdracht_updater]) # naam niet # veranderen self._nieuws = SingleUpdateContainer([nieuws_updater]) # naam niet veranderen self._hints = SingleUpdateContainer([hint_updater]) # naam niet veranderen self._error = set() # naam niet veranderen self._punten = {'opdrachten': 0, 'hints': 0, 'hunts': 0, 'fotos': 0, 'totaal': 0} self._last_update = 0 self.lastPhoto = None self.seenMail = set() self.lastHint = None