content-core 1.0.1__py3-none-any.whl → 1.0.3__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of content-core might be problematic. Click here for more details.

@@ -1,4 +1,3 @@
1
- import asyncio
2
1
  import re
3
2
  import ssl
4
3
 
@@ -69,86 +68,95 @@ async def _extract_youtube_id(url):
69
68
 
70
69
 
71
70
  async def get_best_transcript(video_id, preferred_langs=["en", "es", "pt"]):
72
- max_attempts = 5
73
- for attempt in range(max_attempts):
71
+ try:
72
+ transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
73
+
74
+ # First try: Manual transcripts in preferred languages
75
+ manual_transcripts = []
76
+ try:
77
+ for transcript in transcript_list:
78
+ if not transcript.is_generated and not transcript.is_translatable:
79
+ manual_transcripts.append(transcript)
80
+
81
+ if manual_transcripts:
82
+ # Sort based on preferred language order
83
+ for lang in preferred_langs:
84
+ for transcript in manual_transcripts:
85
+ if transcript.language_code == lang:
86
+ return transcript.fetch()
87
+ # If no preferred language found, return first manual transcript
88
+ return manual_transcripts[0].fetch()
89
+ except NoTranscriptFound:
90
+ pass
91
+
92
+ # Second try: Auto-generated transcripts in preferred languages
93
+ generated_transcripts = []
94
+ try:
95
+ for transcript in transcript_list:
96
+ if transcript.is_generated and not transcript.is_translatable:
97
+ generated_transcripts.append(transcript)
98
+
99
+ if generated_transcripts:
100
+ # Sort based on preferred language order
101
+ for lang in preferred_langs:
102
+ for transcript in generated_transcripts:
103
+ if transcript.language_code == lang:
104
+ return transcript.fetch()
105
+ # If no preferred language found, return first generated transcript
106
+ return generated_transcripts[0].fetch()
107
+ except NoTranscriptFound:
108
+ pass
109
+
110
+ # Last try: Translated transcripts in preferred languages
111
+ translated_transcripts = []
74
112
  try:
75
- transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
76
-
77
- # First try: Manual transcripts in preferred languages
78
- manual_transcripts = []
79
- try:
80
- for transcript in transcript_list:
81
- if not transcript.is_generated and not transcript.is_translatable:
82
- manual_transcripts.append(transcript)
83
-
84
- if manual_transcripts:
85
- # Sort based on preferred language order
86
- for lang in preferred_langs:
87
- for transcript in manual_transcripts:
88
- if transcript.language_code == lang:
89
- return transcript.fetch()
90
- # If no preferred language found, return first manual transcript
91
- return manual_transcripts[0].fetch()
92
- except NoTranscriptFound:
93
- pass
94
-
95
- # Second try: Auto-generated transcripts in preferred languages
96
- generated_transcripts = []
97
- try:
98
- for transcript in transcript_list:
99
- if transcript.is_generated and not transcript.is_translatable:
100
- generated_transcripts.append(transcript)
101
-
102
- if generated_transcripts:
103
- # Sort based on preferred language order
104
- for lang in preferred_langs:
105
- for transcript in generated_transcripts:
106
- if transcript.language_code == lang:
107
- return transcript.fetch()
108
- # If no preferred language found, return first generated transcript
109
- return generated_transcripts[0].fetch()
110
- except NoTranscriptFound:
111
- pass
112
-
113
- # Last try: Translated transcripts in preferred languages
114
- translated_transcripts = []
115
- try:
116
- for transcript in transcript_list:
117
- if transcript.is_translatable:
118
- translated_transcripts.append(transcript)
119
-
120
- if translated_transcripts:
121
- # Sort based on preferred language order
122
- for lang in preferred_langs:
123
- for transcript in translated_transcripts:
124
- if transcript.language_code == lang:
125
- return transcript.fetch()
126
- # If no preferred language found, return translation to first preferred language
127
- translation = translated_transcripts[0].translate(
128
- preferred_langs[0]
129
- )
130
- return translation.fetch()
131
- except NoTranscriptFound:
132
- pass
133
-
134
- raise Exception("No suitable transcript found")
113
+ for transcript in transcript_list:
114
+ if transcript.is_translatable:
115
+ translated_transcripts.append(transcript)
116
+
117
+ if translated_transcripts:
118
+ # Sort based on preferred language order
119
+ for lang in preferred_langs:
120
+ for transcript in translated_transcripts:
121
+ if transcript.language_code == lang:
122
+ return transcript.fetch()
123
+ # If no preferred language found, return translation to first preferred language
124
+ translation = translated_transcripts[0].translate(preferred_langs[0])
125
+ return translation.fetch()
126
+ except NoTranscriptFound:
127
+ pass
128
+
129
+ raise Exception("No suitable transcript found")
135
130
 
