Skip to content

Commit

Permalink
1.0 base code
Browse files Browse the repository at this point in the history
  • Loading branch information
TarunSingh2002 committed Aug 22, 2024
0 parents commit 38ca5d3
Show file tree
Hide file tree
Showing 16 changed files with 6,399 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .dvc/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/config.local
/tmp
/cache
Empty file added .dvc/config
Empty file.
3 changes: 3 additions & 0 deletions .dvcignore
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
env
.env
10 changes: 10 additions & 0 deletions Dockerfile
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"]
21 changes: 21 additions & 0 deletions LICENSE.txt
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 added README.md
Empty file.
93 changes: 93 additions & 0 deletions app.py
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)
Loading

0 comments on commit 38ca5d3

Please sign in to comment.