-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 38ca5d3
Showing
16 changed files
with
6,399 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/config.local | ||
/tmp | ||
/cache |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Add patterns of files dvc should ignore, which could improve | ||
# the performance. Learn more at | ||
# https://dvc.org/doc/user-guide/dvcignore |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
env | ||
.env |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
FROM public.ecr.aws/lambda/python:3.9 | ||
WORKDIR ${LAMBDA_TASK_ROOT} | ||
COPY requirements.txt . | ||
RUN pip install -r requirements.txt | ||
RUN pip install --no-cache-dir -r requirements.txt | ||
COPY app.py ./ | ||
COPY data/raw/data.csv ./data/raw/ | ||
COPY models/similarity.pkl ./models/ | ||
COPY templates/ ./templates/ | ||
CMD ["app.handler"] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Tarun Singh | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import os | ||
import re | ||
import awsgi | ||
import pandas as pd | ||
from dotenv import load_dotenv | ||
from googleapiclient.discovery import build | ||
from flask import Flask, render_template, request, render_template_string | ||
|
||
app = Flask(__name__) | ||
|
||
load_dotenv() | ||
api_keys = os.getenv("YOUTUBE_API_KEYS").split(',') | ||
|
||
def build_youtube_client(api_key): | ||
return build('youtube', 'v3', developerKey=api_key) | ||
|
||
def rotate_api_key(current_index): | ||
return (current_index + 1) % len(api_keys) | ||
|
||
def get_comments(video_id, max_results=1000): | ||
comments = [] | ||
current_index = 0 | ||
youtube = build_youtube_client(api_keys[current_index]) | ||
next_page_token = None | ||
|
||
while len(comments) < max_results: | ||
try: | ||
request = youtube.commentThreads().list( | ||
part="snippet", | ||
videoId=video_id, | ||
maxResults=min(100, max_results - len(comments)), | ||
pageToken=next_page_token, | ||
textFormat="plainText" | ||
) | ||
response = request.execute() | ||
|
||
for item in response.get("items", []): | ||
comment = item["snippet"]["topLevelComment"]["snippet"]["textDisplay"] | ||
comments.append(comment) | ||
|
||
next_page_token = response.get("nextPageToken") | ||
|
||
if not next_page_token: | ||
break | ||
|
||
except Exception as e: | ||
print(f"Error: {e}") | ||
current_index = rotate_api_key(current_index) | ||
youtube = build_youtube_client(api_keys[current_index]) | ||
break | ||
df = pd.DataFrame(comments, columns=["Comment"]) | ||
return df | ||
|
||
def extract_youtube_video_id(url): | ||
|
||
short_link_pattern = r"(https?://)?(www\.)?youtu\.be/([a-zA-Z0-9_-]+)" | ||
long_link_pattern = r"(https?://)?(www\.)?youtube\.com/watch\?v=([a-zA-Z0-9_-]+)" | ||
|
||
short_link_match = re.match(short_link_pattern, url) | ||
long_link_match = re.match(long_link_pattern, url) | ||
|
||
if short_link_match: | ||
video_id = short_link_match.group(3) | ||
elif long_link_match: | ||
video_id = long_link_match.group(3) | ||
else: | ||
return "" | ||
|
||
return video_id | ||
|
||
def trim_whitespace(s): | ||
return s.strip() | ||
|
||
@app.route('/', methods=['GET', 'POST']) | ||
def index(): | ||
if request.method == 'POST': | ||
video_url = trim_whitespace(request.form.get('video_url')) | ||
video_id = extract_youtube_video_id(video_url) | ||
|
||
if video_id: | ||
comments_df = get_comments(video_id) | ||
if not comments_df.empty: | ||
top_comment = comments_df.iloc[0]["Comment"] | ||
return render_template('index.html', comment=top_comment) | ||
else: | ||
return render_template('index.html', error="No comments found for this video.") | ||
else: | ||
return render_template('index.html', error="Invalid link, please try another link.") | ||
|
||
return render_template('index.html') | ||
|
||
if __name__ == "__main__": | ||
app.run(debug=True) |
Oops, something went wrong.