-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmytube.py
74 lines (61 loc) · 2.21 KB
/
mytube.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import os
import sys
from pytube import YouTube
import urllib
import requests
from bs4 import BeautifulSoup
import subprocess
import logging as log
import c
log.basicConfig(level=log.INFO, format= c.LOG_FORMAT,handlers=[ log.StreamHandler(), log.FileHandler(c.LOG_PATH+'/'+c.LOG_FILE+'.log')])
#log.info('Logging Started')
class YoutubeMetadata:
"""A data store to store the information of a youtube video."""
SPACE = "#"
def __init__self(self):
self.title = ""
self.url = ""
self.duration = ""
self.hash = ""
def search_youtube(query):
"""Behold the greatest magic trick ever : crawl and crawl."""
log.info("Searching youtube for :: {}".format(query))
base_url = "https://www.youtube.com"
url = base_url + "//results?sp=EgIQAVAU&q=" + query
response = requests.get(url)
soup = BeautifulSoup(response.content, "html.parser")
videos = []
for tile in soup.find_all(attrs={'class': "yt-lockup-tile"}):
yt_uix_tile = tile.find(attrs={'class': 'yt-uix-tile-link'})
youtube_metadata = YoutubeMetadata()
youtube_metadata.url = base_url + yt_uix_tile['href']
youtube_metadata.title = yt_uix_tile['title']
description = tile.find("div", {'class': 'yt-lockup-description'})
youtube_metadata.description = description.get_text().strip() if description else "No description available"
duration = tile.find("span", {'class': 'video-time'})
youtube_metadata.duration = duration.get_text() if duration else "uknown duration"
videos.append(youtube_metadata)
return videos
def getFirstStream(link):
yt = YouTube(link)
return yt.streams.first()
def getAllStreams(link):
yt= YouTube(link)
return yt.streams.all()
def downloadV(stream):
try:
os.mkdir("Files/Videos")
except Exception as e:
pass
log.info("Downloading, Please wait....")
stream.download(os.path.join(os.getcwd(),'Files/Videos'))
log.info("Downloaded Successfully.")
return
# subprocess.Popen(r'explorer /select,"D:\LTI\OLD"')
def downloadM(stream):
try:
os.mkdir("Files/Musics")
except Exception as e:
pass
stream.download(os.path.join(os.getcwd(),'Files/Musics'))
return