def update_sessions_list(sessions_list): sender = "VK authorization" PATH = datamanager.read_path() loads_json = datamanager.read_json(PATH + "bot_notificator/", "data") sessions_list["admin_session"] =\ get_session(loads_json["admin_token"]) message = "Session of admin has been succesfully created." logger.message_output(sender, message) sessions_list["bot_session"] =\ get_session(loads_json["bot_token"]) message = "Session of bot has been succesfully created." logger.message_output(sender, message) return sessions_list
def update_data(entries_list): PATH = datamanager.read_path() fields_data = datamanager.read_json(PATH + "wall_monitor/", "data") fields_data["access_token"] = entries_list["entry_token"].get_text() fields_data["public_url"] = entries_list["entry_public_url"].get_text() fields_data["date"]["day"] = entries_list["entry_date_day"].get_text() fields_data["date"]["month"] = entries_list["entry_date_month"].get_text() fields_data["date"]["year"] = entries_list["entry_date_year"].get_text() fields_data["post_count"] = entries_list["entry_posts_count"].get_text() fields_data["evaluator_template"] = entries_list[ "evaluator_template"].get_text() datamanager.write_json(PATH + "wall_monitor/", "data", fields_data)
def load_data_for_checker(checker_name, subject, operation_name): PATH = datamanager.read_path() path_to_json = subject["path"] if len(path_to_json) > 0 and path_to_json[len(path_to_json) - 1] != "/": path_to_json = PATH + "bot_notificator/" + path_to_json + "/" else: path_to_json = PATH + "bot_notificator/" + path_to_json subject_section_data = datamanager.read_json(path_to_json, checker_name) subject_data = datamanager.read_json(path_to_json, "subject_data") sender = subject["name"] message = "Starting an operation " + operation_name + "." logger.message_output(sender, message) return PATH, path_to_json, subject_data, subject_section_data
def weather_data(): PATH = datamanager.read_path() request_data = datamanager.read_json(PATH + "weather/", "request_data") url = request_data["url"] headers = request_data["headers"] widget_selector = request_data["widget_selector"] html_text = tools.load_html(url, headers) html_list = bs4.BeautifulSoup(html_text.text, "html.parser") icon_selector = widget_selector + " " + request_data["icon_selector"] tag_name = request_data["icon_titles"]["tag_name"] attr_title = request_data["icon_titles"]["attr_title"] icon_title_list = tools.load_icon_titles(icon_selector, html_list, tag_name, attr_title) time_selector = widget_selector + " " + request_data["time_selector"] tag_name = request_data["time"]["tag_name"] time_list = tools.load_text(time_selector, html_list, tag_name) warm_selector = widget_selector + " " + request_data["warm_selector"] tag_name = request_data["warm"]["tag_name"] warm_list = tools.load_text(warm_selector, html_list, tag_name) time_indexes = tools.get_list_time(time_list) icon_titles = tools.get_list_icon_titles(icon_title_list, time_indexes) warm_intervals = tools.get_list_warm(warm_list, time_indexes) morning = { "icon_title": icon_titles["morning_icon_title"], "warm": warm_intervals["morning_warm_interval"] } day = { "icon_title": icon_titles["day_icon_title"], "warm": warm_intervals["day_warm_interval"] } night = { "icon_title": icon_titles["night_icon_title"], "warm": warm_intervals["night_warm_interval"] } weather_data = {"morning": morning, "day": day, "night": night} return weather_data
def logger_changer(objLabelLogger): last_len = 0 while True: PATH = datamanager.read_path() log_text = datamanager.read_text(PATH + "bot_notificator/", "log") if len(log_text) > last_len: log_text = log_text.split('\n') text = "" i = 0 for line in log_text: text += line if i < 99: text += "\n" i += 1 if i == 100: break objLabelLogger.set_text(text) last_len = len(text) time.sleep(1)
def to_textfile(sender, message): try: PATH = datamanager.read_path() text = datamanager.read_text(PATH + "bot_notificator/", "log") date = datetime.datetime.now().strftime("%d.%m.%Y %H:%M:%S") message = "[" + str(date) + "] " + "[" + str(sender) + "]: " + str( message.encode("utf8")) if len(text) > 2: text = message + "\n" + text else: text += message datamanager.write_text(PATH + "bot_notificator/", "log", text) except Exception as var_except: sender += " -> Message output -> To text file" print("[" + sender + "]: Error, " + str(var_except) + ". Exit from program...") exit(0)
def reach_collector(vk_admin_session, owner_id, str_date, template_filedata): def get_posts(sender, vk_admin_session, owner_id): sender += " -> Get posts" values = { "owner_id": owner_id, "count": 100, "filter": "all" } response = vk_admin_session.method("wall.get", values) return response["items"] def selection_post(sender, str_date, posts): sender += " -> Selection post" UNIX_MINUTE = 60 date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(str_date, '%d-%m-%Y'))) unix_date = int(time.mktime(date.timetuple())) begin_of_day = unix_date - UNIX_MINUTE * 10 end_of_day = begin_of_day + UNIX_MINUTE * 60 * 24 posts_per_day = [] # i = len(posts) - 1 i = 0 while i < len(posts): if posts[i]["date"] >= begin_of_day and\ posts[i]["date"] < end_of_day: posts_per_day.append(copy.deepcopy(posts[i])) i += 1 return posts_per_day def get_reach(sender, vk_admin_session, owner_id, post_id): sender += " -> Get reach" values = { "owner_id": owner_id, "post_id": post_id } response = vk_admin_session.method("stats.getPostReach", values) return response[0] def sort_by_time(sender, posts_reach, template, str_date): sender += " -> Sort by time" log = copy.deepcopy(template) UNIX_MINUTE = 60 i = 0 while i < len(log): str_time_begin = log[str(i)]["time"][:5] str_date_begin = str_date + " " + str_time_begin date_begin = datetime.datetime.fromtimestamp(time.mktime(time.strptime(str_date_begin, '%d-%m-%Y %H:%M'))) unix_date_begin = int(time.mktime(date_begin.timetuple())) str_time_end = log[str(i)]["time"][6:] str_date_end = str_date + " " + str_time_end date_end = datetime.datetime.fromtimestamp(time.mktime(time.strptime(str_date_end, '%d-%m-%Y %H:%M'))) unix_date_end = int(time.mktime(date_end.timetuple())) begin_of_interval = unix_date_begin - UNIX_MINUTE * 10 end_of_interval = unix_date_end - UNIX_MINUTE * 10 if str_time_end == "00:00": end_of_interval += UNIX_MINUTE * 60 * 24 count = 0 reach = 0 j = 0 while j < len(posts_reach): if posts_reach[j]["date"] >= begin_of_interval and\ posts_reach[j]["date"] < end_of_interval: count += 1 reach += posts_reach[j]["reach"] j += 1 if count > 1: reach = reach / count reach = int(round(float(reach) / 100)) * 100 log[str(i)]["value"] = reach i += 1 return log def create_log(sender, str_date, log): date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(str_date, '%d-%m-%Y'))) weekday = date.isoweekday() day = int(datetime.datetime.strftime(date, '%d')) month = int(datetime.datetime.strftime(date, '%m')) loads_json = { "day_number": day, "month_number": month, "day_week": weekday, "coverage": log } return loads_json def set_filename(sender, str_date): sender += " -> Set file name" date = datetime.datetime.fromtimestamp(time.mktime(time.strptime(str_date, '%d-%m-%Y'))) name_weekday = [ "mon", "tue", "wed", "thu", "fri", "sat", "sun" ] name_month = [ "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" ] weekday = date.isoweekday() day = int(datetime.datetime.strftime(date, '%d')) month = int(datetime.datetime.strftime(date, '%m')) file_name = "log_" + str(day) + "-" +\ name_month[month - 1] + "-" +\ name_weekday[weekday - 1] return file_name sender = "Main" PATH = datamanager.read_path() posts = get_posts(sender, vk_admin_session, owner_id) posts_per_day = selection_post(sender, str_date, posts) posts_reach = [] i = len(posts_per_day) - 1 while i >= 0: owner_id = posts_per_day[i]["owner_id"] post_id = posts_per_day[i]["id"] reach = copy.deepcopy(get_reach(sender, vk_admin_session, owner_id, post_id)) date = posts_per_day[i]["date"] post = { "reach": reach["reach_subscribers"], "date": date } posts_reach.append(copy.deepcopy(post)) i -= 1 template_path = template_filedata["path"] template_filename = template_filedata["name"] template = datamanager.read_json(template_path, template_filename) log = copy.deepcopy(sort_by_time(sender, posts_reach, template, str_date)) file_name = set_filename(sender, str_date) loads_json = create_log(sender, str_date, log) datamanager.write_json(PATH + "wall_monitor/reach_data/", file_name, loads_json)
def creator_HBoxWallMonitorWorkplace(): def create_enter_data_frame(fields_data): entries_list = {} objLabelToken = gtk.Label("Admin token") objLabelToken.set_usize(150, 25) objEntryToken = gtk.Entry() objEntryToken.set_text(fields_data["access_token"]) entries_list.update({"entry_token": objEntryToken}) objHBoxToken = gtk.HBox() objHBoxToken.pack_start(objLabelToken, expand=False, fill=False, padding=0) objHBoxToken.pack_start(objEntryToken, expand=True, fill=True, padding=0) objLabelDate = gtk.Label("Date") objLabelDate.set_usize(150, 25) objEntryDateDay = gtk.Entry() objEntryDateDay.set_text(fields_data["date"]["day"]) objEntryDateDay.set_usize(50, 30) objEntryDateMonth = gtk.Entry() objEntryDateMonth.set_text(fields_data["date"]["month"]) objEntryDateMonth.set_usize(50, 30) objEntryDateYear = gtk.Entry() objEntryDateYear.set_text(fields_data["date"]["year"]) objEntryDateYear.set_usize(75, 30) entries_list.update({"entry_date_day": objEntryDateDay}) entries_list.update({"entry_date_month": objEntryDateMonth}) entries_list.update({"entry_date_year": objEntryDateYear}) objHBoxEntriesDate = gtk.HBox() objHBoxEntriesDate.pack_start(objEntryDateDay, expand=False, fill=False, padding=0) objHBoxEntriesDate.pack_start(objEntryDateMonth, expand=False, fill=False, padding=0) objHBoxEntriesDate.pack_start(objEntryDateYear, expand=False, fill=False, padding=0) objHBoxDate = gtk.HBox() objHBoxDate.pack_start(objLabelDate, expand=False, fill=False, padding=0) objHBoxDate.pack_start(objHBoxEntriesDate, expand=False, fill=False, padding=0) objLabelPublicURL = gtk.Label("Public URL") objLabelPublicURL.set_usize(150, 25) objEntryPublicURL = gtk.Entry() objEntryPublicURL.set_text(fields_data["public_url"]) objEntryPublicURL.set_usize(300, 30) entries_list.update({"entry_public_url": objEntryPublicURL}) objHBoxPublicURL = gtk.HBox() objHBoxPublicURL.pack_start(objLabelPublicURL, expand=False, fill=False, padding=0) objHBoxPublicURL.pack_start(objEntryPublicURL, expand=False, fill=False, padding=0) objLabelPostsCount = gtk.Label("Posts count") objLabelPostsCount.set_usize(150, 25) objEntryPostsCount = gtk.Entry() objEntryPostsCount.set_text(fields_data["post_count"]) objEntryPostsCount.set_usize(60, 30) entries_list.update({"entry_posts_count": objEntryPostsCount}) objHBoxPostCount = gtk.HBox() objHBoxPostCount.pack_start(objLabelPostsCount, expand=False, fill=False, padding=0) objHBoxPostCount.pack_start(objEntryPostsCount, expand=False, fill=False, padding=0) dialog = gtk.FileChooserDialog(title="Select evaluator template", action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) dict_template_chooser = { "dialog": dialog, "RESPONSE_OK": gtk.RESPONSE_OK, "STOCK_OPEN": gtk.STOCK_OPEN, "RESPONSE_CANCEL": gtk.RESPONSE_CANCEL, "STOCK_CANCEL": gtk.STOCK_CANCEL, } entries_list.update({"template_chooser": dict_template_chooser}) objButtonTemplateChooser = gtk.Button("Select template") objButtonTemplateChooser.set_usize(150, 30) objButtonTemplateChooser.connect("clicked", buttons_listener.select_template, entries_list) objEntryTemplateChooser = gtk.Entry() objEntryTemplateChooser.set_text(fields_data["evaluator_template"]) entries_list.update({"evaluator_template": objEntryTemplateChooser}) objHBoxTemplateChooser = gtk.HBox() objHBoxTemplateChooser.pack_start(objButtonTemplateChooser, expand=False, fill=False, padding=0) objHBoxTemplateChooser.pack_start(objEntryTemplateChooser, expand=True, fill=True, padding=0) objFrameReackEvaluatorData = gtk.Frame() objFrameReackEvaluatorData.set_label("Reach evaluator data") objFrameReackEvaluatorData.set_label_align(0.0, 0.0) objFrameReackEvaluatorData.add(objHBoxTemplateChooser) objTable = gtk.Table(5, 1, False) objTable.attach(objHBoxToken, 0, 1, 0, 1) objTable.attach(objHBoxDate, 0, 1, 1, 2) objTable.attach(objHBoxPublicURL, 0, 1, 2, 3) objTable.attach(objHBoxPostCount, 0, 1, 3, 4) objTable.attach(objFrameReackEvaluatorData, 0, 1, 4, 5) objFrame = gtk.Frame() objFrame.set_label("Enter data") objFrame.set_label_align(0.0, 0.0) objFrame.add(objTable) return objFrame, entries_list def create_result_data_frame(): entries_list = {} objLabelAllPosts = gtk.Label("All posts") objLabelAllPosts.set_usize(150, 25) objEntryAllPostsCount = gtk.Entry() objEntryAllPostsCount.set_usize(60, 30) objEntryAllPostsCount.set_sensitive(False) entries_list.update({"result_all_posts": objEntryAllPostsCount}) objHBoxAllPosts = gtk.HBox() objHBoxAllPosts.pack_start(objLabelAllPosts, expand=False, fill=False, padding=0) objHBoxAllPosts.pack_start(objEntryAllPostsCount, expand=False, fill=False, padding=0) objLabelAdvertisingPosts = gtk.Label("Advertising posts") objLabelAdvertisingPosts.set_usize(150, 25) objEntryAdvertisingPostsCount = gtk.Entry() objEntryAdvertisingPostsCount.set_usize(60, 30) objEntryAdvertisingPostsCount.set_sensitive(False) entries_list.update({"result_advertising_posts": objEntryAdvertisingPostsCount}) objHBoxAdPosts = gtk.HBox() objHBoxAdPosts.pack_start(objLabelAdvertisingPosts, expand=False, fill=False, padding=0) objHBoxAdPosts.pack_start(objEntryAdvertisingPostsCount, expand=False, fill=False, padding=0) objLabelFirstAdPostURL = gtk.Label() objLabelFirstAdPostURL.set_usize(150, 25) objLabelSecondAdPostURL = gtk.Label() objLabelSecondAdPostURL.set_usize(150, 25) objLabelThirdAdPostURL = gtk.Label() objLabelThirdAdPostURL.set_usize(150, 25) objLabelFourthAdPostURL = gtk.Label() objLabelFourthAdPostURL.set_usize(150, 25) objLabelFifthAdPostURL = gtk.Label() objLabelFifthAdPostURL.set_usize(150, 25) objEntryFirstAdPostURL = gtk.Entry() objEntryFirstAdPostURL.set_usize(235, 30) objEntrySecondAdPostURL = gtk.Entry() objEntrySecondAdPostURL.set_usize(235, 30) objEntryThirdAdPostURL = gtk.Entry() objEntryThirdAdPostURL.set_usize(235, 30) objEntryFourthAdPostURL = gtk.Entry() objEntryFourthAdPostURL.set_usize(235, 30) objEntryFifthAdPostURL = gtk.Entry() objEntryFifthAdPostURL.set_usize(235, 30) objEntryFirstAdPostTime = gtk.Entry() objEntryFirstAdPostTime.set_usize(65, 30) objEntryFirstAdPostTime.set_sensitive(False) objEntrySecondAdPostTime = gtk.Entry() objEntrySecondAdPostTime.set_usize(65, 30) objEntrySecondAdPostTime.set_sensitive(False) objEntryThirdAdPostTime = gtk.Entry() objEntryThirdAdPostTime.set_usize(65, 30) objEntryThirdAdPostTime.set_sensitive(False) objEntryFourthAdPostTime = gtk.Entry() objEntryFourthAdPostTime.set_usize(65, 30) objEntryFourthAdPostTime.set_sensitive(False) objEntryFifthAdPostTime = gtk.Entry() objEntryFifthAdPostTime.set_usize(65, 30) objEntryFifthAdPostTime.set_sensitive(False) objHBoxFirstAdPostURL = gtk.HBox() objHBoxFirstAdPostURL.pack_start(objLabelFirstAdPostURL, expand=False, fill=False, padding=0) objHBoxFirstAdPostURL.pack_start(objEntryFirstAdPostURL, expand=False, fill=False, padding=0) objHBoxFirstAdPostURL.pack_start(objEntryFirstAdPostTime, expand=False, fill=False, padding=0) objHBoxSecondAdPostURL = gtk.HBox() objHBoxSecondAdPostURL.pack_start(objLabelSecondAdPostURL, expand=False, fill=False, padding=0) objHBoxSecondAdPostURL.pack_start(objEntrySecondAdPostURL, expand=False, fill=False, padding=0) objHBoxSecondAdPostURL.pack_start(objEntrySecondAdPostTime, expand=False, fill=False, padding=0) objHBoxThirdAdPostURL = gtk.HBox() objHBoxThirdAdPostURL.pack_start(objLabelThirdAdPostURL, expand=False, fill=False, padding=0) objHBoxThirdAdPostURL.pack_start(objEntryThirdAdPostURL, expand=False, fill=False, padding=0) objHBoxThirdAdPostURL.pack_start(objEntryThirdAdPostTime, expand=False, fill=False, padding=0) objHBoxFourthAdPostURL = gtk.HBox() objHBoxFourthAdPostURL.pack_start(objLabelFourthAdPostURL, expand=False, fill=False, padding=0) objHBoxFourthAdPostURL.pack_start(objEntryFourthAdPostURL, expand=False, fill=False, padding=0) objHBoxFourthAdPostURL.pack_start(objEntryFourthAdPostTime, expand=False, fill=False, padding=0) objHBoxFifthAdPostURL = gtk.HBox() objHBoxFifthAdPostURL.pack_start(objLabelFifthAdPostURL, expand=False, fill=False, padding=0) objHBoxFifthAdPostURL.pack_start(objEntryFifthAdPostURL, expand=False, fill=False, padding=0) objHBoxFifthAdPostURL.pack_start(objEntryFifthAdPostTime, expand=False, fill=False, padding=0) entries_ad_post_url = [ objEntryFirstAdPostURL, objEntrySecondAdPostURL, objEntryThirdAdPostURL, objEntryFourthAdPostURL, objEntryFifthAdPostURL ] entries_list.update({"entries_ad_post_url": entries_ad_post_url}) entries_ad_post_time = [ objEntryFirstAdPostTime, objEntrySecondAdPostTime, objEntryThirdAdPostTime, objEntryFourthAdPostTime, objEntryFifthAdPostTime ] entries_list.update({"entries_ad_post_time": entries_ad_post_time}) objVBoxAdPostsURL = gtk.VBox() objVBoxAdPostsURL.pack_start(objHBoxFirstAdPostURL, expand=False, fill=False, padding=0) objVBoxAdPostsURL.pack_start(objHBoxSecondAdPostURL, expand=False, fill=False, padding=0) objVBoxAdPostsURL.pack_start(objHBoxThirdAdPostURL, expand=False, fill=False, padding=0) objVBoxAdPostsURL.pack_start(objHBoxFourthAdPostURL, expand=False, fill=False, padding=0) objVBoxAdPostsURL.pack_start(objHBoxFifthAdPostURL, expand=False, fill=False, padding=0) objTable = gtk.Table(2, 3, False) objTable.attach(objHBoxAllPosts, 0, 1, 0, 1) objTable.attach(objHBoxAdPosts, 0, 1, 1, 2) objTable.attach(objVBoxAdPostsURL, 0, 2, 2, 3) objFrame = gtk.Frame() objFrame.set_label("Result data") objFrame.set_label_align(0.0, 0.0) objFrame.add(objTable) return objFrame, entries_list objVBoxMain = gtk.VBox() entries_list = {} PATH = datamanager.read_path() fields_data = datamanager.read_json(PATH + "wall_monitor/", "data") objFrameEnterData, enter_entries_list =\ create_enter_data_frame(fields_data) objFrameResultData, result_entries_list = create_result_data_frame() objVBoxCalculateData = gtk.VBox() objVBoxCalculateData.add(objFrameEnterData) objVBoxCalculateData.add(objFrameResultData) objVBoxCalculateData.show_all() entries_list.update(enter_entries_list) entries_list.update(result_entries_list) objButtonCalculate = gtk.Button("Calculate posts") objButtonCalculate.set_border_width(10) objButtonCalculate.connect("clicked", buttons_listener.calculate_posts, entries_list) objButtonCollectReach = gtk.Button("Collect reach") objButtonCollectReach.set_border_width(10) objButtonCollectReach.connect("clicked", buttons_listener.collect_reach, entries_list) objButtonEvaluateReach = gtk.Button("Evaluate reach") objButtonEvaluateReach.set_border_width(10) objButtonEvaluateReach.connect("clicked", buttons_listener.evaluate_reach, entries_list) objButtonBox = gtk.HButtonBox() objButtonBox.set_layout(gtk.BUTTONBOX_CENTER) objButtonBox.add(objButtonCalculate) objButtonBox.add(objButtonCollectReach) objButtonBox.add(objButtonEvaluateReach) objButtonBox.show_all() objHBoxEmptySpace = gtk.HBox() objHBoxEmptySpace.show_all() objLabelStatusLine = gtk.Label() objLabelStatusLine.set_selectable(True) objLabelStatusLine.set_alignment(0.0, 1.0) entries_list.update({"status_line": objLabelStatusLine}) objFrameStatusLine = gtk.Frame() objFrameStatusLine.add(objLabelStatusLine) objFrameStatusLine.set_usize(100, 25) objFrameStatusLine.show_all() objVBoxMain.pack_start(objVBoxCalculateData, expand=False, fill=False, padding=0) objVBoxMain.pack_start(objButtonBox, expand=False, fill=False, padding=10) objVBoxMain.pack_start(objHBoxEmptySpace, expand=True, fill=True, padding=0) objVBoxMain.pack_start(objFrameStatusLine, expand=False, fill=False, padding=0) return objVBoxMain
def collect_files(template_filedata): def sum_coverage(template_filedata, list_logs): list_result_sum = [] day_week = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"] template_path = template_filedata["path"] template_filename = template_filedata["name"] template_json = datamanager.read_json(template_path, template_filename) list_result_sum = { "mon": copy.deepcopy(template_json), "tue": copy.deepcopy(template_json), "wed": copy.deepcopy(template_json), "thu": copy.deepcopy(template_json), "fri": copy.deepcopy(template_json), "sat": copy.deepcopy(template_json), "sun": copy.deepcopy(template_json) } i = 0 while i < len(day_week): count = 0 j = 0 while j < len(list_logs): if list_logs[j].get_day_week() == i + 1: if len(list_logs[j].get_coverage()) ==\ len(list_result_sum[day_week[i]]): coverage = list_logs[j].get_coverage() count += 1 n = 0 while n < len(list_result_sum[day_week[i]]): dw = day_week[i] # because many symbols sn = str(n) # because many symbols next ln list_result_sum[dw][sn]["value"] +=\ coverage[sn]["value"] coverage[sn]["value"] = 0 n += 1 j += 1 m = 0 while m < len(list_result_sum[day_week[i]]): dw = day_week[i] # because many symbols sm = str(m) # because many symbols in next line coverage = list_result_sum[dw][sm]["value"] coverage = int(coverage) / int(count) coverage = float(coverage / 100) coverage = round(coverage) coverage = coverage * 100 list_result_sum[dw][sm]["value"] = int(coverage) m += 1 i += 1 evaluate_coverage(list_result_sum) def algorithm_evaluate(status, coverage): n = 0 while n < len(status): sn = str(n) # because many symbols value = 100 m = 0 while m < len(coverage): sm = str(m) # because many symbols if value < coverage[sm]["value"]: value = coverage[sm]["value"] m += 1 status[sn]["value"] = value m = 0 while m < len(coverage): sm = str(m) # because many symbols if value == coverage[sm]["value"]: status[sn]["time"].append(coverage[sm]["time"]) coverage[sm]["value"] = 0 m += 1 n += 1 return status def evaluate_coverage(list_result_sum): evaluate_status = ["best", "good", "norm"] list_evaluate = { "best": { "0": { "time": [], "value": 0 }, "1": { "time": [], "value": 0 }, "2": { "time": [], "value": 0 } }, "good": { "0": { "time": [], "value": 0 }, "1": { "time": [], "value": 0 }, "2": { "time": [], "value": 0 } }, "norm": { "0": { "time": [], "value": 0 }, "1": { "time": [], "value": 0 }, "2": { "time": [], "value": 0 } } } day_week = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"] list_result_evaluate = { "mon": copy.deepcopy(list_evaluate), "tue": copy.deepcopy(list_evaluate), "wed": copy.deepcopy(list_evaluate), "thu": copy.deepcopy(list_evaluate), "fri": copy.deepcopy(list_evaluate), "sat": copy.deepcopy(list_evaluate), "sun": copy.deepcopy(list_evaluate) } i = 0 while i < len(day_week): dw = day_week[i] coverage = copy.deepcopy(list_result_sum[day_week[i]]) j = 0 while j < len(evaluate_status): es = evaluate_status[j] status = copy.deepcopy(list_result_evaluate[dw][es]) status = algorithm_evaluate(status, coverage) j += 1 list_result_evaluate[dw][es] = copy.deepcopy(status) i += 1 print_evaluate(list_result_evaluate) def print_evaluate(list_result_evaluate): day_week = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"] day_week_ru = [ "Понедельник", "Вторник", "Среда", "Четверг", "Пятница", "Суббота", "Воскресенье" ] evaluate_status = ["best", "good", "norm"] evaluate_status_ru = [ "Отличное время", "Хорошее время", "Нормальное время" ] text_output = "" i = 0 while i < len(day_week): text_output += "\n=== " + str(day_week_ru[i]) + " ===\n" time_range = 1 j = 0 while j < len(list_result_evaluate[day_week[i]]): dw = day_week[i] es = evaluate_status[j] status = list_result_evaluate[dw][es] n = 0 while n < len(status): time_output = "" k = 0 while k < len(status[str(n)]["time"]): if k == len(status[str(n)]["time"]) - 1: time_output += status[str(n)]["time"][k] else: time_output += status[str(n)]["time"][k] +\ ", " k += 1 text_output += "\n" text_output += str(time_range) + ") " text_output += str(time_output) + " (" text_output += str(status[str(n)]["value"]) + ")" time_range += 1 n += 1 j += 1 text_output += "\n" i += 1 export_evaluate(text_output) def export_evaluate(text_output): file_name = "evaluate_each_days" datamanager.write_text(PATH + "wall_monitor/output/", file_name, text_output) PATH = datamanager.read_path() list_files = os.listdir(PATH + "wall_monitor/reach_data/") list_logs = [] i = 0 while i < len(list_files): pos = list_files[i].find(".json") loads_json = datamanager.read_json(PATH + "wall_monitor/reach_data/", list_files[i][0:pos]) obj_day = Day() obj_day.set_day_number(loads_json["day_number"]) obj_day.set_month_number(loads_json["month_number"]) obj_day.set_day_week(loads_json["day_week"]) obj_day.set_coverage(loads_json["coverage"]) list_logs.append(obj_day) i += 1 sum_coverage(template_filedata, list_logs)
def logo_integrate(image_filenames, path_to_images): def get_logo_image(text, font_path, image_size): def get_image(text, font_data): font_size = font_data["size"] font_path = font_data["path"] image_font = ImageFont.truetype(font_path, font_size) height_image = sum(image_font.getmetrics()) width_image = image_font.getsize(text)[0] objImage = Image.new('L', (width_image, height_image)) ImageDraw.Draw(objImage).text((0, 0), text, fill=100, font=image_font) objImage = objImage.rotate(45, resample=Image.BICUBIC, expand=True) return objImage width_target = image_size["width"] height_target = image_size["height"] if int(width_target) > int(height_target): font_size = int(0.012 * int(width_target)) + 2 else: font_size = int(0.020 * int(height_target)) + 2 font_data = { "size": font_size, "path": font_path } objLogoImage = get_image(text, font_data) return objLogoImage PATH = datamanager.read_path() logo_data = datamanager.read_json(PATH + "logo_integrator/", "data") text_logo = logo_data["text"] font_path = logo_data["font_path"] for i in range(len(image_filenames)): image_filename = image_filenames[i] objTargetImage = Image.open(path_to_images + image_filename) (width_image, height_image) = objTargetImage.size image_size = { "width": width_image, "height": height_image } if int(width_image) > int(height_image): width_interval = int(width_image * 0.03) height_interval = int(height_image * 0.12) else: width_interval = int(width_image * 0.03) height_interval = int(height_image * 0.12) objLogoImage = get_logo_image(text_logo, font_path, image_size) (width_logo, height_logo) = objLogoImage.size x = width_interval while x < int(width_image): y = int(height_interval / 4) while y < int(height_image): objTargetImage.paste((255, 255, 255), (x, y), objLogoImage) y += int(height_logo) + height_interval x += int(width_logo) + width_interval if os.path.exists(path_to_images + "output") is False: os.mkdir(path_to_images + "output") objTargetImage.save(path_to_images + "output/watermarked_" + image_filename)
def read_bot_notificator_path(): PATH = datamanager.read_path() folder_storage_path = PATH + "bot_notificator/" return folder_storage_path
def read_local_path(): PATH = datamanager.read_path() LOCAL_PATH = PATH + "AdminAssistant_storage/" return LOCAL_PATH
def check_folder_storage(): PATH = datamanager.read_path() if not os.path.exists(PATH + "AdminAssistant_storage/"): os.mkdir(PATH + "AdminAssistant_storage")