summaryrefslogtreecommitdiffstats
path: root/webapi/youtube.py
blob: a25f5bae5ac172f3bb68171d763411d2832cef7b (plain) (blame)
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
from webapi.web_api import WebAPI


class YouTubeAPI(WebAPI):
    """
    Class for interacting with the YouTube API
    """

    def __init__(self, api_key: str = None):
        self.api_key = api_key
        self.base_url = "https://www.googleapis.com/youtube/v3/"

    def _search_matching_id(self, id: str, data: list) -> dict:
        """
        Searches for a info matching a given ID
        param:
            id: str - the ID to search for
        """
        for entry in data:
            if entry['id'] == id:
                return entry
        return None

    def get_data_all_channels(self, channel_tuples: list) -> list:
        data = []
        members = len(channel_tuples)
        request_chunks = [channel_tuples[i:i + 50] for i in range(0, members, 50)]
        for chunk in request_chunks:
            channel_ids = [x[0] for x in chunk]
            channel_names = [x[1] for x in chunk]
            request_string = ",".join(channel_ids)
            stats = self._download_url(
                f"channels?part=statistics&id={request_string}&key={self.api_key}")
            snippet = self._download_url(
                f"channels?part=snippet&id={request_string}&key={self.api_key}")
            stats_list = stats['items']
            snippet_list = snippet['items']
            for i in range(len(stats_list)):
                try:
                    # group/sub_org is used to further divide channels into subsets (sorta like teams)
                    # can't think of a better match via YouTube API rn other than customUrl
                    data_entry = {'english_name': channel_names[i], 'id': channel_ids[i],
                                  'subscriber_count':
                                        self._search_matching_id(channel_ids[i], stats_list)['statistics']['subscriberCount'], 
                                    'view_count':
                                        self._search_matching_id(channel_ids[i], stats_list)['statistics']['viewCount'],
                                    'photo': 
                                        self._search_matching_id(channel_ids[i], snippet_list)['snippet']['thumbnails']['default']['url'],
                                    'description': 
                                        self._search_matching_id(channel_ids[i], snippet_list)['snippet']['description'],
                                    'group': 
                                        self._search_matching_id(channel_ids[i], snippet_list)['snippet']['customUrl'],
                                    'video_count':
                                        self._search_matching_id(channel_ids[i], stats_list)['statistics']['videoCount']
                                          }
                    data.append(data_entry)
                except TypeError:
                    print("Error NoneType: " + str(channel_ids[i]))
                except KeyError:
                    print("Error KeyError: " + str(channel_ids[i]))
        return data
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage