YouTube API Search and Classification - Python Code Explanation
Goal of the Script
This Python script searches the YouTube Data API for videos related to
violence against religious minorities in Bangladesh, classifies retrieved
videos using keyword-based categories, and produces CSV and HTML reports for
analysis.
Required Modules
The program uses several supporting modules:
-
config.py - provides the YouTube API key.
-
Keywords_list.py - provides category keyword lists and
classification weights.
-
Keywords_list_v3_SearchThemes.py - provides organized search
queries.
Libraries Used
The script uses:
-
pandas for data processing and creating output datasets.
-
googleapiclient for accessing the YouTube Data API.
-
logging for recording search progress and output information.
-
os and datetime for file management and
timestamps.
Video Classification Process
The classify() function examines video titles and descriptions
using the keyword categories defined in CATEGORY_MAP.
When matching terms are found, the script assigns categories, records matched
keywords, and calculates a weighted classification score.
If no keywords are matched, the video is assigned to the
Other category.
Search and Retrieval Workflow
-
The user selects the maximum number of YouTube results per query.
-
The user selects a publication period, such as a specific half-year,
calendar year, or all available years.
-
The script executes searches using the predefined search themes.
-
For each retrieved video, the program collects title, description,
channel, publication date, and URL information.
-
The video text is classified using the keyword-based classification system.
Date Filtering
The program provides multiple publication-date filters, including:
-
Jan-Jun and Jul-Dec periods for 2024, 2025, and 2026
-
Full calendar years
-
2023 and earlier
-
All years
Output Files
The script creates output files in the configured output folder:
-
CSV file containing classified YouTube video records.
-
HTML report displaying classified videos with clickable hyperlinks.
-
Log file documenting search activity and execution details.
Data Cleaning and Reporting
After retrieval, the program:
-
Removes duplicate videos using the VideoID field.
-
Sorts videos by classification score and publication date.
-
Removes videos classified only as "Other" from the HTML report.
-
Formats YouTube URLs as clickable hyperlinks.
Summary
This script provides an automated workflow for collecting and analyzing
YouTube videos. It integrates API-based retrieval, keyword classification,
weighted scoring, data cleaning, and HTML report generation to support
research on violence against religious minorities in Bangladesh.