From 133480a05d7a1840e0d5a59e48d55f73fc70ddcf Mon Sep 17 00:00:00 2001 From: jeffser Date: Wed, 25 Dec 2024 19:26:02 -0600 Subject: [PATCH] Removed PyTube dependency and fixed youtube integration --- com.jeffser.Alpaca.json | 14 -------------- src/generic_actions.py | 4 +++- src/window.py | 22 +++++++--------------- 3 files changed, 10 insertions(+), 30 deletions(-) diff --git a/com.jeffser.Alpaca.json b/com.jeffser.Alpaca.json index d3320f8f..41b09060 100644 --- a/com.jeffser.Alpaca.json +++ b/com.jeffser.Alpaca.json @@ -99,20 +99,6 @@ } ] }, - { - "name": "python3-pytube", - "buildsystem": "simple", - "build-commands": [ - "pip3 install --verbose --exists-action=i --no-index --find-links=\"file://${PWD}\" --prefix=${FLATPAK_DEST} \"pytube\" --no-build-isolation" - ], - "sources": [ - { - "type": "file", - "url": "https://files.pythonhosted.org/packages/51/64/bcf8632ed2b7a36bbf84a0544885ffa1d0b4bcf25cc0903dba66ec5fdad9/pytube-15.0.0-py3-none-any.whl", - "sha256": "07b9904749e213485780d7eb606e5e5b8e4341aa4dccf699160876da00e12d78" - } - ] - }, { "name": "python3-youtube-transcript-api", "buildsystem": "simple", diff --git a/src/generic_actions.py b/src/generic_actions.py index 29bbab1a..bafebe08 100644 --- a/src/generic_actions.py +++ b/src/generic_actions.py @@ -5,6 +5,7 @@ import os, requests from youtube_transcript_api import YouTubeTranscriptApi +from youtube_transcript_api.formatters import TextFormatter from html2text import html2text from .internal import cache_dir @@ -40,7 +41,8 @@ def attach_youtube(video_title:str, video_author:str, watch_url:str, video_url:s else: transcript = YouTubeTranscriptApi.get_transcript(video_id, languages=[caption_name]) - result_text += '\n'.join([t['text'] for t in transcript]) + result_text += TextFormatter().format_transcript(transcript) + #result_text += '\n'.join([t['text'] for t in transcript]) if not os.path.exists(os.path.join(cache_dir, 'tmp/youtube')): os.makedirs(os.path.join(cache_dir, 'tmp/youtube')) diff --git a/src/window.py b/src/window.py index 7bb4d79b..c1234110 100644 --- a/src/window.py +++ b/src/window.py @@ -19,14 +19,13 @@ """ Handles the main window """ -import json, threading, os, re, base64, gettext, uuid, shutil, logging, time +import json, threading, os, re, base64, gettext, uuid, shutil, logging, time, requests import odf.opendocument as odfopen import odf.table as odftable from io import BytesIO from PIL import Image from pypdf import PdfReader from datetime import datetime -from pytube import YouTube import gi gi.require_version('GtkSource', '5') @@ -875,17 +874,10 @@ def current_chat_actions(self, action, user_data): def youtube_detected(self, video_url): try: - tries=0 - while True: - try: - yt = YouTube(video_url) - video_title = yt.title - break - except Exception as e: - tries+=1 - if tries == 4: - raise Exception(e) - transcriptions = generic_actions.get_youtube_transcripts(yt.video_id) + response = requests.get('https://noembed.com/embed?url={}'.format(video_url)) + data = json.loads(response.text) + + transcriptions = generic_actions.get_youtube_transcripts(data['url'].split('=')[1]) if len(transcriptions) == 0: self.show_toast(_("This video does not have any transcriptions"), self.main_overlay) return @@ -895,8 +887,8 @@ def youtube_detected(self, video_url): dialog_widget.simple_dropdown( _('Attach YouTube Video?'), - _('{}\n\nPlease select a transcript to include').format(video_title), - lambda caption_name, yt=yt, video_url=video_url: generic_actions.attach_youtube(yt.title, yt.author, yt.watch_url, video_url, yt.video_id, caption_name), + _('{}\n\nPlease select a transcript to include').format(data['title']), + lambda caption_name, data=data, video_url=video_url: generic_actions.attach_youtube(data['title'], data['author_name'], data['url'], video_url, data['url'].split('=')[1], caption_name), transcriptions ) except Exception as e: