Spaces:
Sleeping
Sleeping
molokhovdmitry
commited on
Commit
·
d684f95
1
Parent(s):
a548d1c
Add main.py, yt_api.py, requirements.txt
Browse files- main.py +25 -0
- requirements.txt +4 -0
- yt_api.py +62 -0
main.py
ADDED
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI
|
2 |
+
from yt_api import get_comments
|
3 |
+
from pydantic_settings import BaseSettings, SettingsConfigDict
|
4 |
+
|
5 |
+
|
6 |
+
class Settings(BaseSettings):
|
7 |
+
YT_API_KEY: str
|
8 |
+
model_config = SettingsConfigDict(env_file='.env')
|
9 |
+
|
10 |
+
|
11 |
+
settings = Settings()
|
12 |
+
app = FastAPI(title='social-stat')
|
13 |
+
|
14 |
+
|
15 |
+
YT_API_KEY = settings.YT_API_KEY
|
16 |
+
|
17 |
+
|
18 |
+
@app.get('/')
|
19 |
+
def home():
|
20 |
+
return 'social-stat'
|
21 |
+
|
22 |
+
|
23 |
+
@app.post('/predict')
|
24 |
+
def predict(video_id):
|
25 |
+
return get_comments(video_id, YT_API_KEY)
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
requests
|
2 |
+
fastapi
|
3 |
+
uvicorn
|
4 |
+
pydantic_settings
|
yt_api.py
ADDED
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import requests
|
2 |
+
# from pprint import pprint
|
3 |
+
|
4 |
+
|
5 |
+
def get_comments(video_id, api_key):
|
6 |
+
"""Yields all `commentThreads` from a YouTube video in batches."""
|
7 |
+
|
8 |
+
# Get comments from the first page.
|
9 |
+
response = get_response(video_id, api_key, max_results=100)
|
10 |
+
comment_list = response_to_comments(response)
|
11 |
+
|
12 |
+
# Get comments from the other pages.
|
13 |
+
while 'nextPageToken' in response.keys():
|
14 |
+
response = get_response(
|
15 |
+
video_id, api_key, page_token=response['nextPageToken'])
|
16 |
+
comment_list.update(response_to_comments(response))
|
17 |
+
|
18 |
+
return comment_list
|
19 |
+
|
20 |
+
|
21 |
+
def get_response(video_id, api_key, page_token=None, max_results=100):
|
22 |
+
"""Gets the response from YouTube API and converts it to JSON."""
|
23 |
+
url = 'https://youtube.googleapis.com/youtube/v3/commentThreads'
|
24 |
+
payload = {
|
25 |
+
'videoId': video_id,
|
26 |
+
'key': api_key,
|
27 |
+
'maxResults': max_results,
|
28 |
+
'part': 'snippet',
|
29 |
+
'pageToken': page_token,
|
30 |
+
}
|
31 |
+
response = requests.get(url, params=payload)
|
32 |
+
return response.json()
|
33 |
+
|
34 |
+
|
35 |
+
def response_to_comments(response):
|
36 |
+
"""Converts JSON response to `comment_list` dict."""
|
37 |
+
comment_list = {}
|
38 |
+
for comment in response['items']:
|
39 |
+
comment = comment['snippet']
|
40 |
+
can_reply = comment['canReply']
|
41 |
+
total_reply_count = comment['totalReplyCount']
|
42 |
+
comment = comment['topLevelComment']
|
43 |
+
comment_id = comment['id']
|
44 |
+
comment = comment['snippet']
|
45 |
+
try:
|
46 |
+
comment_list[comment_id] = {
|
47 |
+
'video_id': comment['videoId'],
|
48 |
+
'channel_id': comment['authorChannelId']['value'],
|
49 |
+
'author_display_name': comment['authorDisplayName'],
|
50 |
+
'text_original': comment['textOriginal'],
|
51 |
+
'text_display': comment['textDisplay'],
|
52 |
+
'published_at': comment['publishedAt'].replace('T', ' ')[:-1],
|
53 |
+
'updated_at': comment['updatedAt'].replace('T', ' ')[:-1],
|
54 |
+
'like_count': comment['likeCount'],
|
55 |
+
'can_reply': can_reply,
|
56 |
+
'total_reply_count': total_reply_count,
|
57 |
+
}
|
58 |
+
except Exception as e:
|
59 |
+
print(f"Error: {e}\nComment: {comment}")
|
60 |
+
continue
|
61 |
+
|
62 |
+
return comment_list
|