Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tqdm import tqdm
- import requests
- import json
- class YTstats:
- def __init__(self, api_key, channel_id) -> None:
- self.api_key = api_key
- self.channel_id = channel_id
- self.channel_stats = None
- self.video_data = None
- def get_channel_stats(self):
- url = f'https://www.googleapis.com/youtube/v3/channels?part=statistics&id={self.channel_id}&key={self.api_key}'
- json_url = requests.get(url)
- data = json.loads(json_url.text)
- try:
- data = data["items"][0]["statistics"]
- except:
- data = None
- self.channel_stats = data
- return data
- def get_channel_video_data(self):
- channel_videos = self.__get_channel_videos(limit=50)
- parts = ["snippet", "statistics", "contentDetails"]
- for videoId in tqdm(channel_videos):
- for part in parts:
- data = self.__get_single_video_data(videoId, part)
- channel_videos[videoId].update(data)
- self.video_data = channel_videos
- return channel_videos
- def __get_single_video_data(self, videoId, part):
- url = f"https://www.googleapis.com/youtube/v3/videos?part={part}&id={videoId}&key={self.api_key}"
- json_url = requests.get(url)
- data = json.loads(json_url.text)
- try:
- data = data["items"][0][part]
- except:
- data = dict()
- return data
- def __get_channel_videos(self, limit=None):
- url = f"https://www.googleapis.com/youtube/v3/search?key={self.api_key}&channelId={self.channel_id}&part=id&order=date"
- if limit != None and isinstance(limit, int):
- url += f"&maxResults={str(limit)}"
- vid, npt = self.__get_videos_per_page(url)
- idx = 0
- while npt != None and idx < 10:
- nextUrl = url + f"&pageToken={npt}"
- next_vid, npt = self.__get_videos_per_page(nextUrl)
- vid.update(next_vid)
- idx += 1
- return vid
- def __get_videos_per_page(self, url):
- json_url = requests.get(url)
- data = json.loads(json_url.text)
- channel_videos = dict()
- if "items" not in data:
- return channel_videos, None
- item_data = data["items"]
- nextPageToken = data.get("nextPageToken", None)
- for item in item_data:
- try:
- kind = item["id"]["kind"]
- if kind == "youtube#video":
- videoId = item["id"]["videoId"]
- channel_videos[videoId] = dict()
- except:
- pass
- return channel_videos, nextPageToken
- def dump(self):
- if self.channel_stats == None or self.video_data == None:
- print("data is none")
- return
- fused_data = {
- self.channel_id: {
- "channel_stats": self.channel_stats,
- "video_data": self.video_data
- }
- }
- channel_titel = self.video_data.popitem()[1].get(
- "channelTitle",
- self.channel_id
- )
- channel_titel = channel_titel.replace(" ", "_").lower()
- file_name = channel_titel + ".json"
- with open(file_name, "w+") as f:
- json.dump(fused_data, f, indent=4)
Advertisement
Add Comment
Please, Sign In to add comment