-
Notifications
You must be signed in to change notification settings - Fork 4
/
exportEpisode.py
195 lines (147 loc) · 7.51 KB
/
exportEpisode.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
import json,sys,requests,re,os,xml
from requests.auth import HTTPDigestAuth
from xml.etree import ElementTree
from xml.dom import minidom
import config
searchrequest = config.engageserver + config.searchendpoint + sys.argv[1]
archiverequest = config.archiveserver + config.archiveendpoint + sys.argv[1]
sourceauth = HTTPDigestAuth(config.sourceuser, config.sourcepassword)
targetauth = HTTPDigestAuth(config.targetuser, config.targetpassword)
# Get mediapackage from search service
searchresult = requests.get(searchrequest, auth=sourceauth, headers=config.header)
mediapackagesearch = searchresult.json()['search-results']['result']['mediapackage']
trackfromarchive=[]
attachmentsfromarchive=[]
mediapackagearchive= dict()
archivePresentationTracks=False
#Opencast sends an Object if list cotains only one Item instead of list
def jsonMakeObjectToList(jsonobject):
if (not isinstance(jsonobject, list)):
tmpObject = jsonobject
jsonobject = []
jsonobject.append(tmpObject)
return jsonobject
else:
return jsonobject
# Get mediapackage from episode/archive service
archiveresult = requests.get(archiverequest, auth=sourceauth, headers=config.header)
if (archiveresult.json()['search-results'].get('result')):
mediapackagearchive = archiveresult.json()['search-results']['result']['mediapackage']
# get Tracks
trackfromarchive = jsonMakeObjectToList(mediapackagearchive['media']['track'])
attachmentsfromarchive = jsonMakeObjectToList(mediapackagearchive['attachments']['attachment'])
else:
archivePresentationTracks=True
print ("Hint: This Episode was not Archived + Archive for Presentation Tracks")
# get all tracks from search
track = mediapackagesearch['media']['track']
# make sure that tracks are lists not only objects
if (isinstance(track, list)) :
trackfrommediapackage = track
else:
trackfrommediapackage = []
trackfrommediapackage.append(track)
# get attachment lists from both services
attachments = mediapackagesearch['attachments']['attachment']
attachmentsfrommediapackage = []
# make sure that tracks are lists not only objects
if (isinstance(attachments, list)) :
attachmentsfrommediapackage = attachments
else:
attachmentsfrommediapackage.append(attachments)
# replace old attachments list
mediapackagesearch['attachments']['attachment'] = attachmentsfromarchive + attachmentsfrommediapackage
# merge both track lists
tracknew = trackfrommediapackage + trackfromarchive
# remove all streaming server entries
trackwithoutrtmp = []
for t in tracknew:
url = str(t.get('url'))
if not re.match("^rtmp", url):
trackwithoutrtmp.append(t)
# add new tracklist to mediapackage again
mediapackagesearch['media']['track'] = trackwithoutrtmp
finalmediapackage = {}
finalmediapackage['mediapackage'] = mediapackagesearch
reload(sys)
sys.setdefaultencoding('utf-8')
# write to json file with current ID
#with open(sys.argv[1]+'.json', 'w') as f:
# json.dump(finalmediapackage, f, ensure_ascii=False)
#################### start ingesting
# empty mediapackage with the right ID
#ingest_mp = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><mediapackage xmlns="http://mediapackage.opencastproject.org" id="' + sys.argv[1] + '" start="2016-08-01T12:44:03Z"><media/><metadata/><attachments/><publications/></mediapackage>'
# create mediapackage with right ID
ingest_track_resp = requests.put(config.targetserver + "/ingest/createMediaPackageWithID/"+sys.argv[1], headers=config.header, auth=targetauth)
ingest_mp = ingest_track_resp.text
# parse Tags to String list seperated by ,
def parseTagsToString(tags):
#fix json bug, on element=not list element
if type(tags) is list :
#tags=t.get("tags")
tags=','.join(tags)
return tags
else:
#tags= t.get("tags").get("tag")
return tags
#create correct json object
mediapackagesearch['metadata']['catalog'] = jsonMakeObjectToList(mediapackagesearch['metadata']['catalog'])
# download catalogs with curl and upload them to the target opencast (no checking for errors yet)
for c in mediapackagesearch['metadata']['catalog']:
if (c.get('type') and c.get('url')):
filename = str(c.get('url')).split("/")[-1]
command = "curl --digest -u " + config.sourceuser +":" + config.sourcepassword + " -H 'X-Requested-Auth: Digest' " + c.get('url') + " -o " + filename
os.system(command)
files = {'file': open(filename, 'rb')}
tags = parseTagsToString(c.get("tags").get("tag"))
payload = {'flavor': c.get("type"), 'mediaPackage': ingest_mp, 'tags' : tags }
ingest_track_resp = requests.post(config.targetserver + "/ingest/addCatalog", headers=config.header, files=files, auth=targetauth, data=payload)
ingest_mp = ingest_track_resp.text
os.remove(filename)
# create correct json object
mediapackagesearch['attachments']['attachment']=jsonMakeObjectToList(mediapackagesearch['attachments']['attachment'])
# download attachments with curl and upload them to the target opencast (no checking for errors yet)
for a in mediapackagesearch['attachments']['attachment']:
if (c.get('type') and c.get('url')):
filename = str(a.get('url')).split("/")[-1]
command = "curl --digest -u " + config.sourceuser +":" + config.sourcepassword + " -H 'X-Requested-Auth: Digest' " + a.get('url') + " -o " + filename
os.system(command)
files = {'file': open(filename, 'rb')}
tags = parseTagsToString(a.get("tags").get("tag"))
payload = {'flavor': a.get("type"), 'mediaPackage': ingest_mp, 'tags' : tags }
ingest_track_resp = requests.post(config.targetserver + "/ingest/addAttachment", headers=config.header, files=files, auth=targetauth, data=payload)
ingest_mp = ingest_track_resp.text
os.remove(filename)
# create correct json object
#mediapackagesearch['media']['track']=jsonMakeObjectToList(mediapackagesearch['media']['track'])
# download tracks with curl and upload them to the target opencast (no checking for errors yet)
for t in mediapackagesearch['media']['track']:
if (c.get('type') and c.get('url')):
filename = str(t.get('url')).split("/")[-1]
command = "curl --digest -u " + config.sourceuser +":" + config.sourcepassword + " -H 'X-Requested-Auth: Digest' " + t.get('url') + " -o " + filename
os.system(command)
files = {'file': open(filename, 'rb')}
tags = parseTagsToString(t.get("tags").get("tag"))
if (archivePresentationTracks):
tags += ','+ 'archive'
payload = {'flavor': t.get("type"), 'mediaPackage': ingest_mp, 'tags' : tags }
ingest_track_resp = requests.post(config.targetserver + "/ingest/addTrack", headers=config.header, files=files, auth=targetauth, data=payload)
ingest_mp = ingest_track_resp.text
os.remove(filename)
def prettify(elem):
"""Return a pretty-printed XML string for the Element.
"""
rough_string = ElementTree.tostring(elem, 'utf-8')
reparsed = minidom.parseString(rough_string)
return reparsed.toprettyxml(indent=" ")
#print(prettify(ElementTree.fromstring(ingest_mp)))
def ingestMediapackage(mediapackage):
mediapackage=prettify(ElementTree.fromstring(ingest_mp))
f = open('mediapackage.xml', 'w')
f.write(mediapackage)
f.close()
payload = {'mediaPackage': mediapackage}
ingest_track_resp = requests.post(config.targetserver + "/ingest/ingest/"+config.targetworkflow, headers=config.header, auth=targetauth, data=payload)
print(ingest_track_resp.text)
print ("Ingesting done")
ingestMediapackage(ingest_mp)