-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathyt_views.py
86 lines (62 loc) · 2.17 KB
/
yt_views.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
from concurrent.futures import ThreadPoolExecutor
from typing import Optional
from pytube import YouTube
from pytube.exceptions import VideoUnavailable
from utils import logger
from video import YoutubeVideo
class YoutubeViews:
"""Get view counts for given video urls
:type video_urls: list
:param video_urls: List of video urls
"""
def __init__(
self,
*,
video_urls: Optional[list[str]] = None,
) -> None:
self._video_urls = video_urls
self._videos = []
def update(self) -> None:
"""Update views of all videos"""
num_workers = self._get_max_workers()
with ThreadPoolExecutor(max_workers=num_workers) as executor:
results = executor.map(self._update_view, self._video_urls)
for result in results:
if result:
self._videos.append(result)
logger.debug(result)
def _update_view(self, video_url: str) -> Optional[YoutubeVideo]:
"""Get title and view count information for the given video url
Raises VideoUnavailable if video is not accessible or PytubeError
for other issues.
:type video_url: str
:param video_url: Video url
:rtype: YoutubeVideo or None
:returns: YoutubeVideo if video is available, otherwise None
"""
youtube_video = None
try:
video = YouTube(video_url)
youtube_video = YoutubeVideo(video_url, video.title, video.views)
except VideoUnavailable:
logger.error(f"Video {video_url} is unavailable, skipping...")
return youtube_video
@property
def videos(self) -> list[YoutubeVideo]:
"""Return all video objects
:rtype: list
:returns: List of all videos
"""
return self._videos
def _get_max_workers(self) -> int:
"""Calculate number of workers
:rtype: int
:returns: Max number of threads to be used
"""
workers = 0
num_urls = len(self._video_urls)
if num_urls <= 100:
workers = num_urls
else:
workers = (num_urls / 10) * 2
return workers