-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathyoutube.py
68 lines (57 loc) · 1.77 KB
/
youtube.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
# -*- coding: utf-8 -*-
"""Youtube.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/10DgyLM2JahuQ6pqNZprmp4gzjiR8sL8r
"""
!pip install pytube
!pip install jinja2
from pytube import YouTube
from jinja2 import Template
# Function to get video details
def get_video_details(video_url):
try:
yt = YouTube(video_url)
return yt
except Exception as e:
print("An error occurred:", str(e))
return None
# Example usage to get video details
video_url = input("Paste the YouTube video link: ")
video = get_video_details(video_url)
if video:
# Generate the HTML template
template = Template('''
<html>
<head>
<title>{{ video_title }}</title>
</head>
<body>
<h1>{{ video_title }}</h1>
<p>{{ video_description }}</p>
<h2>Channel: {{ channel_name }}</h2>
<p>Length (seconds): {{ video_length }}</p>
<p>Age Restricted: {{ video_age_restricted }}</p>
<p>Captions: {{ video_captions }}</p>
<p>Keywords: {{ video_keywords }}</p>
<p>Views: {{ video_views }}</p>
</body>
</html>
''')
# Render the template with the data
rendered_html = template.render(
video_title=video.title,
video_description=video.description,
channel_name=video.author,
video_length=video.length,
video_age_restricted=video.age_restricted,
video_captions=video.captions,
video_keywords=video.keywords,
video_views=video.views
)
# Save the rendered HTML to a file
with open('output.html', 'w', encoding='utf-8') as file:
file.write(rendered_html)
print("HTML file generated successfully.")
else:
print("Invalid YouTube video link.")