|
@@ -8,6 +8,8 @@ from GoogleNews import GoogleNews
|
|
import dataset
|
|
import dataset
|
|
import datetime
|
|
import datetime
|
|
from fastapi.responses import HTMLResponse
|
|
from fastapi.responses import HTMLResponse
|
|
|
|
+import matplotlib.dates as mdates
|
|
|
|
+import matplotlib.pyplot as plt
|
|
#
|
|
#
|
|
|
|
|
|
|
|
|
|
@@ -80,8 +82,6 @@ def get_idea():
|
|
return result
|
|
return result
|
|
|
|
|
|
|
|
|
|
-
|
|
|
|
-
|
|
|
|
def get_kw_ls(past=False):
|
|
def get_kw_ls(past=False):
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/yodb?charset=utf8mb4')
|
|
db = dataset.connect('mysql://choozmo:pAssw0rd@db.ptt.cx:3306/yodb?charset=utf8mb4')
|
|
now = ''
|
|
now = ''
|
|
@@ -142,6 +142,29 @@ async def callback(request: fastapi.Request):
|
|
handler.handle(body.decode('utf-8'), signature)
|
|
handler.handle(body.decode('utf-8'), signature)
|
|
return 'OK'
|
|
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):
|
|
def get_news_by_kw(keyword):
|
|
googlenews = GoogleNews(lang='zh-TW')
|
|
googlenews = GoogleNews(lang='zh-TW')
|
|
@@ -169,19 +192,8 @@ def flex_test():
|
|
row_dict['type'] = 'box'
|
|
row_dict['type'] = 'box'
|
|
row_dict['layout'] = 'baseline'
|
|
row_dict['layout'] = 'baseline'
|
|
row_dict['contents']= [
|
|
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]
|
|
js['body']['contents'] = js['body']['contents'] + [row_dict]
|
|
return js
|
|
return js
|