aboutsummaryrefslogtreecommitdiffstats
path: root/webapi
diff options
context:
space:
mode:
authorPinapelz <donaldshan1@outlook.com>2023-05-04 16:01:14 -0700
committerPinapelz <donaldshan1@outlook.com>2023-05-04 16:01:14 -0700
commited1e2cbe77f93307f0f3bff736e14695a6c59163 (patch)
treeadc588b2c9b2842240ce043e027bc27c7ff7b4d6 /webapi
Fresh initial commit to remove large history
Diffstat (limited to 'webapi')
-rw-r--r--webapi/holodex.py55
-rw-r--r--webapi/web_api.py29
-rw-r--r--webapi/youtube.py55
3 files changed, 139 insertions, 0 deletions
diff --git a/webapi/holodex.py b/webapi/holodex.py
new file mode 100644
index 0000000..9ecad60
--- /dev/null
+++ b/webapi/holodex.py
@@ -0,0 +1,55 @@
+from webapi.web_api import WebAPI
+
+
+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 = []
+
+ def get_data_all_channels(self) -> list:
+ """
+ Gets data for all channels in a particular organization
+ """
+ members = self.member_count
+ data = []
+ filtered_data = []
+ 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:
+ if channel['inactive'] is False:
+ channel['description'] = self.get_channel_description(channel['id'])
+ filtered_data.append(channel)
+ else:
+ self._inactive_channels.append(channel['id'])
+ return filtered_data
+
+ def get_exclude_channels(self) -> list:
+ """
+ Gets the list of excluded channels
+ """
+ return self._inactive_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']
diff --git a/webapi/web_api.py b/webapi/web_api.py
new file mode 100644
index 0000000..1b16681
--- /dev/null
+++ b/webapi/web_api.py
@@ -0,0 +1,29 @@
+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) -> 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 = [('X-APIKEY', 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/webapi/youtube.py b/webapi/youtube.py
new file mode 100644
index 0000000..44d786f
--- /dev/null
+++ b/webapi/youtube.py
@@ -0,0 +1,55 @@
+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:
+ 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']}
+ 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