def generate_image_info(username=None, limit=None): d = {} if username: d = {'instagram_username': username} # last_used_in_api helps us limit the # of API calls infos = InstagramInfo.objects.filter(**d).exclude(end_date=None).\ order_by('last_used_in_api')[:MAX_API_PER_GENERATION] for info in infos: last_saved = info.start_date if InstagramPhoto.objects.filter(license_info=info): latest_saved_photo = InstagramPhoto.objects.filter( license_info=info).order_by('-created_time')[0] last_saved = latest_saved_photo.created_time recent = cache.get('api_rc_%s' % info.instagram_id) if recent is None: # Get the most recent since we last cached from the API try: recent_resp = instagram.api.users(info.instagram_id).media.\ recent.get( access_token=get_access_token(info.user), max_timestamp=to_unix_time(info.end_date), min_timestamp=to_unix_time(last_saved)) except: return recent = recent_resp['data'] # One hour cache per-user cache.set('api_rc_%s' % info.instagram_id, recent, 60 * 60) for item in recent: # The API returns items even if they're before min_timestamp # sometimes, so we have to check by hand here. created_time = from_unix_time(int(item['created_time'])) if (created_time < info.end_date and created_time > info.start_date and created_time > last_saved): save_image_info(item, info) info.last_used_in_api = datetime.now().replace(tzinfo=utc) info.save()
def ban_process(message: Message, bot: telebot) -> Message: """ get_user_or_create check user in db and return user Creating User instance. Kicking user from chat and send group message about it. NOTE! mute_till.strftime('%s') not working on windows platform! Using timestamp() * 1000 :param message: Current message data with user sender, chat_id and so on :param bot: Telebot instance :return Message: Telegram result api message """ msg = message.text.split() if len(msg) > 1: dt, text = to_unix_time(message) user, created = get_or_create_user(message.reply_to_message) BlackList.create(user=user, datetime_add=datetime.datetime.today(), till_date=dt) banned_user = prepare_user_data(user) till_date = dt.strftime('%Y-%m-%d %H:%M:%S') if text: msg = f'*@{user.username} заблокировал пользователя {banned_user} До:{till_date}\nПричина:*\n`{text}`' else: msg = f'*@{user.username} заблокировал пользователя {banned_user} До: {till_date}*' if created: if msg[1] == 'kick': # kick user from chat aka ban forever response = bot.kick_chat_member( message.chat.id, message.reply_to_message.from_user.id) else: bot.send_photo( chat_id=message.chat.id, photo= 'AgACAgIAAxkDAAIBt15iuBjifOydpm759urePec6VHJgAALirDEbV48YS6MzQ4NoFW4IRSbBDgAEAQADAgADbQADhKoDAAEYBA', caption=msg, reply_to_message_id=message.reply_to_message, parse_mode='markdown', ) # ban user for specific time response = bot.restrict_chat_member( message.chat.id, message.reply_to_message.from_user.id, until_date=dt.timestamp() * 1000, can_send_media_messages=False, can_add_web_page_previews=False, can_send_other_messages=False, can_send_messages=False) else: response = '`Пользователь уже забанен`' bot.reply_to(message.reply_to_message, text=response, parse_mode='markdown') return response
def default(self, obj): payload = None if isinstance(obj, models.Administrator): payload = { 'name': obj.name, 'email': obj.email } optional_properties = { 'phone': 'phone', } elif isinstance(obj, models.Vehicle): payload = {'type': obj.vehicle_type} optional_properties = { 'description': 'description', 'license_plate': 'licensePlate', 'color': 'color' } elif isinstance(obj, models.Worker): payload = { } optional_properties = { 'vehicle': 'vehicle', 'tasks': 'tasks', 'name': 'name', 'phone': 'phone', 'teams': 'team_ids' } elif isinstance(obj, models.Address): payload = { } optional_properties = { 'street': 'street', 'number': 'number', 'city': 'city', 'country': 'country', 'name': 'name', 'apartment': 'apartment', 'state': 'state', 'postal_code': 'postalCode', 'unparsed': 'unparsed', } elif isinstance(obj, models.Destination): payload = { } optional_properties = { 'address': 'address', 'location': 'location', 'notes': 'notes', } elif isinstance(obj, models.Task): payload = { 'merchant': obj.merchant, 'executor': obj.executor, 'destination': obj.destination, 'recipients': obj.recipients, } optional_properties = { 'notes': 'notes', 'pickup_task': 'pickupTask', 'dependencies': 'dependencies', 'complete_after': 'completeAfter', 'complete_before': 'completeBefore', } elif isinstance(obj, models.Recipient): payload = { 'name': obj.name, 'phone': obj.phone, 'notes': obj.notes, } optional_properties = {} elif isinstance(obj, models.Administrator): payload = { 'name': obj.name, 'email': obj.email, } optional_properties = {'phone': obj.phone} if payload is None: return json.JSONEncoder.default(self, obj) else: for key, value in optional_properties.iteritems(): if hasattr(obj, key) and getattr(obj, key) is not None: if isinstance(getattr(obj, key), datetime.datetime): payload[value] = utils.to_unix_time(getattr(obj, key)) else: payload[value] = getattr(obj, key) return payload