1
1
mirror of https://github.com/neosubhamoy/pytubepp-helper.git synced 2026-02-04 19:32:21 +05:30

(refactor): switched to pytubepp from pytubefix to fetch video info

This commit is contained in:
2024-10-02 09:08:52 +05:30
Unverified
parent d3811b9df4
commit 0bea9af5c9
4 changed files with 7 additions and 75 deletions

View File

@@ -45,8 +45,8 @@
},
{
"name": "fetch-video-info",
"cmd": "pytubefix",
"args": [{ "validator": "\\S+"}, "--list"]
"cmd": "pytubepp",
"args": [{ "validator": "\\S+"}, "--raw-info"]
}
]

View File

@@ -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 = /<Stream: [^>]+>/g;
const matches = input.match(regex);
return matches ? matches : [];
}
function parseAttributes(attributesString: string): Partial<Stream> {
const attributes: Partial<Stream> = {};
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<Stream>] = value;
}
}
return attributes;
}
export function convert_xml_to_json(xmlStrings: string[]): Stream[] {
return xmlStrings
.map(xmlString => {
const attributesString = xmlString.replace('<Stream: ', '').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);
}