Skip to content

Commit

Permalink
feat(channel): ✨ support get channel by channel handle
Browse files Browse the repository at this point in the history
  • Loading branch information
MerleLiuKun committed Feb 4, 2024
1 parent a531987 commit a918104
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
10 changes: 9 additions & 1 deletion pyyoutube/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,7 @@ def get_channel_info(
self,
*,
channel_id: Optional[Union[str, list, tuple, set]] = None,
for_handle: Optional[str] = None,
for_username: Optional[str] = None,
mine: Optional[bool] = None,
parts: Optional[Union[str, list, tuple, set]] = None,
Expand All @@ -744,6 +745,11 @@ def get_channel_info(
channel_id ((str,list,tuple,set), optional):
The id or comma-separated id string for youtube channel which you want to get.
You can also pass this with an id list, tuple, set.
for_handle (str, optional):
The parameter specifies a YouTube handle, thereby requesting the channel associated with that handle.
The parameter value can be prepended with an @ symbol. For example, to retrieve the resource for
the "Google for Developers" channel, set the forHandle parameter value to
either GoogleDevelopers or @GoogleDevelopers.
for_username (str, optional):
The name for YouTube username which you want to get.
Note: This name may the old youtube version's channel's user's username, Not the the channel name.
Expand All @@ -768,7 +774,9 @@ def get_channel_info(
"part": enf_parts(resource="channels", value=parts),
"hl": hl,
}
if for_username is not None:
if for_handle is not None:
args["forHandle"] = for_handle

Check warning on line 778 in pyyoutube/api.py

View check run for this annotation

Codecov / codecov/patch

pyyoutube/api.py#L778

Added line #L778 was not covered by tests
elif for_username is not None:
args["forUsername"] = for_username
elif channel_id is not None:
args["id"] = enf_comma_separated("channel_id", channel_id)
Expand Down
10 changes: 9 additions & 1 deletion pyyoutube/resources/channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ChannelsResource(Resource):
def list(
self,
parts: Optional[Union[str, list, tuple, set]] = None,
for_handle: Optional[str] = None,
for_username: Optional[str] = None,
channel_id: Optional[Union[str, list, tuple, set]] = None,
managed_by_me: Optional[bool] = None,
Expand All @@ -36,6 +37,11 @@ def list(
Comma-separated list of one or more channel resource properties.
Accepted values: id,auditDetails,brandingSettings,contentDetails,contentOwnerDetails,
localizations,snippet,statistics,status,topicDetails
for_handle:
The parameter specifies a YouTube handle, thereby requesting the channel associated with that handle.
The parameter value can be prepended with an @ symbol. For example, to retrieve the resource for
the "Google for Developers" channel, set the forHandle parameter value to
either GoogleDevelopers or @GoogleDevelopers.
for_username:
The parameter specifies a YouTube username, thereby requesting
the channel associated with that username.
Expand Down Expand Up @@ -90,7 +96,9 @@ def list(
"pageToken": page_token,
**kwargs,
}
if for_username is not None:
if for_handle is not None:
params["forHandle"] = for_handle
elif for_username is not None:
params["forUsername"] = for_username
elif channel_id is not None:
params["id"] = enf_comma_separated(field="channel_id", value=channel_id)
Expand Down
2 changes: 1 addition & 1 deletion testdata/apidata/channel_info_single.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"snippet": {
"title": "Google Developers",
"description": "The Google Developers channel features talks from events, educational series, best practices, tips, and the latest updates across our products and platforms.",
"customUrl": "googledevelopers",
"customUrl": "@googledevelopers",
"publishedAt": "2007-08-23T00:34:43.000Z",
"thumbnails": {
"default": {
Expand Down
8 changes: 8 additions & 0 deletions tests/apis/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,14 @@ def testGetChannelInfo(self) -> None:
)
self.assertEqual(res_by_channel_id.items[0].id, "UC_x5XG1OV2P6uZZ5FSM9Ttw")

res_by_channel_handle = self.api.get_channel_info(
for_username="googledevelopers", return_json=True
)
self.assertEqual(
res_by_channel_handle["items"][0]["snippet"]["customUrl"],
"@googledevelopers",
)

res_by_channel_name = self.api.get_channel_info(
for_username="GoogleDevelopers", return_json=True
)
Expand Down
6 changes: 6 additions & 0 deletions tests/clients/test_channels.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def test_list(self, helpers, authed_cli, key_cli):
assert res.items[0].id == self.channel_id
assert key_cli.channels.api_key == "api key"

res = key_cli.channels.list(
parts="id,snippet",
for_handle="@googledevelopers",
)
assert res.items[0].snippet.customUrl == "@googledevelopers"

res = key_cli.channels.list(
parts=["id", "snippet"], for_username="googledevelopers"
)
Expand Down

0 comments on commit a918104

Please sign in to comment.