From 03a6f8614420f5d6afcbf674a55d2898fee387fd Mon Sep 17 00:00:00 2001 From: Castro0o Date: Wed, 6 May 2015 06:57:06 +0200 Subject: [PATCH] video and gallary parsing running --- mmdc_modules.py | 63 +++++++++-------------------------------------- prototype_page.py | 30 +++++++--------------- 2 files changed, 20 insertions(+), 73 deletions(-) diff --git a/mmdc_modules.py b/mmdc_modules.py index a4b4cae..96ce5ac 100644 --- a/mmdc_modules.py +++ b/mmdc_modules.py @@ -150,68 +150,27 @@ def pandoc(filename, title, creator, date, website, thumbnail, bio, description, def img_fullurl(parent): imgs = parent.findall('.//img') - print 'len IMG', len(imgs) for img in imgs: src = img.get('src') fullurl = api_thumb_url(src) - print '----- IMG', ET.tostring(img ), src, fullurl if fullurl != None: img.set('src', fullurl) - # fileurl = api_request(src, endpoint)# find url of file - - - -gallery_exp=re.compile('.*?', re.S) -imgfile_exp=re.compile('(File:(.*?\.(gif|jpg|jpeg|png)))')# (?=File:|<\/gallery>)') -img_exp=re.compile('(File:|Image:)((.*?)\.(gif|jpg|jpeg|png))(?=\||File:|Image:|<\/gallery>)', re.I) -video_exp=re.compile('\{\{(.*?)\|(.*?)\}\}') - -def replace_gallery(content): - gallery_imgs = [] - gallery_found = re.findall(gallery_exp, content) - for gallery in gallery_found: # in case there is more than 1 - allfiles =re.findall(imgfile_exp, gallery) - print 'ALLFILES', allfiles - for imgfile in allfiles: - imgfile = imgfile[1] - #imgsrc = imgfile - imgsrc = api_file_url(imgfile) # seaarch for original image - newimg = '' - gallery_imgs.append(imgsrc) - print 'GALLERY_IMGS', gallery_imgs - - ungallery_imgs = " ".join(gallery_imgs) - print 'ungallery_imgs', ungallery_imgs - content = re.sub(gallery_exp, ungallery_imgs, content) - print 'images content', content - - ## BUG: Images are being replaced as - # EG: File:Mb-WordNet-tour-version2-08.png File:Labanotation1.jpg +gallery_exp=re.compile('(.*?)', re.S) +imgfile_exp=re.compile('(File:(.*?)\.(gif|jpg|jpeg|png))') +def replace_gallery(content): + content = re.sub(imgfile_exp, '[[\g<1>]]', content) #add [[ ]] to File:.*? + content = re.sub(gallery_exp, '\g<1>', content) #remove gallery wrapper return content +video_exp=re.compile('\{\{(.*?)\|(.*?)\}\}') +vimeo_exp=re.compile('\{\{vimeo\|(.*?)\}\}') +youtube_exp=re.compile('\{\{youtube\|(.*?)\}\}') + def replace_video(content): - videos = [] - videos_found = re.findall(video_exp, content) - for video in videos_found: - video_provider = str(video[0]) - video_hash = str(video[1]) - video_src = None - if (video_provider.lower()) == 'youtube': - video_src="https://www.youtube.com/embed/" + video_hash - elif (video_provider.lower()) == 'vimeo': - video_src="https://player.vimeo.com/video/" + video_hash - if video_src: - videos.append(video_src) - iframe = "".format(video_src) - content = re.sub(video_exp, iframe, content) - else: - content = re.sub(video_exp, '', content) + content = re.sub(vimeo_exp,"", content) + content = re.sub(youtube_exp, "", content) return content -## Video Replacement: problem with video: iframe is placed inside

. It shouldn't - -## replace gallery - not yet there - diff --git a/prototype_page.py b/prototype_page.py index b7985c7..1e26abe 100755 --- a/prototype_page.py +++ b/prototype_page.py @@ -20,48 +20,36 @@ from mmdc_modules import api_request, api_page, api_thumb_url, pandoc2html, pars template = open("web/page-template.html", "r") template = template.read() -# download +# download mw work page pageid='15965'#Qq #'16025' #'15986'Jozeph #'16025'Mina work = 'Q' #'Mina'#'User:Joak/graduation/catalog1' workpage_mw = api_page(pageid, 'content') -print '------------------- workpage_mw' -if re.search(gallery_exp, workpage_mw): - print 'FOUND GALLERY' - workpage_mw = replace_gallery(workpage_mw) -if re.search(video_exp, workpage_mw): - workpage_mw = replace_video(workpage_mw) - print 'FOUND VIDEO' -''' -# parsing workpage_mw -workdict = parse_work(work, workpage_mw) +# parse workpage_mw +workpage_mw = replace_gallery(workpage_mw) +workpage_mw = replace_video(workpage_mw) +workdict = parse_work(work, workpage_mw) # create dictionary workpage_mw template for key in workdict.keys(): if key in ['Extra', 'Description', 'Bio']: workdict[key] = pandoc2html(workdict[key].encode('utf-8')) +# fill template with dictionary/mw_page values workpage_html = template.format(title=workdict['Title'], creator=workdict['Creator'], date=workdict['Date'], website=workdict['Website'], thumbnail=workdict['Thumbnail'], bio=workdict['Bio'],description=workdict['Description'], extra=workdict['Extra'] ) -# Process html +# parse workpage_html # process html: img full url tree = html5lib.parse(workpage_html, namespaceHTMLElements=False) imgs = tree.findall('.//img') for img in imgs: src = img.get('src') newsrc = api_file_url(src) -# print 'new src', newsrc if newsrc: img.set('src', newsrc) - #print 'IMG', ET.tostring(img) - - +# save workpage_html workpage_html = ET.tostring(tree) -#print 'TREE', workpage_html - - -# # save work_filename = 'web/{}-{}-{}.html'.format(workdict['Date'], (workdict['Creator'].encode('ascii', 'ignore')).replace(' ','_'), pageid) work_file = open(work_filename, "w") work_file.write(workpage_html) work_file.close() -''' +