diff --git a/examples/basic/async.py b/examples/basic/async.py new file mode 100644 index 0000000..4108cc0 --- /dev/null +++ b/examples/basic/async.py @@ -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()) diff --git a/examples/basic/sync.py b/examples/basic/sync.py new file mode 100644 index 0000000..62543b1 --- /dev/null +++ b/examples/basic/sync.py @@ -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() diff --git a/examples/decorators.py b/examples/decorators.py index 19e5b55..e003e2f 100644 --- a/examples/decorators.py +++ b/examples/decorators.py @@ -3,8 +3,6 @@ """ -import logging - from ytnoti import YouTubeNotifier, Notification @@ -13,7 +11,6 @@ def main(): Main function """ - logger = logging.getLogger(__name__) notifier = YouTubeNotifier() @notifier.any() @@ -22,8 +19,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) + print("listener 1 called") + print(notification) @notifier.upload() async def listener2(notification: Notification): @@ -31,8 +28,8 @@ 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() @@ -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() diff --git a/examples/logger.py b/examples/logger.py new file mode 100644 index 0000000..84eb760 --- /dev/null +++ b/examples/logger.py @@ -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() diff --git a/examples/multithreading.py b/examples/multithreading.py index c3a5cde..d12918b 100644 --- a/examples/multithreading.py +++ b/examples/multithreading.py @@ -2,7 +2,6 @@ This is an example of how to use this library with multithreading. """ -import logging import time from threading import Thread @@ -14,7 +13,6 @@ def main(): Main function """ - logger = logging.getLogger(__name__) notifier = YouTubeNotifier() @notifier.any() @@ -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) @@ -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")