Create a job for automatically retrieving views and likes of YouTube videos.

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

  1. Jenkins Installed: Ensure Jenkins is installed and running.

  2. Python Installed: Have Python installed on your Jenkins node.

  3. YouTube Data API Key: Obtain an API key from the Google Developers Console.

  4. Required Python Packages: Install the necessary Python package for making HTTP requests:

     pip install requests
    

Step 1: Obtain YouTube Data API Key

  1. Go to the Google Developers Console.

  2. Create a new project.

  3. Enable the YouTube Data API v3 for your project.

  4. Create credentials to get your API key.

Step 2: Create a New Pipeline Job in Jenkins

  1. Open your Jenkins dashboard.

  2. Click on "New Item".

  3. Enter a name for your job (e.g., Retrieve YouTube Video Stats).

  4. Select "Pipeline" and click OK.

Step 3: Configure the Pipeline Job

  1. In the job configuration page, scroll down to the "Pipeline" section.

  2. 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

  1. Environment Variables: The YOUTUBE_API_KEY environment variable uses Jenkins credentials to store the API key securely. Replace your_video_id with the ID of the YouTube video you want to retrieve statistics for.

  2. 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

  1. Click "Save" at the bottom of the job configuration page.

  2. Go back to the job's main page and click "Build Now" to execute the job.

Step 6: Check Output

  1. 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.