Skip to content

Commit 8cffdbf

Browse files
authored
Merge pull request #44 from sns-sdks/feat_subscription
Feat subscription
2 parents d1eea7a + 9a5082d commit 8cffdbf

7 files changed

Lines changed: 130 additions & 12 deletions

File tree

CHANGELOG.rst

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,26 @@
11
Changelog
22
---------
33

4+
Version 0.5.2
5+
=============
6+
7+
What's New
8+
++++++++++
9+
Now you can use authorized access token to get your subscriptions.
10+
You can to the demo `A demo for get my subscription <examples/subscription.py>`_ to see simple usage.
11+
Or you can see the `subscriptions usage <README.rst#subscriptions>`_ docs.
12+
13+
#43 add api for get my subscriptions
14+
#41 add api for channel subscriptions
15+
16+
17+
418
Version 0.5.1
519
=============
620

21+
What's New
22+
++++++++++
23+
724
Now some apis can get all target items just by one method call.
825

926
For example, you can get playlist's all items by follow call::
@@ -14,7 +31,7 @@ For example, you can get playlist's all items by follow call::
1431
In [3]: len(r.items)
1532
Out[4]: 73
1633

17-
You can see the `README <https://github.com/sns-sdks/python-youtube/blob/master/README.rst>`_ to find which methods support this.
34+
You can see the `README`_ to find which methods support this.
1835

1936
Version 0.5.0
2037
=============
@@ -25,7 +42,7 @@ Broken Change
2542
Now introduce new model ApiResponse representing the response from youtube, so previous usage has been invalidated.
2643

2744
You need to read the docs to get more change `Modules Documentation <https://python-youtube.readthedocs.io/en/latest/pyyoutube.html#module-pyyoutube.api>`_,
28-
or see the `README <https://github.com/sns-sdks/python-youtube/blob/v0.5.0/README.rst>`_ to get the simple new usage.
45+
or see the `README`_ to get the simple new usage.
2946

3047
What's New
3148
++++++++++
@@ -35,3 +52,5 @@ Split some method into multiple usage, for example get video has been split thre
3552
* api.get_video_by_id()
3653
* api.get_videos_by_chart()
3754
* api.get_videos_by_myrating()
55+
56+
.. _README: README.rst

README.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,58 @@ Get video categories with region code::
353353
VideoCategory(kind='youtube#videoCategory', id='15'),
354354
...]
355355

356+
-------------
357+
SUBSCRIPTIONS
358+
-------------
359+
360+
You can get subscriptions info by id, by point channel or by yourself.
361+
362+
.. note::
363+
If you want to get the subscriptions not set to public. You need do authorization first and get the access token.
364+
You can see the demo `A demo for get my subscription <examples/subscription.py>`_.
365+
366+
Get subscriptions info by id(s), this need your token have the permission for the subscriptions belongs channel or user::
367+
368+
In [6]: r = api.get_subscription_by_id(
369+
...: subscription_id=[
370+
...: "zqShTXi-2-Tx7TtwQqhCBwViE_j9IEgnmRmPnqJljxo",
371+
...: "zqShTXi-2-Rya5uUxEp3ZsPI3fZrFQnSXNQCwvHBGGo"])
372+
In [7]: r
373+
Out[7]: SubscriptionListResponse(kind='youtube#subscriptionListResponse')
374+
In [8]: r.items
375+
Out[8]:
376+
[Subscription(kind='youtube#subscription', id='zqShTXi-2-Tx7TtwQqhCBwViE_j9IEgnmRmPnqJljxo', snippet=SubscriptionSnippet(title='PyCon 2015', description='')),
377+
Subscription(kind='youtube#subscription', id='zqShTXi-2-Rya5uUxEp3ZsPI3fZrFQnSXNQCwvHBGGo', snippet=SubscriptionSnippet(title='ikaros-life', description='This is a test channel.'))]
378+
379+
Get yourself subscriptions, this need you do authorization first or give the authorized access token::
380+
381+
In [9]: r = api.get_subscription_by_me(
382+
...: mine=True,
383+
...: parts=["id", "snippet"],
384+
...: count=2
385+
...:)
386+
In [10]: r
387+
Out[10]: SubscriptionListResponse(kind='youtube#subscriptionListResponse')
388+
In [11]: r.items
389+
Out[11]:
390+
[Subscription(kind='youtube#subscription', id='zqShTXi-2-Tx7TtwQqhCBwtJ-Aho6DZeutqZiP4Q79Q', snippet=SubscriptionSnippet(title='Next Day Video', description='')),
391+
Subscription(kind='youtube#subscription', id='zqShTXi-2-Tx7TtwQqhCBwViE_j9IEgnmRmPnqJljxo', snippet=SubscriptionSnippet(title='PyCon 2015', description=''))]
392+
393+
Get public channel's subscriptions::
394+
395+
In [12]: r = api.get_subscription_by_channel(
396+
...: channel_id="UCAuUUnT6oDeKwE6v1NGQxug",
397+
...: parts="id,snippet",
398+
...: count=2
399+
...: )
400+
In [13]: r
401+
Out[13]: SubscriptionListResponse(kind='youtube#subscriptionListResponse')
402+
In [14]: r.items
403+
Out[14]:
404+
[Subscription(kind='youtube#subscription', id='FMP3Mleijt-52zZDGkHtR5KhwkvCcdQKWWWIA1j5eGc', snippet=SubscriptionSnippet(title='TEDx Talks', description="TEDx is an international community that organizes TED-style events anywhere and everywhere -- celebrating locally-driven ideas and elevating them to a global stage. TEDx events are produced independently of TED conferences, each event curates speakers on their own, but based on TED's format and rules.\n\nFor more information on using TED for commercial purposes (e.g. employee learning, in a film, or in an online course), please submit a media request using the link below.")),
405+
Subscription(kind='youtube#subscription', id='FMP3Mleijt_ZKvy5M-HhRlsqI4wXY7VmP5g8lvmRhVU', snippet=SubscriptionSnippet(title='TED Residency', description='The TED Residency program is an incubator for breakthrough ideas. It is free and open to all via a semi-annual competitive application. Those chosen as TED Residents spend four months at TED headquarters in New York City, working on their idea. Selection criteria include the strength of their idea, their character, and their ability to bring a fresh perspective and positive contribution to the diverse TED community.'))]
406+
407+
356408
====
357409
TODO
358410
====
@@ -368,6 +420,7 @@ Now this has follows api.
368420
- Comment Info
369421
- Video Categories Info
370422
- Guide Categories Info
423+
- Subscriptions Info
371424

