forked from spaceforinception/dost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
video.py
90 lines (69 loc) · 2.24 KB
/
video.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#https://www.youtube.com/results?search_query=tree
import requests
from bs4 import BeautifulSoup
from urllib.request import urlopen
from messages import n # New Line or \n, in a whatsapp friendly format
from sender import sendMessage
def video(search=''):
if isinstance(search, list):
search_item = '+'.join(search)
search = " ".join(search)
elif search == '':
search_item = "India"
search = "India"
else:
search_item = search
# search = search.replace(" ", "+")
base_url = "https://www.youtube.com"
query = f"/results?search_query={search_item}"
soup = BeautifulSoup(
requests.get(base_url + query).content, # Web Page to Open
'html.parser' # Parser to use
)
results = soup.find_all('a',{'class':'yt-uix-sessionlink'}) # Get all contents from the Session Links
resp = []
vid_counter = 0
sub = {
1:'st',
2:'nd',
3:'rd',
4:'th',
5:'th',
6:'th',
7:'th',
8:'th',
9:'th',
10:'th'
}
for link_no in range(0, len(results), 2):
vid_link = results[link_no].get("href")
if "watch" in vid_link:
vid_counter+=1
resp.append(f"The {int(vid_counter)}{sub[int(vid_counter)]} *most relevent* \nVideo reguarding --> {search} : "+ n + base_url + vid_link)
if len(resp) == 3:
break
if len(resp) == 0:
resp = "No Response From the Server, please Try Again.... "
return resp
def download_video(search=""):
youtubeLinks = video(search)
dlLinks = []
if isinstance(youtubeLinks, list):
place = 63 + len(" ".join(search))
for link in youtubeLinks:
dlLinks.append(link[:place]+'ss'+link[place:])
else:
dlLinks = youtubeLinks
return dlLinks
if __name__ == '__main__':
# search = input("Enter what to Search : ")
search = "Ped Paudha"
search = search.split(' ')
response = download_video(search=search)
if isinstance(response,list):
for video in response[:-1]:
sendMessage(msg=video)
print(video)
response = response[-1]
sendMessage(msg=response)
# print(response)