From 0bea9af5c968c978b8b153c1923e5fa21b85c87b Mon Sep 17 00:00:00 2001 From: neosubhamoy Date: Wed, 2 Oct 2024 09:08:52 +0530 Subject: [PATCH] (refactor): switched to pytubepp from pytubefix to fetch video info --- linux/src-tauri/tauri.conf.json | 4 ++-- linux/src/lib/utils.ts | 36 +---------------------------- windows/src-tauri/tauri.conf.json | 4 ++-- windows/src/lib/utils.ts | 38 ++----------------------------- 4 files changed, 7 insertions(+), 75 deletions(-) diff --git a/linux/src-tauri/tauri.conf.json b/linux/src-tauri/tauri.conf.json index d069d41..587c50b 100644 --- a/linux/src-tauri/tauri.conf.json +++ b/linux/src-tauri/tauri.conf.json @@ -55,8 +55,8 @@ }, { "name": "fetch-video-info", - "cmd": "pytubefix", - "args": [{ "validator": "\\S+"}, "--list"] + "cmd": "pytubepp", + "args": [{ "validator": "\\S+"}, "--raw-info"] } ] diff --git a/linux/src/lib/utils.ts b/linux/src/lib/utils.ts index 31fbd02..34ec9a5 100644 --- a/linux/src/lib/utils.ts +++ b/linux/src/lib/utils.ts @@ -1,41 +1,8 @@ import { type ClassValue, clsx } from "clsx" import { twMerge } from "tailwind-merge" import { Command } from '@tauri-apps/api/shell'; -import { Stream } from "@/types"; import { invoke } from "@tauri-apps/api"; -export function extractXml(input: string): string[] { - const regex = /]+>/g; - const matches = input.match(regex); - return matches ? matches : []; -} - - -function parseAttributes(attributesString: string): Partial { - const attributes: Partial = {}; - const regex = /(\w+)="([^"]*)"/g; - let match; - - while ((match = regex.exec(attributesString)) !== null) { - const key = match[1]; - const value = match[2]; - if (['itag', 'mime_type', 'res', 'fps', 'vcodec'].includes(key)) { - attributes[key as keyof Partial] = value; - } - } - - return attributes; -} - -export function convertXmlToJson(xmlStrings: string[]): Stream[] { - return xmlStrings - .map(xmlString => { - const attributesString = xmlString.replace('', ''); - return parseAttributes(attributesString); - }) - .filter(stream => stream.res !== undefined) as Stream[]; -} - export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } @@ -116,8 +83,7 @@ export async function sendStreamInfo(url: string) { console.log(output.stdout); const sendStreamData = async () => { try { - const streamsstr = JSON.stringify(convertXmlToJson(extractXml(output.stdout))) - await invoke('receive_frontend_response', { response: streamsstr }); + await invoke('receive_frontend_response', { response: output.stdout }); } catch (error) { console.error(error); } diff --git a/windows/src-tauri/tauri.conf.json b/windows/src-tauri/tauri.conf.json index 3bbef61..0b14b9f 100644 --- a/windows/src-tauri/tauri.conf.json +++ b/windows/src-tauri/tauri.conf.json @@ -45,8 +45,8 @@ }, { "name": "fetch-video-info", - "cmd": "pytubefix", - "args": [{ "validator": "\\S+"}, "--list"] + "cmd": "pytubepp", + "args": [{ "validator": "\\S+"}, "--raw-info"] } ] diff --git a/windows/src/lib/utils.ts b/windows/src/lib/utils.ts index 7757ce1..4421f55 100644 --- a/windows/src/lib/utils.ts +++ b/windows/src/lib/utils.ts @@ -1,41 +1,8 @@ import { type ClassValue, clsx } from "clsx" import { twMerge } from "tailwind-merge" import { Command } from '@tauri-apps/api/shell'; -import { Stream } from "@/types"; import { invoke } from "@tauri-apps/api"; -export function extract_xml(input: string): string[] { - const regex = /]+>/g; - const matches = input.match(regex); - return matches ? matches : []; -} - - -function parseAttributes(attributesString: string): Partial { - const attributes: Partial = {}; - const regex = /(\w+)="([^"]*)"/g; - let match; - - while ((match = regex.exec(attributesString)) !== null) { - const key = match[1]; - const value = match[2]; - if (['itag', 'mime_type', 'res', 'fps', 'vcodec'].includes(key)) { - attributes[key as keyof Partial] = value; - } - } - - return attributes; -} - -export function convert_xml_to_json(xmlStrings: string[]): Stream[] { - return xmlStrings - .map(xmlString => { - const attributesString = xmlString.replace('', ''); - return parseAttributes(attributesString); - }) - .filter(stream => stream.res !== undefined) as Stream[]; -} - export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } @@ -76,13 +43,12 @@ export function extract_version(output: string): string | null { export async function sendStreamInfo(url: string) { const fetchData = async () => { try { - const output = await new Command('fetch-video-info', [url, '--list']).execute(); + const output = await new Command('fetch-video-info', [url, '--raw-info']).execute(); if (output.code === 0) { console.log(output.stdout); const sendStreamData = async () => { try { - const streamsstr = JSON.stringify(convert_xml_to_json(extract_xml(output.stdout))) - await invoke('receive_frontend_response', { response: streamsstr }); + await invoke('receive_frontend_response', { response: output.stdout }); } catch (error) { console.error(error); }