2021-03-24 15:24:34 +00:00
|
|
|
import { useEffect, useState, useRef } from 'preact/hooks'
|
2021-03-11 20:24:44 +00:00
|
|
|
import axios from 'axios'
|
|
|
|
import ical from 'ical'
|
|
|
|
import config from '../data/config'
|
|
|
|
|
2021-03-24 15:24:34 +00:00
|
|
|
export const useEventStream = () => {
|
|
|
|
const [data, setData] = useState([])
|
2021-03-11 20:24:44 +00:00
|
|
|
const [loading, setLoading] = useState(true)
|
|
|
|
|
|
|
|
async function fetchData() {
|
|
|
|
setLoading(true)
|
|
|
|
|
|
|
|
const { data: responseData } = await axios.get(`${config.calendar}`)
|
2021-03-24 15:24:34 +00:00
|
|
|
const calItems = Object.values(ical.parseICS(responseData))
|
2021-03-11 20:24:44 +00:00
|
|
|
.filter(feedItem => feedItem.type === 'VEVENT')
|
|
|
|
.sort((a, b) => new Date(a.start) - new Date(b.start))
|
2021-03-24 15:24:34 +00:00
|
|
|
|
|
|
|
await Promise.all(
|
|
|
|
calItems.map(async calItem => {
|
|
|
|
if (calItem.url) {
|
|
|
|
const id = calItem.url.val.split('/').pop()
|
|
|
|
const {
|
|
|
|
data: {
|
|
|
|
account,
|
|
|
|
category,
|
|
|
|
channel,
|
|
|
|
embedPath,
|
|
|
|
language,
|
|
|
|
state,
|
|
|
|
previewPath,
|
|
|
|
views,
|
|
|
|
duration,
|
|
|
|
},
|
|
|
|
data: nesd,
|
|
|
|
} = await axios.get(`https://tv.undersco.re/api/v1/videos/${id}`)
|
|
|
|
|
|
|
|
console.log({ nesd })
|
|
|
|
|
|
|
|
const item = {
|
|
|
|
title: calItem.summary,
|
|
|
|
account,
|
|
|
|
category,
|
|
|
|
channel,
|
|
|
|
description: calItem.description,
|
|
|
|
embedPath,
|
|
|
|
language,
|
|
|
|
state,
|
|
|
|
previewPath,
|
|
|
|
views,
|
|
|
|
start: calItem.start,
|
|
|
|
end: calItem.end,
|
|
|
|
id,
|
|
|
|
duration,
|
|
|
|
videoUrl: calItem?.url?.val,
|
|
|
|
}
|
|
|
|
setData(arr => [...arr, item])
|
|
|
|
}
|
|
|
|
})
|
|
|
|
)
|
|
|
|
|
2021-03-11 20:24:44 +00:00
|
|
|
setLoading(false)
|
|
|
|
}
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
fetchData()
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
return { loading, data }
|
|
|
|
}
|