def chat_loop(): while True: #print(f"==={timestamp()} CHAT MODULE RESTART") try: db.reconnect() vk.reconnect() for event in vk.longpoll.listen(): if (event.type == vk.VkEventType.MESSAGE_NEW and event.to_me): vk_user = vk.user_get(event.user_id) id = event.user_id first_name = (vk_user[0])['first_name'] last_name = (vk_user[0])['last_name'] user = vk.User(id, event.text, first_name, last_name) try: kb.keyboard_browser(user, event.payload) except AttributeError: message_analyzer(user) except psycopg2.Error as err: print( f"---{timestamp()} Database Error (longpull_loop), description:" ) traceback.print_tb(err.__traceback__) print(err.args) try: print(f"---{timestamp()} Try to recconnect database...") db.reconnect() print(f"---{timestamp()} Database connected successful") time.sleep(1) except: print(f"---{timestamp()} Recconnect database failed") time.sleep(10) except OSError as err: print(f"---{timestamp()} OSError (longpull_loop), description:") traceback.print_tb(err.__traceback__) print(err.args) try: print(f"---{timestamp()} Try to recconnect VK...") vk.reconnect() print(f"---{timestamp()} VK connected successful") time.sleep(1) except: print(f"---{timestamp()} Recconnect VK failed") time.sleep(10) except BaseException as err: print( f"---{timestamp()} BaseException (longpull_loop), description:" ) traceback.print_tb(err.__traceback__) print(err.args) time.sleep(5) except: print('---Something go wrong. (chat_loop)')
def notify_loop(): while True: #print(f"==={timestamp()} NOTIFY MODULE RESTART") try: db.reconnect() vk.reconnect() last_send_time = -1 while True: if (last_send_time != dt.time_now_obj()): last_send_time = -1 users = db.get_users_who_sub_at(dt.time_now_obj()) for data in users: user_id, examdate, notifytime, subscribe, tz, firstname, lastname = data ans = notify_mesage(data) vk.write_notify_msg(data[0], ans) last_send_time = dt.time_now_obj() time.sleep(10) except psycopg2.Error as err: #print(f"---{timestamp()} psycopg2.Error error (notify_loop), description:") #traceback.print_tb(err.__traceback__) #print(err.args[0]) try: time.sleep(2) #print(f"---{timestamp()} Try to recconnect database") db.reconnect() #print(f"---{timestamp()} Database connected successful") time.sleep(2) except: #print(f"---{timestamp()} Recconnect database failed") time.sleep(10) except OSError as err: #print(f"---{timestamp()} OSError (notify_loop), description:") #traceback.print_tb(err.__traceback__) #print(err.args[0]) try: #print(f"---{timestamp()} Try to recconnect VK...") vk.reconnect() #print(f"---{timestamp()} VK connected successful") time.sleep(2) except: #print(f"---{timestamp()} Recconnect VK failed") time.sleep(10) except BaseException as err: print(f"---{timestamp()} Unknown Exception (notify_loop):") traceback.print_tb(err.__traceback__) print(err.args[0]) time.sleep(10) except: print('---Something go wrong. (notify_loop)')
def yesterday_plot(id): # Check user in members if not vk.method('groups.isMember', { 'group_id': 'sessiyabot', 'user_id': id }): raise KeyError('Member doesnt subscribe') # Load data from database db.reconnect() with db.connection.cursor() as cur: cur.execute("select * from sessiyabot.yesterday_bins where id =%s;", (id, )) data = cur.fetchall() if len(data) < 5: raise NameError('Small member data') # Parse data u_id, year, month, day, hour, sum_minutes = np.transpose(data) hours_bins = list(hour) sum_minutes = list(sum_minutes) hours_labels = list(map(str, map(int, hours_bins))) # Setting plot and update temp.png fig, axs = plt.subplots() lbl = 'Статистика за ' + str(int(day[0])) + '.' + str(int( month[0])) + '.' + str(int(year[0])) axs.bar(hours_labels, sum_minutes, label=lbl, align='edge', color='#8b00ff') axs.legend() plt.xticks(ticks=range(0, len(hours_labels)), labels=hours_labels, size='small') plt.xlabel('Часы') plt.ylabel('Минут в сети') plt.xlim([0, len(hours_labels)]) # Save image plt.savefig('data/temp.png', dpi=120, bbox_inches='tight') # Clear memory axs.cla() fig.clf() plt.cla() plt.clf() plt.close('all') time_online = str(datetime.timedelta(minutes=int(sum(sum_minutes)))) return 'Вчера вы были онлайн: ' + time_online.split( ':')[0] + 'ч ' + time_online.split(':')[1] + 'м'
def week_plot(id): # Check user in members if not vk.method('groups.isMember', { 'group_id': 'sessiyabot', 'user_id': id }): raise KeyError('Member doesnt subscribe') # Load data from database db.reconnect() with db.connection.cursor() as cur: cur.execute("select * from sessiyabot.week_bins where id =%s;", (id, )) data = cur.fetchall() if len(data) < 3: raise NameError('Small member data') # Parse data u_id, year, month, day, sum_hours = np.transpose(data) days_bins = list(day) sum_hours = list(sum_hours) days_labels = [] for i, d in enumerate(days_bins): days_labels.append(str(int(d)) + '.' + str(int(month[i]))) # Setting plot and update temp.png fig, axs = plt.subplots() axs.bar(days_labels, sum_hours, label='Статистика за последнюю неделю', color='#8b00ff') axs.legend() plt.xticks(ticks=range(0, len(days_labels)), labels=days_labels) plt.ylabel('Часов в сети') # Save image plt.savefig('data/temp.png', dpi=120, bbox_inches='tight') # Clear memory axs.cla() fig.clf() plt.cla() plt.clf() plt.close('all') time_full = str(datetime.timedelta(hours=sum(sum_hours))) time_per_day = sum(sum_hours) / len(sum_hours) tpd = str(datetime.timedelta(hours=time_per_day)) d = time_full.split(' ') if len(data) < 7: period = 'данный период' else: period = 'неделю' if len(d) == 1: ans = 'За ' + period + ' вы бесполезно потратили: ' + time_full.split( ':')[0] + 'ч ' + time_full.split( ':')[1] + 'м\nВ среднем ' + tpd.split( ':')[0] + 'ч ' + tpd.split(':')[1] + 'м в день.' else: h = d[2].split(':') if d[0] == '1': ans = 'За ' + period + ' вы бесполезно потратили: 1 сутки ' + h[ 0] + 'ч ' + h[1] + 'м\nВ среднем: ' + tpd.split( ':')[0] + 'ч ' + tpd.split(':')[1] + 'м в день.' else: ans = 'За ' + period + ' вы бесполезно потратили: ' + d[ 0] + ' суток ' + h[0] + 'ч ' + h[ 1] + 'м\nВ среднем: ' + tpd.split( ':')[0] + 'ч ' + tpd.split(':')[1] + 'м в день.' return ans