136
- except Exception as e:
137
- if e.__class__.__name__ == "ParserError":
138
- logger.warning(
139
- f"ParserError on attempt {attempt+1}/5 for video {video_id}. Retrying..."
140
- )
141
- if attempt == max_attempts - 1:
142
- logger.error(
143
- f"Failed to get transcript for video {video_id} after {max_attempts} attempts due to repeated ParserError."
144
- )
145
- return None
146
- await asyncio.sleep(2)
147
- continue
148
- else:
149
- logger.error(f"Failed to get transcript for video {video_id}: {e}")
150
- return None
151
- return None
131
+ except Exception as e:
132
+ logger.error(f"Failed to get transcript for video {video_id}: {e}")
133
+ return None
134
+
135
+
136
+ def extract_transcript_pytubefix(url, languages=["en", "es", "pt"]):
137
+ from pytubefix import YouTube
138
+
139
+ yt = YouTube(url)
140
+ print(yt.captions)
141
+
142
+ # Try to get captions in the preferred languages
143
+ if yt.captions:
144
+ for lang in languages:
145
+ if lang in yt.captions:
146
+ caption = yt.captions[lang]
147
+ break
148
+ elif f"a.{lang}" in yt.captions:
149
+ caption = yt.captions[f"a.{lang}"]
150
+ break
151
+ else: # No preferred language found, use the first available
152
+ caption_key = next(iter(yt.captions))
153
+ caption = yt.captions[caption_key]
154
+
155
+ srt_captions = caption.generate_srt_captions()
156
+ txt_captions = caption.generate_txt_captions()
157
+ return txt_captions, srt_captions
158
+
159
+ return None, None
152
160
 
153
161
 
154
162
  async def extract_youtube_transcript(state: ProcessSourceState):
@@ -162,11 +170,10 @@ async def extract_youtube_transcript(state: ProcessSourceState):
162
170
  "preferred_languages", ["en", "es", "pt"]
163
171
  )
164
172
 
173
+ # quick fix since transcripts api is not working for now
174
+ engine = "pytubefix"
165
175
  video_id = await _extract_youtube_id(state.url)
166
- transcript = await get_best_transcript(video_id, languages)
167
176
 
168
- logger.debug(f"Found transcript: {transcript}")
169
- formatter = TextFormatter()
170
177
  try:
171
178
  title = await get_video_title(video_id)
172
179
  except Exception as e:
@@ -174,19 +181,29 @@ async def extract_youtube_transcript(state: ProcessSourceState):
174
181
  logger.exception(e)
175
182
  title = ""
176
183
 
177
- try:
178
- formatted_content = formatter.format_transcript(transcript)
179
- except Exception as e:
180
- logger.critical(f"Failed to format transcript for video_id: {video_id}")
181
- logger.exception(e)
182
- formatted_content = ""
184
+ if engine == "pytubefix":
185
+ formatted_content, transcript_raw = extract_transcript_pytubefix(
186
+ state.url, languages
187
+ )
188
+ if engine == "transcripts-api":
189
+ transcript = await get_best_transcript(video_id, languages)
183
190
 
184
- try:
185
- transcript_raw = transcript.to_raw_data()
186
- except Exception as e:
187
- logger.critical(f"Failed to get raw transcript for video_id: {video_id}")
188
- logger.exception(e)
189
- transcript_raw = ""
191
+ logger.debug(f"Found transcript: {transcript}")
192
+ formatter = TextFormatter()
193
+
194
+ try:
195
+ formatted_content = formatter.format_transcript(transcript)
196
+ except Exception as e:
197
+ logger.critical(f"Failed to format transcript for video_id: {video_id}")
198
+ logger.exception(e)
199
+ formatted_content = ""
200
+
201
+ try:
202
+ transcript_raw = transcript.to_raw_data()
203
+ except Exception as e:
204
+ logger.critical(f"Failed to get raw transcript for video_id: {video_id}")
205
+ logger.exception(e)
206
+ transcript_raw = ""
190
207
 
