-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapp.py
More file actions
232 lines (181 loc) · 6.99 KB
/
app.py
File metadata and controls
232 lines (181 loc) · 6.99 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import logging
import os
import re
from collections import Counter
from datetime import datetime, timedelta
from pathlib import Path
from flask import Flask, jsonify, render_template, request
from src import api_validator, errors, logger_utils, tag_processing, utilities
app = Flask(__name__)
logger = logging.getLogger(__name__)
logging_config_path = Path(__file__).parent / "config" / "logging_config.json"
try:
logger_utils.create_custom_logger(str(logging_config_path))
except FileNotFoundError:
logger.exception(
"Logging configuration file incorrectly specified. "
"Ensure log directory path is at root level: %s",
logging_config_path,
)
print("Logging configuration file not found. Please check the path and try again.")
exit(1)
# Set a delineator for a new application run in log file
logger.debug("\n%s NEW LOG RUN %s\n", "=" * 60, "=" * 60)
try:
youtube_api_key = api_validator.check_youtube_api_key()
rapid_api_key = api_validator.check_rapid_api_key()
except errors.MissingKeyError as exc:
logger.error(
"Missing required API key: %s. Please set the environment variable.",
exc.key_name,
)
print("The required API key is missing. Please set the environment variable.")
exit(1)
except errors.InvalidAPIKeyError as exc:
logger.error("Invalid API key: %s", exc.message)
print(
"The provided API key is invalid. "
+ "Please check your configuration and the log for more details."
)
exit(1)
@app.route("/")
def index():
return render_template("index.html")
@app.route("/get_tags", methods=["POST"])
def get_tags():
topic = request.form.get("topic", "").strip()
max_results = int(request.form.get("max_results", 30))
try:
if not topic:
logger.error("No topic provided in request. Request data: %s", request.form)
return jsonify({"error": "Please enter a topic"}), 400
# Get tags from YouTube API
tags = get_youtube_tags(topic, max_results)
return jsonify(
{
"tags": tags,
"source": "YouTube API",
},
)
except errors.TooManyRequestsError as exc:
return jsonify({exc.status_code: exc.message}), exc.status_code
except errors.NonStandardResponseCodeError as exc:
return jsonify({exc.status_code: exc.message}), exc.status_code
except Exception:
logger.exception("Error getting tags.")
# SECURITY: Do not return stack trace or exception details to user
return (
jsonify(
{"error": "An internal error has occurred. Please try again later."},
),
500,
)
def get_youtube_tags(topic, max_results=30):
# Step 1: Search for videos related to the topic
search_url = "https://www.googleapis.com/youtube/v3/search"
params = {
"part": "snippet",
"q": topic,
"type": "video",
"order": "relevance",
"maxResults": min(50, max_results * 2), # Get more to filter later
"key": youtube_api_key,
"publishedAfter": (datetime.now() - timedelta(days=30)).isoformat() + "Z",
}
search_response = utilities.check_response_status(search_url, params=params)
logger.debug("Querying YouTube API - Response: %s", search_response.status_code)
videos = search_response.json().get("items", [])
# Step 2: Get tags from each video
all_tags = []
video_ids = [video["id"]["videoId"] for video in videos]
if video_ids:
# Batch get video details
videos_url = "https://www.googleapis.com/youtube/v3/videos"
videos_params = {
"part": "snippet",
"id": ",".join(video_ids),
"key": youtube_api_key,
}
videos_response = utilities.check_response_status(
videos_url,
params=videos_params,
)
logger.debug(
"Querying YouTube API for video details - Response: %s",
videos_response.status_code,
)
for item in videos_response.json().get("items", []):
snippet = item.get("snippet", {})
video_tags = snippet.get("tags", [])
title = snippet.get("title", "")
description = snippet.get("description", "")
# Process tags from video
if video_tags:
all_tags.extend(tag_processing.filter_tags_by_topic(video_tags, topic))
# Extract keywords from title and description
all_tags.extend(extract_keywords(title, topic))
all_tags.extend(extract_keywords(description, topic))
# Step 3: Filter and rank tags
filtered_tags = filter_and_rank_tags(all_tags, topic, max_results)
return filtered_tags
def extract_keywords(text, topic):
if not text:
return []
# Clean text
text = text.lower()
text = re.sub(r"[^\w\s-]", " ", text) # Replace special chars with space
words = re.findall(r"\b[\w-]+\b", text) # Split into words
minimum_word_length_threshold = 3
# Filter relevant words
keywords = []
for word in words:
if (
len(word) > minimum_word_length_threshold
and topic.lower() in word
and word not in ["youtube", "video", "watch", "channel"]
):
keywords.append(word)
return keywords
def filter_and_rank_tags(tags, topic, max_results):
# Count tag occurrences
tag_counts = Counter(tags)
# Score tags based on:
# 1. Frequency
# 2. Length (longer tags are better)
# 3. Contains topic
scored_tags = []
for tag, count in tag_counts.items():
score = count * 10 # Frequency weight
score += len(tag) # Length weight
if topic.lower() in tag:
score += 20 # Topic match bonus
scored_tags.append((score, tag))
# Sort by score descending
scored_tags.sort(reverse=True, key=lambda x: x[0])
# Get top tags
top_tags = [tag for (score, tag) in scored_tags[: max_results * 2]]
# Remove similar tags (avoid duplicates like "cool gadget" and "cool gadgets")
final_tags = []
seen_tags = set()
similarity_threshold = 0.7 # 70% similarity threshold
for tag in top_tags:
# Check if similar tag already exists
words = set(tag.split())
is_duplicate = False
for existing_tag in seen_tags:
existing_words = set(existing_tag.split())
similarity = len(words & existing_words) / len(words | existing_words)
if similarity > similarity_threshold:
is_duplicate = True
break
if not is_duplicate:
final_tags.append(tag)
seen_tags.add(tag)
if len(final_tags) >= max_results:
break
return final_tags
if __name__ == "__main__":
debug = os.environ.get("FLASK_DEBUG", "0") == "1"
port = int(os.environ.get("FLASK_PORT", 5000))
print(f"Starting Flask app at http://127.0.0.1:{port} with debug = {debug}")
app.run(host="0.0.0.0", port=port, debug=debug)