Create a job for automatically retrieving views and likes of YouTube videos.
To create a Jenkins job that automatically retrieves the views and likes of YouTube videos, you can use the YouTube Data API. Below is a step-by-step guide on how to set up this Jenkins job, including obtaining an API key, writing the necessary scripts, and configuring the Jenkins pipeline.
Prerequisites
Jenkins Installed: Ensure Jenkins is installed and running.
Python Installed: Have Python installed on your Jenkins node.
YouTube Data API Key: Obtain an API key from the Google Developers Console.
Required Python Packages: Install the necessary Python package for making HTTP requests:
pip install requests
Step 1: Obtain YouTube Data API Key
Go to the Google Developers Console.
Create a new project.
Enable the YouTube Data API v3 for your project.
Create credentials to get your API key.
Step 2: Create a New Pipeline Job in Jenkins
Open your Jenkins dashboard.
Click on "New Item".
Enter a name for your job (e.g.,
Retrieve YouTube Video Stats
).Select "Pipeline" and click OK.
Step 3: Configure the Pipeline Job
In the job configuration page, scroll down to the "Pipeline" section.
Choose "Pipeline script" from the dropdown.
Step 4: Add the Pipeline Script
In the "Pipeline" script text area, add the following script:
pipeline {
agent any
environment {
YOUTUBE_API_KEY = credentials('youtube-api-key') // Jenkins credentials ID for your YouTube API key
VIDEO_ID = 'your_video_id' // Replace with the YouTube video ID you want to retrieve stats for
}
stages {
stage('Retrieve Video Stats') {
steps {
script {
def response = sh(script: """
python -c "
import requests
API_KEY = '${YOUTUBE_API_KEY}'
VIDEO_ID = '${VIDEO_ID}'
url = f'https://www.googleapis.com/youtube/v3/videos?id={VIDEO_ID}&key={API_KEY}&part=statistics'
response = requests.get(url)
data = response.json()
if 'items' in data and len(data['items']) > 0:
statistics = data['items'][0]['statistics']
print('Views:', statistics.get('viewCount', 'N/A'))
print('Likes:', statistics.get('likeCount', 'N/A'))
else:
print('No video found.')
"
""", returnStdout: true).trim()
// Print the retrieved statistics
echo response
}
}
}
}
}
Explanation of the Pipeline Script
Environment Variables: The
YOUTUBE_API_KEY
environment variable uses Jenkins credentials to store the API key securely. Replaceyour_video_id
with the ID of the YouTube video you want to retrieve statistics for.Retrieve Video Stats Stage:
The Python script makes a request to the YouTube Data API to retrieve video statistics.
It checks for the existence of video statistics and prints the number of views and likes.
Step 5: Save and Run the Job
Click "Save" at the bottom of the job configuration page.
Go back to the job's main page and click "Build Now" to execute the job.
Step 6: Check Output
- Once the job completes, check the console output of the job run to see the retrieved views and likes for the specified YouTube video.
Additional Considerations
API Quota: Keep in mind that the YouTube Data API has usage limits, so avoid excessive calls.
Error Handling: Consider adding error handling in your Python script to manage potential failures, such as network issues or invalid video IDs.
Dynamic Video ID: You can modify the job to take a dynamic video ID as a parameter if needed.
Conclusion
By following these steps, you have successfully created a Jenkins job that retrieves the views and likes of a specified YouTube video. This setup can be customized further based on your requirements.