Last active
January 9, 2021 09:21
-
-
Save ashkanRmk/f8047303fb2d02ea10793d2d6a51fc25 to your computer and use it in GitHub Desktop.
this snippet code choose 3 random winner of your specific YouTube video. implementation flow on : https://youtu.be/x3GqBhm3XuA
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests as rq | |
import random | |
video_id = "[YOUR_VIDEO_ID]" | |
token = "[YOUR_TOKEN]" | |
url = "https://www.googleapis.com/youtube/v3/commentThreads" | |
response = rq.get(url, params={ | |
'part' : 'replies,snippet', | |
'maxResults' : '100', | |
'videoId' : video_id, | |
'key' : token | |
}) | |
response_json = response.json()['items'] | |
users = set() | |
datas = [] | |
for item in response_json: | |
if item['snippet']['topLevelComment']['snippet']['authorDisplayName'] != "Ashkan Rahmani - Happy Developer": | |
user = item['snippet']['topLevelComment']['snippet']['authorDisplayName'] | |
msg = item['snippet']['topLevelComment']['snippet']['textOriginal'] | |
users.add(user) | |
datas.append({ | |
'user': user, | |
'msg': msg | |
}) | |
print(f"All commentators: {len(users)}") | |
print("--------------------") | |
winners = [random.sample(users, 1) for x in range(3)] | |
for index, winner in enumerate(winners): | |
dup = False | |
for data in datas: | |
if data['user'] == winner[0] and not dup: | |
dup = True | |
print(f"winner{index+1}: ") | |
print(f"user: {data['user']}") | |
print(f"msg: {data['msg']}") | |
print("--------------------") | |
# print(winner) | |
# print(*winners, sep="\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment