-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_videos_and_xml.py
executable file
·275 lines (231 loc) · 11.8 KB
/
get_videos_and_xml.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
import argparse
import cgi
import json
import re
import magic
import os
import shutil
import sys
import urllib
import lxml.etree
import lxml.html
import lxml.html.clean
import requests
from config import MAX_FILENAME_SIZE
parser = argparse.ArgumentParser(description='TIB AV Portal: Upload and metadata generation ')
parser.add_argument('schedule', help='schedule.xml file name', default='schedule.xml')
parser.add_argument('--verbose', '-v', action='store_true', default=False)
args = parser.parse_args()
# lxml does only support http and not https, compare https://stackoverflow.com/a/26164472
# schedule = etree.fromstring(requests.get("https://events.ccc.de/congress/2017/Fahrplan/schedule.xml").content)
# schedule = lxml.etree.parse("temp/schedule.xml")
schedule = lxml.etree.parse(args.schedule)
acronym = schedule.find('conference').find('acronym').text
# TODO: use argument parser?
ignore_license = True
download_dir = 'videos_c3voc'
mime = magic.Magic(mime=True)
def escape_html_sc(event, element):
return cgi.escape(event.find(element).text.encode('utf-8').strip())
def main():
errors = []
for event_id in schedule.xpath(u'day/room/event/@id'):
event = schedule.xpath('day/room/event[@id="' + event_id + '"]')[0]
slug = event.find('slug').text.encode('utf-8').strip() # slug will be escaped below in xml
slug_short = slug[0:MAX_FILENAME_SIZE]
title = ''
try:
title = escape_html_sc(event, 'title')
if args.verbose:
print('\n== ' + title)
except:
sys.stderr.write(' \033[91mWARNING: ' + slug + ' Title not found. \033[0m\n')
subtitle = ''
try:
subtitle = escape_html_sc(event, 'subtitle')
except:
sys.stderr.write(' \033[91mWARNING: ' + slug + ' Subtitle not found. \033[0m\n')
link = ''
try:
link = escape_html_sc(event, 'url')
except:
sys.stderr.write(' \033[91mWARNING: ' + slug + ' Link not found. \033[0m\n')
try:
optout = event.find('recording').find('optout').text
except:
sys.stderr.write(' \033[91mERROR: No optout information for ' + slug + ' \033[0m\n')
errors.append('ERROR: ' + event_id + ' : "' + slug + '" : No optout information found.')
continue
if optout == 'true':
sys.stderr.write(' \033[91mERROR: Ignoring ' + slug + ' due to optout. \033[0m\n')
errors.append('ERROR: ' + event_id + ' : "' + slug + '" : Ignored due to optout.')
continue
if not ignore_license and event.find('recording').find('license').text is None:
sys.stderr.write(' \033[91mERROR: ' + slug + ' has empty recording license. \033[0m\n')
errors.append('ERROR: ' + event_id + ' : "' + slug + '" : Empty recording license.')
continue
# request recording from voctoweb aka media.ccc.de
guid = event.attrib['guid']
try:
recording = find_recoding(guid)
file_url = recording['recording_url']
except:
sys.stderr.write(' INFO: ' + slug + ' : Using alternative video repository (live.ber.c3voc.de).\n')
file_url = 'http://live.ber.c3voc.de/releases/{}/{}-hd.mp4'.format(acronym, event_id)
# open file url
download_path = download_dir + '/{0}'.format(slug_short)
try:
if not os.path.exists(download_path):
os.mkdir(download_path)
video_filename = download_dir + '/{0}/{0}.mp4'.format(slug_short)
urllib.urlretrieve(file_url, video_filename)
mime_type = mime.from_file(video_filename)
#print(mime_type)
if mime_type != 'video/mp4':
shutil.rmtree(download_path)
sys.stderr.write(' \033[91mERROR: ' + slug + ' : Video is not valid mp4. \033[0m\n')
errors.append('ERROR: ' + event_id + ' : "' + slug + '" : Video is not valid mp4.')
continue
except:
if os.path.exists(download_path):
shutil.rmtree(download_path)
sys.stderr.write(' \033[91mERROR: ' + slug + ' : HTTPError ocurred. \033[0m\n')
errors.append('ERROR: ' + event_id + ' : "' + slug + '" : HTTPError ocurred.')
continue
# format person names to library conventions – random search result:
# https://books.google.de/books?id=wJyoBgAAQBAJ&pg=PA68&lpg=PA68&dq=bibliotheken+mehrere+vornamen&source=bl&ots=bP4gjj1Zft&sig=2HxD9qHWHzo7Z0kMc5vMITo83ps&hl=en&sa=X&redir_esc=y#v=onepage&q=bibliotheken%20mehrere%20vornamen&f=false
persons = []
for p in event.find('persons'):
p = cgi.escape(p.text)
p = p.split(' ')
if len(p) > 3:
print(' \033[91mWARNING: Person name consists of more than three parts: ' + str(p) + '\033[0m')
if len(p) == 1:
persons.append(p[0])
elif len(p) == 2:
persons.append(p[1] + ', ' + p[0])
else:
persons.append(p[-1] + ', ' + p[0] + ' ' + p[1])
# see https://github.com/voc/scripts/blob/master/slides/get_attachments.py
material = []
pdfs = [f for f in os.listdir(download_path) if os.path.isfile(f) and mime.from_file(f) == 'application/pdf']
if pdfs is not None and len(pdfs) > 0:
for pdf in pdfs:
pdf_formatted = pdf[:-4] + '.pdf' # extension is already checked in get_attachments.py
material.append(('File', 'Slides as PDF', pdf_formatted))
lang = event.find('language').text
if lang == 'en':
lang = 'eng'
elif lang == 'de':
lang = 'ger'
else:
lang = ''
# no escaping for abstract, will be placed in <![CDATA[]]>
abstract = ''
try:
abstract = event.find('abstract').text.encode('utf-8').strip()
except:
try:
# use description when abstract is empty
abstract = event.find('description').text.encode('utf-8').strip()
except:
sys.stderr.write(' \033[91mWARNING: ' + slug + ' has empty abstract. \033[0m\n')
track = ''
try:
track = escape_html_sc(event, 'track')
except:
sys.stderr.write(' \033[91mWARNING: ' + slug + ' : Track not found. \033[0m\n')
links = []
try:
links = event.find('links')
except:
sys.stderr.write(' \033[91mWARNING: ' + slug + ' : Links not found. \033[0m\n')
with open(download_dir + '/{0}/{0}.xml'.format(slug_short), 'wt') as f:
# TODO: Test if XML generation via external library e.g. via LXML produces nicer code
metadata = '''<?xml version="1.0" encoding="UTF-8" ?>
<resource xmlns="http://www.tib.eu/fileadmin/extern/knm/NTM-Metadata-Schema_v_2.2.xsd">
<externalLocalID externalLocalIDType="local-frab-event-id">''' + guid + '''</externalLocalID>
<titles><title language="''' + lang + '''">''' + title + '''</title>
<title titleType="Subtitle" language="''' + lang + '''">''' + subtitle + '''</title></titles>
<creators>''' + '\n '.join(
['<creator><creatorName>{}</creatorName></creator>'.format(cgi.escape(p.encode('utf-8').strip()))
for p in persons])\
+ '''</creators><language>''' + lang + '''</language><genre>Conference</genre>'''
if abstract:
metadata = metadata \
+ '''<descriptions><description descriptionType="Abstract" language="''' \
+ lang + '''"><![CDATA[''' \
+ abstract + ''']]></description></descriptions>'''
metadata = metadata + '''<additionalMaterials>'''
if material is not None:
metadata = metadata + '\n '.join([
'<additionalMaterial additionalMaterialType="{a[0]}" additionalMaterialTitle="{a[1]}" relationType="isSupplementedBy">{a[2]}</additionalMaterial>'.format(
a=a) for a in material])
if links is not None:
metadata = metadata + '\n '.join([
'<additionalMaterial additionalMaterialType="URL" additionalMaterialTitle="{0}" relationType="isSupplementedBy">{1}</additionalMaterial>'.format(
cgi.escape(a.text.encode('utf-8').strip().replace('"', '')), cgi.escape((a.attrib['href']).encode('utf-8').strip())) for a in links])
metadata = metadata + '''<additionalMaterial additionalMaterialType="URL" additionalMaterialTitle="media.ccc.de" relationType="isCitedBy">https://media.ccc.de/v/'''\
+ cgi.escape(slug) + '''</additionalMaterial>'''
if link != '':
r = re.search('(https?://)((\w+\.)+\w+)?(/\S+)?', link)
try:
additional_material_title_link = r.group(2)
except IndexError:
sys.stderr.write(' \033[91mWARNING: ' + slug + ' : Link for additional material title could not be extracted. \033[0m\n')
if additional_material_title_link is None or additional_material_title_link == '':
sys.stderr.write(' \033[91mWARNING: ' + slug + ' : Link for additional material title is empty and won\'t be added. \033[0m\n')
else:
additional_material_title_link = '''additionalMaterialTitle="''' + additional_material_title_link + '''" '''
metadata = metadata + '''<additionalMaterial additionalMaterialType="URL" ''' + additional_material_title_link + '''relationType="isCitedBy">''' + link + '''</additionalMaterial>'''
metadata = metadata + '''</additionalMaterials>
<keywords><keyword language="''' + lang + '''">''' + track + '''</keyword></keywords>
<publishers><publisher><publisherName>Chaos Computer Club e.V.</publisherName></publisher></publishers>
<publicationYear>2017</publicationYear></resource>'''
# TODO get year from event date
f.write(metadata)
if errors != '':
with open('errors_' + acronym + '.txt', 'w') as error_file:
error_file.write('\n'.join(errors))
def find_recoding(guid):
# request event + recordings from voctoweb aka media.ccc.de
global voctoweb_event
voctoweb_url = 'https://media.ccc.de/public/events/' + guid
guid_exist = True
try:
voctoweb_event = requests.get(voctoweb_url).json()
except:
guid_exist = False
if not guid_exist:
return False
else:
results = []
print('GUID: ' + guid)
try:
for r in voctoweb_event['recordings']:
# select mp4 which contains only the orginal language
if r['folder'] == 'h264-hd' and r['mime_type'] == 'video/mp4' and r['language'] == voctoweb_event['original_language']:
# and 'slides' not in r['folder']:
results.append(r)
if len(results) > 1:
sys.stderr.write('\033[91mFATAL: API returned multiple recordings: {} \033[0m\n'.format(voctoweb_url))
sys.stderr.write(json.dumps(results, indent=4))
return False
elif len(results) == 1:
return results[0]
return None
except:
event_exist = False
if not event_exist:
return False
# from https://stackoverflow.com/a/42461722/521792
def strip_tags(string):
tree = lxml.html.fromstring(string)
clean_tree = lxml.html.clean.clean_html(tree)
return clean_tree.text_content()
if __name__ == '__main__':
if not os.path.exists(download_dir):
os.mkdir(download_dir)
main()