diff options
| author | Pinapelz <yukais@pinapelz.com> | 2024-04-13 01:20:53 -0700 |
|---|---|---|
| committer | Pinapelz <yukais@pinapelz.com> | 2024-04-13 01:20:53 -0700 |
| commit | 6d084b98235bf1799e17fc17aad7ab9894621915 (patch) | |
| tree | 3ef588dbe0b5bfb723e17d94f90e2303455852f8 /backend/webapi | |
| parent | 5066658bf9c6f5a515efeb0c69793734a53e9755 (diff) | |
change backend API to a submodule
Diffstat (limited to 'backend/webapi')
| -rw-r--r-- | backend/webapi/holodex.py | 75 | ||||
| -rw-r--r-- | backend/webapi/web_api.py | 29 | ||||
| -rw-r--r-- | backend/webapi/youtube.py | 61 |
3 files changed, 0 insertions, 165 deletions
diff --git a/backend/webapi/holodex.py b/backend/webapi/holodex.py deleted file mode 100644 index 5f81892..0000000 --- a/backend/webapi/holodex.py +++ /dev/null @@ -1,75 +0,0 @@ -from webapi.web_api import WebAPI -from typing import Iterable - - -class HolodexAPI(WebAPI): - """ - Class for interacting with the Holodex API - """ - - def __init__(self,api_key: str = None,member_count: int = 300,organization: str = "Nijisanji"): - super().__init__(api_key=api_key, base_url="https://holodex.net/api/v2/") - self.member_count = member_count - self.organization = organization - self._inactive_channels = [] - self._channel_data = [] - - def get_subscriber_data(self) -> Iterable: - """ - Gets data for all channels in a particular organization - """ - members = self.member_count - data = [] - active_channels = [] - offset = 0 - while members > 0: - data += self._download_url( - f"channels?type=vtuber&offset={offset}&limit=100&org={self.organization}" - ) - members -= 100 - offset += 100 - for channel in data: - print("DEBUG: ", channel["id"]) - try: - channel["description"] = self.get_channel_description(channel["id"]) - if channel["inactive"]: - self._inactive_channels.append(channel["id"]) - continue - active_channels.append(channel) - except (KeyError, TypeError, ValueError): - print("DEBUG:","An error occured with parsing ", channel["id"], channel["name"]) - continue - self._channel_data = active_channels - return active_channels - - def get_view_count(self, channel_id: str) -> int: - """ - Gets the view count for a particular channel - """ - data = self._download_url(f"channels/{channel_id}") - return data["view_count"] - - def get_channel_description(self, channel_id: str) -> str: - """ - Gets the description for a particular channel - """ - data = self._download_url(f"channels/{channel_id}") - return data["description"] - - def set_organization(self, organization: str): - """ - Sets the organization for the API - """ - self.organization = organization - - def get_inactive_channels(self) -> list: - """ - Gets the list of inactive channels - """ - return self._inactive_channels - - def get_generated_channel_data(self) -> list: - """ - Gets the list of channel data - """ - return self._channel_data diff --git a/backend/webapi/web_api.py b/backend/webapi/web_api.py deleted file mode 100644 index 525994c..0000000 --- a/backend/webapi/web_api.py +++ /dev/null @@ -1,29 +0,0 @@ -import urllib.request -import json - - -class WebAPI: - """ - General class for interacting with web APIs - """ - - def __init__(self, api_key: str, base_url: str) -> None: - self.api_key = api_key - self.base_url = base_url - - def _download_url(self, query: str, header = 'X-APIKEY') -> dict: - """ - Downloads the URL and returns the result as a string - param: - query: str - the query to be appended to the base URL - """ - if self.api_key is None: - raise Exception("API key not set") - opener = urllib.request.build_opener() - opener.addheaders = [(header, self.api_key)] - urllib.request.install_opener(opener) - response = urllib.request.urlopen(self.base_url + query) - json_results = response.read() - r_obj = json.loads(json_results) - response.close() - return r_obj diff --git a/backend/webapi/youtube.py b/backend/webapi/youtube.py deleted file mode 100644 index a25f5ba..0000000 --- a/backend/webapi/youtube.py +++ /dev/null @@ -1,61 +0,0 @@ -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 |
