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

lot of changes

parent 27747c50
Branches
No related tags found
No related merge requests found
......@@ -5,6 +5,8 @@ pip install requests
pip install requests_toolbelt
rename and adapt config.sample.py to config.py
rename and adapt list.sample.csv to config.csv
rename and adapt usersList.sample.csv to usersList.csv
rename and adapt videosList.sample.csv to videosList.csv
python3 putVideo.py
\ No newline at end of file
python3 createUsers.py usersList.csv
python3 putVideos.py videosList.csv
\ 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/"
......@@ -7,3 +6,4 @@ 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/"
LAUNCH_ENCODE_AFTER_PUT=False
\ No newline at end of file
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)
}
inputCsvFile=sys.argv[1]
try:
with open(inputCsvFile, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
if(row[0].startswith("#") != True):
# print(row)
username=row[0]
user_first_name=row[1]
user_last_name=row[2]
user_email=row[3]
# print("%s %s %s %s" % (username, user_first_name, user_last_name, user_email))
# 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))
except FileNotFoundError as e:
print('File [%s] not found' % inputCsvFile)
\ No newline at end of file
#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
......@@ -12,97 +12,82 @@ headers_json={
'Authorization': 'Token {}'.format(config.POD_TOKEN)
}
try:
inputCsvFile=sys.argv[1]
except IndexError as e:
sys.exit('No file name in input param')
# 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))
try:
with open(inputCsvFile, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
if(row[0].startswith("#") != True):
username=row[0]
video_type=row[1]
video_title=row[2]
video_description=row[3]
video_allow_downloading=row[4]
video_is_draft=row[5]
video_file=row[6]
# LAUNCH VIDEO ENCODE
payload_encode_video={
"slug": slug
# print("%s %s %s %s %s %s %s" % (username, 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_LAUNCH_ENCODE, headers=headers_json, params=payload_encode_video)
response = requests.get(config.POD_API_USERS, headers=headers_json, params=payload_search_user)
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"))))
print("ERROR Test User exists [%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))
result = json.loads(response.content.decode("utf-8"))
if(result['count'] != 1):
print("User not found [%s] [%s]" % (username, video_file))
else:
# print(result)
user_url=result['results'][0]['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
if(config.LAUNCH_ENCODE_AFTER_PUT):
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))
except FileNotFoundError as e:
print('File [%s] not found' % inputCsvFile)
\ No newline at end of file
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)
}
try:
inputCsvFile=sys.argv[1]
except IndexError as e:
sys.exit('No file name in input param')
# READ CSV FILE
try:
with open(inputCsvFile, newline='') as csvfile:
reader = csv.reader(csvfile, delimiter=',', quotechar='|')
for row in reader:
if(row[0].startswith("#") != True):
username=row[0]
video_type=row[1]
video_title=row[2]
video_description=row[3]
video_allow_downloading=row[4]
video_is_draft=row[5]
video_file=row[6]
# print("%s %s %s %s %s %s %s" % (username, 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 not foung [%s] [%s]" % (username))
else:
# print(result)
user_url=result['results'][0]['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))
except FileNotFoundError as e:
print('File [%s] not found' % inputCsvFile)
\ No newline at end of file
#username,user_first_name,user_last_name,user_email
bond@esup-portail.org,James,Bond,James.Bond@esup-portail.org
\ No newline at end of file
#username,video_type,video_title,video_description,video_allow_downloading,video_is_draft,video_file
bond@esup-portail.org,1,Titre de test,Description de test,false,true,./videos/test1.mp4
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment