Skip to content
Snippets Groups Projects
Commit a794aff6 authored by MARCHAL Julien's avatar MARCHAL Julien
Browse files

Initial commit

parents
No related branches found
No related tags found
No related merge requests found
config.py
list.csv
.venv/**
__pycache__/**
\ No newline at end of file
README 0 → 100644
python3 -m venv .venv
source .venv/bin/activate
pip install requests
pip install requests_toolbelt
\ No newline at end of file
INPUT_FILE='./list.csv'
POD_TOKEN = "xxxxxx"
POD_BASE_URL = "https://pod.univ.fr"
POD_API_UPLOAD_VIDEO = POD_BASE_URL + "/rest/videos/"
POD_API_USERS = POD_BASE_URL + "/rest/users/"
POD_API_LAUNCH_ENCODE = POD_BASE_URL + "/rest/launch_encode_view/"
VIDEO_TYPE_BASE = POD_BASE_URL + "/rest/types/"
VIDEO_SITE = POD_BASE_URL + "/rest/sites/1/"
#username,user_first_name,user_last_name,user_email,video_type,video_title,video_description,video_allow_downloading,video_is_draft,video_file
bond@esup-portail.org,James,Bond,James.Bond@esup-portail.org,1,Titre de test,Description de test,false,true,./videos/test1.mp4
import requests
from requests_toolbelt.multipart.encoder import MultipartEncoder
import csv
import sys
import json
import os
import config
headers_json={
'Content-Type':'application/json',
'Authorization': 'Token {}'.format(config.POD_TOKEN)
}
# READ CSV FILE
with open(config.INPUT_FILE, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
if(row[0].startswith("#") != True):
username=row[0]
user_first_name=row[1]
user_last_name=row[2]
user_email=row[3]
video_type=row[4]
video_title=row[5]
video_description=row[6]
video_allow_downloading=row[7]
video_is_draft=row[8]
video_file=row[9]
# print("%s %s %s %s %s %s %s %s %s %s" % (username, user_first_name, user_last_name, user_email, video_type, video_title, video_description, video_allow_downloading, video_is_draft, video_file))
# TEST IF USER EXIST
payload_search_user={
"username": username
}
response = requests.get(config.POD_API_USERS, headers=headers_json, params=payload_search_user)
if(response.status_code != 200):
print("ERROR Test User exists [%s] : [CODE:%s] [MSG:%s]" % (username,response.status_code, json.loads(response.content.decode("utf-8"))))
else:
result = json.loads(response.content.decode("utf-8"))
if(result['count'] != 1):
print("User don't exists [%s]" % (username))
# CREATE USER
payload_create_user={
"username": username,
"first_name": user_first_name,
"is_staff": "true",
"last_name": user_last_name,
"email": user_email,
"groups": []
}
response = requests.post(config.POD_API_USERS, headers=headers_json, data=json.dumps(payload_create_user))
if(response.status_code != 201):
print("ERROR Create User [%s] : [CODE:%s] [MSG:%s]" % (username,response.status_code, json.loads(response.content.decode("utf-8"))))
else:
result = json.loads(response.content.decode("utf-8"))
username=result['username']
user_url=result['url']
print("New user created [%s] [%s]" % (username, user_url))
else:
user_url=result['results'][0]['url']
print("User already exists [%s] [%s]" % (username, user_url))
# INSERT NEW VIDEO
stream_video_file = open(video_file, 'rb')
name_video_file = os.path.basename(video_file)
multipart_data = MultipartEncoder(
fields={
'owner': user_url,
'type': config.VIDEO_TYPE_BASE+video_type+"/",
'title': video_title,
'description': video_description,
'allow_downloading': video_allow_downloading,
'is_draft': video_is_draft,
'sites': config.VIDEO_SITE,
'video': (name_video_file, stream_video_file, 'text/plain')
}
)
headers_multipart=headers_json
headers_multipart['Content-Type']=multipart_data.content_type
response = requests.post(config.POD_API_UPLOAD_VIDEO, headers=headers_multipart, data=multipart_data)
if(response.status_code != 201):
print("ERROR Insert video [%s] : [CODE:%s] [MSG:%s]" % (username,response.status_code, json.loads(response.content.decode("utf-8"))))
else:
result = json.loads(response.content.decode("utf-8"))
slug = result['slug']
print("Insert video [USER:%s] [FILE:%s] [SLUG:%s]" % (username, video_file, slug))
# LAUNCH VIDEO ENCODE
payload_encode_video={
"slug": slug
}
response = requests.get(config.POD_API_LAUNCH_ENCODE, headers=headers_json, params=payload_encode_video)
if(response.status_code != 200):
print("ERROR Launch encode video [%s] : [CODE:%s] [MSG:%s]" % (username,response.status_code, json.loads(response.content.decode("utf-8"))))
else:
print("Encode video is launch [USER:%s] [FILE:%s] [SLUG:%s]" % (username, video_file, slug))
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment