-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
426 lines (336 loc) · 12.6 KB
/
main.py
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# /// script
# dependencies = [
# "requests<3",
# "google-api-python-client",
# "youtube-transcript-api",
# "openai",
# "python-dotenv",
# ]
# ///
## NEW NAME: FlashCraft
import email
import imaplib
import json
import logging
import os
import re
import subprocess
import time
from email.header import decode_header
import requests
from dotenv import load_dotenv
from googleapiclient.discovery import build
from openai import OpenAI
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import TextFormatter
# Load environment variables from .env file
load_dotenv(".env")
# Get environment variables
IMAP_SERVER = os.getenv("IMAP_SERVER")
EMAIL = os.getenv("EMAIL")
PASSWORD = os.getenv("PASSWORD")
OPENAI_API_KEY = os.getenv("OPENAI_API_KEY")
YOUTUBE_DATA_API_KEY = os.getenv("YOUTUBE_DATA_API_KEY")
ANKI_CONNECT_URL = os.getenv("ANKI_CONNECT_URL")
ANKI_API_KEY = os.getenv("ANKI_API_KEY")
# Create an instance of the OpenAI client
OPENAI_CLIENT = OpenAI(api_key=OPENAI_API_KEY)
# Configure logging
logging.basicConfig(
level=logging.INFO, # Set the logging level
format="%(asctime)s - %(levelname)s - %(message)s", # Log format
)
def check_email():
"""Check the email inbox for unread emails containing YouTube links.
Returns:
list: A list of YouTube video IDs extracted from unread emails.
"""
logging.info("Checking email for YouTube links...")
# Connect to the email server
mail = imaplib.IMAP4_SSL(IMAP_SERVER)
mail.login(EMAIL, PASSWORD)
mail.select("inbox")
# Search for all unread emails
status, messages = mail.search(None, "UNSEEN")
email_ids = messages[0].split()
# Pattern to extract YouTube video ID from either full URL or shortened URL
pattern = (
r"(?:https?://(?:www\.)?youtube\.com/watch\?v=|https?://youtu\.be/)([\w-]+)"
)
youtube_video_ids = []
for email_id in email_ids:
# Fetch the email by ID
status, msg_data = mail.fetch(email_id, "(RFC822)")
msg = email.message_from_bytes(msg_data[0][1])
# Get the email subject
subject, encoding = decode_header(msg["Subject"])[0]
if isinstance(subject, bytes):
subject = subject.decode(encoding if encoding else "utf-8")
# Check if the email contains a YouTube link
if msg.is_multipart():
for part in msg.walk():
if part.get_content_type() == "text/plain":
body = part.get_payload(decode=True).decode()
video_ids = re.findall(pattern, body)
youtube_video_ids.extend(video_ids)
else:
body = msg.get_payload(decode=True).decode()
video_ids = re.findall(pattern, body)
youtube_video_ids.extend(video_ids)
# Close the connection and logout
mail.close()
mail.logout()
logging.info(
f"Found {len(email_ids)} unread emails with {len(youtube_video_ids)} YouTube links."
)
# Remove duplicate video IDs
unique_youtube_video_ids = list(set(youtube_video_ids))
# If there were duplicate video IDs, log the number of duplicates removed
if len(unique_youtube_video_ids) < len(youtube_video_ids):
logging.info(
f"Removed {len(youtube_video_ids) - len(unique_youtube_video_ids)} duplicate video IDs."
)
return unique_youtube_video_ids
def get_youtube_video_details(video_id):
"""Fetch the title and channel name of a YouTube video by its ID.
Args:
video_id (str): The ID of the YouTube video.
Returns:
tuple: A tuple containing the video title and channel name, or (None, None) if not found.
"""
# Initialize the YouTube API client
youtube = build("youtube", "v3", developerKey=YOUTUBE_DATA_API_KEY)
# Make the API request
request = youtube.videos().list(part="snippet", id=video_id)
response = request.execute()
# Extract video title and channel name
if "items" in response and len(response["items"]) > 0:
video_title = response["items"][0]["snippet"]["title"]
channel_name = response["items"][0]["snippet"]["channelTitle"]
return video_title, channel_name
else:
return None, None
def extract_transcript_from_youtube(video_id):
"""Extract the transcript from a YouTube video.
Args:
video_id (str): The ID of the YouTube video.
Returns:
str: The formatted transcript of the video as plain text.
"""
# Get the transcript for the YouTube video
transcript = YouTubeTranscriptApi.get_transcript(video_id)
# Format the transcript as plain text
formatted_transcript = TextFormatter().format_transcript(transcript)
# Remove newlines from the formatted transcript and return it
return formatted_transcript.replace("\n", " ")
def openai_call(prompt, model="gpt-4o-mini"):
"""Call the OpenAI API with a given prompt.
Args:
prompt (str): The prompt to send to the OpenAI API.
model (str): The model to use for the API call.
Returns:
str: The response content from the OpenAI API.
"""
chat_completion = OPENAI_CLIENT.chat.completions.create(
messages=[
{
"role": "user",
"content": prompt,
}
],
model=model,
)
return chat_completion.choices[0].message.content
def summarize_transcript(transcript):
"""Summarize a YouTube video transcript using the OpenAI API.
Args:
transcript (str): The transcript of the YouTube video.
Returns:
str: The summary of the transcript.
"""
logging.info("Summarizing the YouTube video transcript with OpenAI API...")
# Define the prompt with the preparatory instruction and append the transcript
with open("prompts/summarization.txt", "r") as file:
prompt = file.read().strip()
full_prompt = f"{prompt}: {transcript}"
message = openai_call(full_prompt.replace("\n", ""))
# Return the summary
return message
def generate_flashcards_from_summary(summary, language="english"):
"""Generate flashcards from a summarized transcript.
Args:
summary (str): The summarized transcript.
language (str): The language for the flashcards.
Returns:
dict: A JSON object containing the generated flashcards.
"""
logging.info("Generating flashcards from the summarized transcript...")
with open("prompts/flashcard_generation.txt", "r") as file:
prompt = file.read().strip()
flashcards_prompt = f"{prompt}\n\n{summary}"
# Call the OpenAI API to generate flashcards
flashcards = openai_call(flashcards_prompt)
# Convert to json
flashcards_json = json.loads(flashcards)
# Placeholder for your OpenAI API code to generate flashcards
return flashcards_json
def generate_tags(text):
"""Generate tags from a given text using the OpenAI API.
Args:
text (str): The text to generate tags from.
Returns:
list: A list of generated tags.
"""
with open("prompts/tags_generation.txt", "r") as file:
prompt = file.read().strip()
tags_prompt = f"{prompt}\n\n{text}"
# Call the OpenAI API to generate tags
tags_response = openai_call(tags_prompt)
print(tags_response)
# Convert the response to a list of tags
tags = tags_response.split(" ")
return tags
def save_to_file(content: str, path: str = ""):
"""Save content to a file with a title derived from the content.
Args:
content (str): The content to save.
path (str): The directory path to save the file in.
"""
# Save the summarized transcript to a file
# Get the title from first line
title = (
content.split("\n")[0]
.replace("#", "")
.replace("*", "")
.strip()
.lower()
.replace(" ", "_")
)
with open(os.path.join(path, f"{title}.md"), "w") as file:
file.write(content)
def deck_exists(deck_name):
"""Check if a specified Anki deck exists.
Args:
deck_name (str): The name of the deck to check.
Returns:
bool: True if the deck exists, False otherwise.
"""
payload = {
"action": "deckNames",
"version": 6,
"key": ANKI_API_KEY,
}
response = requests.post("http://localhost:8765", json=payload)
if response.status_code != 200:
raise Exception("Failed to fetch deck names from AnkiConnect")
deck_names = response.json().get("result", [])
return deck_name in deck_names
def create_deck(deck_name):
"""Create a new Anki deck.
Args:
deck_name (str): The name of the deck to create.
"""
payload = {
"action": "createDeck",
"version": 6,
"params": {"deck": deck_name},
"key": ANKI_API_KEY,
}
response = requests.post("http://localhost:8765", json=payload)
if response.status_code != 200:
raise Exception("Failed to create deck in AnkiConnect")
def add_anki_card(deck_name, note_type, front, back, tags=None):
"""Add a new card to an Anki deck.
Args:
deck_name (str): The name of the deck to add the card to.
note_type (str): The type of the note (card).
front (str): The front content of the card.
back (str): The back content of the card.
tags (list, optional): A list of tags for the card.
"""
full_deck_name = f"YouTube Flashcards::{deck_name}"
if not deck_exists(full_deck_name):
create_deck(full_deck_name)
# Define the card note structure
note = {
"deckName": full_deck_name,
"modelName": note_type,
"fields": {"Front": front, "Back": back},
"tags": tags or [],
"options": {"allowDuplicate": False},
"audio": [],
"video": [],
"picture": [],
}
send_anki_request("addNote", {"note": note})
def send_anki_request(action, params=None):
"""Send a request to AnkiConnect.
Args:
action (str): The action to perform.
params (dict, optional): The parameters for the action.
"""
# Prepare the request payload
payload = {
"action": action,
"version": 6,
"params": params or {},
"key": ANKI_API_KEY,
}
# Send the request to AnkiConnect
response = requests.post("http://localhost:8765", json=payload)
# Check the response
if response.status_code == 200:
result = response.json()
if not ("error" in result and result["error"] is None):
print(f"Error syncing media: {result['error']}")
else:
print(f"HTTP Error: {response.status_code}")
def main():
"""Main function to execute the application logic."""
# Check email for unread YouTube links
video_ids = check_email()
if not video_ids:
logging.info("No new YouTube links found.")
return
# Open Anki
with open("anki_output.log", "w") as f:
anki_process = subprocess.Popen(["anki"], stdout=f, stderr=f)
# Give Anki some time to start up
time.sleep(5)
# Sync Anki
send_anki_request("sync")
# video_ids = ["pmOi0crbkEE"]
for video_id in video_ids:
logging.info("----------------------------------")
logging.info(f"Processing video with ID: {video_id}")
# Get the title and channel name of the YouTube video
video_title, channel_name = get_youtube_video_details(video_id)
logging.info(f"Channel name: {channel_name}")
logging.info(f"Video title: {video_title}")
# Get the transcript for the YouTube video
transcript = extract_transcript_from_youtube(video_id)
# Get the summary of the transcript
summarized_transcript = summarize_transcript(transcript)
# Save the summarized transcript to a file
save_to_file(summarized_transcript, "summaries")
# Generate flashcards from the summarized transcript
flashcards = generate_flashcards_from_summary(summarized_transcript)
logging.info(f"Created {len(flashcards)} flashcards for the video.")
# Generate tags for the flashcards
tags = generate_tags(summarized_transcript)
logging.info(f"Generated {len(tags)} tags: {tags}.")
# Upload the flashcards to Anki
for card in flashcards:
# Add channel name and video title as a header to the card
front = (
f"<h1>{channel_name}</h1><h2>{video_title}</h2><br>{card['question']}"
)
add_anki_card(channel_name, "Basic", front, card["answer"], tags=tags)
logging.info("Uploaded flashcards to Anki.")
# Sync the media files with Anki
send_anki_request("sync")
logging.info("Sync completed!")
# Close Anki
anki_process.kill()
if __name__ == "__main__":
main()