def notify_answer(room_id, clue_id): global _timer _timer.cancel() url = "https://api.hipchat.com/v1/rooms/message?auth_token={1}".format( room_id, os.environ.get(_hipchat_auth_token)) key = Trebek.clue_key.format(room_id) uri = urlparse(os.environ.get(_redis_url)) r = redis.StrictRedis(host = uri.hostname, port = uri.port, password = uri.password) if r.exists(key): o = r.get(key) obj = entities.Question(**json.loads(o.decode())) if obj.id == clue_id: r.delete(key) parameters = {} parameters['message'] = "The answer was: {0}".format(obj.answer) parameters['room_id'] = room_id parameters['color'] = 'gray' parameters['from'] = 'Trebek' resp = requests.post(url, data = parameters, timeout=5) if resp.status_code != 200: print('failed to post message to hipchat') else: print('no redis key exists, do not notify')
def fetch_invalid_clue(): global _fetch_count, _invalid_clue clue = get_clue_json() if _fetch_count == 0: clue = _invalid_clue _fetch_count += 1 return entities.Question(**clue)
def get_active_clue(self): key = self.clue_key.format(self.room_id) obj = None if self.redis.exists(key): o = self.redis.get(key) obj = entities.Question(**json.loads(o.decode())) return obj
def test_when_answer_contains_HTML_word_is_filtered(self): # e.g.: ANSWER: the <i>Stegosaurus</i> c = { 'id': 1, 'title': 'foo', 'created_at': 'bar', 'updated_at': 'foobar', 'clues_count': 1 } q = entities.Question(1, answer="the <i>Stegosaurus</i>", category=c) self.assertEqual("the Stegosaurus", q.answer) # e.g.: ANSWER: <i>the Seagull</i> q = entities.Question(1, answer="<i>the Seagull</i>", category=c) self.assertEqual("the Seagull", q.answer) q = entities.Question(1, answer="Theodore Roosevelt", category=c) self.assertEqual("Theodore Roosevelt", q.answer)
def fetch_random_clue(self): url = "http://jservice.io/api/random?count=1" req = requests.get(url) # print(req.json()) print("ANSWER: {0}".format(req.json()[0]['answer'])) return entities.Question(**req.json()[0])
def fake_fetch_random_clue(): return entities.Question(**get_clue_json())