Answer the question
In order to leave comments, you need to log in
Answer the question
In order to leave comments, you need to log in
I don't know why Long Poll is here.
import time
import sqlite3
import hashlib
import vk_api
class GroupAlerts:
def __init__(self, token):
self.__groups_list = []
self.__vk = vk_api.VkApi(token=token)
self.__connection = sqlite3.connect("GroupAlerts.db")
self.__cursor = self.__connection.cursor()
self.__cursor.execute("""CREATE TABLE IF NOT EXISTS group_info(group_id INTEGER NOT NULL,
last_wall_id VARCHAR,
last_check INTEGER);
""")
self.__connection.commit()
def add_new(self, *args):
for group in args:
group_screen_name = "public{}".format(group) if str(group).isdigit() else group
group_resolved_name = self.__vk.method("utils.resolveScreenName", {"screen_name": group_screen_name})
if group_resolved_name == []:
print("{} - invalid group id!".format(group))
continue
self.__groups_list.append(group_resolved_name["object_id"])
walls = self.__vk.method("wall.get", {"owner_id": -group_resolved_name["object_id"], "count": 1})
if walls["items"] != []:
last_wall = walls["items"][0]
last_wall_id = "{}_{}".format(last_wall["owner_id"], last_wall["id"])
self.__cursor.execute("INSERT INTO group_info VALUES(?, ?, ?)",
(group_resolved_name["object_id"], last_wall_id, time.time()))
self.__connection.commit()
def check_wall(self):
for group_id in self.__groups_list:
last_db_wall = self.__cursor.execute("SELECT * FROM group_info WHERE group_id=?",
(group_id,)).fetchone()
if last_db_wall != [] and time.time() - last_db_wall[2] > 5:
walls = self.__vk.method("wall.get", {"owner_id": -group_id, "count": 1})
if walls["items"] != []:
last_wall = walls["items"][0]
last_wall_id = "{}_{}".format(last_wall["owner_id"], last_wall["id"])
if last_db_wall[1] != last_wall_id and last_wall["id"] > int(last_db_wall[1].split("_")[1]):
self.__cursor.execute("UPDATE group_info SET last_wall_id=? WHERE group_id=?",
(last_wall_id, group_id))
yield last_wall
self.__cursor.execute("UPDATE group_info SET last_check=? WHERE group_id=?",
(time.time(), group_id))
self.__connection.commit()
if __name__ == '__main__':
gAlerts = GroupAlerts('ТОКЕН')
gAlerts.add_new("ГРУППА")
while True:
for new_wall in gAlerts.check_wall():
pass
# Обработка записи здесь
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question