Mass upload from an Excel file using Python SDK
Our REST API allows “Mass Upload” for Partners who need to transfer large video catalogs to Dailymotion. One of the easiest ways to do it is to upload files via an Excel file. Follow this tutorial to know how to use a Python library that will automatically upload your videos.
1. Create the Excel file
First step: create your Excel file. This very simple document must include specific information about your videos:
- Titles
- URLs
- Tags
- Channels
- Descriptions
- Is it already published? (true/false)
- Is it created for kids? (true/false)
Here is an Excel template that you can use to list the videos you want to upload via Dailymotion’s API:
2. Upload using Python
Now that your Excel file is created, you need to integrate it in your Python code. For that, we suggest you to use Openpyxl. This open-source Python library can read and write Excel 2010 xlsx, xlsm, xltx and xltm files.
Code sample
# Import the dailymotion sdk
import importlib
# Import `load_workbook` module from `openpyxl`
from openpyxl import load_workbook
# It's imported that way due to the "-" in the module name :/
_DAILYMOTION = importlib.import_module("dailymotion-sdk-python.dailymotion")
# dailymotion credentials
_API_KEY = "YOUR_API_KEY"
_API_SECRET = "YOUR_API_SECRET"
_USERNAME = "YOUR_DAILYMOTION_LOGIN"
_PASSWORD = "YOUR_DAILYMOTION_PASSWORD"
# Loading the workbook
_FILENAME = "Videos.xlsx"
_XLS = load_workbook(_FILENAME)
# Change your sheet name if necessary
_SHEET = _XLS.get_sheet_by_name("Sheet1")
# Adding some info returned by the API on the sheet
_DM_XID = _SHEET.cell(
row=1, column=_SHEET.max_column + 1, value="Dailymotion xid"
)
_DM_URL = _SHEET.cell(
row=1, column=_SHEET.max_column + 1, value="Dailymotion link"
)
# Instanciating the sdk
_DM = _DAILYMOTION.Dailymotion()
try:
# Providing the authentication information
_DM.set_grant_type(
"password",
api_key=_API_KEY,
api_secret=_API_SECRET,
scope=["manage_videos"],
info={"username": _USERNAME, "password": _PASSWORD},
)
# Looping into the rows and skiping the headers
for row in _SHEET[2 : _SHEET.max_row]:
# Uploading the file on dailymotion servers
url = _DM.upload(row[1].value)
# Filling the information about the video
parameters = {
"url": url,
"title": row[0].value,
"tags": row[2].value,
"channel": row[3].value,
"description": row[4].value,
"published": row[5].value,
}
# Sending the information to create the video on dailymotion
result = _DM.post("/me/videos?fields=id,url", parameters)
# write the xid returned by the API
_DM_XID = _SHEET.cell(
row=_DM_XID.row + 1, column=_DM_XID.column, value=result["id"]
)
_DM_URL = _SHEET.cell(
row=_DM_URL.row + 1, column=_DM_URL.column, value=result["url"]
)
print("Upload from excel file to dailymotion finished")
_XLS.save(_FILENAME)
except Exception as e:
print("An error occured: %s" % str(e))
With this code, our Python SDK will upload your videos to Dailymotion and include all information from your Excel file. The upload time will mostly depend on the size of your files. To help you keep track of the process, you will receive a script every time a new video is uploaded.