191
208
  return {
192
209
  "content": formatted_content,
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: content-core
3
- Version: 1.0.1
3
+ Version: 1.0.3
4
4
  Summary: Extract what matters from any media source
5
5
  Author-email: LUIS NOVO <lfnovo@gmail.com>
6
6
  License-File: LICENSE
@@ -11,7 +11,7 @@ Requires-Dist: asciidoc>=10.2.1
11
11
  Requires-Dist: bs4>=0.0.2
12
12
  Requires-Dist: dicttoxml>=1.7.16
13
13
  Requires-Dist: docling>=2.34.0
14
- Requires-Dist: esperanto[openai]>=1.2.0
14
+ Requires-Dist: esperanto>=1.2.0
15
15
  Requires-Dist: firecrawl-py>=2.7.0
16
16
  Requires-Dist: jinja2>=3.1.6
17
17
  Requires-Dist: langdetect>=1.0.9
@@ -24,8 +24,10 @@ Requires-Dist: pillow>=10.4.0
24
24
  Requires-Dist: pymupdf>=1.25.5
25
25
  Requires-Dist: python-docx>=1.1.2
26
26
  Requires-Dist: python-dotenv>=1.1.0
27
+ Requires-Dist: python-magic-bin==0.4.14; sys_platform == 'win32'
27
28
  Requires-Dist: python-magic>=0.4.27
28
29
  Requires-Dist: python-pptx>=1.0.2
30
+ Requires-Dist: pytubefix>=9.1.1
29
31
  Requires-Dist: readability-lxml>=0.8.4.1
30
32
  Requires-Dist: validators>=0.34.0
31
33
  Requires-Dist: youtube-transcript-api>=1.0.3
@@ -27,13 +27,13 @@ content_core/processors/pdf.py,sha256=9jf-eROAqw6yQwdlbsxPXsaJXY26hVG7nSTPH9n4af
27
27
  content_core/processors/text.py,sha256=kKHA60-NYjLmCTYUnk8TdJxQQ0Shkg-K61Ezqaelz7k,1158
28
28
  content_core/processors/url.py,sha256=6WT8Sw2VHiKyhgWXi_jZjKjwnT_QPSPcH4P99RKbjgU,7521
29
29
  content_core/processors/video.py,sha256=3WnZwTswvTLm8PtQhKwoqJ2BH6YZi62dMUjALwJiebo,5196
30
- content_core/processors/youtube.py,sha256=pLyNy6ebz80T0e-XnBnj36o22Gm4s_jfjkGCNtKz0so,7254
30
+ content_core/processors/youtube.py,sha256=y-1KH_1uYyO6l6QpjBtzP1KSBjclYeGlj6zDrloAcPc,7490
31
31
  content_core/tools/__init__.py,sha256=DuJmd7fE-NpDvLP8IW1XY5MUkAQcdks52rn2jk4N8jQ,231
32
32
  content_core/tools/cleanup.py,sha256=5IdKedsFyRQMdYzgFSKtsfyxJldbroXQXHesHICNENI,523
33
33
  content_core/tools/extract.py,sha256=-r2_jsuMMXyXxGVqWhh1ilNPo_UMYAbw3Pkp1FzPy5g,577
34
34
  content_core/tools/summarize.py,sha256=DPfeglLWB08q8SvHrsKpOKZ35XjduUDs2J02ISwjdj0,596
35
- content_core-1.0.1.dist-info/METADATA,sha256=ANYJN5e0FUF1rozZd1dpbncKwHiUJlLlu_DsF43DDAE,11819
36
- content_core-1.0.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
37
- content_core-1.0.1.dist-info/entry_points.txt,sha256=9fGQUk6bxBVXj9PRwfWVPn54ClSEJV7J-KBLXtjOhQw,99
38
- content_core-1.0.1.dist-info/licenses/LICENSE,sha256=myj0z2T4qIkenCgLsRfx7Wk6UqCQNj5c7O14Qx4zpGg,1066
39
- content_core-1.0.1.dist-info/RECORD,,
35
+ content_core-1.0.3.dist-info/METADATA,sha256=R6woFztrB88fGVSOVMkrBFAgqwPAPrIw5MClIUKq1Xo,11908
36
+ content_core-1.0.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
37
+ content_core-1.0.3.dist-info/entry_points.txt,sha256=9fGQUk6bxBVXj9PRwfWVPn54ClSEJV7J-KBLXtjOhQw,99
38
+ content_core-1.0.3.dist-info/licenses/LICENSE,sha256=myj0z2T4qIkenCgLsRfx7Wk6UqCQNj5c7O14Qx4zpGg,1066
39
+ content_core-1.0.3.dist-info/RECORD,,