def get_metadata(self, for_date): """ Note that querying for video metadata on a particular date may yield video clips dated for other days. Their video MMS URLs will match the specified date, but their actual dates can differ. """ for mms_url, clips in group_clips(self.get_clips(for_date)).items(): clips = list(clips) if clips[0].title.startswith('Due to Technical Difficulties'): continue # Langley sometimes has meetings that don't have a clip encompassing the entire meeting. # Create a dummy 'entire meeting' clip that does. if not any(map(lambda x: is_root_clip(x.title), clips)): fake_root = InsIncVideoClip( clips[0].category, 'Entire Meeting', clips[0].mms_url, clips[0].for_date, min(map(lambda x: x.start_time, clips)), max(map(lambda x: x.start_time, clips)), ) clips.insert(0, fake_root) for root, subclips in group_root_and_subclips(clips).items(): start_ts = pendulum.combine(root.for_date, pendulum.parse(root.start_time).time()).tz_(self.tz) timecodes = [TimeCode(c.start_time, c.title, c.end_time) for c in subclips] if not timecodes: timecodes.append(TimeCode(root.start_time, root.title, root.end_time)) yield VideoMetadata( video_id=os.path.basename(root.mms_url).replace('.wmv', ''), category=root.category, title=root.title, url=root.mms_url, start_ts=start_ts.isoformat(), end_ts=root.end_time, timecodes=timecodes, )
def _set(self, value): self.validate_value(value) # Force to appropriate Pendulum instance for consistency if value is not None: if self.input_type == self._type_interval: # Pendulum interval value = pendulum.interval.instance(value) else: if self.input_type == self._type_date: # Pendulum date if isinstance(value, date): value = pendulum.combine(value, pendulum.time(0)) elif self.input_type == self._type_time: # Pendulum time if isinstance(value, time): value = pendulum.combine(pendulum.date.today(), value) # Convert to Pendulum instance in UTC value = UTC.convert(pendulum.instance(value)) return super(DatetimeField, self)._set(value)
def _set(self, value): # Force to appropriate Pendulum instance for consistency if value is not None: if self.input_type == self._type_interval: # Pendulum interval value = pendulum.interval.instance(value) else: if self.input_type == self._type_date: # Pendulum date if isinstance(value, date): value = pendulum.combine(value, pendulum.time(0)) elif self.input_type == self._type_time: # Pendulum time if isinstance(value, time): value = pendulum.combine(pendulum.date.today(), value) # Convert to Pendulum instance in UTC value = UTC.convert(pendulum.instance(value)) # Drop nanosecond precision to match Mongo precision value = value.microsecond_( int(math.floor(value.microsecond / 1000) * 1000)) return super(DatetimeField, self)._set(value)
async def on_chat_message(self, msg): """Handle chat message.""" logging.debug(msg) content_type, chat_type, chat_id = telepot.glance(msg) logging.debug(content_type) if chat_type != 'private': if content_type not in CHAT_MEMBERS: await self.sender.sendMessage( _("Sorry, I work only in private chats.")) return if content_type != 'text': if content_type not in CHAT_MEMBERS: await self.sender.sendMessage( _("Sorry, I don't understand only text.")) return user_id = msg['from']['id'] text = msg['text'] if text[0] == '/': # hadling commands command = text.strip().lower() if command == '/newgame': is_authorized = await self.is_authorized(user_id) if is_authorized: if self._stage == GameInputStage.start: await self.move_to(GameInputStage.location, user_id=user_id) else: await self.sender.sendMessage( _('You are already in process of entering the results!' )) else: await self.sender.sendMessage( _('Sorry, bro, but you are not a member of the league chat.' )) elif command == '/cancel': if self._stage == GameInputStage.start: await self.sender.sendMessage( _("Nothing to cancel. You don't even started")) else: self._stage = GameInputStage.start await self.sender.sendMessage( _("Ok. Full Reset! Start enter new game with /newgame") ) elif command == '/back': if self._stage != GameInputStage.start: await self.move_to(GameInputStage(self._stage.value - 1)) else: await self.sender.sendMessage( _("Oh, we can't go back darling. We've just started... Do you mean /cancel?" )) else: # Basic messages if self._stage == GameInputStage.location: text = text.strip().replace("🔥", "") if text in self.locations: self._location = text self.redis.zadd("loc:{}".format(user_id), 1, text) await self.move_to(GameInputStage.date) else: await self.sender.sendMessage( _("""Sorry, I don't know about this place. If this place is new, please contact administrators to add this court to our list.""" )) elif self._stage == GameInputStage.date: text = text.strip() try: time = pendulum.from_format(text, "%d.%m.%y", MSK) except ValueError as ex: await self.sender.sendMessage( _("""Sorry, I cannot recognize the date. You can input custom date in 12.12.12 format""" )) else: if time.is_future(): await self.sender.sendMessage( _("""Looks like your game is in the future! No time travelers allowed!""" )) else: self._time = time await self.move_to(GameInputStage.time) elif self._stage == GameInputStage.time: text = text.strip() try: time = pendulum.from_format(text, "%H:%M", MSK) except ValueError as ex: await self.sender.sendMessage( _("""Sorry, I cannot recognize time. Please post something like 15:45 or 24.10.2016 13:20.""" )) else: time = pendulum.combine(self._time, time.time()).timezone_(MSK) if time.is_future(): await self.sender.sendMessage( _("""Looks like your game is in the future! No time travelers allowed!""" )) else: self._time = time await self.move_to(GameInputStage.first_player, user_id=user_id) elif self._stage == GameInputStage.first_player: text = text.strip().replace("🔥", "") if text not in self.players: ps = process.extract(text, self.players.keys(), limit=10) await self.sender.sendMessage( _("""I don't know that man!!! I suggested some names for you below""" ), reply_markup=ReplyKeyboardMarkup( keyboard=[[p] for p, _ in ps if p != self._player1], one_time_keyboard=True)) else: self._player1 = text self.redis.zadd("ps:{}".format(user_id), 1, text) await self.move_to(GameInputStage.second_player, user_id=user_id) elif self._stage == GameInputStage.second_player: text = text.strip().replace("🔥", "") if (text not in self.players) or (text == self._player1): ps = process.extract(text, self.players.keys(), limit=10) await self.sender.sendMessage( _("""I don't know that man!!! I suggested some names for you below""" ), reply_markup=ReplyKeyboardMarkup( keyboard=[[p] for p, _ in ps if p != self._player1], one_time_keyboard=True)) else: self._player2 = text self.redis.zadd("ps:{}".format(user_id), 1, text) await self.move_to(GameInputStage.result) elif self._stage == GameInputStage.result: text = text.strip() if (text not in GAME_RESULTS): await self.sender.sendMessage( _("""Strange result! Try something look like 3:1.""")) else: self._result = text await self.move_to(GameInputStage.confirmation) elif self._stage == GameInputStage.confirmation: text = text.strip().lower() if text != 'ok': await self.sender.sendMessage( _("""Please confirm the result!""")) else: await self.sender.sendMessage( _("""Well done! We'll notify everyone about the game!\nEnter new game with /newgame.""" ), reply_markup=ReplyKeyboardRemove()) from_data = msg['from'] name =\ "@{}".format(from_data['username']) if 'username' in from_data else from_data['first_name'] r1, r2 = [int(x) for x in self._result.split(':')] data = dict(lg=self.league, p1=self.players[self._player1], p2=self.players[self._player2], r1=r1, r2=r2, loc=self.locations[self._location], time=self._time.to_atom_string()) logging.debug(data) result = self.api.publish_result(**data) logging.debug(result) await self.bot.sendMessage( self._admin_chat, _("""{p1} - {p2}\n{result} {loc} {time}\n#result by {author}""" ).format(author=name.replace('_', '\_'), loc=self._location, time=self._time.format('%d/%m %H:%M'), p1=markdown_link( title=self._player1, url=self.api.link_for_player( self.league, self.players[self._player1])), p2=markdown_link( title=self._player2, url=self.api.link_for_player( self.league, self.players[self._player2])), result=self._result), parse_mode='Markdown') self._stage = GameInputStage.start
def test_combine(self): self.assertEqual( datetime.combine(date(2016, 1, 1), time(1, 2, 3, 123456)), pendulum.combine(date(2016, 1, 1), time(1, 2, 3, 123456)))
def combine_dates_times(time_entry): start_dt = pendulum.combine(time_entry['start_date'], time_entry['start_time']) stop_dt = pendulum.combine(time_entry['stop_date'], time_entry['stop_time']) return start_dt, stop_dt