Skip to content

Commit 3bb85c0

Browse files
author
atj61275
committed
autoCommit 20240229.163512
1 parent 5f0eb79 commit 3bb85c0

7 files changed

+158
-0
lines changed

data/youtube_comments.db

12 KB
Binary file not shown.

fetch_comments_to_db.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from googleapiclient.discovery import build
2+
import sqlite3
3+
4+
# YouTube API setup
5+
API_KEY = 'AIzaSyAZJRxunHxliV4-xAfvxi6LKEIDjb5ItiM';
6+
youtube = build('youtube', 'v3', developerKey=API_KEY)
7+
8+
path = '../data/youtube_comments.db'
9+
10+
# Database setup
11+
conn = sqlite3.connect(path)
12+
cursor = conn.cursor()
13+
14+
def fetch_comments(video_id, search_string):
15+
comments = []
16+
response = youtube.commentThreads().list(
17+
part='snippet',
18+
videoId=video_id,
19+
maxResults=100, # Adjust based on your needs
20+
textFormat='plainText'
21+
).execute()
22+
23+
for item in response['items']:
24+
comment = item['snippet']['topLevelComment']['snippet']['textDisplay']
25+
if search_string.lower() in comment.lower():
26+
comments.append(comment)
27+
if len(comments) == 3:
28+
break
29+
30+
return comments
31+
32+
def insert_comments_into_db(video_id, comments):
33+
for comment in comments:
34+
sql = "INSERT INTO `youtube_comments` (`video_id`, `comment`) VALUES (?, ?)"
35+
cursor.execute(sql, (video_id, comment))
36+
conn.commit()
37+
38+
video_ids = ['HQKwgk6XkIA', 'qGyp0Y5ewBI'] # Your video IDs
39+
search_string = '2' # String to search in comments
40+
41+
for video_id in video_ids:
42+
comments = fetch_comments(video_id, search_string)
43+
if comments:
44+
insert_comments_into_db(video_id, comments)
45+
46+
conn.close()

src/o1_create_empty_db.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import sqlite3
2+
import os
3+
4+
path = '../data/youtube_comments.db'
5+
import os
6+
if os.path.exists(path):
7+
os.remove(path)
8+
else:
9+
print("The file does not exist")
10+
11+
# Connect to SQLite database (this will create the database file if it doesn't exist)
12+
conn = sqlite3.connect(path)
13+
cursor = conn.cursor()
14+
sql = '''CREATE TABLE youtube_comments (
15+
id INTEGER PRIMARY KEY AUTOINCREMENT,
16+
video_id TEXT NOT NULL,
17+
comment TEXT NOT NULL
18+
)'''
19+
cursor.execute(sql)
20+
21+
# Commit your changes in the database
22+
conn.commit()
23+
conn.close()
24+
25+
print("Table created successfully")

src/o2_fetch_comments_to_db.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from googleapiclient.discovery import build
2+
import sqlite3
3+
4+
# YouTube API setup
5+
API_KEY = 'AIzaSyAZJRxunHxliV4-xAfvxi6LKEIDjb5ItiM';
6+
youtube = build('youtube', 'v3', developerKey=API_KEY)
7+
8+
path = '../data/youtube_comments.db'
9+
10+
# Database setup
11+
conn = sqlite3.connect(path)
12+
cursor = conn.cursor()
13+
14+
def fetch_comments(video_id, search_string):
15+
comments = []
16+
response = youtube.commentThreads().list(
17+
part='snippet',
18+
videoId=video_id,
19+
order='relevance',
20+
maxResults=1000,
21+
textFormat='plainText'
22+
).execute()
23+
24+
for item in response['items']:
25+
comment = item['snippet']['topLevelComment']['snippet']
26+
if search_string.lower() in comment['textDisplay'].lower():
27+
comments.append({
28+
'author': comment['authorDisplayName'],
29+
'text': comment['textDisplay'],
30+
'likes': comment['likeCount'],
31+
'publishedAt': comment['publishedAt']
32+
})
33+
sorted_comments = sorted(comments, key=lambda x: x['likes'], reverse=True)
34+
return sorted_comments[0:3]
35+
36+
def insert_comments_into_db(video_id, comments):
37+
for comment in comments:
38+
sql = "INSERT INTO `youtube_comments` (`video_id`, `comment`) VALUES (?, ?)"
39+
cursor.execute(sql, (comment['author'], comment['text']))
40+
conn.commit()
41+
42+
video_ids = ['qGyp0Y5ewBI', 'BSXoI9MDvU0'] # Your video IDs
43+
search_string = '' # String to search in comments
44+
45+
for video_id in video_ids:
46+
comments = fetch_comments(video_id, search_string)
47+
if comments:
48+
insert_comments_into_db(video_id, comments)
49+
50+
conn.close()

src/o3_flask_load_db.py

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from flask import Flask, render_template
2+
import sqlite3
3+
4+
app = Flask(__name__)
5+
6+
path = '../data/youtube_comments.db'
7+
8+
def get_comments():
9+
conn = sqlite3.connect(path)
10+
cursor = conn.cursor()
11+
cursor.execute("SELECT video_id, comment FROM youtube_comments")
12+
comments = cursor.fetchall()
13+
conn.close()
14+
return comments
15+
16+
@app.route('/')
17+
def show_comments():
18+
comments = get_comments()
19+
return render_template('comments.html', comments=comments)
20+
21+
if __name__ == '__main__':
22+
app.run(debug=True)
File renamed without changes.

src/templates/comments.html

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>YouTube Comments</title>
6+
</head>
7+
<body>
8+
<h1>YouTube Comments</h1>
9+
<ul>
10+
{% for video_id, comment in comments %}
11+
<li><strong>NAME:</strong> {{ video_id }} <strong>Comment:</strong> {{ comment }}</li>
12+
{% endfor %}
13+
</ul>
14+
</body>
15+
</html>

0 commit comments

Comments
 (0)