SMALL
์ด๋ฐ ์์ผ๋ก api ๋ฅผ ์ฐ๋๊ฒ ๋ง๋ ์ง ๋ชจ๋ฅด๊ฒ ์ผ๋..
์๋ง api - axios.get ์ ์ธํ ๋ถ๋ถ์ ๋ฐ๋ก ๋นผ๋ ๊ฒ ์ข์ ๊ฒ ๊ฐ๊ธด ํ๋ค.
...api.js
import axios from 'axios';
import request from './request';
// ๋ฉ์ธํ์ด์ง BodySlider - ์ ํ๋ฒ ์์ ์ฌ๋ผ์ด๋์ ์ฌ์ฉ๋ youtube data api
export const mainSliderDataClient = axios.create({
baseURL: import.meta.env.VITE_APP_YOUTUBE_BASE_URL_GET,
headers: {
'Content-Type': 'application/json'
},
responseType: 'json'
});
// input: ์์id output: ์์์ธ๋ค์ผ์ด๋ฏธ์งurl ๋ฐ ํด๋น ์ฑ๋ ์ ๋ณด๊ฐ ๋ด๊ธด ๊ฐ์ฒด
export const getVideoChannelDatabyId = async (videoId) => {
// ์์id ํ๋ฒ๋ง ๋ฐ์์ ์ฑ๋id๊น์ง ์ฐพ์ ์ ์์ผ๋ ํ๋ฒ์ ์ฑ๋id๋ก ์ฐ๊ฒฐ์์ผ์ ์ฑ๋idํตํด ์ ํ๋ฒ(์ฑ๋)๋ช
, ๊ตฌ๋
์์ ๋ฑ ๊ฐ์ฒด๋ก ์ ๋ฌํด์ฃผ๊ธฐ
try {
const videoResponse = await mainSliderDataClient.get(`${request.getVideoSnippet}&id=${videoId}`);
// console.log(videoResponse.data);
const snippet = videoResponse.data.items[0].snippet;
const channelId = snippet.channelId; // ์์ idํตํด ์ฑ๋ id ์ป๊ธฐ
const channelTitle = snippet.channelTitle; // ์ฑ๋๋ช
(์ ํ๋ฒ๋ช
)
const thumbnailUrl = snippet.thumbnails.standard.url; // ์์ ์ธ๋ค์ผ์ด๋ฏธ์ง url
// ์์ ๋ฐ์ ์ฑ๋ id ์ฌ์ฉ
const channelResponse = await mainSliderDataClient.get(`${request.getChannelStatistics}&id=${channelId}`);
// console.log(channelResponse.data);
const statistics = channelResponse.data.items[0].statistics;
const initSubscriberCount = statistics.subscriberCount; // ์ฑ๋ ๊ตฌ๋
์์
const videoCount = statistics.videoCount; // ์ฑ๋ ์ด ์์์
const viewCount = statistics.viewCount; // ์ฑ๋ ์ด ์กฐํ์(๋ชจ๋ ์์ ์กฐํ์์ ํฉ)
const initAverageViewCount = viewCount / videoCount; // (์ผ๋ฐ์ ์ธ) ์ฑ๋ ํ๊ท ์กฐํ์ (์ด ์กฐํ์ / ์ด ์์ ์)
const subscriberCount =
initSubscriberCount > 10000
? Math.round(initSubscriberCount / 10000) + '๋ง'
: Math.round((initSubscriberCount / 1000) * 10) / 10 + '์ฒ';
const averageViewCount =
initAverageViewCount > 10000
? Math.round(initAverageViewCount / 10000) + '๋ง'
: Math.round((initAverageViewCount / 1000) * 10) / 10 + '์ฒ';
return { channelTitle, thumbnailUrl, subscriberCount, averageViewCount };
// ์ฑ๋๋ช
(์ ํ๋ฒ๋ช
), ์์์ธ๋ค์ผ์ด๋ฏธ์งurl, ์ฑ๋๊ตฌ๋
์์(๋ง ๋จ์), ์ฑ๋ํ๊ท ์กฐํ์(๋ง ๋จ์)
} catch (error) {
console.error('failed to get data by function getVideoChannelDatabyId', error.message);
}
};
SMALL