TerrificTable55

Untitled

Jan 6th, 2022
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1. from tqdm import tqdm
  2. import requests
  3. import json
  4.  
  5.  
  6. class YTstats:
  7.     def __init__(self, api_key, channel_id) -> None:
  8.         self.api_key = api_key
  9.         self.channel_id = channel_id
  10.         self.channel_stats = None
  11.         self.video_data = None
  12.  
  13.     def get_channel_stats(self):
  14.         url = f'https://www.googleapis.com/youtube/v3/channels?part=statistics&id={self.channel_id}&key={self.api_key}'
  15.         json_url = requests.get(url)
  16.         data = json.loads(json_url.text)
  17.  
  18.         try:
  19.             data = data["items"][0]["statistics"]
  20.         except:
  21.             data = None
  22.  
  23.         self.channel_stats = data
  24.         return data
  25.  
  26.     def get_channel_video_data(self):
  27.         channel_videos = self.__get_channel_videos(limit=50)
  28.  
  29.         parts = ["snippet", "statistics", "contentDetails"]
  30.         for videoId in tqdm(channel_videos):
  31.             for part in parts:
  32.                 data = self.__get_single_video_data(videoId, part)
  33.                 channel_videos[videoId].update(data)
  34.  
  35.         self.video_data = channel_videos
  36.         return channel_videos
  37.  
  38.     def __get_single_video_data(self, videoId, part):
  39.         url = f"https://www.googleapis.com/youtube/v3/videos?part={part}&id={videoId}&key={self.api_key}"
  40.         json_url = requests.get(url)
  41.         data = json.loads(json_url.text)
  42.  
  43.         try:
  44.             data = data["items"][0][part]
  45.         except:
  46.             data = dict()
  47.  
  48.         return data
  49.  
  50.     def __get_channel_videos(self, limit=None):
  51.         url = f"https://www.googleapis.com/youtube/v3/search?key={self.api_key}&channelId={self.channel_id}&part=id&order=date"
  52.  
  53.         if limit != None and isinstance(limit, int):
  54.             url += f"&maxResults={str(limit)}"
  55.             vid, npt = self.__get_videos_per_page(url)
  56.             idx = 0
  57.  
  58.             while npt != None and idx < 10:
  59.                 nextUrl = url + f"&pageToken={npt}"
  60.                 next_vid, npt = self.__get_videos_per_page(nextUrl)
  61.                 vid.update(next_vid)
  62.                 idx += 1
  63.         return vid
  64.  
  65.     def __get_videos_per_page(self, url):
  66.         json_url = requests.get(url)
  67.         data = json.loads(json_url.text)
  68.         channel_videos = dict()
  69.  
  70.         if "items" not in data:
  71.             return channel_videos, None
  72.  
  73.         item_data = data["items"]
  74.         nextPageToken = data.get("nextPageToken", None)
  75.  
  76.         for item in item_data:
  77.             try:
  78.                 kind = item["id"]["kind"]
  79.                 if kind == "youtube#video":
  80.                     videoId = item["id"]["videoId"]
  81.                     channel_videos[videoId] = dict()
  82.             except:
  83.                 pass
  84.         return channel_videos, nextPageToken
  85.  
  86.     def dump(self):
  87.         if self.channel_stats == None or self.video_data == None:
  88.             print("data is none")
  89.             return
  90.  
  91.         fused_data = {
  92.             self.channel_id: {
  93.                 "channel_stats": self.channel_stats,
  94.                 "video_data": self.video_data
  95.             }
  96.         }
  97.  
  98.         channel_titel = self.video_data.popitem()[1].get(
  99.             "channelTitle",
  100.             self.channel_id
  101.         )
  102.         channel_titel = channel_titel.replace(" ", "_").lower()
  103.         file_name = channel_titel + ".json"
  104.  
  105.         with open(file_name, "w+") as f:
  106.             json.dump(fused_data, f, indent=4)
  107.  
Advertisement
Add Comment
Please, Sign In to add comment