def sendtelegram(tipe, user_id, notice, caption): if tipe == 1: handler = "Document" pramas = {"chat_id": user_id, "document": notice, "caption": caption} elif tipe == 2: handler = "Message" pramas = { "chat_id": user_id, "text": caption, } elif tipe == 3: handler = "Animation" pramas = {"chat_id": user_id, "animation": notice, "caption": caption} try: token = TG_BOT_TOKEN r = requests.get( "https://api.telegram.org/bot{}/send{}".format(token, handler), params=pramas, ) logging.info(r.status_code) if r.status_code == 200 and r.json()["ok"]: return 200 elif r.status_code == 403: return 403 else: raise Exception except Exception as e: logging.error(e) logging.info("[*] Could not send telegram message.") return 69
def get_mod(client: Client): req_result = request_time(Client) mes2 = "[{}]: DTU Website has not been Updated.\nLast Notice - \n{}".format( datetime.now().strftime("%Y-%m-%d %H:%M:%S"), req_result[1]) if req_result[0] == 404: logging.info("[*] DTU Website has not been Updated.") elif req_result[0] == 200: file_id = getDocId(req_result[4]) broadcast_list = user_list() total = len(broadcast_list) mongo_url, db1 = MONGO_URL.split("net/") mongo_url = mongo_url + 'net/dtu' os.system( "mongoexport --uri={} -c=users --type json --out bot/hf/users_{}". format(mongo_url, datetime.now().strftime("%Y_%m_%d_%H_%M_%S"))) time.sleep(10) alerts = 0 while alerts < 2: failed = 0 for i in range(0, (total)): try: pp = "[{}]: DTU Site has been Updated!\n\nLatest Notice Title - \n{}\n\nUnder Tab --> {}\n\nCheers!".format( datetime.now().strftime("%Y-%m-%d %H:%M:%S"), req_result[3], req_result[5]) send_status = sendtelegram(1, broadcast_list[i], file_id, pp) if send_status == 200: i += 1 logging.info("[*] Alert Sent to {}/{} people.".format( i, total)) time.sleep(0.3) elif send_status == 404: failed += 1 i += 1 remove_client_from_db(broadcast_list[i]) time.sleep(0.3) except Exception as e: logging.error("[*] {}".format(e)) alerts += 1 time.sleep(1) os.remove("bot/hf/recorded_status.json") time.sleep(2) done = "[*] Notice Alert Sent to {}/{} people.\n {} user(s) were removed from database.".format( (int(total - failed)), total, failed) logging.critical(done) sendtelegram(3, AUTH_CHANNEL, "https://telegra.ph/file/d88f31ee50c8362e86aa8.mp4", done) with open("bot/plugins/check.txt", "w+") as f: f.write(mes2) f.close() try: looped = threading.Timer(int(REQUEST_INTERVAL), lambda: get_mod(Client)) looped.daemon = True looped.start() except Exception as e: logging.critical(e) sendtelegram(2, AUTH_CHANNEL, "_", e) return mes2
def redis_dump(): try: for key in r.keys(): exec( 'INSERT INTO redis_persistence (key, data) VALUES (%s, %s) ON CONFLICT UPDATE SET data = EXCLUDED.data', (key, r.dump(key)), commit=True) except Exception as e: logging.error('Redis dump failed', e)
async def missed_noti(client: Client, message: Message): inputm = message.text try: comm, url, title, tab = inputm.split("|") except: await client.send_message( chat_id=AUTH_CHANNEL, text="Format:\n/send|notice_url|notice_title|notice_tab", ) return file_id = getDocId(url) broadcast_list = user_list() total = len(broadcast_list) mongo_url, db1 = MONGO_URL.split("net/") mongo_url = mongo_url + "net/dtu" os.system( "mongoexport --uri={} -c=users --type json --out bot/hf/users_{}". format(mongo_url, datetime.now().strftime("%Y_%m_%d_%H_%M_%S"))) _, _, filenames = next(os.walk("bot/hf/")) logging.info(filenames) for f in filenames: if not ".py" in f: await message.reply_document("bot/hf/{}".format(f)) time.sleep(3) alerts = 1 failed_users = set() failed = 0 while alerts < 2: for i in range(0, (total)): try: pp = "[{}]: DTU Site has been Updated!\n\nLatest Notice Title - \n{}\n\nUnder Tab --> {}\n\nCheers!".format( datetime.now().strftime("%Y-%m-%d %H:%M:%S"), title, tab) send_status = sendtelegram(1, broadcast_list[i], file_id, pp) if send_status == 200: i += 1 logging.info("[*] Alert Sent to {}/{} people.".format( i, total)) time.sleep(0.3) elif send_status == 404: failed += 1 i += 1 failed_users.add(broadcast_list[i]) time.sleep(0.3) except Exception as e: logging.error("[*] {}".format(e)) alerts += 1 time.sleep(1) time.sleep(1) done = "[*] Notice Alert Sent to {}/{} people.\n {} user(s) were removed from database.".format( (int(total - failed)), total, failed) sendtelegram(3, AUTH_CHANNEL, "https://telegra.ph/file/d88f31ee50c8362e86aa8.mp4", done) logging.critical(done) sys.exit()
def getDocId(notice): try: token = TG_BOT_TOKEN r = requests.get( "https://api.telegram.org/bot{}/sendDocument".format(token), params={ "chat_id": AUTH_CHANNEL, "document": notice, "caption": "[Logs] New Notice." }) if r.status_code == 200 and r.json()["ok"]: doc_file_id = r.json()['result']['document']['file_id'] return doc_file_id else: raise Exception except Exception as e: logging.error(e) logging.info("[*] [{}]: Could not send telegram message.".format( datetime.now())) doc_file_id = 0 return doc_file_id
def getDocId(notice): try: token = TG_BOT_TOKEN r = requests.get( "https://api.telegram.org/bot{}/sendDocument".format(token), params={ "chat_id": AUTH_CHANNEL, "document": notice, "caption": "[Logs] New Notice.", }, ) if r.status_code == 200 and r.json()["ok"]: doc_file_id = r.json()["result"]["document"]["file_id"] return doc_file_id else: raise Exception except Exception as e: logging.error(e) logging.info("[*] [{}]: Error Sending Logs File!!.".format( datetime.now())) doc_file_id = 0 sys.exit() return doc_file_id
async def missed_noti(client: Client, message: Message): inputm = message.text try: comm, mess = inputm.split("|") except: await client.send_message( chat_id=AUTH_CHANNEL, text="Format:\n/promo |message", ) return broadcast_list = user_list() total = len(broadcast_list) failed_users = [] failed = 0 for i in range(0, (total)): try: send_status = sendtelegram(2, broadcast_list[i], '', mess) if send_status == 200: i += 1 logging.info("[*] Alert Sent to {}/{} people.".format( i, total)) time.sleep(0.3) elif send_status == 403: failed += 1 i += 1 failed_users.append(broadcast_list[i]) time.sleep(0.3) except Exception as e: logging.error("[*] {}".format(e)) done = "[*] Notice Alert Sent to {}/{} people.\n {} user(s) were removed from database.".format( (int(total - failed)), total, failed) logging.info(failed_users) sendtelegram(3, AUTH_CHANNEL, "https://telegra.ph/file/d88f31ee50c8362e86aa8.mp4", done) logging.critical(done) sys.exit()
def request_time(client: Client): print("[*] Checking DTU Website for notices now....") try: r = requests.get(('http://dtu.ac.in/'), timeout=25) except Timeout: print("[{}]: The request timed out.".format( datetime.now().strftime("%Y-%m-%d %H:%M:%S"))) sys.exit(1) tree = html.fromstring(r.content) try: top_notice = tree.xpath( '//*[@id="tab4"]/div[1]/ul/li[1]/h6/a/text()')[0] if top_notice == ' ': raise IndexError except IndexError: try: top_noticee = tree.xpath( '//*[@id="tab4"]/div[1]/ul/li[1]/h6/a/font/text()') top_notice = top_noticee[0] except Exception as e: logging.error(e) top_notice = "-Please check yourself-" top_link = tree.xpath('//*[@id="tab4"]/div[1]/ul/li[1]/h6/a/@href')[0] top_link = top_link.split('.', 1)[1] top_link = 'dtu.ac.in' + top_link dates = {} recorded_dates = {} tabs = 1 while tabs < 9: for i in range(1, 8): try: date_text = tree.xpath( '//*[@id="tab{}"]/div[1]/ul/li[{}]/small/em/i/text()'. format(tabs, i)) if date_text != []: dates["Date.{}.{}".format(tabs, i)] = date_text if not os.path.exists("recorded_status.json"): recorded_dates["Date.{}.{}".format(tabs, i)] = date_text i = i + 1 else: i = i + 1 except Exception as e: print(e) i = i + 1 tabs += 1 if not path.exists("bot/hf/recorded_status.json"): data = json.dumps(recorded_dates) with open("bot/hf/recorded_status.json", "w+") as f: f.write(data) print("[*] Recorded Current Status.\n[*] Latest dates: {}".format( data)) return_values = [404, top_notice, top_link, ' ', ' '] return return_values else: with open("bot/hf/recorded_status.json", "r") as f: data = f.read() recorded_dates = json.loads(data) modified_key = dict_compare(recorded_dates, dates) if modified_key != []: Tabb = [ '.', 'Notices', 'Jobs', 'Tenders', 'Latest News', 'Forthcoming Events', 'Press Release', '-', '1st Year Notices' ] temp, tab, link = modified_key[0].split('.') try: new_notice = tree.xpath( '//*[@id="tab{}"]/div[1]/ul/li[1]/h6/a/text()'.format( tab))[0] except IndexError: new_notice = tree.xpath( '//*[@id="tab{}"]/div[1]/ul/li[1]/h6/a/font/text()'.format( tab))[0] if tab == 2: context = tree.xpath( '//*[@id="tab2"]/div[1]/ul/li[1]/h6/a/text()') try: vacancy = tree.xpath( '//*[@id="tab2"]/div[1]/ul/li[1]/h6/a/font/text()') new_notice = context[0] + vacancy[0] + context[1] except IndexError: new_notice = context[0] new_link = tree.xpath( '//*[@id="tab{}"]/div[1]/ul/li[1]/h6/a/@href'.format(tab))[0] new_link = new_link.split('.', 1)[1] new_link = 'dtu.ac.in' + new_link Tabb = Tabb[int(tab)] return_values = [ 200, top_notice, top_link, new_notice, new_link, Tabb ] return return_values else: return_values = [404, top_notice, top_link, ' ', ' '] return return_values
def redis_load(): try: for r in exec('SELECT key, data FROM redis_persistence', fetch='*'): r.restore(r[0], r[1]) except Exception as e: logging.error('Redis load failed', e)