def index(): log_control.logging.debug("/.index: Started.") pagetitle = "Kindleセール通知 管理画面" text = "Kindleセール通知 管理画面" item_list_msg = "セール通知済みアイテム" # 登録済みアイテムを格納する変数を用意 item_list = "" # SQLite実行用のインスタンスを生成 sql = SqlQuery() # セール通知済みのアイテムリストを取得 try: sale_noticed_result = sql.sale_noticed_item() except: raise for id,asin,title,price,discount_rate,flag in sale_noticed_result: # ASIN番号からWebページURLを生成 url = base_url + asin + "/" item_list = item_list + "<tr><td><a href=" + url + ">" + title + "</a></td>" \ "<td>" + str(price) + "</td>" \ "<td>" + str(discount_rate) + "</td></tr>\n" return render_template('index.html', text=text, pagetitle=pagetitle, item_list_msg=item_list_msg, item_list=item_list)
def register_exec(): log_control.logging.debug("/register_exec: Started.") pagetitle = "Kindleセール通知 管理画面" text = "Kindleセール通知 管理画面" registered_msg = "以下のアイテムを登録完了しました。" regist_err_msg = "以下のアイテムの登録に失敗しました。" # 登録完了したアイテム/失敗したアイテムの格納先を作成 registered_item = "" regist_err_item = "" if request.method == 'POST': # POSTされた値を取得 title_list = request.form.getlist('title[]') asin_list = request.form.getlist('asin[]') discount_rate_list = request.form.getlist('discount_rate[]') # SQLite実行用のインスタンスを生成 sql = SqlQuery() for (title, asin, discount_rate) in zip(title_list, asin_list, discount_rate_list): # POST値のdiscount_rateに有効な値がセットされているアイテムのみ、新規登録実行 if len(str(discount_rate)) > 0: sql_result = sql.initial_regist(title, asin, discount_rate) log_control.logging.debug("initial_result sql result:\n" + str(sql_result)) #if len(str(sql_result)) > 0: if sql_result is not None: regist_err_item = regist_err_item + "<li>" + title + "<br>" + str(sql_result) +"</li>\n" else: registered_item = registered_item + "<li>" + title + "</li>\n" return render_template('index.html', text=text, pagetitle=pagetitle, registered_msg=registered_msg, regist_err_msg=regist_err_msg, registered_item=registered_item, regist_err_item=regist_err_item) else: # POST以外で遷移してきた時はIndexページにリダイレクト return redirect(url_for('index'))
def __init__(self): try: self.base_url = ci.base_url self.log_dir = ci.log_dir self.currency_symbol = ci.currency_symbol self.today = datetime.date.today() except: err = "Read config failed.\n" log_control.logging.error(err + traceback.format_exc()) raise # Creating a SQL instance self.sql = SqlQuery()
# -*- coding: utf-8 -*- import log_control from mail_send import MailSend from sale_check import SaleCheck from sqlite_query import SqlQuery from get_price import GetPrice # セール検出時の報告メール用の変数を作成 sale_msg = "" # エラーメッセージ格納用のリストを作成 err_msg = [] # SQLite3実行用のインスタンスを生成 sql = SqlQuery() # クエリを発行し、DBに登録済みのアイテムを取得 try: all_item = sql.select_all_item() except: err_msg.append("sql.select_all_item failed.") if 'all_item' in locals(): # 価格取得処理のインスタンスを生成 gp = GetPrice() # セール発生判定処理のインスタンスを生成 sc = SaleCheck()
class SaleCheck: def __init__(self): try: self.base_url = ci.base_url self.log_dir = ci.log_dir self.currency_symbol = ci.currency_symbol self.today = datetime.date.today() except: err = "Read config failed.\n" log_control.logging.error(err + traceback.format_exc()) raise # Creating a SQL instance self.sql = SqlQuery() def sale_check(self, id, asin, title, price, price_today, discount_rate): log_control.logging.debug("Started.") # Initializing variables item_logfile = "" sale_check_result = "" url = "" discount_today = "" # Constructing item-price logfile item_logfile = self.log_dir + asin + ".csv" if price is not None: # Calculating today's discount rate discount_today = round((((price - price_today) / price) * 100)) f = open(item_logfile, mode="a", encoding="utf-8") f.write(str(self.today) + "," + str(price) + "," + str(discount_today) + "\n") f.close() # Deciding whether "today's discount rate" is greater than or equal "threshold rate" if discount_today >= discount_rate: url = self.base_url + asin + "/" # Constructing mail report text line1 = title + " ("+ asin +")\n" line2 = "登録価格: ¥" + str(price) + "\n" line3 = "本日価格: ¥" + str(price_today) + "\n" line4 = "値引率: " + str(discount_today) + "%\n" line5 = url + " \n" line6 = "\n" sale_check_result = line1+line2+line3+line4+line5+line6 log_control.logging.debug(asin + title + " sale has detected. result:\n" + sale_check_result) # Execute query to activate "This item has reported" flag try: self.sql.update_flag(id) return sale_check_result except: log_control.logging.error(asin + title + " update_flag failed.") raise else: # If "today's discount rate" is less than "threshold rate", return with nothing log_control.logging.debug(asin + title + " sale has not detected.") return sale_check_result # DBから取得した価格(price)の中身が空の場合は"今回が初回実行"と判断し # 本日の価格を当該アイテムのpriceカラムにInsertして処理を終える else: # UPDATEのクエリを発行 try: self.sql.update_price(id, price_today) return sale_check_result except: raise