aboutsummaryrefslogtreecommitdiffstats
path: root/pages/api
diff options
context:
space:
mode:
authorGravatar piotrruss <mail@pruss.it> 2021-09-12 23:11:25 +0200
committerGravatar piotrruss <mail@pruss.it> 2021-09-12 23:11:25 +0200
commit16dab011c575eaf96630cab406ec2d8086403d0b (patch)
treeaf53af45b8cb52317cef3d4d59216b1c58d8d8ff /pages/api
parentd79f4c0bf3dae76eaae0d36469f5b279272d6944 (diff)
downloadmy_apps-16dab011c575eaf96630cab406ec2d8086403d0b.tar.gz
my_apps-16dab011c575eaf96630cab406ec2d8086403d0b.tar.bz2
my_apps-16dab011c575eaf96630cab406ec2d8086403d0b.zip
added youtube & player apps
Diffstat (limited to 'pages/api')
-rw-r--r--pages/api/youtube/player.js32
-rw-r--r--pages/api/youtube/search.js26
2 files changed, 58 insertions, 0 deletions
diff --git a/pages/api/youtube/player.js b/pages/api/youtube/player.js
new file mode 100644
index 0000000..a6d3a7e
--- /dev/null
+++ b/pages/api/youtube/player.js
@@ -0,0 +1,32 @@
+import withSession from 'hocs/withSession'
+import ytdl from 'ytdl-core'
+
+const getId = url => {
+ const regExp = /^.*((youtu.be\/)|(v\/)|(\/u\/\w\/)|(embed\/)|(watch\?))\??v?=?([^#&?]*).*/
+ const match = url.match(regExp)
+ return (match && match[7].length === 11) ? match[7] : false
+}
+
+export default withSession(async (req, res) => {
+ switch (req.method) {
+ case 'POST':
+ try {
+ const user = req.session.get('user')
+ const { url } = req.body
+
+ if (!user || !user?.isVerified || !url) {
+ throw new Error('Something went wrong')
+ }
+
+ const info = await ytdl.getInfo(getId(url))
+
+ res.status(200).json(info)
+ } catch (error) {
+ res.status(400).json([])
+ }
+ break
+ default:
+ res.status(400).send()
+ break
+ }
+})
diff --git a/pages/api/youtube/search.js b/pages/api/youtube/search.js
new file mode 100644
index 0000000..b17d008
--- /dev/null
+++ b/pages/api/youtube/search.js
@@ -0,0 +1,26 @@
+import withSession from 'hocs/withSession'
+import youtube from 'scrape-youtube'
+
+export default withSession(async (req, res) => {
+ switch (req.method) {
+ case 'POST':
+ try {
+ const user = req.session.get('user')
+ const { quote } = req.body
+
+ if (!user || !user?.isVerified || !quote) {
+ throw new Error('Something went wrong')
+ }
+
+ const video = await youtube.search(quote)
+
+ res.status(200).json(video)
+ } catch (error) {
+ res.status(400).json([])
+ }
+ break
+ default:
+ res.status(400).send()
+ break
+ }
+})