ソースを参照

add dynamic title

ming 3 年 前
コミット
caed0810e7
1 ファイル変更12 行追加16 行削除
  1. 12 16
      newbot.py

+ 12 - 16
newbot.py

@@ -82,17 +82,15 @@ def get_idea():
 
 
 
-def get_hotkeys_ls(past=False):
+def get_kw_ls(past=False):
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/yodb?charset=utf8mb4')
     now = ''
     sql_get_newest_data_date = 'SELECT * FROM trending_searches ORDER BY ts_date DESC'
     for row in db.query(sql_get_newest_data_date):
         now = row['ts_date']
         break
-
     if past:
         now =now-datetime.timedelta(days=1)
-
     now_start = now.strftime("%Y-%m-%d 00:00:00")
     now_end = now.strftime("%Y-%m-%d 23:59:59")
     sqls = 'SELECT * FROM trending_searches WHERE "'+now_end+'">= ts_date and "'+now_start+'"<=ts_date ORDER BY ts_date DESC'
@@ -102,21 +100,19 @@ def get_hotkeys_ls(past=False):
     ls = list(dict.fromkeys(ls))
     return ls
 
-def get_hotkeys(past=False):
-    js=json.load(open('test.json','r',encoding='utf-8'))
-    keys = get_hotkeys_ls(past)
+def get_kw(past=False):
+    keys = get_kw_ls(past)
+    title = '熱門搜尋關鍵字' if past == True else '歷史關鍵字走勢'
 
     carousel_dict = {"type": "carousel","contents": []}
     carousel_idx = -1
     num = 1
     for k in keys:
         if (num-1)%12 ==0:
-            carousel_dict['contents'] = carousel_dict['contents']+[make_bubble()]
+            carousel_dict['contents'] = carousel_dict['contents']+[make_bubble(title)]
             carousel_idx+=1
-       
-        carousel_dict['contents'][carousel_idx]['body']['contents'] += [make_box(num,k)] 
+        carousel_dict['contents'][carousel_idx]['body']['contents'] += [make_box(num,k,title)] 
         num+=1
-
     return carousel_dict
 
 
@@ -189,7 +185,7 @@ def flex_test():
             ]
         js['body']['contents'] = js['body']['contents'] + [row_dict]
     return js
-def make_bubble():
+def make_bubble(title):
     box_dict = {
       "type": "bubble",
       "hero": {
@@ -206,7 +202,7 @@ def make_bubble():
         "contents": [
           {
             "type": "text",
-            "text": "熱門搜尋關鍵字",
+            "text": title,
             "size": "xl",
             "weight": "bold",
             "align": "center"
@@ -287,11 +283,11 @@ def message_text(event):
         return
     ############################
     if event.message.text == 'hotkeys':
-        js = get_hotkeys(False)
+        js = get_kw(False)
         line_bot_api.reply_message(event.reply_token, FlexSendMessage('ChoozMo',js))
         return
     if event.message.text == 'past_hotkeys':
-        js = get_hotkeys(True)
+        js = get_kw(True)
         line_bot_api.reply_message(event.reply_token, FlexSendMessage('ChoozMo',js))
         return
 
@@ -311,12 +307,12 @@ def message_text(event):
             
 
     if event.message.text=='熱門關鍵字':
-        js = get_hotkeys(False)
+        js = get_kw(False)
         line_bot_api.reply_message(event.reply_token, FlexSendMessage('ChoozMo',js))
         return
             
     if event.message.text=='關鍵字歷史走勢':
-        js = get_hotkeys(True)
+        js = get_kw(True)
         line_bot_api.reply_message(event.reply_token, FlexSendMessage('ChoozMo',js))
         return