372425
Doing
373426

docs/models.rst

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@ Python-YouTube provides following models of the the objects returned by the API:
55

66
* :py:class:`pyyoutube.models.AccessToken`
77
* :py:class:`pyyoutube.models.UserProfile`
8-
* :py:class:`pyyoutube.models.Channel`
9-
* :py:class:`pyyoutube.models.Video`
10-
* :py:class:`pyyoutube.models.PlayList`
11-
* :py:class:`pyyoutube.models.PlaylistItem`
12-
* :py:class:`pyyoutube.models.Comment`
13-
* :py:class:`pyyoutube.models.CommentThread`
14-
* :py:class:`pyyoutube.models.VideoCategory`
15-
* :py:class:`pyyoutube.models.GuideCategory`
8+
* :py:class:`pyyoutube.models.ChannelListResponse`
9+
* :py:class:`pyyoutube.models.VideoListResponse`
10+
* :py:class:`pyyoutube.models.PlaylistListResponse`
11+
* :py:class:`pyyoutube.models.PlaylistItemListResponse`
12+
* :py:class:`pyyoutube.models.CommentListResponse`
13+
* :py:class:`pyyoutube.models.CommentThreadListResponse`
14+
* :py:class:`pyyoutube.models.VideoCategoryListResponse`
15+
* :py:class:`pyyoutube.models.GuideCategoryListResponse`
16+
* :py:class:`pyyoutube.models.SubscriptionListResponse`
1617

1718
And so on.

examples/subscription.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""
2+
This demo show how to use this library to do authorization and get your subscription.
3+
"""
4+
5+
import pyyoutube
6+
import webbrowser
7+
8+
CLIENT_ID = "your app id"
9+
CLIENT_SECRET = "your app secret"
10+
11+
12+
def get_subscriptions():
13+
api = pyyoutube.Api(client_id=CLIENT_ID, client_secret=CLIENT_SECRET)
14+
15+
# need follows scope
16+
scope = ["https://www.googleapis.com/auth/youtube.readonly"]
17+
18+
url, _ = api.get_authorization_url(scope=scope)
19+
20+
print(
21+
"Try to start a browser to visit the authorization page. If not opened. you can copy and visit by hand:\n"
22+
f"{url}"
23+
)
24+
webbrowser.open(url)
25+
26+
auth_response = input(
27+
"\nCopy the whole url if you finished the step to authorize:\n"
28+
)
29+
30+
api.exchange_code_to_access_token(authorization_response=auth_response)
31+
32+
sub_res = api.get_subscription_by_me(mine=True, parts="id,snippet", count=None)
33+
34+
with open("subscriptions.json", "w+") as f:
35+
f.write(sub_res.to_json())
36+
37+
print("Finished.")
38+
39+
40+
if __name__ == "__main__":
41+
get_subscriptions()

pyyoutube/__version__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,6 @@
66
# 88 YP YP YP YP `Y88P' VP V8P YP `Y88P' ~Y8888P' YP ~Y8888P' Y8888P' Y88888P
77

88

9-
VERSION = (0, 5, 1)
9+
VERSION = (0, 5, 2)
1010

1111
__version__ = ".".join(map(str, VERSION))

pyyoutube/api.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ class Api(object):
5656
>>> api.get_comments()
5757
>>> api.get_guide_categories()
5858
>>> api.get_video_categories()
59+
>>> api.get_subscription_by_id()
60+
>>> api.get_subscription_by_channel()
61+
>>> api.get_subscription_by_me()
5962
"""
6063

6164
BASE_URL = "https://www.googleapis.com/youtube/v3/"

setup.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
# Package meta-data.
1515
NAME = "python-youtube"
1616
DESCRIPTION = "A Python wrapper around for YouTube Data API."
17-
URL = "https://github.com/MerleLiuKun/python-youtube"
17+
URL = "https://github.com/sns-sdks/python-youtube"
1818
1919
AUTHOR = "Ikaros Kun"
2020
REQUIRES_PYTHON = ">=3.6.0"
@@ -119,6 +119,7 @@ def run(self):
119119
"Programming Language :: Python :: 3",
120120
"Programming Language :: Python :: 3.6",
121121
"Programming Language :: Python :: 3.7",
122+
"Programming Language :: Python :: 3.8",
122123
"Programming Language :: Python :: Implementation :: CPython",
123124
"Programming Language :: Python :: Implementation :: PyPy",
124125
],

0 commit comments

Comments
 (0)