def __init__(self): #calling database connection function self._db=DBHelper() self.load_login_window()
#!/usr/bin/env python #-*-coding: utf-8-*- # Version: 0.1 # Author: Don Li <*****@*****.**> # License: Copyright(c) 2013 Don.Li # Summary: import config from dbhelper import DBHelper try: db except NameError: db = DBHelper(**config.db_conf)
# - *- coding: utf- 8 - *- import sys import os import cgi modulePath = os.path.dirname(__file__) + '/../../' # modulePath = os.path.abspath('/home/weather') + '/' sys.path.append(modulePath) from dbhelper import DBHelper method = 'mtd' sensorNumber = 'sensornumber' dbFileName = modulePath + 'weatherstation.db' # dbFileName = modulePath + 'genweather.db' db = DBHelper(dbFileName) args = cgi.FieldStorage() if len(args) == 0: sensors = db.getSensors() print 'Content-Type: text/html; charset=utf-8' print sensorshtml = """ <title>Метеостанция</title> <h1>Датчики</h1> <hr> <table border=0> <tr> <td> № </td> <td>Тип</td> <td> s/n </td> <td>Описание</td>
def __init__(self): self.sessionId = 0 self.db = DBHelper() super(Tinder, self).__init__(self.login, self.loadRegWindow)
def main(): db = DBHelper() try: cursor = db.getDictCursor()
def __init__(self): """Construction fucntion """ self.db_client = DBHelper()
import datetime import dateparser from dbhelper import DBHelper from flask import Flask, render_template, request import json import string app = Flask(__name__) DB = DBHelper() categories = ['mugging', 'break-in'] @app.route('/') def home(error_message=None): crimes = DB.get_all_crimes() crimes = json.dumps(crimes) return render_template('home.html', crimes=crimes, categories=categories, error_message=error_message) @app.route('/submitcrime', methods=['POST']) def submitcrime(): category = request.form.get('category') if category not in categories: return home() date = format_date(request.form.get('date')) if not date:
def button(bot, update): query = update.callback_query chatsession = checkforactivesession(query.message.chat_id) if chatsession == None or not isinstance( chatsession, ButtonSession ): ##check session created for chat and reply is valid bot.edit_message_text(chat_id=query.message.chat_id, message_id=query.message.message_id, text='Type /start to begin') return res = chatsession.handle(query.data, datetime.datetime.now(), query.message.message_id) sessions.remove(chatsession) if res is None: sessions.append(Waitactionsession(chatsession)) bot.edit_message_text(chat_id=query.message.chat_id, message_id=query.message.message_id, text='Type /start to begin') else: ##special actions if not (res.reply is None): ##data unhandled by session if res.reply == "Bye Bye": ##logout bot.edit_message_text(chat_id=query.message.chat_id, message_id=query.message.message_id, text=res.reply) else: ##return to /start menu s = Waitactionsession(res.session, res.reply) sessions.append(s) ##add new session to list, wait for user bot.edit_message_text(chat_id=query.message.chat_id, message_id=query.message.message_id, text=s.reply, reply_markup=InlineKeyboardMarkup( s.keyboard)) if res.reply == "Leave approved": ##send message to inform user leave approved db = DBHelper() chat_id = db.findchatid(res.session.checkleave) db.close() bot.send_message( chat_id=chat_id, text="Your leave has been approved. Login to check") elif res.reply[ -8:] == "canceled" and res.reply[: 14] == "Approved Leave": ##send message to inform admin leave canceled if not res.session.admin: db = DBHelper() super_id = db.findsuper(res.session.dept) db.close() bot.send_message(chat_id=super_id, text=res.reply + " by " + res.session.user) else: ##no special action needed, add new session to list and wait for user action sessions.append( res.session) ##add new session to list, wait for user if not res.session.keyboard is None: #reply_markup = None give error bot.edit_message_text(chat_id=query.message.chat_id, message_id=query.message.message_id, text=res.session.reply, reply_markup=InlineKeyboardMarkup( res.session.keyboard)) else: bot.edit_message_text(chat_id=query.message.chat_id, message_id=query.message.message_id, text=res.session.reply)
def __init__(self): self.db = DBHelper() self.constructGUI()
from dbhelper import DBHelper from flask import Flask, render_template, request app = Flask(__name__) dbHelper = DBHelper() @app.route("/") def home(): try: descriptionData = dbHelper.select_all() except Exception as e: print e descriptionData = None return render_template("home.html") @app.route('/add', methods=['POST']) def add(): try: userinput = request.form.get('userinput') dbHelper.insert(des=userinput) except Exception as e: print e return home() @app.route('/clear') def clear(): try: dbHelper.delete_all()
def __init__(self): coll_name = "inventory" self.yaml_file = config_file("/cloudmesh_mac.yaml") self.db_client = DBHelper(coll_name) self.bm_status = BaremetalStatus()
import pandas from uuid import uuid4 import datetime import os import cloudinary.api import argparse from telegram.error import (TelegramError, Unauthorized, BadRequest, TimedOut, ChatMigrated, NetworkError) from threading import Thread import sys from dbhelper import DBHelper from utilities import CloudinaryHelper # handle database connections db = DBHelper(choice='mysql') # change to 'psql' if you're using psql # handle cloudinary operations cloudinary_connector = CloudinaryHelper() # Enable logging logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) logger = logging.getLogger(__name__) # Command handlers # To start a bot def start(update, context):
def __init__(self): self.db = DBHelper() # Load GUI self.load_login_window()
def main(): db = DBHelper() db.resetleave() ##<<run to add leave for new year db.close()
import paho.mqtt.client as mqtt import time from datetime import datetime from datetime import timedelta import random import dbconfig from dbhelper import DBHelper import mqconfig_stat as mqconfig DB_HOST = dbconfig.IP_ADDR DB = DBHelper(DB_HOST) MQ_HOST = mqconfig.mq_host MQ_TOPIC = mqconfig.mq_topic count = 0 cpu_temp = 50 def pushData2DB(tim, dat): global count try: DB.insertStatusRec(tim, dat) except Exception as e: print ("Exception", e) def on_connect(client, userdata, flags, rc): try:
def handle(msg): content_type, chat_type, chat_id = telepot.glance(msg) print(content_type, chat_type, chat_id) db = DBHelper() pprint(msg) dataBase.addChat_id(chat_id, db) dataBase.addUtente(msg['from']['id'], db, chat_type, msg['from']['username'], msg['from']['first_name']) if (content_type == 'text'): dataBase.addMex(chat_id, msg['from']['id'], msg['text'], db) if ("/ann" in msg['text']) & (msg['from']['id'] == 223772637): for chat in dataBase.lista_chat: try: bot.sendMessage(chat.chat_id, msg['text'][5:]) print("message sent") except telepot.exception.BotWasBlockedError: print("user block the bot") if ('reply_to_message' in msg) & (msg['text'].lower() == "fugo nome"): last_name = "" if 'last_name' in msg['reply_to_message']['from']: last_name = msg['reply_to_message']['from']['last_name'] bot.sendMessage(chat_id, "si chiama: " + msg['reply_to_message']['from']['first_name'] + " " + last_name, reply_to_message_id=msg['message_id']) if (msg['text'] == "ciao"): bot.sendMessage(chat_id, "afangul") if (msg['text'].lower() == "buonanotte"): n = randint(0, 6) print(n) now = datetime.datetime.now() print(now) if (now.hour >= 18 - 2) & (now.hour <= 20 - 2) & (n <= 3): bot.sendMessage(chat_id, "mamma mia che poppante ahasha", reply_to_message_id=msg['message_id']) elif (now.hour > 20 - 2) & (now.hour <= 22 - 2) & (n <= 3): bot.sendMessage(chat_id, "di già", reply_to_message_id=msg['message_id']) elif (now.hour >= 22 - 2) & (now.hour <= 2) & (n <= 4): bot.sendMessage(chat_id, "buonanotte °^°", reply_to_message_id=msg['message_id']) if (msg['text'] == "/start"): bot.sendMessage( chat_id, "Ciao, per conoscere tutte le funzionalità di questo bot digita /help" ) if (msg['text'] == "/help"): bot.sendMessage( chat_id, "Questo bot è in grado di rispondere ad alcune domande postegli. E possiede alcuni comandi speciali: \n\n» /rfoto o rfoto/Rfoto: manda una foto casuale con didascalia casuale inviate nel gruppo stesso al costo di 2 jjm coin, per rimuovere una foto non gradita rispondere alla stessa digitando il comando /rimuovi \n\n» \"Jjm scherzo\" (in risposta a un utente): al costo di 200 jjm coin farà un bello scherzetto all'utente scelto\n\n» \"jjm coin\": mostrerà i coin che possiedi.\n\nPer conoscere ulteriori informazioni riguardo ai coin premi /help_coin" ) if (msg['text'] == "/help_coin"): bot.sendMessage( chat_id, "I coin vengono guadagnati: \n 1 ogni 10 messaggi inviati\n 10 ogni complimento fatto a un utente (premere /complimento per la lista dei complimenti)\n\nI coin vengono persi:\n -50 ogni insulto fatto a un utente VIP (premere /insulto per la lista degli insulti)\n\nSe si vuole donare dei coin a un altro utente basta premere in risposta a un utente /dona seguito da uno spazio e l'importo che si vuole donare. Es: /dona 100\n\nQuando un utente raggiunge 1000 coin diventa Vip, il che significa che verrà difeso dal bot in caso di insulti da parte di altri utenti e guadagnerà 50 coin ad ogni insulto ricevuto, prelevati direttamente dall'utente che lo ha insultato." ) if "?" in msg['text']: metti = True mex = msg['text'].lower() for domanda in domande: if (domanda.domanda == mex): metti = False r = randint(-1, len(domanda.risposte) - 1) if (len(domanda.risposte)) > 0: bot.sendMessage(chat_id, domanda.risposte[r], reply_to_message_id=msg['message_id']) break if (metti): domanda = Domanda() domanda.domanda = mex domande.append(domanda) db.add_domanda(mex) print(domande[0].domanda) for chat in dataBase.lista_chat: if (chat.chat_id == chat_id): if (chat.gioco == True): for sol in chat.gioco_soluzione: if (sol.lower() in msg['text'].lower()): for utente in dataBase.utenti: if msg['from']['id'] == utente.id: chat.gioco = False del chat.gioco_soluzione[:] #giochi = giochi-1 utente.coin += 50 bot.sendMessage( chat_id, "BRAVO HAI VINTOOO 50 jjm COIN!", reply_to_message_id=msg['message_id']) db.add_utente_coin(msg['from']['id'], 50, "COIN") break if (msg['text'] == "gioco reset") | (msg['text'] == "/gioco_reset"): for chat in dataBase.lista_chat: if (chat.chat_id == chat_id): if (chat.gioco == True): chat.gioco = False del chat.gioco_soluzione[:] bot.sendMessage(chat_id, "_gioco resettato_", parse_mode='Markdown') break else: bot.sendMessage(chat_id, "_nessun gioco in corso_", parse_mode='Markdown') for utente in dataBase.utenti: if (utente.id == msg['from']['id']) & (utente.scherzo == True): utente.mex_scherzo += 1 if (utente.mex_scherzo >= 7): utente.mex_scherzo = 0 utente.scherzo = False r = randint(-1, len(insulti) - 1) insulto = insulti[r] bot.sendMessage(chat_id, msg['text'] + " e sono" + " " + insulto) if (("gay" in msg['text']) | ("Gay" in msg['text'])) & (chat_type == "supergroup"): for utente in dataBase.utenti: if msg['from']['id'] == utente.id: utente.coin += 1 db.add_utente_coin(msg['from']['id'], 1, "COIN") break if (msg['text'] == "jjm coin") | (msg['text'] == "Jjm coin"): for utente in dataBase.utenti: if msg['from']['id'] == utente.id: bot.sendMessage(chat_id, "Hai *" + str(utente.coin) + " jjm coin*.", reply_to_message_id=msg['message_id'], parse_mode='Markdown') break if msg['text'] == "lista": for utente in dataBase.utenti: print(utente.id) if (msg['text'] == "/insulto"): if ('reply_to_message' in msg): if (msg['from']['id'] == 223772637): if (insulti.__contains__( msg['reply_to_message']['text'].lower())) == False: insulti.append(msg['reply_to_message']['text'].lower()) db.add_chat("insulti", msg['reply_to_message']['text'].lower()) else: bot.sendMessage(chat_id, "_Chiedi il permesso a_ @ermanichino", reply_to_message_id=msg['message_id'], parse_mode='Markdown') else: bot.sendMessage(chat_id, insulti) if (msg['text'] == "/complimento"): if ('reply_to_message' in msg): if (msg['from']['id'] == 223772637): if (complimenti.__contains__( msg['reply_to_message']['text'].lower())) == False: complimenti.append( msg['reply_to_message']['text'].lower()) db.add_chat("complimenti", msg['reply_to_message']['text'].lower()) else: bot.sendMessage(chat_id, "_Chiedi il permesso a_ @ermanichino", reply_to_message_id=msg['message_id'], parse_mode='Markdown') else: bot.sendMessage(chat_id, complimenti) if (msg['text'] == "/epiteto"): if ('reply_to_message' in msg): if (msg['from']['id'] == 223772637): if (epiteti.__contains__( msg['reply_to_message']['text'])) == False: epiteti.append(msg['reply_to_message']['text']) db.add_chat("epiteti", msg['reply_to_message']['text'].lower()) else: bot.sendMessage(chat_id, "_Chiedi il permesso a_ @ermanichino", reply_to_message_id=msg['message_id'], parse_mode='Markdown') else: bot.sendMessage(chat_id, epiteti) if (msg['text'] == "addCoin") & (msg['from']['id'] == 223772637): for utente in dataBase.utenti: if utente.id == 223772637: utente.coin += 10000 db.add_utente_coin(utente.id, 10000, "COIN") break if ('reply_to_message' in msg): if ('photo' in msg['reply_to_message']): if (msg['text'] == "/rimuovi"): for chat in dataBase.lista_chat: if chat.chat_id == chat_id: id_foto = msg['reply_to_message']['photo'][-1][ 'file_id'] if (id_foto in chat.foto): chat.foto.remove(id_foto) db.delete_photo(id_foto) bot.sendMessage(chat_id, "_Foto rimossa_", parse_mode='Markdown') else: bot.sendMessage( chat_id, "_Foto già rimossa o non presente_", parse_mode='Markdown') elif ('animation' in msg['reply_to_message']): 0 else: for domanda in domande: if domanda.domanda == msg['reply_to_message'][ 'text'].lower(): id_d = db.get_id( "domande", "ID", msg['reply_to_message']['text'].lower()) domanda.addRisp(msg['text'], id_d, db) print(domanda.risposte[0]) break for insulto in insulti: if (insulto in msg['text'].lower()) & ( ("è" in msg['text']) == False) & ( ("È" in msg['text']) == False) & ( msg['reply_to_message']['from']['is_bot'] == False): utenteInsultato = Utente() for utente in dataBase.utenti: if utente.id == msg['reply_to_message']['from'][ 'id']: utenteInsultato = utente if utente.coin >= 1000: r = randint(-1, len(epiteti) - 1) bot.sendMessage( chat_id, epiteti[r], reply_to_message_id=msg['message_id']) if utente.id == msg['from']['id']: if (utenteInsultato.coin >= 1000): utenteInsultato.coin += 50 db.add_utente_coin(utenteInsultato.id, 50, "COIN") if utente.coin < 50: utente.coin = 0 else: utente.coin -= 50 db.add_utente_coin(utente.id, -50, "COIN") if (chat_type == 'supergroup') & ( msg['reply_to_message']['from']['id'] != msg['from']['id'] ) & (msg['reply_to_message']['from']['is_bot'] == False): for complimento in complimenti: if (complimento == msg['text'].lower()): for utente in dataBase.utenti: if utente.id == msg['from']['id']: utente.coin += 10 db.add_utente_coin(utente.id, 10, "COIN") break if ("/componi" in msg['text']): for chat in dataBase.lista_chat: if (chat.chat_id == chat_id): for utente in dataBase.utenti: if (utente.id == msg['from']['id']): if (utente.coin >= 10): utente.coin -= 10 mex = msg['reply_to_message'][ 'text'].lower() m_words = mex.split() last_word = m_words[-1] list_mex = list() list_one = list() trovato = False for message in chat.messaggi: mess = message.lower() list_m = mess.split() if (last_word in list_m) & (len( list_m) > 1) & (mex != mess): if (list_m[0] == last_word): list_m.remove(last_word) list_mex.append(list_m) trovato = True if (trovato): r2 = randint(-1, len(list_mex) - 1) mex1 = list_mex[r2] mex2 = "" for m in mex1: mex2 = mex2 + " " + m bot.sendMessage( chat_id, "" + mex + "" + mex2) break elif (trovato == False): for message in chat.messaggi: mess = message.lower() list_m = mess.split() i = 0 for w in reversed(m_words): list_m2 = m_words if (len(m_words) / 2): if (i == 3): break if (w in list_m) & ( len(list_m) > 1) & (mex != mess): del list_m2[list_m2. index(w):] del list_m[0:list_m. index(w)] list_mex.append(list_m) list_one.append(list_m2) break i += 1 r1 = randint(-1, len(list_mex) - 1) mex1 = list_one[r1] mex2 = list_mex[r1] m1 = "" for m in mex1: m1 = m1 + " " + m m2 = "" for m in mex2: m2 = m2 + " " + m bot.sendMessage( chat_id, "" + m1 + "" + m2) break else: bot.sendMessage( chat_id, "trovato un cazz o non hai 10 coin" ) break if ("/dona" in msg['text']) & (msg['text'].__contains__("-") == False): num = msg['text'][6:] pagato = False utentePagante = Utente() utentePagato = Utente() for utente in dataBase.utenti: if utente.id == msg['from']['id']: if utente.coin >= int(num): pagato = True utentePagante = utente else: bot.sendMessage( chat_id, "Uaglio non hai tutti sti soldi!", reply_to_message_id=msg['message_id']) break if utente.id == msg['reply_to_message']['from']['id']: utentePagato = utente if pagato: utentePagante.coin -= int(num) db.add_utente_coin(utentePagante.id, int("-" + num), "COIN") utentePagato.coin += int(num) db.add_utente_coin(utentePagato.id, int(num), "COIN") bot.sendMessage( chat_id, "Hai donato *" + num + " coin* a _" + msg['reply_to_message']['from']['first_name'] + "_ con successo.", reply_to_message_id=msg['message_id'], parse_mode='Markdown') if (msg['text'] == "jjm scherzo") | (msg['text'] == "Jjm scherzo"): utenteInfame = Utente() utenteScherzato = Utente() for utente in dataBase.utenti: if utente.id == msg['from']['id']: utenteInfame = utente if utente.id == msg['reply_to_message']['from']['id']: utenteScherzato = utente if (utenteInfame.coin >= 200) & (utenteScherzato.scherzo == False): utenteInfame.coin -= 200 db.add_utente_coin(msg['from']['id'], -200, "COIN") utenteScherzato.scherzo = True elif utenteScherzato.scherzo == True: bot.sendMessage( chat_id, "Lascia sto stu puveret che è già sotto scherz!", reply_to_message_id=msg['message_id']) else: bot.sendMessage( chat_id, "Non hai abbastanza soldi per sta bravata!", reply_to_message_id=msg['message_id']) if ((msg['text'] == "rgioco") | (msg['text'] == "Rgioco") | (msg['text'] == "/rgioco")): for chat in dataBase.lista_chat: if (chat.chat_id == chat_id): if (chat.gioco == False) & (len(chat.mex_gioco) > 50): chat.gioco = True #giochi = giochi+1 n = 0 n = randint(-1, len(chat.mex_gioco) - 1) mex = chat.mex_gioco[n] utente_sol = chat.mex_utenti[n] username_utente = db.get_item("utenti", "USERNAME", utente_sol) firstname_utente = db.get_item("utenti", "FIRST_NAME", utente_sol) if (username_utente != None): chat.gioco_soluzione.append(username_utente) chat.gioco_soluzione.append(firstname_utente) bot.sendMessage( chat_id, "*Indovina chi ha mandato il messaggio:*\n\n" + mex + "\n\n_Per rispondere digita il nome/username della persona._", parse_mode='Markdown') #bot.sendMessage(chat_id, username_utente+" "+firstname_utente) break elif (len(chat.mex_gioco) < 50): bot.sendMessage( chat_id, "_troppi pochi messaggi per iniziare il gioco..._", parse_mode='Markdown') else: bot.sendMessage(chat_id, "_c'è già un gioco in corso_", parse_mode='Markdown') if ((msg['text'] == "rfoto") | (msg['text'] == "Rfoto") | (msg['text'] == "/rfoto")): for chat in dataBase.lista_chat: if (chat.chat_id == chat_id): if (len(chat.foto) > 0): for utente in dataBase.utenti: if (utente.id == msg['from']['id']): if (utente.coin >= 2): utente.coin -= 2 r = randint(-1, len(chat.messaggi) - 1) r2 = randint(-1, len(chat.foto) - 1) bot.sendPhoto(chat_id, chat.foto[r2], chat.messaggi[r]) break else: bot.sendMessage( chat_id, "Non hai abbastanza *jjm coin* per questa funzione.", reply_to_message_id=msg['message_id'], parse_mode='Markdown') break else: bot.sendMessage( chat_id, "_Non sono ancora state inviate immagini su questo gruppo._", parse_mode='Markdown') break if (content_type == 'photo'): file_id = msg['photo'][-1]['file_id'] dataBase.addFoto(chat_id, file_id, db, msg['from']['id'])
def __init__(self): # when the file is run,the constructor of DBHelper is called , and the file gets connected to database self.db = DBHelper() self.constructGUI()
from dbhelper import DBHelper from models import QuestionsList from telegram.ext import Updater import logging db = DBHelper("chatbotDB") TOKEN = "1056892394:AAGb7FVokUYC--nqYA9KgIKjExqL76W221o" URL = "https://api.telegram.org/bot{}/".format(TOKEN) updater = Updater(token=TOKEN, use_context=True) dispatcher = updater.dispatcher questionsList = QuestionsList() usersDict = {} logging.basicConfig( format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
### Run only after 'download.py' ### ### Adapted for GDELT mentions.CSV ### ### By Lee Boon Keong ### ### ### ################################################### ################################################### import pandas as pd # To handle dataframes import numpy as np # To help handle dataframes from dbhelper import DBHelper # For database. The "dbhelper.py" file should be in the same dir. import os, re # To help access files and directories. import pickle # To save objects (variables) for future use. import sys # For progress bar, and exiting script. # Set global variables and/or functions db = DBHelper() # Abbreviate the database helper for ease of use. Path_CSV = "./csv/" # Sets the path to "csv" folder. Remember to include "/" at the end. proc_stat = { } # A dictionary of the list of files and its process status. To be Pickled! # Function to build a dictionary with 'keys' the CSV file names in the Path_CSV directory, # and assigns "0" as its value to indicate the unprocess status. def build_csv_dict(pickle="ProcessStatus.pickle"): # Function that gets only the CSV filenames and save them into an array. def list_all(ftype="CSV", path="./"): onlyfiles = [ f for f in os.listdir(path) if os.path.isfile(os.path.join(path, f)) ]
def main(): # Read command line arguments parser = argparse.ArgumentParser(description='CdE Akademie Countdown Bot') parser.add_argument('-c', '--config', default="config.ini", help="Path of config file. Defaults to 'config.ini'") parser.add_argument( '-d', '--database', default='akademien.sqlite', help="Path of SQLite database. Defaults to 'akademien.sqlite'") parser.add_argument( '-v', '--verbose', action='count', default=0, help="Reduce logging level to provide more verbose log output. " "(Use twice for even more verbose logging.)") args = parser.parse_args() # Setup DB db = DBHelper(args.database) db.setup() # Initialize logging logging.basicConfig( level=30 - args.verbose * 10, format="%(asctime)s [%(levelname)-8s] %(name)s - %(message)s") # Read configuration and setup Telegram client config = configparser.ConfigParser() config.read(args.config) tclient = TClient(config['telegram']['token']) admins = [int(x) for x in config['telegram']['admins'].split()] spam_protection_time = datetime.timedelta( seconds=float(config['general'].get('spam_protection', 300))) countdown_bot = CountdownBot(db, tclient, admins, spam_protection_time) # Initialize subscription update interval last_subscription_send = datetime.datetime.min subscription_interval = datetime.timedelta( seconds=float(config['general'].get('interval_sub', 60))) subscription_max_age = datetime.timedelta( seconds=float(config['general'].get('max_age_sub', 1800))) # Main loop while True: # Wait for Telegram updates (up to 10 seconds) and process them countdown_bot.await_and_process_updates(timeout=10) # Send subscriptions (if subscription_interval since last check) now = datetime.datetime.utcnow() if (now - last_subscription_send) > subscription_interval: try: countdown_bot.send_subscriptions('1', (last_subscription_send, now), subscription_max_age) except Exception as e: logger.error("Error while processing Subscriptions:", exc_info=e) last_subscription_send = now # Sleep for half a second time.sleep(0.5)
import uuid import pandas as pd import matplotlib matplotlib.use('Agg') import matplotlib.pyplot as plt import io from flask import Flask from flask import render_template from flask import session from flask import jsonify from dbhelper import DBHelper import dbconfig DB = DBHelper(dbconfig.IP_ADDR) plt.rcParams["figure.figsize"] = (8, 4) app = Flask(__name__) app.secret_key = 'qmOdTeca0nM2sq01JEN53DTTHoTlWStk' GETPRICE_URL = "http://127.0.0.1:5008/rpistatus/{}" @app.route("/") def home(): return render_template("home.html") @app.route("/statusplot/<num>")
def __init__(self): self.driver = webdriver.Chrome() self.api = Api(api_key=apikey) self.db = DBHelper()
import json from flask import Flask from dbhelper import DBHelper app = Flask(__name__) DB = DBHelper('test') @app.route('/') def hello_world(): try: data = DB.get_all_inputs() except Exception as e: print(e) data = None return json.dumps(data) if __name__ == '__main__': app.run(port=5000, debug=True)
class BackupFiles: drivepathinternal = os.getenv('BMU_INT_ROOT') drivepathexternal = os.getenv('BMU_EXT_ROOT') log_helper = LogHelper() log = log_helper.getLogger() db_helper = DBHelper() db_data = db_helper.getDictCursor() cursor = db_data["cursor"] file_helper = FileHelper() def __init__(self): pass def backup_files(self, backupgroup_id, external): logger = self.log filehelper = self.file_helper if external: drivepath = self.drivepathexternal else: drivepath = self.drivepathinternal drive_info = self.get_drive(backupgroup_id, external) logger.info({ 'action': 'Starting Backuping Files', 'backup_group': backupgroup_id, 'external': external, 'Drive Info': drive_info }) free_disk_space, free_quota = self.get_free_space( drive_info, drivepath) logger.info({ 'action': 'Free Space', 'backup_group': backupgroup_id, 'external': external, 'Drive Info': drive_info, 'free_quota': free_quota, 'free_space': free_disk_space }) if free_disk_space <= 0 or free_quota <= 0: logger.warn({ 'action': 'Disk Full, Aborting', 'backup_group': backupgroup_id, 'external': external, 'Drive Info': drive_info, 'free_quota': free_quota, 'free_space': free_disk_space }) return drive_info["id"] files_to_save = self.get_filestosave(backupgroup_id, external) total_files = len(files_to_save) files_saved = 0 logger.info({ 'action': 'Files To backup', 'backup_group': backupgroup_id, 'external': external, 'files_to_backup': total_files }) skip_big = 0 for file_to_save in files_to_save: # # temporaray code for testing # # if file_to_save["filesize"] > 5000000000: # logger.info("Skipping File to big because of temporary file Size limit 5GB : %s" % file_to_save) # continue # # End of Temporary Code if free_disk_space < file_to_save[ "filesize"] or free_quota < file_to_save["filesize"]: logger.info({ 'action': 'Skipping File to big for remaining Space', 'backup_group': backupgroup_id, 'external': external, 'file_to_backup': file_to_save }) skip_big += 1 continue target = filehelper.path_from_hash(drivepath, drive_info["name"], file_to_save["hash"]) source = filehelper.buffer_path_from_hash(file_to_save["hash"], backupgroup_id) logger.info({ 'action': 'Copying File', 'backup_group': backupgroup_id, 'external': external, 'file_to_backup': file_to_save }) if not filehelper.copy_file(source, target): logger.error({ 'action': 'Copying File', 'backup_group': backupgroup_id, 'external': external, 'file_to_backup': file_to_save, 'source': source, 'target': target }) self.mark_item(backupgroup_id, file_to_save["hash"], external, -9) continue hash_tgt = filehelper.hash_file(target) if hash_tgt != file_to_save["hash"]: logger.error({ 'action': 'Hash not Matching', 'backup_group': backupgroup_id, 'external': external, 'file_to_backup': file_to_save, 'hash_target': hash_tgt, 'target': target }) hash_src_new = filehelper.hash_file(source) if file_to_save["hash"] == hash_src_new: filehelper.delete_file(target) self.mark_item(backupgroup_id, file_to_save["hash"], external, -1) logger.error( "File changed during copying from buffer %s : %s != %s" % (target, hash_tgt, hash_src_new)) logger.error({ 'action': 'File changed during copying from buffer', 'backup_group': backupgroup_id, 'external': external, 'file_to_backup': file_to_save, 'hash_target': hash_tgt, 'target': target, 'hash_src_new': hash_src_new }) continue else: filehelper.delete_file(target) self.mark_item(backupgroup_id, file_to_save["hash"], external, -2) logger.error({ 'action': 'Buffered File does not produce correct hash', 'backup_group': backupgroup_id, 'external': external, 'file_to_backup': file_to_save, 'hash_target': hash_tgt, 'target': target, 'hash_src_new': hash_src_new }) continue else: self.mark_item(backupgroup_id, file_to_save["hash"], external, drive_info["id"]) logger.info({ 'action': 'Backup File Successful', 'backup_group': backupgroup_id, 'external': external, 'file_to_backup': file_to_save, 'hash_target': hash_tgt, 'target': target }) files_saved += 1 free_quota = free_quota - file_to_save["filesize"] free_disk_space = filehelper.freespace(drivepath) logger.info({ 'action': 'Remaining Free Space', 'backup_group': backupgroup_id, 'external': external, 'Drive Info': drive_info, 'free_quota': free_quota, 'free_space': free_disk_space }) logger.info({ 'action': 'Finished Backup', 'backup_group': backupgroup_id, 'external': external, 'Drive Info': drive_info, 'free_quota': free_quota, 'free_space': free_disk_space, 'Files_To_Save': total_files, 'Files_Saved': files_saved }) if skip_big > 0: return drive_info["id"] else: return 0 def get_filestosave(self, backupgroup_id: int, external: bool): cursor = self.cursor tracking_field = 'DRIVE1_ID' if external: tracking_field = 'DRIVE2_ID' sql_getfilesforrun = """ Select i.id as item_id, i.hash as hash, i.filesize as filesize, i.drive1_id as drive1_id, i.drive2_id as drive2_id, i.buffer_status from ITEMS i where (i.%s is null or i.%s = 0) and i.buffer_status = 1 and i.backupgroup_id = %s order by filesize desc """ % (tracking_field, tracking_field, backupgroup_id) # print(sql_getfilesforrun) try: cursor.execute(sql_getfilesforrun) files = cursor.fetchall() return files except Exception as e: print("Exception") # sql error print(e) tb = e.__traceback__ traceback.print_tb(tb) def get_drive(self, backupgroup_id, external): cursor = self.cursor sql_getdrive = """SELECT id, name, drivefull, extern, maxsize, drive_id, group_id FROM DRIVES d inner join DRIVES_GROUPS dg on d.id = dg.drive_id where group_id = %s and drivefull = false and extern = %s limit 1 """ % (backupgroup_id, external) try: cursor.execute(sql_getdrive) result = cursor.fetchone() return result except Exception as e: print("Exception") # sql error print(e) tb = e.__traceback__ traceback.print_tb(tb) return {} def get_free_space(self, drive_info: dict, drivepath: str): filehelper = self.file_helper cursor = self.cursor disk = filehelper.freespace(drivepath) sql_getusedspace = """ select sum(size) size from ( select max(filesize) as size, i.hash from ITEMS i where i.backupgroup_id = %s and (i.DRIVE1_ID = %s or i.DRIVE2_ID = %s) group by i.hash) x """ % (drive_info["group_id"], drive_info["id"], drive_info["id"]) # print(sql_getusedspace) try: cursor.execute(sql_getusedspace) result = cursor.fetchone() # print(result) if result["size"] is None: logical = int(drive_info["maxsize"]) else: logical = int(drive_info["maxsize"]) - int(result["size"]) return disk, logical except Exception as e: print("Exception") # sql error print(e) tb = e.__traceback__ traceback.print_tb(tb) return disk, 0 def mark_item(self, bg_id, hash, external, status): tracking_field = 'DRIVE1_ID' if external: tracking_field = 'DRIVE2_ID' cursor = self.cursor sql_updateitem = 'update ITEMS i set %s = %s where backupgroup_id= %s and hash = "%s" ' % \ (tracking_field, status, bg_id, hash) try: cursor.execute(sql_updateitem) except Exception as e: print("Exception") # sql error print(e) tb = e.__traceback__ traceback.print_tb(tb) def is_hash_known(self, hash, backup_group): cursor = self.cursor sql_updateitem = 'select id from ITEMS where backupgroup_id = %s and hash = \'%s\'' % \ (backup_group, hash) try: cursor.execute(sql_updateitem) data = cursor.fetchall() if len(data) == 0: return 0 else: return data[0]["id"] except Exception as e: print("Exception") # sql error print(e) tb = e.__traceback__ traceback.print_tb(tb) return 0 def change_item_in_bui(self, bui_id, item_id, hash): cursor = self.cursor sql_updatebuitem = 'update BACKUPITEMS set item_id = %s, hash = \'%s\' where id = %s ' % \ (item_id, hash, bui_id) print(sql_updatebuitem) try: cursor.execute(sql_updatebuitem) except Exception as e: print("Exception") # sql error print(e) tb = e.__traceback__ traceback.print_tb(tb) def create_item(self, bg_id, hash, external, status, size): tracking_field = 'DRIVE1_ID' sql_insertitem = 'insert into ITEMS (backupgroup_id, hash, %s, filesize) values (%s, \'%s\', %s, %s)' % \ (tracking_field, bg_id, hash, status, size) if external: tracking_field = 'DRIVE2_ID' sql_insertitem = 'insert into ITEMS (backupgroup_id, hash, DRIVE1_ID, DRIVE2_ID, filesize) values (%s, \'%s\', -12, %s, %s)' % \ (bg_id, hash, status, size) cursor = self.cursor try: cursor.execute(sql_insertitem) except Exception as e: print("Exception") # sql error print(e) tb = e.__traceback__ traceback.print_tb(tb) def close_finished_runs(self): sql_get_finished = """ Select id, coalesce(x.count, 0) as count from RUNS r LEFT OUTER JOIN ( Select run_id, count(*) as count from BACKUPITEMS b inner join ITEMS i on (b.item_id = i.id) where i.DRIVE1_ID < 0 or i.DRIVE2_ID < 0 group by run_id ) x on r.id = x.run_id where (ALL_SAVED IS NULL or ALL_SAVED = 0) and id not in ( Select distinct b.run_id as run_id from BACKUPITEMS b inner join ITEMS i on (b.item_id = i.id) where ((i.DRIVE1_ID is null or i.DRIVE1_ID = 0) or (i.DRIVE2_ID is null or i.DRIVE2_ID = 0)) ) """ sql_update_run = "UPDATE RUNS SET ALL_SAVED = 1, ERRORS_SAVING = %s where ID = %s" cursor = self.cursor try: cursor.execute(sql_get_finished) runs = cursor.fetchall() logger = self.log for run in runs: cursor.execute(sql_update_run, (run["count"], run["id"])) logger.info("Saved Run %s with %s Errors" % (run["id"], run["count"])) logger.info({ 'action': 'Saved Runs', 'run_id': run["id"], 'Errors': run["count"] }) except Exception as e: print("Exception") # sql error print(e) tb = e.__traceback__ traceback.print_tb(tb) def cleanupBuffer(self): fh = FileHelper() dbh = DBHelper() logger = self.log sql_savedbuffer = "select * from ITEMS where (DRIVE1_ID > 0 and DRIVE2_ID > 0) and buffer_status = 1 order by id " sql_updatebufferstatus = "UPDATE ITEMS SET BUFFER_STATUS = 2 WHERE ID = %s" usage = fh.bufferusage() print(usage) try: db = dbh.getDictCursor() cursor = db["cursor"] cursor.execute(sql_savedbuffer) result = cursor.fetchall() for file in result: if usage <= 0.8: break fh.removefrombuffer(file["HASH"], file["BACKUPGROUP_ID"]) usage = fh.bufferusage() cursor.execute(sql_updatebufferstatus, (file["ID"])) print("removed %s from buffer for BG %s " % (file["HASH"], file["BACKUPGROUP_ID"])) print(usage) logger.info({ 'action': 'Removed from Buffer', 'hash': file["HASH"], 'bachup_group': file["BACKUPGROUP_ID"], "size": file["FILESIZE"] }) except Exception as e: print("Exception") # sql error print(e) tb = e.__traceback__ traceback.print_tb(tb) def set_drive_full(self, id): cursor = self.cursor sql_updateitem = 'update DRIVES set drivefull = 1 where id=%s ' % id try: cursor.execute(sql_updateitem) except Exception as e: print("Exception") # sql error print(e) tb = e.__traceback__ traceback.print_tb(tb)
'2': ['Fotografía y videocámaras', 'Móviles y telefonía', 'TV, Vídeo y Home Cinema', 'Audio y Hi-Fi', 'GPS', 'Instrumentos musicales', 'Accesorios de electrónica', 'Informática', 'Hogar Digital', 'Amazon Renewed', 'Todo en Electrónica'], '3': ['Portátiles', 'Tablets', 'Sobremesas', 'Monitores', 'Componentes', 'Accesorios de informática', 'Impresoras y tinta', 'Software', 'Gaming Store', 'Juegos para PC', 'Todo en Informática', 'Material de oficina', 'Papel', 'Bolígrafos y material de escritura', 'Electrónica de oficina', 'Todo en Oficina y papelería'], '4': ['Todo en Videojuegos', 'Consolas y accesorios', 'Juegos para PC', 'Merchandising', 'Infantil'], '5': ['Juguetes y juegos', 'Bebé', 'Lista de Nacimiento', 'Amazon Familia', 'Amazon Kids'], '6': ['Cocina y comedor', 'Dormitorio y salón', 'Baño', 'Jardín', 'Iluminación', 'Pequeño electrodoméstico', 'Aspiración, limpieza y planchado', 'Almacenamiento y organización', 'Climatización y calefacción', 'Todo en Hogar y cocina', 'Chollos', 'Descubre tu estilo', 'Explorar Showroom', 'Herramientas de bricolaje', 'Hogar Digital', 'Herramientas de jardinería', 'Instalación eléctrica', 'Fontanería de baño y cocina', 'Seguridad y prevención', 'Todo en Bricolaje y herramientas', 'Todo para mascotas', 'Perfiles de mascota'], '7': ['Alimentación y bebidas', 'Bebidas alcohólicas', 'Productos eco', 'Alimentación y Vinos de España', 'Pantry'], '8': ['Belleza', 'Belleza Luxury', 'Dermocosmética', 'Belleza masculina', 'Cosmética Natural', 'Salud y cuidado personal', 'Limpieza del hogar', 'Pantry'], '9': ['Mujer', 'Hombre', 'Niña', 'Niño', 'Bebé', 'The Drop', 'Bolsos', 'Joyería', 'Relojes', 'Equipaje', 'Outlet', 'Chollos'], '10': ['Running', 'Fitness y ejercicio', 'Ciclismo', 'Tenis y pádel', 'Golf', 'Deportes de equipo', 'Deportes acuáticos', 'Deportes de invierno', 'Acampada y senderismo', 'Tienda de Yoga', 'Movilidad urbana', 'Todo en Deportes', 'Ropa deportiva', 'Calzado deportivo', 'GPS y electrónica'], '11': ['Accesorios y piezas para coche', 'Herramientas y equipos', 'GPS', 'Accesorios y piezas para moto'], '12': ['Todo en Industria, empresas y ciencia', 'Laboratorio', 'Limpieza', 'Seguridad'] } app = Flask(__name__, static_url_path='/static') db = DBHelper() db.setup() DEPLOYED = True if DEPLOYED: BASE_URL = 'https://delazona.herokuapp.com' else: BASE_URL = 'https://5ae6b60b.ngrok.io' JSON_DATA = {} def populate_db_with_datasets(): global JSON_DATA with open('data.txt') as json_file: print('data.txt opened')
def checknameexist(self, name): db = DBHelper() res = db.findnameinstance(name, self.dept) db.close() return res