stream/src/hooks/data.js

77 lines
1.9 KiB
JavaScript
Raw Normal View History

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'
2021-03-29 16:03:46 +00:00
import ICAL from 'ical.js'
2021-03-11 20:24:44 +00:00
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-29 16:03:46 +00:00
const jCalData = ICAL.parse(responseData)
const comp = new ICAL.Component(jCalData)
const vevents = comp.getAllSubcomponents('vevent')
const calEvents = vevents
.map(vevent => new ICAL.Event(vevent))
.sort((a, b) => a.startDate.toJSDate() - b.startDate.toJSDate())
2021-03-24 15:24:34 +00:00
await Promise.all(
2021-03-29 16:03:46 +00:00
calEvents.map(async calItem => {
const url = calItem.component.getAllProperties('url')[0]
if (url) {
console.log('url', url)
const id = url
.getFirstValue()
.split('/')
.pop()
2021-03-24 15:24:34 +00:00
const {
data: {
account,
category,
channel,
embedPath,
language,
state,
previewPath,
views,
duration,
},
} = await axios.get(`https://tv.undersco.re/api/v1/videos/${id}`)
const item = {
title: calItem.summary,
account,
category,
channel,
description: calItem.description,
embedPath,
language,
state,
previewPath,
views,
2021-03-29 16:03:46 +00:00
start: calItem.startDate.toJSDate(),
end: calItem.endDate.toJSDate(),
2021-03-24 15:24:34 +00:00
id,
duration,
2021-03-29 16:03:46 +00:00
videoUrl: url.getFirstValue(),
2021-03-24 15:24:34 +00:00
}
setData(arr => [...arr, item])
}
})
)
2021-03-11 20:24:44 +00:00
setLoading(false)
}
useEffect(() => {
fetchData()
}, [])
return { loading, data }
}