'SOLVED: making program to distill Titles and URLS from Youtube Search results
I am writing a program (code below) in Python that is a kind of directer way to go to a youtube video, avoiding the Youtube user interface.
My problems are solved, I share the code here in case anybody is interested, I managed very elegantly with Regex.
#! /usr/bin/python3
#!/usr/bin/env python
from bs4 import BeautifulSoup
import re
import urllib.request
import webbrowser
from tkinter import *
win=Tk()
win.title("video search")
win.geometry('800x400')
var = Entry(win, width=20)
var.place(x=2,y=2)
def titlee(URL):
html = urllib.request.urlopen(URL)
xx=html.read().decode()
video_ids = re.findall(r"watch\?v=(\S{11})", xx)
titleee= re.findall('title\":{.*?accessibility', xx)
title=[0,0,0,0,0,0,0,0]
URL=[0,0,0,0,0,0,0,0]
for x in range(0, 6):
print(titleee[x])
title[x] = re.findall(':\".*?\"}',titleee[x])
URL[x] = video_ids[x]
return(URL, title)
def func():#function of the button
x = str(var.get())
URL = "https://www.youtube.com/results?search_query=" + str(x) + "&sp=EgIQAQ%253D%253D"
URL = URL.replace(" ","+")
URL, title = titlee(URL)
for x in range(0, 6):
NAME = "btn" + str(x)
globals()[NAME] = Button(win,text=title[x], width=45,height=1, anchor="w", command=(lambda t=x:func2(URL[t], title[t])))
position = 70 + x * 40
globals()[NAME].place(x=2,y=position)
def func2(URL, title):
Func = open("GFG-1.html","w")
Func.write("""<html>\n<head>\n<title> """ + str(title) + """
</title>\n</head> <body><iframe width="560" height="315" src="https://www.youtube.com/embed/""" + URL + """?autoplay=1 "title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></body></html>""")
Func.close()
webbrowser.open('GFG-1.html')
btn=Button(win,text="OK", width=2,height=1,command=func)
btn.place(x=0,y=30)
mainloop()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
