def __init__(self): self.__username = u"" self.__idealname = u"" self.__namelist = [] self.__countlist = [] self.__DBinstance = DatabaseConnection.DatabaseConnection() self.__chosung_list= [ u'ㄱ', u'ㄲ', u'ㄴ', u'ㄷ', u'ㄸ', u'ㄹ', u'ㅁ', u'ㅂ', u'ㅃ', u'ㅅ', u'ㅆ', u'ㅇ', u'ㅈ', u'ㅉ', u'ㅊ', u'ㅋ', u'ㅌ', u'ㅍ', u'ㅎ'] self.__jungsung_list = [ u'ㅏ', u'ㅐ', u'ㅑ', u'ㅒ', u'ㅓ', u'ㅔ', u'ㅕ', u'ㅖ', u'ㅗ', u'ㅘ', u'ㅙ', u'ㅚ', u'ㅛ', u'ㅜ', u'ㅝ', u'ㅞ', u'ㅟ', u'ㅠ', u'ㅡ', u'ㅢ', u'ㅣ'] self.__jongsung_list = [ u'', u'ㄱ', u'ㄲ', u'ㄳ', u'ㄴ', u'ㄵ', u'ㄶ', u'ㄷ', u'ㄹ', u'ㄺ', u'ㄻ', u'ㄼ', u'ㄽ', u'ㄾ', u'ㄿ', u'ㅀ', u'ㅁ', u'ㅂ', u'ㅄ', u'ㅅ', u'ㅆ', u'ㅇ', u'ㅈ', u'ㅊ', u'ㅋ', u'ㅌ', u'ㅍ', u'ㅎ'] self.__chosung_count= [2,4,2,3,6,5,4,4,8,2,4,1,3,6,4,3,4,4,3] self.__jungsung_count = [2,3,3,4,2,3,3,4,2,4,5,3,3,2,4,5,3,3,1,2,1] self.__jongsung_count = [0,2,4,4,2,5,3,3,5,7,9,9,7,9,9,8,4,4,6,2,4,1,3,4,3,4,4,3]
def html_page_parser(argv): # get html source code in a file html = urllib2.urlopen(argv).read().decode("utf-8") soup = BeautifulSoup(html, "html5lib") tags = soup.find_all('img') # title=soup.find_all('title') title = soup.title.string imgLinks = list() caption = list() if len(tags)==0: print("This website doesn't contain any image in it's content!") exit(0) elif len(tags)>0: for element in tags: try: src = extractImageSrc(str(element))[0] alt = extractImageSrc(str(element))[1] except IndexError: pass if src.endswith((".jpg", ".png", ".jpeg", ".gif", ".svg")): if len(alt) < 1: alt = title if src.startswith("http") or src.startswith("www."): imgLinks.append(src) caption.append(alt) elif src.startswith("//upload."): imgLinks.append("https:" + src) caption.append(alt) else: caption.append(alt) imgLinks.append(checkAddress(argv[1]) + src) current_directory_path = os.path.dirname(os.path.realpath(__file__)) current_directory_path = os.path.join(current_directory_path, 'result') global GlobalContor for i in imgLinks: filename="HtmlImg"+str(GlobalContor)+getImgExtension(i) filepath = os.path.join('result', filename) GlobalContor+=1 try: urllib2.urlretrieve(i, filepath) except: pass images_list=list() images_list=get_images(current_directory_path) finalResult=[] contor=0 for i in images_list: result_json = ImageSerialization.get_info_from_image(i) result_json['caption']=caption[contor] finalResult.append(result_json) contor += 1 db = DatabaseConnection.DatabaseConnection() db.insert_entry(finalResult)
class Discovery: """Purpose is to automatically discover Fog devices through a client push system""" #Threaded function TODO add handler to kill this thread correctly on sigkil. def start_rest(db): rest = RestService(db) db = DatabaseConnection() threading.Thread(target=start_rest, args=(db,)).start()
def __init__(self): self.__mode=u"" self.__limitnum=0 self.__indexnum=0 self.__ideallist = [] self.__DBinstance = DatabaseConnection.DatabaseConnection() self.__dirname = str(id(self)) self.__imgname = id(self) os.mkdir("/home/ec2-user/database_project/static/"+self.__dirname,0777)
def __init__(self): self.__height = "" self.__age = "" self.__facecolor = u"" self.__faceshape = u"" self.__istwineye = u"" self.__DBinstance = DatabaseConnection.DatabaseConnection() self.__containinsertlist = [] self.__dirname = str(id(self)) self.__isfinished = False os.mkdir("/home/ec2-user/database_project/static/" + self.__dirname, 0777)
def pdf_document_parser(argv): checkXObject = r"/Type(?= */XObject)" checkImage = r"/Subtype(?= */Image)" if len(argv) != 2: print("Usage: %s <wrong number of parameters>" % argv[0]) exit(0) this_document = fitz.open(argv[1]) all_image_count = 0 results = [] for each_page in range(len(this_document)): image_list = this_document.getPageImageList(each_page) current_page = this_document.loadPage(each_page) current_page_text = current_page.getText(output='txt') for each_image in image_list: this_object = each_image[0] pixels = fitz.Pixmap(this_document, this_object) all_image_count = all_image_count + 1 if pixels.n < 4: pixels.writePNG("%sresult/image_%s.png" % (argv[0], all_image_count)) else: rgb_image = fitz.Pixmap(fitz.csRGB, pixels) rgb_image.writePNG("%sresult/image_%s.png" % (argv[0], all_image_count)) rgb_image = None pixels = None result = { "path": "%simage_%s.png" % (argv[0], all_image_count), "text": current_page_text, "page": each_page } results.append(result) database = DatabaseConnection.DatabaseConnection() database.insert_entry(results)
def PPTX_extractImages(filepath): if zipfile.is_zipfile(filepath): z = zipfile.ZipFile(filepath) images = list() for file in z.namelist(): path, name = os.path.split(file) if path == "ppt/media": images.append(file) caption = PPT_extract_caption(filepath) finalResult = [] contor = 0 for img in images: z.extract(img, r'.') contor += 1 result_json = ImageSerialization.get_info_from_image(img) newpath = os.path.join('.', img) saveImage(newpath, img) if contor < len(caption): result_json['caption'] = caption[contor] else: result_json['caption'] = os.path.splitext( os.path.basename(filepath))[0] del result_json['pixels'] result_json['path'] = os.path.join('.', 'result', os.path.basename(img)) finalResult.append(result_json) contor += 1 file = open("%sresult.json" % argv[0], 'w') json.dump(finalResult, file, separators=(',', ':')) db = DatabaseConnection.DatabaseConnection() db.insert_entry(finalResult) file.close() shutil.rmtree('./ppt')
def field_names_from_sql(sql, env='MDW'): ''' Assumes that the sql return 2 columns. The first column is assumed to contain the name of the field, and the 2nd column is assumed to contain an nstring expression. ''' # Connecting to the database host = 'NZDEV.RES.BEC.DK' database = 'DEV_{}'.format(env) # DEV because tables are always in DEV with dbc.DatabaseConnection(host=host, database=database) as db: db.sql = sql data, column_names, _ = db.sql_results assert len(column_names) == 2 field_names = [d[0] for d in data] expressions = [d[1] for d in data] transformfields = [ ofield(info[0], datatype='nstring', expression="'{}'".format(info[1]), precision='{}'.format(len(info[1]))) for info in zip(field_names, expressions) ] return transformfields
def save_full_page_data(page_id, since, until, access_token, config_path): """ Extract all posts, comments and subcomment, also refering reactions from page between given date since and until and save in database """ database_conn = DatabaseConnection.DatabaseConnection( DATABASE_SERVER, DATABASE_NAME, DATABASE_USER, DATABASE_PASSWORD) FacebookConnect.init(access_token, config_path) page = FacebookConnect.get_page(page_id) # Retrieve pageinformation database_conn.insertPage(page) # Retrieve all posts posts = FacebookConnect.get_posts(page_id, LIMIT_POSTS, since, until) counter_posts = 0 bar_posts = progressbar.ProgressBar(maxval=len(posts), widgets=[ progressbar.Bar('=', '[', ']'), ' ', progressbar.Percentage() ]) bar_posts.start() database_conn.insertPosts(posts) for post in posts: bar_posts.update(counter_posts) time.sleep(0.1) # Retrieve comments of post comments = FacebookConnect.get_comments(post[0], LIMIT_COMMENTS) database_conn.insertComments(comments) for comment in comments: # Retrieve subcomments of comment subcomments = FacebookConnect.get_comments(comment[0], LIMIT_COMMENTS) database_conn.insertComments(subcomments) counter_posts = counter_posts + 1 bar_posts.update(len(posts))
def target_from_sql(sql, env='MDW'): # Janky way to find name from sql: trg_name = sql.split(' ')[3] trg = Target(name=trg_name) # Connecting to the database host = 'NZDEV.RES.BEC.DK' database = 'DEV_{}'.format(env) with dbc.DatabaseConnection(host=host, database=database) as db: db.sql = sql _, column_names, descriptions = db.sql_results column_types = [d[1] for d in descriptions] # Converting datatypes from the DatabaseConnection to # useful types in both PowerCenter and Netezza nz_datatypes = map(get_nz_datatype_from_description, descriptions) nz_col_name_and_type = zip(descriptions, column_names, nz_datatypes) # Creating fields for the pwc.Target target_fields = [ targetfield(name.upper(), datatype, precision=str(_col_precision(description)), scale=str(_scale(description)), fieldnumber=str(i + 1), nullable='NULL' if _null_ok(description) else 'NOTNULL') for i, (description, name, datatype) in enumerate(nz_col_name_and_type) ] # Assume that (only) the first key in the table is the primary key target_fields[0][1]['KEYTYPE'] = 'PRIMARY KEY' trg.add_fields(target_fields) trg.load_order = '0' return trg
# parse dictionaries info('--- Parsing dictionary files ---') dict = dictionary.Dictionary('dictionaries/dictionary') # connect to database if main_config['DATABASE']['enable']: info('--- Connecting to database ---') # set driver name if main_config['DATABASE']['type'] == 'postgresql': DatabaseConnection.dbadapterName = 'psycopg' else: DatabaseConnection.dbadapterName = 'MySQLdb' # connect to host and store connection globally available try: DatabaseConnection.dbh1 = DatabaseConnection.DatabaseConnection('dbh1') DatabaseConnection.dbh1.connect( host=main_config['DATABASE']['host'], user=main_config['DATABASE']['user'], password=main_config['DATABASE']['pass'], dbname=main_config['DATABASE']['name']) except: misc.printExceptionError() misc.quit( "Error connecting to database. Check DATABASE section in config file.", 1) # start server itself authport = main_config["SERVER"]["auth_port"] acctport = main_config["SERVER"]["acct_port"] srv = BsdRadiusServer.BsdRadiusServer(dict = dict, authport = authport, \
def __init__(self): self.__DBinstance = DatabaseConnection.DatabaseConnection() self.__dirname = str(id(self))
def __init__(self): self.dbConn = db.DatabaseConnection("wasap_mq.db")
import RobotServer import DatabaseConnection import RobotMovement import DataParser import psutil p = psutil.Process() p.cpu_affinity([0]) print("mainThread running on core " + str(p.cpu_affinity())) mainQueue = Queue.Queue() serverThread = RobotServer.RobotServer(mainQueue) movementThread = RobotMovement.RobotMovement(mainQueue) dbThread = DatabaseConnection.DatabaseConnection(mainQueue) serverThread.setDaemon(True) serverThread.start() movementThread.setDaemon(True) movementThread.start() dbThread.setDaemon(True) dbThread.start() def checkCommand(receivedData): print(receivedData) if (receivedData["Command"] == "Halt"): movementThread.insertMovementQueue(movementThread.setPanic)
class RuBot: databaseConnection = DatabaseConnection.DatabaseConnection() dailyMenus = {} def findWeek(self, soup): paragraphs = soup.find_all('p') week = [] for p in paragraphs: if('Semana' in p.text): week.append(p) return week def getMenu(self, campus): URL_MENU_RU_CCO = 'https://www.uffs.edu.br/campi/' + campus + '/restaurante_universitario' page = requests.get(URL_MENU_RU_CCO) soup = BeautifulSoup(page.text, 'html.parser') table = soup.find_all('table') week = self.findWeek(soup) firstWeek = re.search(r'(\d+/\d+/\d+)', week[0].text).group(1).split('/') weekNumberToday = datetime.today().isocalendar()[1] weekNumberCalendar = date(int(firstWeek[2]), int(firstWeek[1]), int(firstWeek[0])).isocalendar()[1] if(weekNumberToday == weekNumberCalendar): html = str(week[0]) + str(table[0]) else: html = str(week[1]) + str(table[1]) img = requests.post('https://hcti.io/v1/image', data = {'HTML': html}, auth=(htciId, htciKey)) imgUrl = img.text.split('"')[3] query = "INSERT INTO images (weekNumber, imgUrl, imgHtml, campus) VALUES ({}, '{}', '{}', '{}');".format(weekNumberToday, imgUrl, html, campus) self.databaseConnection.executeQuery(query) print('Inserido nova imagem ao banco\nweekNumber: ', Utils.getWeekNumber(), '\tCampus: ', campus) return imgUrl def selectCampus(self, bot, update): keyboard = [ [ telegram.InlineKeyboardButton('RU Chapecó', callback_data = 'RU-chapeco'), telegram.InlineKeyboardButton('RU Cerro Largo', callback_data = 'RU-cerro-largo'), telegram.InlineKeyboardButton('RU Erechim', callback_data = 'RU-erechim') ], [ telegram.InlineKeyboardButton('RU Laranjeiras', callback_data = 'RU-laranjeiras-do-sul'), telegram.InlineKeyboardButton('RU Realeza', callback_data = 'RU-realeza') ], [ telegram.InlineKeyboardButton('← Menu principal', callback_data = 'main-menu') ] ] replyMarkup = telegram.InlineKeyboardMarkup(keyboard) bot.editMessageText( message_id = update.callback_query.message.message_id, chat_id = update.callback_query.message.chat.id, text = '*Selecione o campus:*', parse_mode = 'Markdown', reply_markup = replyMarkup ) def selectCampusAuto(self, bot, update, period): try: keyboard = [ [ telegram.InlineKeyboardButton('Chapecó', callback_data = 'AUTO/'+period+'/chapeco'), telegram.InlineKeyboardButton('Cerro Largo', callback_data = 'AUTO/'+period+'/cerro-largo'), telegram.InlineKeyboardButton('Erechim', callback_data = 'AUTO/'+period+'/erechim') ], [ telegram.InlineKeyboardButton('Laranjeiras', callback_data = 'AUTO/'+period+'/laranjeiras-do-sul'), telegram.InlineKeyboardButton('Realeza', callback_data = 'AUTO/'+period+'/realeza') ], [ telegram.InlineKeyboardButton('← Menu principal', callback_data = 'main-menu') ] ] replyMarkup = telegram.InlineKeyboardMarkup(keyboard) bot.editMessageText( message_id = update.callback_query.message.message_id, chat_id = update.callback_query.message.chat.id, text = 'Selecione o campus:', parse_mode = 'HTML', reply_markup = replyMarkup ) except Exception as e: print("selectCampusAuto: "+str(e)+"\n") def showCardapio(self, bot, update, campus): chatId = Utils.getChatId(bot, update) query = "SELECT imgUrl FROM images WHERE weekNumber = {} AND campus = '{}';".format(Utils.getWeekNumber(), campus) image = self.databaseConnection.fetchAll(query) if len(image): imgToSend = image[0][0] else: bot.sendMessage(chatId, 'Aguarde enquanto baixamos o cardápio...') imgToSend = self.getMenu(campus) if imgToSend: bot.send_photo(chat_id=chatId, photo=imgToSend) print('Enviado cardápio para', Utils.getUsername(bot, update)) def isInDataBase(self, chat_id, period): try: query = "SELECT chat_id FROM users WHERE chat_id = '{}' AND period = '{}';".format(str(chat_id), period) users = self.databaseConnection.fetchAll(query) if len(users): return True else: return False except Exception as e: print("isInDataBase: "+str(e)+"\n") def subToPeriodicMenu(self, bot, update, callback_data):#, campus, period): try: callback_data=callback_data.split('/') period = callback_data[1] campus = callback_data[2] chat_id = Utils.getChatId(bot, update) username = Utils.getUsername(bot, update) if self.isInDataBase(chat_id, period): query = "UPDATE users SET campus = '{}', period = '{}', username = '******' WHERE chat_id = '{}';".format(campus, period, username, chat_id) else: query = "INSERT INTO users (chat_id, username, campus, period) VALUES ('{}', '{}', '{}', '{}');".format(str(chat_id), username, campus, period) self.databaseConnection.executeQuery(query) message = 'Cardápio ' + Utils.getPeriodFormated(period) + ' ativado para ' + Utils.getCampusFormated(campus) + '\nCardápio desta semana:' bot.send_message( chat_id=chat_id, text=message ) Utils.showStartMenuInExistingMsg(bot, update) print('Usuário', username, 'ativou o cardápio automático', period, 'para o campus', campus) self.showCardapio(bot, update, campus) except Exception as e: print("subToPeriodicMenu: "+str(e)+"\n") def unsubToPeriodicMenu(self, bot, update): try: chat_id = Utils.getChatId(bot, update) query = "DELETE FROM users WHERE chat_id = '{}';".format(chat_id) self.databaseConnection.executeQuery(query) bot.send_message( chat_id=chat_id, text='Cardápio automático desativado' ) Utils.showStartMenuInExistingMsg(bot, update) print('Usuário', Utils.getUsername(bot, update), 'desativou o cardápio automático') except Exception as e: print("unsubToPeriodicMenu: "+str(e)+"\n") def sendMenuToSubs(self, bot, period): try: query = "SELECT chat_id, username, campus FROM users WHERE period = '{}';".format(period) users = self.databaseConnection.fetchAll(query) for user in users: chat_id = user[0] username = user[1] campus = user[2] try: #Tenta enviar mensagem para o chat_id cadastrado if period == 'weekly': query = "SELECT imgUrl FROM images WHERE weekNumber = {} AND campus = '{}';".format(Utils.getWeekNumber(), campus) image = self.databaseConnection.fetchAll(query) bot.send_photo( chat_id=chat_id, photo=image[0][0] ) elif period == 'daily': bot.send_message( chat_id=chat_id, text=self.getDailyMenu(campus) ) print('Enviado', period, 'para', username) except Exception as e: print("sendMenuToUser: "******"\n") except Exception as e: print("sendMenuToSubs: "+str(e)+"\n") def getDailyMenu(self, campus): try: today = str(Utils.getWeekNumber()) + campus + str(date.today().weekday()) if today not in self.dailyMenus: query = "SELECT imgHtml FROM images WHERE weekNumber = {} AND campus = '{}';".format(Utils.getWeekNumber(), campus) image = self.databaseConnection.fetchAll(query) soup = BeautifulSoup(image[0][0], 'html.parser') column = '' for row in soup.findAll('table')[0].tbody.findAll('tr'): column = column + '\n' + row.findAll('td')[date.today().weekday()].p.text self.dailyMenus[today] = column return self.dailyMenus[today] except Exception as e: print("getDailyMenu: "+str(e)+"\n") def selectPeriod(self, bot, update): chat_id = update.callback_query.message.chat.id dailyButton = 'Diário' weeklyButton = 'Semanal' if self.isInDataBase(chat_id, 'daily'): dailyButton += ' ✔' if self.isInDataBase(chat_id, 'weekly'): weeklyButton += ' ✔' keyboard = [ [ telegram.InlineKeyboardButton(dailyButton, callback_data = 'daily'), telegram.InlineKeyboardButton(weeklyButton, callback_data = 'weekly') ], [ telegram.InlineKeyboardButton('Desativar cardapio automático', callback_data = 'unsub') ], [ telegram.InlineKeyboardButton('← Menu principal', callback_data = 'main-menu') ] ] replyMarkup = telegram.InlineKeyboardMarkup(keyboard) bot.editMessageText( message_id = update.callback_query.message.message_id, chat_id = chat_id, text = 'Selecione a periodicidade:', parse_mode = 'HTML', reply_markup = replyMarkup ) def getImages(self): try: listOfCampus = ['chapeco', 'cerro-largo', 'erechim', 'laranjeiras-do-sul', 'realeza'] for i in range(len(listOfCampus)): query = "SELECT * FROM images WHERE weekNumber = {} AND campus = '{}';".format(Utils.getWeekNumber(), listOfCampus[i]) image = self.databaseConnection.fetchAll(query) if len(image) == 0: self.getMenu(listOfCampus[i]) except Exception as e: print("getImages: "+str(e)+"\n") def sendMenuPeriodically(self, bot): try: # Download do cardápio toda segundas às 08h caso já não tenha sido baixado schedule.every().monday.at('08:00').do(self.getImages) #Todo dias as 09:00 manda o cardaio para os cadastrados schedule.every().monday.at('09:00').do(self.sendMenuToSubs, bot, "daily") schedule.every().tuesday.at('09:00').do(self.sendMenuToSubs, bot, "daily") schedule.every().wednesday.at('09:00').do(self.sendMenuToSubs, bot, "daily") schedule.every().thursday.at('09:00').do(self.sendMenuToSubs, bot, "daily") schedule.every().friday.at('09:00').do(self.sendMenuToSubs, bot, "daily") #Toda segunda as 09:00 manda o cardapio para os cadastrados schedule.every().monday.at('09:00').do(self.sendMenuToSubs, bot, "weekly") while True: schedule.run_pending() time.sleep(1) except Exception as e: print("sendMenuPeriodically: "+str(e)+"\n")
def __init__(self, app): db = DatabaseConnection(app) self.mysql = db.mysql
class RuBot: databaseConnection = DatabaseConnection.DatabaseConnection() dailyMenus = {} def findWeek(self, soup): paragraphs = soup.find_all('p') week = [] for p in paragraphs: if ('Semana' in p.text): week.append(p) return week def getMenu(self, campus): URL_MENU_RU_CCO = 'https://www.uffs.edu.br/campi/' + campus + '/restaurante_universitario' page = requests.get(URL_MENU_RU_CCO) soup = BeautifulSoup(page.text, 'html.parser') table = soup.find_all('table') if not table: return week = self.findWeek(soup) firstWeek = re.search(r'(\d+/\d+/\d+)', week[0].text).group(1).split('/') weekNumberToday = datetime.today().isocalendar()[1] weekNumberCalendar = date(int(firstWeek[2]), int(firstWeek[1]), int(firstWeek[0])).isocalendar()[1] if (weekNumberToday == weekNumberCalendar): html = str(week[0]) + str(table[0]) else: html = str(week[1]) + str(table[1]) img = requests.post('https://hcti.io/v1/image', data={'HTML': html}, auth=(htciId, htciKey)) imgUrl = img.text.split('"')[3] self.databaseConnection.executeQuery( sql.SQL( "INSERT INTO images (weekNumber, imgUrl, imgHtml, campus) VALUES (%s, %s, %s, %s)" ), [weekNumberToday, imgUrl, html, campus]) print('Inserido nova imagem ao banco\nweekNumber: ', Utils.getWeekNumber(), '\tCampus: ', campus) return imgUrl def selectCampus(self, bot, update): buttonsNameAndData = [['RU Chapecó', 'RU-chapeco'], ['RU Cerro Largo', 'RU-cerro-largo'], ['RU Erechim', 'RU-erechim'], ['RU Laranjeiras', 'RU-laranjeiras-do-sul'], ['RU Realeza', 'RU-realeza']] textToShow = '*Selecione o campus:*' numButtonsPerLine = 3 Utils.keyboardOptions(bot, update, buttonsNameAndData, textToShow, numButtonsPerLine, True) def selectCampusAuto(self, bot, update, period): buttonsNameAndData = [ ['Chapecó', 'AUTO/' + period + '/chapeco'], ['Cerro Largo', 'AUTO/' + period + '/cerro-largo'], ['Erechim', 'AUTO/' + period + '/erechim'], ['Laranjeiras', 'AUTO/' + period + '/laranjeiras-do-sul'], ['Realeza', 'AUTO/' + period + '/realeza'] ] textToShow = '*Selecione o campus:*' numButtonsPerLine = 3 Utils.keyboardOptions(bot, update, buttonsNameAndData, textToShow, numButtonsPerLine, True) def showCardapio(self, bot, update, campus): chatId = Utils.getChatId(bot, update) image = self.databaseConnection.fetchAll( sql.SQL( "SELECT imgUrl FROM images WHERE weekNumber = %s AND campus = %s;" ), [Utils.getWeekNumber(), campus]) if len(image): imgToSend = image[0][0] else: bot.sendMessage(chatId, 'Aguarde enquanto baixamos o cardápio...') imgToSend = self.getMenu(campus) if imgToSend: bot.send_photo(chat_id=chatId, photo=imgToSend) print('Enviado cardápio para', Utils.getUsername(bot, update)) else: bot.send_message( chat_id=chatId, text= "Desculpe-nos, o cardápio atualizado do RU ainda não foi publicado no site.\nTentaremos lhe enviar o cardápio novamente daqui 10 minutos.", parse_mode='Markdown') print("Tentando enviar cardápio novamente daqui a 15 min...") Timer(900.0, self.showCardapio, [bot, update, campus]).start() def isInDataBase(self, chat_id, period): try: users = self.databaseConnection.fetchAll( sql.SQL( "SELECT chat_id FROM users WHERE chat_id = %s AND period = %s;" ), [chat_id, period]) if len(users): return True else: return False except Exception as e: print("isInDataBase: " + str(e) + "\n") def subToPeriodicMenu(self, bot, update, callback_data): #, campus, period): try: callback_data = callback_data.split('/') period = callback_data[1] campus = callback_data[2] chat_id = Utils.getChatId(bot, update) username = Utils.getUsername(bot, update) if self.isInDataBase(chat_id, period): self.databaseConnection.executeQuery( sql.SQL( "UPDATE users SET campus = %s, period = %s, username = %s WHERE chat_id = %s AND period = %s;" ), [campus, period, username, chat_id, period]) else: self.databaseConnection.executeQuery( sql.SQL( "INSERT INTO users (chat_id, username, campus, period) VALUES (%s, %s, %s, %s)" ), [chat_id, username, campus, period]) message = 'Cardápio ' + Utils.getPeriodFormated( period) + ' ativado para ' + Utils.getCampusFormated( campus) + '\nCardápio desta semana:' bot.send_message(chat_id=chat_id, text=message) Utils.showStartMenuInExistingMsg(bot, update) print('Usuário', username, 'ativou o cardápio automático', period, 'para o campus', campus) self.showCardapio(bot, update, campus) except Exception as e: print("subToPeriodicMenu: " + str(e) + "\n") def unsubToPeriodicMenu(self, bot, update): try: chat_id = Utils.getChatId(bot, update) self.databaseConnection.executeQuery( sql.SQL("DELETE FROM users WHERE chat_id = %s;"), [chat_id]) bot.send_message(chat_id=chat_id, text='Cardápio automático desativado') Utils.showStartMenuInExistingMsg(bot, update) print('Usuário', Utils.getUsername(bot, update), 'desativou o cardápio automático') except Exception as e: print("unsubToPeriodicMenu: " + str(e) + "\n") def sendMenuToSubs(self, bot, period): results = self.databaseConnection.fetchAll( "SELECT value FROM status WHERE description = %s;", ['menuAvailable']) menuAvailable = results[0] if menuAvailable: try: users = self.databaseConnection.fetchAll( "SELECT chat_id, username, campus FROM users WHERE period = %s;", [period]) for user in users: chat_id = user[0] username = user[1] campus = user[2] try: # Tenta enviar mensagem para o chat_id cadastrado if period == 'weekly': image = self.databaseConnection.fetchAll( "SELECT imgUrl FROM images WHERE weekNumber = %s AND campus = %s;", [Utils.getWeekNumber(), campus]) try: msgSent = bot.send_photo(chat_id=chat_id, photo=image[0][0]) except telegram.error.ChatMigrated as chatMigratedError: print( 'Grupo mudou de id, atualizando informação no banco...' ) self.databaseConnection.executeQuery( sql.SQL( "UPDATE users SET chat_id = %s WHERE chat_id = %s;" ), [chatMigratedError.new_chat_id, chat_id]) chat_id = chatMigratedError.new_chat_id msgSent = bot.send_photo(chat_id=chat_id, photo=image[0][0]) try: bot.pin_chat_message( chat_id=chat_id, message_id=msgSent.message_id, disable_notification=None) except Exception as errPinMsg: print( 'O bot tentou fixar o cardápio porém não tem permissão para isso -> ', errPinMsg) elif period == 'daily': try: bot.send_message( chat_id=chat_id, text=self.getDailyMenu(campus), parse_mode='Markdown') except telegram.error.ChatMigrated as chatMigratedError: print( 'Grupo mudou de id, atualizando informação no banco...' ) self.databaseConnection.executeQuery( sql.SQL( "UPDATE users SET chat_id = %s WHERE chat_id = %s;" ), [chatMigratedError.new_chat_id, chat_id]) chat_id = chatMigratedError.new_chat_id bot.send_message( chat_id=chat_id, text=self.getDailyMenu(campus), parse_mode='Markdown') print('Enviado', period, 'para', username) except Exception as e: print("sendMenuToUser: "******"\n") except Exception as e: print("sendMenuToSubs: " + str(e) + "\n") else: print("Tentando enviar cardápio novamente daqui a 15 min...") Timer(900.0, self.sendMenuToSubs, [bot, period]).start() def getDailyMenu(self, campus): try: today = str(Utils.getWeekNumber()) + campus + str( date.today().weekday()) if today not in self.dailyMenus: image = self.databaseConnection.fetchAll( "SELECT imgHtml FROM images WHERE weekNumber = %s AND campus = %s;", [Utils.getWeekNumber(), campus]) soup = BeautifulSoup(image[0][0], 'html.parser') column = '*O cardápio de ' for i, row in enumerate( soup.findAll('table')[0].tbody.findAll('tr')): cell = row.findAll('td')[date.today().weekday()].findAll( 'p') text = '' for line in cell: text += line.text + ' ' text = text.strip() if (i == 0): column += text.lower() + ' será:*' continue column += '\n' + text self.dailyMenus[today] = column return self.dailyMenus[today] except Exception as e: print("getDailyMenu: " + str(e) + "\n") def selectPeriod(self, bot, update): chat_id = update.callback_query.message.chat.id dailyButton = 'Diário' weeklyButton = 'Semanal' if self.isInDataBase(chat_id, 'daily'): dailyButton += ' ✔' if self.isInDataBase(chat_id, 'weekly'): weeklyButton += ' ✔' buttonsNameAndData = [[dailyButton, 'daily'], [weeklyButton, 'weekly']] textToShow = '*Selecione a periodicidade:*' numButtonsPerLine = 2 Utils.keyboardOptions(bot, update, buttonsNameAndData, textToShow, numButtonsPerLine, True) def getImages(self): try: listOfCampus = [ 'chapeco', 'cerro-largo', 'erechim', 'laranjeiras-do-sul', 'realeza' ] self.databaseConnection.executeQuery( sql.SQL( "UPDATE status SET value = %s WHERE description = %s;"), [True, 'menuAvailable']) for campus in listOfCampus: image = self.databaseConnection.fetchAll( "SELECT * FROM images WHERE weekNumber = %s AND campus = %s;", [Utils.getWeekNumber(), campus]) if len(image) == 0: print('Tentando baixar o cardápio de', campus) if not self.getMenu(campus): print( "Tentando baixar cardápio novamente daqui a 10 min..." ) Timer(600.0, self.getImages).start() self.databaseConnection.executeQuery( sql.SQL( "UPDATE status SET value = %s WHERE description = %s;" ), [False, 'menuAvailable']) except Exception as e: print("getImages: " + str(e) + "\n") def sendMenuPeriodically(self, bot): try: # Download do cardápio toda segunda às 08h caso já não tenha sido baixado schedule.every().monday.at('08:00').do(self.getImages) # Todos os dias às 09:00 manda o cardápio para os cadastrados schedule.every().monday.at('09:00').do(self.sendMenuToSubs, bot, "daily") schedule.every().tuesday.at('09:00').do(self.sendMenuToSubs, bot, "daily") schedule.every().wednesday.at('09:00').do(self.sendMenuToSubs, bot, "daily") schedule.every().thursday.at('09:00').do(self.sendMenuToSubs, bot, "daily") schedule.every().friday.at('09:00').do(self.sendMenuToSubs, bot, "daily") # Toda segunda às 09:00 manda o cardápio para os cadastrados schedule.every().monday.at('09:00').do(self.sendMenuToSubs, bot, "weekly") while True: schedule.run_pending() time.sleep(1) except Exception as e: print("sendMenuPeriodically: " + str(e) + "\n")
import os from telegram.ext import Updater, CommandHandler, CallbackQueryHandler import telegram from Utils import Utils import RuBot, BusBot, CalendarBot, CanteenBot, EventsBot, DateBot, DatabaseConnection from conf.settings import telegramToken import threading ruBot = RuBot.RuBot() canteenBot = CanteenBot.CanteenBot() busBot = BusBot.BusBot() calendarBot = CalendarBot.CalendarBot() eventsBot = EventsBot.EventsBot() dateBot = DateBot.DateBot() databaseConnection = DatabaseConnection.DatabaseConnection() def callHandler(bot, update): if update.callback_query.data == 'menu-ru': ruBot.selectCampus(bot, update) elif update.callback_query.data[:2] == 'RU': ruBot.showCardapio(bot, update, update.callback_query.data[3:]) elif update.callback_query.data == 'unsub': ruBot.unsubToPeriodicMenu(bot, update) elif update.callback_query.data[:4] == 'AUTO': ruBot.subToPeriodicMenu(bot, update, update.callback_query.data) elif update.callback_query.data == 'auto-menu':
def getInfo(filepath): parentDir = os.path.dirname(filepath) dictRels = dict() docName = os.path.basename(filepath) relsFilePath = os.path.join(parentDir, "word", "_rels", "document.xml.rels") tree = ET.parse(relsFilePath) root = tree.getroot() for child in root: if 'image' in child.attrib['Type']: dictRels[child.attrib['Id']] = child.attrib['Target'] finalArray = [] docFilePath = os.path.join(parentDir, "word", "document.xml") tree = ET.parse(docFilePath) root = tree.getroot() paragraphsList = root.findall('.//w:p', namespaces) numParagraphs = len(paragraphsList) for paragraphIndex in range(0, numParagraphs): e = paragraphsList[paragraphIndex] if e.findall('.//w:drawing', namespaces) != []: for pic in e.findall( './/w:drawing/wp:anchor/a:graphic/a:graphicData/pic:pic', namespaces): for item in pic.findall('pic:blipFill/a:blip', namespaces): for key, value in item.attrib.items(): if 'embed' in key: image = value break newpath = os.path.join('.', 'word', dictRels[image]) dictNew = ImageSerialization.get_info_from_image(newpath) del dictNew['pixels'] saveImage(newpath, dictRels[image], docName) for item in pic.findall('.//pic:nvPicPr/pic:cNvPr', namespaces): if 'title' in item.attrib: dictNew['caption'] = item.attrib['title'] elif 'descr' in item.attrib: dictNew['caption'] = item.attrib['descr'] else: paragraphs = [ paragraph for paragraph in root.findall( ".//w:p", namespaces) if paragraph.get( '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidP' ) == e.get( '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidP' ) ] for paragraph in paragraphs: if paragraph.findall('.//w:t', namespaces) != []: dictNew['caption'] = getText(paragraph) break dictNew['text'] = "" if paragraphIndex > 0: prevParagraph = paragraphsList[paragraphIndex - 1] dictNew['text'] += getText(prevParagraph) if paragraphIndex < numParagraphs - 1: nextParagraph = paragraphsList[paragraphIndex + 1] dictNew['text'] += getText(nextParagraph) dictNew['position'] = str(paragraphIndex) + "/" + str( numParagraphs) dictNew['document'] = docName db = DatabaseConnection.DatabaseConnection() db.insert_entry(dictNew) finalArray.append(dictNew) for pic in e.findall( './/w:drawing/wp:inline/a:graphic/a:graphicData/pic:pic', namespaces): for item in pic.findall('.//pic:blipFill/a:blip', namespaces): for key, value in item.attrib.items(): if 'embed' in key: image = value break newpath = os.path.join('.', 'word', dictRels[image]) dictNew = ImageSerialization.get_info_from_image(newpath) del dictNew['pixels'] saveImage(newpath, dictRels[image], docName) for item in pic.findall('.//pic:nvPicPr/pic:cNvPr', namespaces): if 'title' in item.attrib: dictNew['caption'] = item.attrib['title'] elif 'descr' in item.attrib: dictNew['caption'] = item.attrib['descr'] else: paragraphs = [ paragraph for paragraph in root.findall( ".//w:p", namespaces) if paragraph.get( '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidP' ) == e.get( '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidP' ) ] for paragraph in paragraphs: if paragraph.findall('.//w:t', namespaces) != []: dictNew['caption'] = getText(paragraph) break dictNew['text'] = "" if paragraphIndex > 0: prevParagraph = paragraphsList[paragraphIndex - 1] dictNew['text'] += getText(prevParagraph) if paragraphIndex < numParagraphs - 1: nextParagraph = paragraphsList[paragraphIndex + 1] dictNew['text'] += getText(nextParagraph) dictNew['position'] = str(paragraphIndex) + "/" + str( numParagraphs) dictNew['document'] = docName db = DatabaseConnection.DatabaseConnection() db.insert_entry(dictNew) finalArray.append(dictNew) if e.findall('.//w:pict', namespaces) != []: for pic in e.findall('.//w:pict/v:shape/v:imagedata', namespaces): for key, value in pic.attrib.items(): if 'id' in key: image = value break newpath = os.path.join('.', 'word', dictRels[image]) dictNew = ImageSerialization.get_info_from_image(newpath) del dictNew['pixels'] dictNew = {} saveImage(newpath, dictRels[image], docName) if 'title' in pic.attrib: dictNew['caption'] = item.attrib['title'] elif 'descr' in pic.attrib: dictNew['caption'] = item.attrib['descr'] else: paragraphs = [ paragraph for paragraph in root.findall(".//w:p", namespaces) if paragraph.get( '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidP' ) == e.get( '{http://schemas.openxmlformats.org/wordprocessingml/2006/main}rsidP' ) ] for paragraph in paragraphs: if paragraph.findall('.//w:t', namespaces) != []: dictNew['caption'] = getText(paragraph) break dictNew['text'] = "" if paragraphIndex > 0: prevParagraph = paragraphsList[paragraphIndex - 1] dictNew['text'] += getText(prevParagraph) if paragraphIndex < numParagraphs - 1: nextParagraph = paragraphsList[paragraphIndex + 1] dictNew['text'] += getText(nextParagraph) dictNew['position'] = str(paragraphIndex) + "/" + str( numParagraphs) dictNew['document'] = docName dictNew['path'] = os.path.join( "result", docName[:-4] + os.path.basename(dictRels[image])) print(dictNew) db = DatabaseConnection.DatabaseConnection() db.insert_entry(dictNew) finalArray.append(dictNew) return finalArray