def add_source(bot, user_id): account_id = read_aссount_id(bot, user_id) subscription_type = read_subscription_type(bot, user_id) if account_id is None or subscription_type is None: return vk = get_vk(account_id) bot.messages_to_send[user_id].append({ 'text': 'Type url of the wall (e.g., id1 or club1387831 or ' 'oldlentach)' }) message = bot.incoming_messages[user_id].pop_message() if subscription_type == 'Chat': if message.text[0] == 'c' and message.text[1:].isdigit(): chat_id = int(message.text[1:]) + 2 * 10**9 else: chat_id = vk.get_wall_id_by_url(message.text) add_vk_chat_subscription(chat_id, account_id) elif subscription_type == 'Wall': add_vk_wall_subscription(vk.get_wall_id_by_url(message.text), account_id) bot.messages_to_send[user_id].append({ 'text': 'Subscription added!', 'keyboard': default_keyboard })
def read_subscription(bot, user_id, account_id): subscription_type = read_subscription_type(bot, user_id) if subscription_type is None: return vk = get_vk(account_id) ids = get_chat_subscriptions(account_id) if subscription_type == 'Chat' \ else get_wall_subscriptions(account_id) sources_names = \ [name(subscription_type, id_, vk) + ' (id={0})'.format(id_) for id_ in ids] bot.messages_to_send[user_id].append({ 'text': 'Select ' + subscription_type.lower(), 'keyboard': [[sources_name] for sources_name in sources_names] + [['Exit']] }) while True: message = bot.incoming_messages[user_id].pop_message() if message.text == 'Exit': start(bot, user_id, False) return if message.text not in sources_names: bot.messages_to_send[user_id].append( {'text': 'Incorrect account!'}) else: break id_ = int(re.match(r'.+ \(id=(.+)\)$', message.text).group(1)) return subscription_type, id_
def update_posts(account_id): vk = get_vk(account_id) subscriptions = get_wall_subscriptions(account_id) for wall_id in subscriptions: for raw_post in vk.get_posts(wall_id, get_last_post_id(wall_id, account_id)): post = VkPost(raw_post, vk.account_id) post.date_to_show = datetime.datetime.today() post.save_to_database()
def update_messages(account_id): vk = get_vk(account_id) subscriptions = set(get_chat_subscriptions(account_id)) for message in vk.get_new_messages_raw(): msg = VkMessage(message, vk.account_id) if msg.chat_id in subscriptions and msg.author_id != vk.get_own_id(): msg.date_to_show = datetime.datetime.today() else: msg.date_to_show = None msg.save_to_database()
def __init__(self, raw_msg, account_id): Record.__init__(self, account_id) if raw_msg is None: return self.record_type = 'vk_post' self.vk = get_vk(account_id) self.wall_id = raw_msg['owner_id'] self.post_id = raw_msg['id'] self.text = raw_msg['text'] self.author_id = raw_msg['from_id'] self.date = datetime.datetime.fromtimestamp(raw_msg['date']) self.likes = raw_msg['likes']['count'] self.reposts = raw_msg['reposts']['count'] self.views = 0 if 'views' not in raw_msg else raw_msg['views']['count']
def __init__(self, raw_msg, account_id): Record.__init__(self, account_id) if raw_msg is None: return self.record_type = 'vk_message' self.vk = get_vk(account_id) self.message_id = raw_msg['id'] if 'chat_id' in raw_msg: self.chat_id = 2 * 10**9 + raw_msg['chat_id'] else: self.chat_id = raw_msg['user_id'] self.author_id = self.vk.get_own_id() if raw_msg['out'] \ else raw_msg['user_id'] self.text = raw_msg['body'] self.date = datetime.datetime.fromtimestamp(raw_msg['date'])
def read_aссount_id(bot, user_id): vk_account_ids = get_vk_ids_and_account_ids(user_id) vk_account_names = [ get_vk(account_id).get_user_name() + " (id={0})".format(vk_id) for vk_id, account_id in vk_account_ids ] bot.messages_to_send[user_id].append({ 'text': 'Select vk account', 'keyboard': [[name] for name in vk_account_names] + [['Exit']] }) while True: message = bot.incoming_messages[user_id].pop_message() if message.text == 'Exit': start(bot, user_id, False) return if message.text not in vk_account_names: bot.messages_to_send[user_id].append( {'text': 'Incorrect account!'}) else: break num = vk_account_names.index(message.text) account_id = vk_account_ids[num][1] return account_id
def after_load(self): self.vk = get_vk(self.account_id)