Skip to content

Commit

Permalink
Add more examples
Browse files Browse the repository at this point in the history
  • Loading branch information
SeoulSKY committed Jun 29, 2024
1 parent b93e28d commit eae8c69
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 21 deletions.
30 changes: 30 additions & 0 deletions examples/basic/async.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""
This example demonstrates how to use the AsyncYouTubeNotifier to listen for new video uploads from a channel.
"""

import asyncio

from ytnoti import AsyncYouTubeNotifier, Notification


async def main():
"""
Main function
"""

notifier = AsyncYouTubeNotifier()

@notifier.upload()
async def listener(notification: Notification):
"""
Listener called when a video is uploaded for any channel
"""

print(f"New video from {notification.channel.name}: {notification.video.title}")

await notifier.subscribe("UC9EEyg7QBL-stRX-7hTV3ng") # Channel ID of SpeedyStyle
await notifier.serve()


if __name__ == "__main__":
asyncio.run(main())
24 changes: 24 additions & 0 deletions examples/basic/sync.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
This example demonstrates how to use the YouTubeNotifier to listen for new video uploads from a channel.
"""

from ytnoti import YouTubeNotifier, Notification


def main():
"""
Main function
"""

notifier = YouTubeNotifier()

@notifier.upload()
async def listener(notification: Notification):
print(f"New video from {notification.channel.name}: {notification.video.title}")

notifier.subscribe("UC9EEyg7QBL-stRX-7hTV3ng") # Channel ID of SpeedyStyle
notifier.run()


if __name__ == "__main__":
main()
17 changes: 6 additions & 11 deletions examples/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
"""


import logging

from ytnoti import YouTubeNotifier, Notification


Expand All @@ -13,7 +11,6 @@ def main():
Main function
"""

logger = logging.getLogger(__name__)
notifier = YouTubeNotifier()

@notifier.any()
Expand All @@ -22,17 +19,17 @@ async def listener1(notification: Notification):
Listener called when a video is uploaded or edited for any channel
"""

logger.info("listener 1 called")
logger.info(notification)
print("listener 1 called")
print(notification)

@notifier.upload()
async def listener2(notification: Notification):
"""
Listener called when a video is uploaded for any channel
"""

logger.info("listener 2 called")
logger.info(notification)
print("listener 2 called")
print(notification)

@notifier.upload(channel_ids="UCupvZG-5ko_eiXAupbDfxWw")
@notifier.edit()
Expand All @@ -41,10 +38,8 @@ async def listener3(notification: Notification):
Listener called when a video is uploaded on a specific channel and when a video is edited on any channel
"""

logger.info("listener 3 called")
logger.info(notification)

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
print("listener 3 called")
print(notification)

notifier.subscribe(["UCupvZG-5ko_eiXAupbDfxWw", "UChLtXXpo4Ge1ReTEboVvTDg"])
notifier.run()
Expand Down
34 changes: 34 additions & 0 deletions examples/logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""
Example of a simple YouTube Notifier with logging module
"""


import logging

from ytnoti import YouTubeNotifier, Notification


def main():
"""
Main function
"""

logger = logging.getLogger(__name__)
notifier = YouTubeNotifier()

@notifier.upload()
async def listener(notification: Notification):
"""
Listener called when a video is uploaded or edited for any channel
"""

logger.info("New video from %s: %s", notification.channel.name, notification.video.title)

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")

notifier.subscribe("UC9EEyg7QBL-stRX-7hTV3ng") # Channel ID of SpeedyStyle
notifier.run()


if __name__ == "__main__":
main()
16 changes: 6 additions & 10 deletions examples/multithreading.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
This is an example of how to use this library with multithreading.
"""

import logging
import time
from threading import Thread

Expand All @@ -14,7 +13,6 @@ def main():
Main function
"""

logger = logging.getLogger(__name__)
notifier = YouTubeNotifier()

@notifier.any()
Expand All @@ -23,10 +21,8 @@ async def listener1(notification: Notification):
Listener called when a video is uploaded or edited for any channel
"""

logger.info("listener 1 called")
logger.info(notification)

logging.basicConfig(level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s")
print("listener 1 called")
print(notification)

notifier.subscribe("UCupvZG-5ko_eiXAupbDfxWw")
thread = Thread(target=notifier.run)
Expand All @@ -35,18 +31,18 @@ async def listener1(notification: Notification):
# Simulate adding listener and subscriber after some time
seconds = 10
for i in range(seconds):
logging.info("Waiting for adding another listener in %d seconds", seconds - i)
print("Waiting for adding another listener in %d seconds", seconds - i)
time.sleep(1)

logging.info("Adding listener 2 and subscribing to another channel")
print("Adding listener 2 and subscribing to another channel")

async def listener2(notification: Notification):
"""
Listener called when a video is uploaded or edited for any channel
"""

logger.info("listener 2 called")
logger.info(notification)
print("listener 2 called")
print(notification)

notifier.add_any_listener(listener2)
notifier.subscribe("UChLtXXpo4Ge1ReTEboVvTDg")
Expand Down

0 comments on commit eae8c69

Please sign in to comment.