Building a YouTube Video Fetcher with .NET Core: A Step-by-Step Guide
Effortlessly fetch and integrate YouTube video feeds into your C# projects with this comprehensive guide.
As the internet has expanded, users have come to expect information from many different sources every day. Keeping up with updates from news sites, blogs, podcasts, and video-sharing platforms can be quite challenging. This is where RSS (Really Simple Syndication) and Atom feeds come in handy. These feeds allow users to follow content from various sources in one place.
RSS and Atom feeds are streams of data in XML format, usually containing the latest content, articles, videos, or updates from websites. With these feeds, users can easily stay updated with new content from different websites using a feed reader or similar application.
C# is a great tool for reading and processing feed data because of its strong XML handling capabilities. In this guide, I’ll show you how to read RSS and Atom feeds using C#.
We’ll also look at a practical example: reading video information from a YouTube channel using a console application.
⚠️ Disclosure: This article is for educational purposes only and is written by a human, not AI.
What is a RSS Feed?
To better explain the topic, we’ll continue using YouTube in our example project. YouTube provides RSS feeds for each channel, and these feeds are presented in XML format.
Every YouTube channel also has a Feed page.
Below, I’ve shared the access link for the Google Developers channel:
https://www.youtube.com/c/GoogleDevelopers
The “GoogleDevelopers” at the end of the link is the channel’s username.
You can also view the feed page using the channel’s username.
Here is the access link to Google Developers’ feed page:
https://www.youtube.com/feeds/videos.xml?user=GoogleDevelopers
Simply put, appending the channel’s username to “https://www.youtube.com/feeds/videos.xml?user=” is all it takes.
Below, I’m sharing a screenshot of the Google Developers channel’s XML-format feed page.
RSS Feed Structure of a YouTube Channel
This RSS feed structure provides an XML data stream containing videos from the “Google Developers” channel on YouTube. Below is a detailed description of the feed structure:
<feed> Element: This is the main element of the RSS feed, defined using the Atom namespace.
<id>: The unique identifier of the feed.
<title>: The title of the feed (“Google Developers” channel).
<link>: The alternate link of the feed (“https://www.youtube.com/channel/UC_x5XG1OV2P6uZZ5FSM9Ttw").
<updated>: The last updated timestamp of the feed.
<entry> Elements: Each video is represented by an <entry> element, containing details about the video.
<id>: The video ID.
<title>: The title of the video.
<link>: The alternate link of the video.
<published>: The published date of the video.
<updated>: The last updated timestamp of the video.
<author>: The author of the video.
<media:group>: The media content of the video.
<media:title>: The title of the video.
<media:description>: The description of the video.
<media:thumbnail>: The thumbnail image of the video.
This structure provides an XML data feed that includes details of the latest videos from the “Google Developers” channel. It enables integration of YouTube channel videos dynamically into other applications or services.
Let’s read the data.
Step 1: Create A Console Application
First, let’s add a new console project to our solution. This will serve as the main entry point for our application.
Follow these steps:
- Open the olution in Visual Studio: Open your existing solution in Visual Studio,
- Add a New Project: Right-click on the solution in the Solution Explorer, select “Add,” and then “New Project.”,
- Choose Console App: In the new project dialog, select “Console App” under the .NET Core category. Name the project. I named it “ReadYouTubeChannel”,
- Configure the project: Click “Create” to add the new console project to the solution.
At this point, you should see the new console project in your Solution Explorer.
Here is mine.
Step 2: Creating a Class Library for Shared Services
Next, let’s create a class library that will contain the services shared by our projects. This helps in keeping the code modular and reusable.
- Add a Class Library Project: Similar to adding the console project, right-click on the solution, select “Add,” and then “New Project.”
- Choose the class library: Select “Class Library” under the .NET Core category. Name the project something like “Shared” as I did.
- Configure the Project: Click “Create” to add the new class library project to the solution.
Step 3: Installing the Required Libraries For Reading XML Feed
To read and process RSS and Atom feeds in C#, we’ll need a few special libraries. These libraries make it easier to fetch and understand feed data.
Here they are:
- System.Xml: This is a fundamental library used for reading and processing XML data. Since RSS and Atom feeds are in XML format, this library provides various classes and methods to load, read, and process XML files.
- System.ServiceModel.Syndication: This library is specifically designed for handling RSS and Atom feeds. It offers useful classes for loading, parsing, and accessing the elements within the feeds.
The SyndicationFeed
class represents RSS and Atom feeds and makes it easy to access information like the feed's title, description, and items.
Step 4: Creating ReadYouTube Class to Read Data From a YouTube Channel
I added the ReadYouTube
class with a Read
method to our shared project. This method retrieves YouTube video data from an RSS feed and displays it based on the publication date.
Here is the code for the ReadYouTube
class and a detailed explanation of the code:
Explanation of the Code:
- Line 1–2: We import the necessary namespaces for handling syndication feeds and XML data.
- Line 4: The
Shared
namespace is declared, indicating this class is part of the shared project. - Line 6: We define the
ReadYouTube
class. - Line 8: The
Read
method is declared, taking two parameters:url
(the RSS feed URL) anddaysBefore
(the number of days to look back for videos). - Line 10: An
XmlReader
is created for the provided URL. - Line 11: The
SyndicationFeed.Load
method loads the feed from the XML reader. - Line 13: We calculate the date
daysBefore
days ago. - Line 15–18: If the feed is null, we print a message indicating no videos were found.
- Line 20–32: If the feed is not null, we iterate through each item in the feed. For each item, if its publish date is within the specified range, we print its title, link, and publish date.
This method effectively reads an RSS feed from a YouTube channel, filters videos based on their publish date, and displays the relevant information in the console.
By structuring our project this way, we maintain a clean separation of concerns and promote reusability.
Step 5: Adding the Project Reference to Access the ReadYouTube Class
In order to access the ReadYouTube
class from our ReadYouTubeChannel
project, we need to add the Shared
project as a reference in the ReadYouTubeChannel
project's dependencies.
This will allow us to utilize the shared services and classes defined in the Shared
project.
Here’s how to do it:
- Under ReadYouTubeChannel project, right-click on the dependencies,
- Click on “Add project reference”,
- On the left menu, select Projects > Solution,
- Check the shared project,
- Click on the OK button.
Step 6: Displaying YouTube Channel Videos in the Program.cs File
In the Program.cs
file of the ReadYouTubeChannel
project, we will utilize the ReadYouTube
class from the Shared
project to read and display video data from a specified YouTube channel.
Here's the complete code, along with detailed explanations for each line:
Explanation of the Code:
- Line 1: This line imports the
Shared
namespace, allowing us to use theReadYouTube
class defined in theShared
project. Without this import, we would not be able to access the classes and methods in theShared
project. - Line 3: This line prints a message to the console, indicating the start of the program. It helps to provide context to the user about what the program is doing.
- Line 5: Here, we define a string variable named
url
and assign it the URL of the YouTube channel's RSS feed. This URL is used to fetch the latest videos from the specified YouTube channel. - Line 7: We create an instance of the
ReadYouTube
class and store it in a variable namedmyClass
. This instance will be used to call theRead
method and fetch video data from the specified YouTube channel. - Line 9: This line calls the
Read
method on themyClass
instance, passing in theurl
variable and the number3
as arguments. Theurl
parameter specifies the RSS feed URL, and the number3
indicates that we want to list videos published within the last 3 days. TheRead
method will process the feed and print the video details to the console.
Step 7: Results of the Application
I executed the application to list videos from a specified YouTube channel.
By providing the channel’s RSS feed URL and a parameter to filter videos from the last few days, the application retrieves and displays the video details on the console.
Here is the result:
Conclusion
In this project, we successfully created a .NET Core console application that retrieves and displays video data from a specified YouTube channel. By structuring our solution with a shared project for common services, we ensured a clean and modular design. The ReadYouTube
class in the Shared
project encapsulates the logic for reading and processing YouTube RSS feeds, making it reusable across different parts of our application.
The step-by-step process involved:
- Setting Up the Projects: We added a console project and a class library project to our solution, establishing a clear separation of concerns.
- Implementing the ReadYouTube Class: We developed the
ReadYouTube
class with aRead
method to fetch and display YouTube video data based on a provided URL and time filter. - Integrating and Running the Application: We added the
Shared
project as a reference in our console project, instantiated theReadYouTube
class, and called itsRead
method to list recent videos on the console.
Upon execution, the application effectively listed the videos from the specified YouTube channel, demonstrating the functionality and robustness of our implementation.
Source Code
You can access the source code for my project on Github. I would be very pleased if you gave stars to the project and followed me.
👏 If you found the content useful, you can follow me and make me happy in this way.
💬 If there is something I overlooked, you can share it in the comments or contact me on my profile.
⬇️ Check out my other articles.
💬 Let me know in the comment section what have I missed? Where I was wrong?
👨👦👦 You can also share this article with your friend. Argue a little on it. It is known that the truth is born in a dispute.
🙃 Stay determined, stay focused, and keep going.
Thanks for reading.