Skip to content

Commit 9de6dbb

Browse files
authored
Create pyyoutube.py
1 parent cddab73 commit 9de6dbb

File tree

1 file changed

+258
-0
lines changed

1 file changed

+258
-0
lines changed

py_youtube/pyyoutube.py

Lines changed: 258 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,258 @@
1+
"""
2+
(C) @mrlokaman
3+
All rights reserved by LN Technical
4+
5+
"""
6+
7+
"""
8+
MIT License
9+
10+
Copyright (c) 2021 Lntechnical
11+
12+
Permission is hereby granted, free of charge, to any person obtaining a copy
13+
of this software and associated documentation files (the "Software"), to deal
14+
in the Software without restriction, including without limitation the rights
15+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16+
copies of the Software, and to permit persons to whom the Software is
17+
furnished to do so, subject to the following conditions:
18+
19+
The above copyright notice and this permission notice shall be included in all
20+
copies or substantial portions of the Software.
21+
22+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28+
SOFTWARE.
29+
30+
"""
31+
32+
33+
# This Modules Helps To Get Youtube Video Data
34+
35+
36+
import re
37+
import urllib.request
38+
import json
39+
40+
class Data:
41+
42+
"""
43+
Get YouTube Video Data
44+
45+
"""
46+
def __init__(self,link):
47+
headers = { 'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64)' }
48+
res = urllib.request.Request(link, headers=headers)
49+
html = urllib.request.urlopen(res)
50+
self.source = html.read().decode('utf8')
51+
52+
53+
# Get Video id
54+
def id(self):
55+
try:
56+
videodetails=re.findall("\"videoDetails\":\{(.+?),\"isOwnerViewing", self.source)[0]
57+
except:
58+
return None
59+
try:
60+
#Get id , title From videodetails variable
61+
id = re.findall("\"videoId\":\"(\S{11})",videodetails)[0]
62+
return id
63+
except:
64+
return None
65+
66+
67+
68+
# Get Video Title
69+
def title(self):
70+
try:
71+
videodetails=re.findall("\"videoDetails\":\{(.+?),\"isOwnerViewing", self.source)[0]
72+
except:
73+
return None
74+
try:
75+
#Get id , title From videodetails variable
76+
title = re.findall("\"title\":\"(.+?)\",",videodetails)[0]
77+
return title
78+
except:
79+
return None
80+
81+
82+
83+
#Get Thumbnails Link From Youtube Video
84+
def thumb(self):
85+
try :
86+
thumb= re.findall("\"thumbnails\":\[\{\"url\":\"(.+?)\",\"width",self.source )[0]
87+
return thumb
88+
except:
89+
return None
90+
91+
92+
# Get Video Publish Date
93+
def publish_date(self):
94+
try:
95+
publish_date = re.findall("\"publishDate\":\"(\d{4}-\d{2}-\d{2})", self.source)[0]
96+
return publish_date
97+
except:
98+
return None
99+
100+
101+
# Get Views Of the Video
102+
def views(self):
103+
try:
104+
views = re.findall("\"viewCount\":\"(\d+)",self.source)[0]
105+
return views
106+
except:
107+
return None
108+
109+
110+
# Get Category Of The Video
111+
def category(self):
112+
try:
113+
category = re.findall("\"category\":\"(.+?)\",", self.source)[0]
114+
return category
115+
except:
116+
return None
117+
118+
119+
120+
# Get Channel Name
121+
def channel_name(self):
122+
try:
123+
channelName = e.findall("\"channelName\":\"(.+?)\",", self.source)[0]
124+
return channelName
125+
except:
126+
try:
127+
channelName = re.findall("\"ownerChannelName\":\"(.+?)\",\"uploadDate",self.source)[0]
128+
return channelName
129+
except:
130+
return None
131+
132+
133+
134+
#Get likes Of The Video
135+
def likes(self):
136+
try:
137+
likes = re.findall("accessibilityData\":{\"label\":\"(\S+) likes", self.source)[0]
138+
return likes
139+
except:
140+
return None
141+
142+
143+
144+
#Get dislikes Of The Video
145+
def dislikes(self):
146+
try:
147+
dislikes = re.findall("accessibilityData\":{\"label\":\"(\S+) dislike", self.source)[0]
148+
return dislikes
149+
except:
150+
return None
151+
152+
# Get YouTube Videos tag
153+
def tags(self):
154+
try:
155+
tags = re.findall("\<meta name=\"keywords\" content=\"(.+?)\">",self.source)[0]
156+
return tags
157+
except:
158+
return None
159+
160+
161+
162+
#Get Subscriber of The video
163+
def subscriber(self):
164+
try:
165+
sub = re.findall("subscriberCountText\":{\"accessibility\":{\"accessibilityData\":{\"label\":\"(.+?)\"}}", self.source)[0]
166+
return sub
167+
except:
168+
try:
169+
sub = re.findall("accessibilityData\":{\"label\":\"(\S+) subscribers", self.source)[0]
170+
return sub
171+
except:
172+
return None
173+
174+
def data(self):
175+
try:
176+
# Get Video Details
177+
videodetails=re.findall("\"videoDetails\":\{(.+?),\"isOwnerViewing", self.source)[0]
178+
except:
179+
videodetails = None
180+
try:
181+
#Get id , title From videodetails variable
182+
id = re.findall("\"videoId\":\"(\S{11})",videodetails)[0]
183+
title = re.findall("\"title\":\"(.+?)\",",videodetails)[0]
184+
except:
185+
title = None
186+
id = None
187+
try :
188+
#Get Thumbnails Link From Youtube Video
189+
thumb= re.findall("\"thumbnails\":\[\{\"url\":\"(.+?)\",\"width",self.source )[0]
190+
except:
191+
thumb = None
192+
try:
193+
# Get Video Publish Date
194+
publish_date = re.findall("\"publishDate\":\"(\d{4}-\d{2}-\d{2})", self.source)[0]
195+
except:
196+
publish_date = None
197+
try:
198+
# Get Views Of the Video
199+
views = re.findall("\"viewCount\":\"(\d+)",self.source)[0]
200+
except:
201+
views = None
202+
try:
203+
# Get Category Of The Video
204+
category = re.findall("\"category\":\"(.+?)\",", self.source)[0]
205+
except:
206+
category = None
207+
try:
208+
# Get Channel Name
209+
channelName = e.findall("\"channelName\":\"(.+?)\",", self.source)[0]
210+
except:
211+
try:
212+
channelName = re.findall("\"ownerChannelName\":\"(.+?)\",\"uploadDate",self.source)[0]
213+
except:
214+
channelName = None
215+
216+
try:
217+
#Get likes Of The Video
218+
likes = re.findall("accessibilityData\":{\"label\":\"(\S+) likes", self.source)[0]
219+
except:
220+
likes = None
221+
try:
222+
#Get dislikes Of The Video
223+
dislikes = re.findall("accessibilityData\":{\"label\":\"(\S+) dislike", self.source)[0]
224+
except:
225+
dislikes = None
226+
try:
227+
#Get Subscriber of The video
228+
sub = re.findall("accessibilityData\":{\"label\":\"(\S+) subscribers", self.source)[0]
229+
except:
230+
try:
231+
sub = re.findall("subscriberCountText\":{\"accessibility\":{\"accessibilityData\":{\"label\":\"(.+?)\"}}", self.source)[0]
232+
except:
233+
sub = None
234+
# kewords(tag)
235+
try:
236+
tags = re.findall("\<meta name=\"keywords\" content=\"(.+?)\">",self.source)[0]
237+
except:
238+
tags = None
239+
240+
241+
242+
243+
DATA = {
244+
"id": id,
245+
"title": title,
246+
"thumbnails": thumb,
247+
"duration":duration,
248+
"views": views,
249+
"likes": likes,
250+
"dislikes":dislikes,
251+
"publishdate": publish_date,
252+
"category": category,
253+
"channel_name": channelName,
254+
"subscriber":sub,
255+
"keywords":tags
256+
}
257+
258+
return DATA

0 commit comments

Comments
 (0)