-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebscrapping.py
More file actions
82 lines (57 loc) · 2.15 KB
/
webscrapping.py
File metadata and controls
82 lines (57 loc) · 2.15 KB
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
from pyyoutube import Api
import re
import urllib.request
# '' ""
def prepare_str(text):
return text.replace('"', '\"').replace("'", "\'").replace("&", "&")
# '' ""
def video_title(url):
if re.search("list=", url):
return "Playlist "
api = Api(api_key="AIzaSyDcdVmOCOABKydxtgqP7H8ucvRSpIxDnP4")
if re.search(r"(&|\?)t=\d+[s]?", url):
url = re.sub(r"(&|\?)t=\d+[s]?", "", url)
if re.search("&ab_channel=", url):
url = url.split("&ab_channel=")
url = url[0]
url = re.sub(r"http[s]?://", "", url)
url = re.sub(r"www\.youtube\.com/(watch\?v=|embed/)", "", url)
video_id = url.replace("youtu\.be\/", "")
data = api.get_video_by_id(video_id=video_id, return_json=True)
data = data["items"][0]["snippet"]["title"]
return prepare_str(data)
# '' "" AIzaSyDcdVmOCOABKydxtgqP7H8ucvRSpIxDnP4
def yt_search(search, searches):
s_links = []
s_titles = []
s_thumbnail = []
api = Api(api_key="AIzaSyDcdVmOCOABKydxtgqP7H8ucvRSpIxDnP4")
data = api.search_by_keywords(
q=search, search_type=["video", "playlist"], count=searches).to_dict()
data = data["items"]
# print(data[0])
for video_data in data:
key = "videoId" if video_data["id"]["kind"] == "youtube#video" else "playlistId"
before_id = "/playlist?list=" if key == "playlistId" else "/watch?v="
title = prepare_str(video_data["snippet"]["title"])
if title in s_titles:
old_name = title
n = 1
while True:
if title in s_titles:
title = f"{old_name}{n*' '}"
else:
break
n += 1
s_titles.append(title)
else:
s_titles.append(title)
with urllib.request.urlopen(prepare_str(video_data["snippet"]["thumbnails"]["default"]["url"])) as response:
s_thumbnail.append(response.read())
s_links.append(f'{before_id}{video_data["id"][key]}')
return s_titles, s_links, s_thumbnail
# ''
if __name__ == "__main__":
while True:
search = input("search: ")
yt_search(search, 35)