Ver Fonte

plot image init

ming há 3 anos atrás
pai
commit
cdeba170b1
1 ficheiros alterados com 27 adições e 15 exclusões
  1. 27 15
      newbot.py

+ 27 - 15
newbot.py

@@ -8,6 +8,8 @@ from GoogleNews import GoogleNews
 import dataset
 import datetime
 from fastapi.responses import HTMLResponse
+import matplotlib.dates as mdates
+import matplotlib.pyplot as plt
 #
 
 
@@ -80,8 +82,6 @@ def get_idea():
     return result
 
 
-
-
 def get_kw_ls(past=False):
     db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/yodb?charset=utf8mb4')
     now = ''
@@ -142,6 +142,29 @@ async def callback(request: fastapi.Request):
     handler.handle(body.decode('utf-8'), signature)
     return 'OK'
 
+@app.get('/get_trend_image')
+async def get_trend_image(kw):
+    db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/yodb?charset=utf8mb4')
+    sql_get_newest_data_date = 'SELECT * FROM interest_over_time WHERE iot_kword="'+kw+'" ORDER BY iot_date'
+    x_axis = []
+    y_axis = []
+    for row in db.query(sql_get_newest_data_date):
+        x_axis += [row['iot_date']]
+        y_axis += [row['iot_value']]
+        
+    #fomat example
+    #x = ['7/15', '7/18', '7/19', '7/24', '7/25', '7/26']
+    #y = [1, 5, 2, 7, 9, 1]
+
+    plt.gca().xaxis.set_major_formatter(mdates.DateFormatter('%Y/%m/%d'))
+    plt.gca().xaxis.set_major_locator(mdates.DayLocator(interval=3)) #座標軸刻度1天
+
+    target_path = 'trend_image/'+kw+".png"
+    plt.plot(x, y,'r-^') # 設定樣式
+    plt.savefig(target_path, bbox_inches='tight') #存檔,第二個參數表示把圖表外多餘的空間刪除
+    
+    return target_path
+
 
 def get_news_by_kw(keyword):
     googlenews = GoogleNews(lang='zh-TW')
@@ -169,19 +192,8 @@ def flex_test():
         row_dict['type'] = 'box'
         row_dict['layout'] = 'baseline'
         row_dict['contents']= [
-            {
-                "type": "text",
-                "text": "第"+str(i)+"名",
-                "size": "sm",
-                "color": "#aaaaaa",
-                "flex": 2
-            },
-            {
-                "type": "text",
-                "text": "hello, world"+str(1),
-                "flex": 5,
-                "weight": "regular"
-            }
+            {"type": "text","text": "第"+str(i)+"名","size": "sm","color": "#aaaaaa","flex": 2},
+            {"type": "text","text": "hello, world"+str(1),"flex": 5,"weight": "regular"}
             ]
         js['body']['contents'] = js['body']['contents'] + [row_dict]
     return js