sahilee26 / fetch-youtube-api-django

An API to fetch latest videos sorted in reverse chronological order of their publishing date-time from YouTube for a given tag/search query in a paginated response.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Fetch YouTube api django

Table of Contents

Project Goal

To make an API to fetch latest videos sorted in reverse chronological order of their publishing date-time from YouTube for a given tag/search query in a paginated response.

Running the Project Locally

  1. First, clone the repository to your local machine:
git clone https://github.com/sahilee26/fetch-youtube-api-django.git
  1. Change the current directory to the project directory
cd fetch-youtube-api-django
  1. Build and run the docker container with the command
docker-compose up
  1. Create an admin user:
python manage.py createsuperuser

Follow the prompts on the terminal to create an admin user.

  1. We're almost there! There's just one more thing we need to do! We need to create a keys.json file in the root directory of the project where we will store our YouTube Data API Key which will be required to fetch the videos using that API.
touch keys.json

Now open this file with any text editor. The structure of keys.json is required to be as follows:

{
	"YOUTUBE_DATA_API_KEYS": {
		"key1": "Enter your API Key 1 here",
		"key2": "Enter your API Key 2 here",
		"key3": "Enter your API Key 3 here",
		"key4": "Enter your API Key 4 here",
	}
}

You can get an API key by following this link. Make sure that you have atleast one API Key in this file with valid amount of quota left.

  1. Finally, run the development server:
python manage.py runserver

The project will be available at 127.0.0.1:8000.

Different Endpoints

There are 4 endpoints in the project:

  1. /admin: This allows user to login with the admin credentials and manage the project. The user can view the tables of the database and can also edit them directly in the provided interface.

  2. /youtube/startFetching: This endpoint triggers a background asynchronorous job to fetch information of videos. This endpoint also supports an optional query parameter to take the search query:

http://localhost:8000/youtube/startFetching?searchQuery=Cricket
http://localhost:8000/youtube/startFetching?searchQuery=Badminton

The searchQuery is optional and defaults to Friends TV Show if not provided.

  1. /youtube: This endpoint returns the stored video data in a paginated response sorted in descending order of published datetime. This endpoint also supports a query parameter to facilitate pagination as following:
http://localhost:8000/youtube/?page=1
http://localhost:8000/youtube/?page=2
.
.
http://localhost:8000/youtube/?page=pageNumber
  1. /youtube/search: This endpoint implements search API to fetch stored video data using their title and description and takes a query parameter to take the keywords. It shows results
http://localhost:8000/youtube/search?keywords=Cricket
http://localhost:8000/youtube/search?keywords=Westeros

The searchQuery defaults to None if not provided.

You can find the demonstration of each of these in the Demonstration section.

Key Features

  1. Information about YouTube videos is being fetched asynchronously as a background task on regular intervals (25 seconds as per the current implementation).
  2. Pagination support with latest published videos shown first.
  3. Stores Video ID, Title, Description, Publishing Datetime and Thumbnails URLs in the database with indexing done on Publishing Datetime column in reverse chronological ordering for faster access.
  4. I have also dockerized the project.

Bonus Points

  • Add support for supplying multiple API keys so that if quota is exhausted on one, it automatically uses the next available key (completed)

Video Demonstration

References

  1. YouTube Data v3 API
  2. Search API reference
  3. Django Official Documentation

License

The source code is released under the [MIT License]

About

An API to fetch latest videos sorted in reverse chronological order of their publishing date-time from YouTube for a given tag/search query in a paginated response.

License:MIT License


Languages

Language:Python 83.6%Language:HTML 12.0%Language:Dockerfile 4.5%