============= 과제용 보고서에 작성했던거 ================
이 밑으로는 주어지지 않았던 구독자 수 데이터가 나온다. youtube data api를 사용하였다.
어떻게 되는지 설명하면 우선 api key를 발급받고 url 에서 "https://www.youtube.com/watch?v=t-9WoZnDo10" v= 뒤에있는 걸 복사하고 밑의 api 설명처럼 형식에 맞춰 get을 요청한다.
Videos: list | YouTube Data API | Google Developers
Videos: list | YouTube Data API | Google Developers
Videos: list API 요청 매개변수와 일치하는 동영상의 목록을 반환합니다. 지금 사용해 보거나 예를 참조하세요. 요청 HTTP 요청 GET https://www.googleapis.com/youtube/v3/videos 매개변수 아래 표는 이 쿼리가 지
developers.google.com

그럼 그 영상의 채널id를 가져올 수 있다. 이 채널id도 밑의 설명처럼 넣으면 해당 채널의 구독자 수, 총 조회수, 총 비디오 수를 가져올 수 있다. 구독자 수는 채널 관리자가 임의로 안보이게 할 수도 있기 때문에 가져 올 수 없으면 None으로 저장하였다.
Channels: list | YouTube Data API | Google Developers
Channels: list | YouTube Data API | Google Developers
Channels: list 요청 기준과 일치하는 0개 이상의 channel 리소스 집합을 반환합니다. 지금 사용해 보세요. 요청 HTTP 요청 GET https://www.googleapis.com/youtube/v3/channels 매개변수 아래 표는 이 쿼리가 지원하는
developers.google.com

============= 과제용 보고서에 작성했던거 ================
====코드(Python)===
import requests
def get_subscriberCount_totalViewCount(file_name,key='personal_api_key'):
'''
youtube data api를 사용해서 비디오 url을 받아 해당 비디오 채널의 구독자 수와 총 재생수를 불러옵니다.
:param file_name: 'https://www.youtube.com/watch?v=t-9WoZnDo10' 중 v=의 뒷부분
:param key: google cloud platform 의 api
:return: 해당 비디오 채널의 구독자 수, 해당 채널의 총 비디오 재생 수
'''
try:
video_url = 'https://www.googleapis.com/youtube/v3/videos'
video_params = {
'part': 'snippet',
'id': file_name,
'key': key
}
video_r = requests.get(video_url, video_params)
video_data = video_r.json()
if not video_data['items']: return None, None
channel_url = 'https://www.googleapis.com/youtube/v3/channels'
channel_params = {
'part': 'statistics',
'id': video_data['items'][0]['snippet']['channelId'],
'key': key
}
channel_r = requests.get(channel_url, channel_params)
channel_data = channel_r.json()
return (channel_data['items'][0]['statistics']['subscriberCount'] if 'subscriberCount' in channel_data['items'][0]['statistics'] else None
, channel_data['items'][0]['statistics']['viewCount'])
except:
print(f"{file_name} 에서 에러남.")
return None,None
==========
'기술 > 기타' 카테고리의 다른 글
CORS 두 장 설명 (0) | 2025.03.05 |
---|---|
Multi-task learning (0) | 2022.01.05 |
공통적으로 자주쓰는 colab 명령어들 모음 (0) | 2021.08.28 |