不同金融市場的新聞篩選(增加股票代號複數搜尋,新聞時間範圍搜尋)
// index.vue
<label for="news" class="font-bold text-[18px]">股票代號搜尋</label>
<input
id="news"
type="text"
placeholder="格式(代號 代號)"
class="w-[100%] mb-4 mt-1 p-1 bg-white border border-solid border-black rounded shadow :active:border-white text-[14px]"
v-model="stockToNews"
@keydown.native.enter="chooseNews"
/>
<script setup>
// 股票代號搜尋新聞
const stockToNews = ref('')
const checkStockToNews = computed(() => {
const stockToNewsArr = stockToNews.value
.split(' ')
.map((v) => `&tickers=${v}`)
const stockToNewsString = stockToNewsArr.join(',')
return stockToNewsString || ''
})
const newsApi = computed(
() =>
`https://www.alphavantage.co/query?function=NEWS_SENTIMENT&topics=${checkedNewsString.value}${checkStockToNews.value}&time_from=${timeStartStr.value}&time_to=${timeEndStr.value}&apikey=${alpha}`
)
</script>
要搜尋股票的話api要加上&tickers=股票
但如果要同時搜尋多支股票必須是&tickers=股票1&tickers=股票2
所以input輸入時先把股票用空格隔開
再用split把股票組成array後
用map都加上&tickers=${v}
再組成string
一般搜尋
股票代號搜尋後
<div>
<label class="font-bold text-[18px]">新聞發佈時間</label>
<label for="start" class="block text-[14px] font-bold mt-1">從</label>
<input type="date" id="start" v-model="timeStart" class="text-[14px] w-[100%] mb-4 mt-1 p-1 bg-white border border-solid border-black rounded shadow">
<label for="end" class="block text-[14px] font-bold">到</label>
<input type="date" id="end" v-model="timeEnd" class="text-[14px] w-[100%] mb-4 mt-1 p-1 bg-white border border-solid border-black rounded shadow">
</div>
<script setup>
// 日期搜尋新聞
const timeStart = ref('')
const timeEnd = ref('')
const timeStartStr = computed(()=>{
timeStart.value= timeStart.value?timeStart.value.replace(/-/g, '') + 'T0000':''
return timeStart.value
})
const timeEndStr = computed(()=>{
timeEnd.value= timeEnd.value?timeEnd.value.replace(/-/g, '') + 'T2399':''
return timeEnd.value
})
</script>
這邊api採用 YYYYMMDDTHHMM
格式
但input是YYYY-MM-DD
所以先用replace把所有 - 清除掉後
再加上THHMM
小結:
明天應該會回到highChart的深入操作
還有好多沒有玩到的啊
祝大家中秋愉快!!
月圓人團圓但人不要變圓!!