728x90
100분동안 댓글이 없을시 100만원!
10분동안 댓글이 없을시 1000만원!
자 이러한 것들이 오랜 기간동안 유지되는 이유를 곰곰히 생각해보았지만 아무리 봐도 이건 매크로 기능을 이용하는 사람들이 있다는 결론을 내렸고 궁금증에 나도 한번 만들어 보기로 하였다
1. Google Console 만들기
Youtube Data Api사용하기 위해 필수적이기에 아무프젝을 만든 후 검색을 통해 사용한다
2. OAuth 생성하기
동의화면 구성하라고 나오는데 여기에서 테스트 사용자를 꼭 지정해 줘야한다
다음은 다운로드 표시부분에서 Json파일을 일단 다운받아둔다
3. 코드 구성 전체
import os
import time
import schedule
import google.auth.transport.requests
import google.oauth2.credentials
import google_auth_oauthlib.flow
import googleapiclient.discovery
import googleapiclient.errors
# Scope for the YouTube API
scopes = ["https://www.googleapis.com/auth/youtube.force-ssl"]
def post_comment():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtube"
api_version = "v3"
client_secrets_file = "secret.json" # your OAuth 2.0 Client ID JSON file
token_file = "token.json" # File to store the user's access and refresh tokens
credentials = None
# Load the credentials from the token file if it exists
if os.path.exists(token_file):
credentials = google.oauth2.credentials.Credentials.from_authorized_user_file(token_file, scopes)
# If there are no valid credentials, perform the OAuth flow
if not credentials or not credentials.valid:
if credentials and credentials.expired and credentials.refresh_token:
credentials.refresh(google.auth.transport.requests.Request())
else:
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(client_secrets_file, scopes)
credentials = flow.run_local_server(port=0)
# Save the credentials for the next run
with open(token_file, 'w') as token:
token.write(credentials.to_json())
# Create an API client using the credentials
youtube = googleapiclient.discovery.build(api_service_name, api_version, credentials=credentials)
# Create the request to post a comment
request = youtube.commentThreads().insert(
part="snippet",
body={
"snippet": {
"videoId": "videoID", # Replace with your video ID
"topLevelComment": {
"snippet": {
"textOriginal": "API Machine In Operation" # Replace with your comment text
}
}
}
}
)
# Execute the request and print the response
response = request.execute()
print(response)
if __name__ == "__main__":
# Schedule the post_comment function to run every 10 minutes
schedule.every(1).minutes.do(post_comment)
# Keep the script running
while True:
schedule.run_pending()
time.sleep(1)
너무 길다 하지막 딱 4부분만 확인하면 된다
def post_comment():
client_secrets_file = "secret.json" # your OAuth 2.0 Client ID JSON file
token_file = "token.json" # File to store the user's access and refresh tokens
client_secrets_file부분으로 아까 다운받은 json파일을 등록해 두면 되고 아래 token의 경우는 처음 인증을 해야하는데 추후 다시 인증하는것을 없애기 위해 자동으로 생성되니 그냥 놔두면 된다
# Create the request to post a comment
request = youtube.commentThreads().insert(
part="snippet",
body={
"snippet": {
"videoId": "videoID", # Replace with your video ID
"topLevelComment": {
"snippet": {
"textOriginal": "API Machine In Operation" # Replace with your comment text
}
}
}
}
)
videoId 의 경우 영상을 들어갔을경우 v = 뒤에 오는 코드다
textOriginal 이 내가 달고 싶은 댓글이다
더 많은 기능은 youtube Api를 검색해서 나오는 것을 확인하면 된다
if __name__ == "__main__":
# Schedule the post_comment function to run every 10 minutes
schedule.every(1).minutes.do(post_comment)
# Keep the script running
while True:
schedule.run_pending()
time.sleep(1)
이부분은 특정 시간마다 위의 스크립트가 실행되게 하는 것이다
이제 위 코드를 실행해 주면 처음에 인증을 해야되고 인증을 완료하면 그 유튜브 댓글에 댓글이 달려있을 것이다
인증 오류가 발생하였을 경우 테스트유저에 잘 넣었는지 확인하자
처음 실행할때 schedule.every()를 이용하면 인증자체가 안되는것 같아 이때만 아래코드로 token을 생성해 두자
if __name__ == "__main__":
post_comment()
728x90
'심심해서 만들어 보는것들' 카테고리의 다른 글
티스토리 반 자동 커밋 - selenium 사용 (0) | 2025.03.20 |
---|---|
오토 마우스 제작 - windowform사용 (0) | 2024.08.27 |
Unity - 포켓몬 이미지 가져오는 방법 (0) | 2024.08.25 |
포켓로그 - 티켓 복사 치트, 이로치 치트 or 치트로 알 빠르게 부활시키고 싶은경우 (69) | 2024.07.07 |