forked from piyush-98/CricView
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutube-comment.py
More file actions
116 lines (89 loc) · 3.62 KB
/
Copy pathyoutube-comment.py
File metadata and controls
116 lines (89 loc) · 3.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/python
# Usage:
# python sample.py --videoid='<video_id>'
import httplib2
import os
import sys
from apiclient.discovery import build_from_document
from apiclient.errors import HttpError
from oauth2client.client import flow_from_clientsecrets
from oauth2client.file import Storage
from oauth2client.tools import argparser, run_flow
from firebase import firebase
firebase=firebase.FirebaseApplication("https://lifesaver-18f28.firebaseio.com/")
CLIENT_SECRETS_FILE = "client_secrets.json"
YOUTUBE_READ_WRITE_SSL_SCOPE = "https://www.googleapis.com/auth/youtube.force-ssl"
YOUTUBE_API_SERVICE_NAME = "youtube"
YOUTUBE_API_VERSION = "v3"
MISSING_CLIENT_SECRETS_MESSAGE = "MISSING_CLIENT_SECRETS_MESSAGE"
os.path.abspath(os.path.join(os.path.dirname(__file__),
CLIENT_SECRETS_FILE))
# Authorize the request and store authorization credentials.
def get_authenticated_service(args):
flow = flow_from_clientsecrets(CLIENT_SECRETS_FILE, scope=YOUTUBE_READ_WRITE_SSL_SCOPE,
message=MISSING_CLIENT_SECRETS_MESSAGE)
storage = Storage("%s-oauth2.json" % sys.argv[0])
credentials = storage.get()
if credentials is None or credentials.invalid:
credentials = run_flow(flow, storage, args)
# Trusted testers can download this discovery document from the developers page
# and it should be in the same directory with the code.
with open("youtube-v3-discoverydocument.json", "r") as f:
doc = f.read()
return build_from_document(doc, http=credentials.authorize(httplib2.Http()))
# Call the API's commentThreads.list method to list the existing comment threads.
def get_comment_threads(youtube, video_id):
c=0
results = youtube.commentThreads().list(
part="snippet",
videoId=video_id,
textFormat="plainText"
).execute()
for item in results["items"]:
comment = item["snippet"]["topLevelComment"]
author = comment["snippet"]["authorDisplayName"]
text = comment["snippet"]["textDisplay"]
print (author)
print (text)
#res=firebase.put("/dir","count","forward")
if "extreme left" == text:
res=firebase.put("/","dir",1)
if "left" == text:
res=firebase.put("/","dir",2)
elif "right" == text:
res=firebase.put("/","dir",4)
elif "extreme right" == text:
res=firebase.put("/","dir",5)
elif "zoom" == text:
if c==0:
c=1
res=firebase.put("/","zoom",c)
elif c==1:
res=firebase.put("/","zoom",2)
break
return results["items"]
# Call the API's comments.list method to list the existing comment replies.
def get_comments(youtube, parent_id):
results = youtube.comments().list(
part="snippet",
parentId=parent_id,
textFormat="plainText"
).execute()
for item in results["items"]:
author = item["snippet"]["authorDisplayName"]
text = item["snippet"]["textDisplay"]
print (author)
print (text)
return results["items"]
if __name__ == "__main__":
# The "videoid" option specifies the YouTube video ID that uniquely
# identifies the video for which the comment will be inserted.
argparser.add_argument("--videoid",
help="Required; ID for video for which the comment will be inserted.")
args = argparser.parse_args()
if not args.videoid:
exit("Please specify videoid using the --videoid= parameter.")
youtube = get_authenticated_service(args)
video_comment_threads = get_comment_threads(youtube, args.videoid)
parent_id = video_comment_threads[0]["id"]
video_comments = get_comments(youtube, parent_id)