def hourly(): while True: try: sleep(300) objects.printer('работаю ' + objects.log_time(time_now() - 2 * 60 * 60)) goodies(time_now() - 2 * 60 * 60, 5) sleep(3300) except IndexError and Exception: executive()
def daily(): while True: try: now_dict = stamp_dict(time_now()) if now_dict['hour'] == '21': objects.printer('работаю ' + objects.log_time(time_now() - 24 * 60 * 60)) goodies(time_now() - 24 * 60 * 60, 10) sleep(3600) sleep(300) except IndexError and Exception: executive()
async def repeat_channel_messages(message: types.Message): global start_message try: if str(message['chat']['id']) == digest_channel: battle = None search = re.search(r'Битва (\d{2}/\d{2}/\d{4} \d{2}:\d{2})', message['text']) if search: battle_stamp = objects.stamper(search.group(1), delta=3, pattern='%d/%m/%Y %H:%M') if (time_now() - battle_stamp) < 1800 and ( time_now() - dict(message).get('date')) < 1800: battle = Auth.time(battle_stamp, form='normal', sep='/', tag=italic, seconds=None) if battle: db = SQL(db_path) users = db.get_users() stamp = datetime.now(tz).timestamp() db.close() coroutines = [ sender(message, user=user, func=bot.forward_message, id=user['id']) for user in users ] await asyncio.gather(*coroutines) text = bold( chats_to_human( len(users), round(datetime.now(tz).timestamp() - stamp, 3))) start_message = Auth.message( old_message=start_message, text=f"\n\nСводки {battle}:\n{text}") else: if str(message['chat']['id']) not in channels: db = SQL(db_path) text, log_text = None, None user = db.get_user(message['chat']['id']) if user is None: text, log_text = first_start(message), ' [#Впервые]' await sender(message, user=user, text=text, log_text=log_text) db.close() except IndexError and Exception: await Auth.dev.async_except(message)
def former(text): response = 'False' soup = BeautifulSoup(text, 'html.parser') is_post_not_exist = soup.find('div', class_='tgme_widget_message_error') if is_post_not_exist is None: lot_raw = str( soup.find( 'div', class_='tgme_widget_message_text js-message_text')).replace( '<br/>', '\n') get_au_id = soup.find('div', class_='tgme_widget_message_link') if get_au_id: au_id = re.sub('t.me/.*?/', '', get_au_id.get_text()) lot = BeautifulSoup(lot_raw, 'html.parser').get_text() stamp = stamper( str(soup.find('time', class_='datetime').get('datetime')), '%Y-%m-%dT%H:%M:%S+00:00') if stamp <= objects.time_now() - 24 * 60 * 60: response = au_id + '/' + re.sub('/', '/', lot).replace( '\n', '/') if is_post_not_exist: search_error_requests = re.search( 'Channel with username .*? not found', is_post_not_exist.get_text()) if search_error_requests: response += 'Requests' return response
def form_mash(au_id, lot): from timer import timer text = '' price = 0 lot_id = '' channel = 'au' status = 'None' modifiers = 'None' stamp_now = time_now() - 24 * 60 * 60 stamp = stamp_now - 10 for g in lot.split('\n'): for i in search_array: search = re.search(search_array.get(i), g) if search: if i == 'Лот #': text += i + search.group(1) + ' : ' + objects.bold( search.group(2)) + '\n' lot_id = search.group(1) elif i == 'Модификаторы:': text += i + '\n{0}' modifiers = '' elif i == 'Цена: ': text += i + search.group(1) + ' 👝\n' price = int(search.group(1)) elif i == 'cw3': price = int(search.group(1)) channel = i elif i == 'Срок: ': stamp = timer(search) text += i + str(time_mash(stamp)) + '\n' elif i == 'Статус: ': text += i if search.group(1) in ['Cancelled', 'Failed', 'Отменен']: status = 'Отменен' elif search.group(1) == '#активен': status = '#активен' elif search.group(1) == '#active': if stamp < stamp_now: status = 'Закончился' else: status = '#активен' else: status = 'Закончился' text += status if status == '#активен': text += '\n\n/bet_{1} /l_{1}' else: if search.group(1) == 'None': text += i + 'Нет\n' else: text += i + search.group(1) + '\n' if modifiers != 'None' and g.startswith(' '): modifiers += g + '\n' if channel == 'au': text = text.format(modifiers, lot_id) return [au_id, text, price, status] else: return [au_id, price, status]
def time(stamp, lang=None): day = 0 text = '' if lang is None: lang = {'day': '{1}', 'hour': '{2}', 'min': '{3}', 'ends': '{4}'} seconds = stamp - objects.time_now() hours = int(seconds / (60 * 60)) if hours > 24: day = int(hours / 24) hours -= day * 24 text += f"{day}{lang['day']}{hours}{lang['hour']}" elif hours > 0: text += f"{hours}{lang['hour']}" elif hours < 0: hours = 0 minutes = int((seconds / 60) - (day * 24 * 60) - (hours * 60)) if minutes >= 0: text += f"{minutes}{lang['min']}" else: text += lang['ends'] return text
def time_mash(stamp, lang=None): day = 0 text = '' if lang is None: lang = {'day': 'д. ', 'hour': 'ч. ', 'min': ' мин.'} seconds = stamp - time_now() hours = int(seconds / (60 * 60)) if hours > 24: day = int(hours / 24) hours -= day * 24 text += str(day) + lang['day'] + str(hours) + lang['hour'] elif hours > 0: text += str(hours) + lang['hour'] elif hours < 0: hours = 0 minutes = int((seconds / 60) - (day * 24 * 60) - (hours * 60)) response = objects.log_time(stamp, tag=objects.italic, form='au_normal') if minutes >= 0: response += '\nОсталось:' + objects.italic(' ~ ' + text + str(minutes) + lang['min']) return response
def tut_checker(): while True: try: global unused_box checker( 'https://jobs.tut.by/search/vacancy?order_by=publication_time&clusters=true&area=16&' 'currency_code=BYR&enable_snippets=true&only_with_salary=true', 'vacancy-serp-item', 'bloko-link', tut_quest) if len(unused_box) > 0 and (11 <= hour() < 21): if (last_date + 122 * 60) < objects.time_now() and block != 'True': site_search = re.search(r'tut\.by|hh\.ru', unused_box[0]) if site_search: post = tut_quest(unused_box[0]) else: post = praca_quest(unused_box[0]) google(unused_box[0]) poster(idMain, former(post[1], 'MainChannel', post[0])) objects.printer(unused_box[0] + ' сделано') unused_box.pop(0) sleep(3) except IndexError and Exception: executive()
def checker(address, main_sep, link_sep, quest): global used_array global unused_box sleep(3) time_now = objects.time_now() text = requests.get(address, headers=headers) soup = BeautifulSoup(text.text, 'html.parser') posts_raw = soup.find_all('div', class_=main_sep) posts = [] for i in posts_raw: link = i.find('a', class_=link_sep) if link is not None: posts.append(link.get('href')) for i in posts: if i not in used_array and i not in unused_box and (11 <= hour() < 21): if (last_date + 120 * 60) < time_now and block != 'True': google(i) used_array.insert(0, i) post = quest(i) poster(idMain, former(post[1], 'MainChannel', post[0])) objects.printer(i + ' сделано') sleep(3) else: unused_box.append(i)
import objects import _thread import requests from SQL import SQL from PIL import Image from time import sleep from aiogram import types from copy import copy, deepcopy from string import ascii_uppercase from aiogram.utils import executor from objects import bold, code, time_now from objects import GoogleDrive as Drive from aiogram.dispatcher import Dispatcher from datetime import datetime, timezone, timedelta # ================================================================================================================= stamp1 = time_now() def download_alt_image(): download_path = 'alt.png' response = requests.get(os.environ.get('alt_image'), stream=True) with open(download_path, 'wb') as file: file.write(response.content) image = Image.open(download_path) image = image.convert('RGB') image.save('images/alt.jpg') os.remove(download_path) def users_db_creation(): db = SQL(db_path)
def form(self, lot_raw, depth='hard'): from timer import timer split = re.sub('️', '', lot_raw).replace('\'', ''').split('/') stamp_now = objects.time_now() - 36 * 60 * 60 au_id = re.sub(r'\D', '', split[0]) lot = {} for key in properties_title_list: lot[key] = 'None' if key in ['au_id', 'lot_id', 'cost', 'stamp']: lot[key] = 0 if au_id.isdigit(): lot['au_id'] = int(au_id) for chunk in split: for key in self.server['form']: search = re.search(self.server['form'].get(key), chunk) if search: if key == 'title': lot['lot_id'] = int(search.group(1)) lot = self.lot_title(lot, search.group(2), depth) elif key == 'condition': lot[key] = re.sub(' ⏰.*', '', search.group(1)) elif key == 'modifiers': lot[key] = '' elif key == 'cost': lot[key] = int(search.group(1)) elif key in ['seller', 'buyer']: user = search.group(1) search_guild = re.search(r'\[(.*?)]', user) search_castle = re.search( self.server['castle_list'], user) if search_guild: lot[f'{key[0]}_guild'] = search_guild.group(1) user = re.sub(r'\[.*?]', '', user, 1) if search_castle: lot[f'{key[0]}_castle'] = search_castle.group( 1) user = re.sub(self.server['castle_list'], '', user, 1) guild_emoji = re.sub( self.server['non_emoji_symbols'], '', user) if len(guild_emoji) > 0: lot[f'{key[0]}_emoji'] = guild_emoji user = re.sub(guild_emoji, '', user) lot[f'{key[0]}_name'] = user.strip() elif key == 'stamp': lot[key] = timer(search) elif key == 'status': status = search.group(1) if status == 'Failed': status = 'Cancelled' if status == '#active': if lot['stamp'] < stamp_now: status = 'Finished' lot[key] = status else: lot[key] = search.group(1) if lot['modifiers'] != 'None' and chunk.startswith(' '): lot['modifiers'] += f' {chunk.strip()}\n' if lot['modifiers'] != 'None' and lot['modifiers'].endswith('\n'): lot['modifiers'] = lot['modifiers'][:-1] return lot
async def lot_updater(): global db, limiter, update_id while True: try: await asyncio.sleep(1) printer('начало') g_actives = google('col_values', 1) stamp2 = datetime.now().timestamp() session = FuturesSession() temp_db = copy.copy(db) update_array = [] update_id += 1 futures = [] for i in temp_db: lot = db.get(i) if lot['action'] == 'Add': google('insert', [i, lot['@cw3auction'][0]]) g_actives.insert(2, str(i)) db[i]['action'] = 'None' for i in temp_db: if db[i]['action'] != 'deleted': lot = db.get(i) if lot['update_id'] + 1 < update_id: update_array = [] update_id -= 1 limiter = 1 if lot['update_id'] + 1 == update_id and limiter <= 300: db[i]['update_id'] = update_id update_array.append(i) limiter += 1 for i in update_array: url = 'https://t.me/chatwars3/' + str(i) + '?embed=1' futures.append(session.get(url)) for future in concurrent.futures.as_completed(futures): result = former(future.result().content) last_time_request() if result[0] != 'False': db[result[0]]['@chatwars3'] = result lot_cw3 = db[result[0]]['@cw3auction'] if result[2] != lot_cw3[1]: db[result[0]]['action'] = 'Update' if result[3] != '#активен': db[result[0]]['action'] = 'Delete' for i in temp_db: lot = db.get(i) if lot['action'] in ['Update', 'Delete']: try: post = await bot.edit_message_text( lot['@chatwars3'][1], idChannel, lot['@cw3auction'][0], parse_mode='HTML') print_text = 'Пост обновлен' if lot['action'] == 'Update': db[i]['action'] = 'None' db[i]['@cw3auction'] = form_mash( post['message_id'], post['text']) else: google('delete', g_actives.index(str(i)) + 1) g_actives.pop(g_actives.index(str(i))) db[i]['action'] = 'deleted' print_text += ' (закончился) и удален из обновлений' except IndexError and Exception as e: print_text = 'Пост не изменился' search = re.search( 'exactly the same as a current content', str(e)) if search: print_text += ', потому что точно такой же' if lot['action'] == 'Update': db[i]['@cw3auction'][1] = db[i]['@chatwars3'][ 2] db[i]['@cw3auction'][2] = db[i]['@chatwars3'][ 3] db[i]['action'] = 'None' else: print_text += ', а еще он закончился и удален из обновлений' google('delete', g_actives.index(str(i)) + 1) g_actives.pop(g_actives.index(str(i))) db[i]['action'] = 'deleted' else: print_text += ' ' + str(e) printer( str(i) + '-' + str(lot['@cw3auction'][0]) + ' ' + print_text) limiter = 1 printer('конец ' + str(datetime.now().timestamp() - stamp2)) delay = 60 - (time_now() - last_requested) if delay >= 0: await asyncio.sleep(delay) except IndexError and Exception: await executive()
def update(self, table, item_id, dictionary, google_update=None): if table == 'users' and google_update is None: dictionary.update({'last_update': time_now(), 'updates': 1}) self.request( f"UPDATE {table} SET {self.upd_kv(dictionary)} WHERE id = '{item_id}'" )
def create_user(self, user): user.update({'last_update': time_now(), 'updates': 1}) self.request(f'REPLACE INTO users {self.ins_kv(user)}')
import os import re import random import _thread import gspread import objects import requests from time import sleep from telebot import types from telegraph import upload from bs4 import BeautifulSoup from datetime import datetime from PIL import Image, ImageFont, ImageDraw from objects import bold, code, under, stamper, log_time stamp1 = objects.time_now() allowed_head_elements = ['условия', 'требования'] starting = ['title', 'place', 'tags', 'geo', 'money', 'org_name', 'schedule', 'employment', 'short_place', 'experience', 'education', 'contact', 'numbers', 'description', 'email', 'metro', 'tag_picture'] headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36' ' (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36'} emoji_list = '([💻🏙🏅🎓💸📔🚇💼🔋])' emoji = { '💻': Image.open('emoji/comp.png'), '🏙': Image.open('emoji/city.png'), '🏅': Image.open('emoji/star.png'), '🎓': Image.open('emoji/edu.png'), '💸': Image.open('emoji/money.png'), '📔': Image.open('emoji/note.png'), '🚇': Image.open('emoji/metro.png'), '💼': Image.open('emoji/case.png'),
def last_time_request(): global last_requested last_requested = time_now()
def hour(): return int(datetime.utcfromtimestamp(objects.time_now() + 3 * 60 * 60).strftime('%H'))
stamp = datetime.now().timestamp() chrome_client = chrome(os.environ.get('local')) for key in db: updater(chrome_client, key) chrome_client.close() print(f"Проход {', '.join(db.keys())} за {datetime.now().timestamp() - stamp}") except IndexError and Exception as error: print(error) import sys import traceback exc_type, exc_value, exc_traceback = sys.exc_info() error_raw = traceback.format_exception(exc_type, exc_value, exc_traceback) error = ''.join(error_raw) print(error) reboot = True if chrome_client: try: chrome_client.close() reboot = False except IndexError and Exception: pass if reboot and os.environ.get('api'): connection = heroku3.from_key(os.environ['api']) for app in connection.apps(): for dyno in app.dynos(): dyno.restart() if os.environ.get('local'): start(objects.time_now())