Skip to content

Commit

Permalink
add channel with @handle
Browse files Browse the repository at this point in the history
  • Loading branch information
kenu committed May 16, 2024
1 parent a7ce97a commit bd3bb09
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 24 deletions.
24 changes: 24 additions & 0 deletions cron/cron-channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,25 @@ async function getChannelInfo(channelId) {
}
}

async function findChannelInfo(forHandle) {
try {
const response = await youtube.channels.list({
forHandle,
part: 'id,snippet,contentDetails',
})
const item = response.data.items[0]
const data = {
title: item.snippet.title,
customUrl: item.snippet.customUrl,
channelId: item.id,
thumbnail: item.snippet.thumbnails.medium.url,
}
return data
} catch (error) {
console.error('Error:', error)
}
}

async function processChannels() {
const channelList = await dao.findAllEmpty()
for (const channel of channelList) {
Expand All @@ -32,3 +51,8 @@ async function processChannels() {
}

processChannels()

export default {
getChannelInfo,
findChannelInfo,
}
27 changes: 5 additions & 22 deletions tests/ch.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,20 @@
import youtube from './youtub.js'
import dao from '../youtubeDao.js'
import channels from '../channels.js'

async function getChannelInfo(channelId) {
try {
const response = await youtube.channels.list({
id: channelId,
part: 'snippet,contentDetails', // 필요한 정보를 지정합니다.
})
const items = response.data.items[0]
const data = {
title: items.snippet.title,
customUrl: items.snippet.customUrl,
thumbnail: items.snippet.thumbnails.high.url,
channelId: items.id,
}
return data
} catch (error) {
console.error('Error:', error)
}
}
import cr from '../cron/cron-channel.js'

// 채널 ID를 입력하여 실행합니다.
channels.dev[0].forEach(async (channelId) => {
const data = await getChannelInfo(channelId)
const data = await cr.getChannelInfo(channelId)
data.lang = 'ko'
data.category = 'dev'
dao.create(data)
})
// 채널 ID를 입력하여 실행합니다.
channels.dev[1].forEach(async (channelId) => {
const data = await getChannelInfo(channelId)
const data = await cr.getChannelInfo(channelId)
data.lang = 'en'
data.category = 'dev'
dao.create(data)
})

cr.findChannelInfo('@kenuheo')
14 changes: 13 additions & 1 deletion web/routes/admin.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import express from 'express'
import dao from '../../youtubeDao.js'
import yapi from '../../cron/cron-channel.js'
import dayjs from 'dayjs'
import passport from 'passport'
import util from '../utils/uri.js'
Expand Down Expand Up @@ -56,8 +57,19 @@ router.get('/admin/channel', async function (req, res, next) {
user: req.user,
})
})

router.post('/api/channel', async function (req, res, next) {
const channel = req.body
const channelId = req.body.channelId
let channel;
if (channelId.indexOf('@') === 0) {
channel = await yapi.findChannelInfo(channelId)
} else {
channel = await yapi.getChannelInfo(channelId)
}
channel = {
...req.body,
...channel,
}
const result = await dao.create(channel)
res.json(result.dataValues)
})
Expand Down
2 changes: 1 addition & 1 deletion web/views/admin/channel.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<a href="/admin">비디오</a>
</nav>
<section>
<input type="text" id="channelId" name="channelId" placeholder="channel id" value="">
<input type="text" id="channelId" name="channelId" placeholder="channel id or @handle" value="">
<select id="lang" name="lang" value="ko">
<option value="ko">dev ko</option>
<option value="en">dev en</option>
Expand Down

0 comments on commit bd3bb09

Please sign in to comment.