빅데이터 김교수의 "AI노마드연구소" AI로 열어가는 노마드 세상!

빅데이터 김교수의 "AI노마드연구소" AI로 열어가는 노마드 세상입니다. AI 코딩작성, SNS 분석, AI업무자동화 컨설팅 0507-1419-0222

자세히보기

교육/인공지능교육

노아 AI 주요기능 구현 코드_ 좋아요, 구독자, 게시일 확인

빅데이터 김교수 2023. 2. 9. 21:18

안녕하세요 구독자님^^

 

유튜브에 방송해드린 내용에 문의가 많으셔서, 코드를 공유해드립니다. 

 

하지만, 여러분이 코드를 옮기는 것보다 챗지피티와 소통하는 방법을 아시는 것이 중요하고, 기본기인 파이썬과 코랩은 기초를 가지셔야 정확히 이해하실 수 있습니다. 그래야 정확한 의사 소통이 되신다는 말씀입니다^^

 

 이하 코드는 다음 단계의 프롬프트의 대화 결과 입니다. 

 

 프롬프트 

 

1. 유튜브 API를 사용하여 사용자의 검색 입력을 받아, 받아진 검색 쿼리를 YouTube API로 전송하고 검색 결과 중 50개 이상의 동영상 제목, 주소, 조회 수, 좋아요 수를 구하는 파이썬 프로그램을 작성해줘.  


2. 위의 코드를 CSV 파일로 저장하는 코드를 만들어줘

3. 위에코드에 채널별로 구독자수를 각각 추가하는 파이썬 코드를 만들어줘

4. 엑셀... 파일...생성
 
 

파이썬 코드

YOUR_API_KEY = "여러분의 유튜브 코드" 
import os
import pandas as pd
from googleapiclient.discovery import build

# API Key
API_KEY = YOUR_API_KEY

# Define the search query
query = input('Enter the search query: ')

# Build the YouTube API client
youtube = build("youtube", "v3", developerKey=API_KEY)

# Call the search.list method to retrieve the search results
search_response = youtube.search().list(
    q=query,
    type="video",
    part="id,snippet",
    maxResults=50
).execute()

# Get the video details for each video in the search results
video_details = []
for video in search_response["items"]:
    video_id = video["id"]["videoId"]
    video_response = youtube.videos().list(
        id=video_id,
        part="statistics,snippet"
    ).execute()
    channel_response = youtube.channels().list(
        id=video_response["items"][0]["snippet"]["channelId"],
        part="statistics"
    ).execute()
    video_details.append({
        "title": video["snippet"]["title"],
        "url": f"https://www.youtube.com/watch?v={video_id}",
        "view_count": video_response["items"][0]["statistics"]["viewCount"],
        "like_count": video_response["items"][0]["statistics"].get("likeCount",0),
        "subscriber_count": channel_response["items"][0]["statistics"]["subscriberCount"],
        "published_at": video_response["items"][0]["snippet"]["publishedAt"],
    })

# Create a pandas DataFrame from the video details
df = pd.DataFrame(video_details)

# Save the results to a CSV file
df.to_csv(query+"_results.csv", encoding='UTF-8-sig', index=False)

 화이팅하시기 바랍니다^^