You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
5.4 KiB
Python

#! /usr/bin/env python
# -*- coding: utf-8 -*-
import urllib2, json, pprint, re
import xml.etree.ElementTree as ET
import subprocess, shlex
sid = '1234'
useragent = "Mozilla/5.001 (windows; U; NT4.0; en-US; rv:1.0) Gecko/25250101"
endpoint = "http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&"
# API MODULES
def api_request(action, pagename): #get page: content, metadata, images, imageifnp
url = endpoint + action.format(pagename)
request = urllib2.urlopen(url)
jsonp = json.loads(request.read() )
json_dic= (jsonp.get('query').get('pages'))
# pprint.pprint( json_dic )
page_id = json_dic.keys()[0]
page_content = json_dic.get(page_id)
return page_content
def api_page(pageid, query):
if query == 'content':
api_response = api_request('action=query&pageids={}&prop=revisions&rvprop=content', pageid)
response = ((api_response.get('revisions'))[0])['*']
elif query == 'metadata':
response = api_request('action=query&pageids={}&prop=info', pageid)
elif query == 'articleimgs':
response = api_request('action=query&pageids={}&prop=images', pageid)
elif query == 'file':
response = api_request('action=query&titles=File:{}&prop=imageinfo&iiprop=url',pageid)
pprint.pprint( response )
elif query == 'imageinfo':
pagename = pageid # in imageinfo titles are used instead of id
response = api_request('action=query&titles=File:{}&prop=imageinfo&iiprop=url&iiurlwidth=500', pagename) # iiurlwidht dermines with of thumbnail
return response
def api_file_url(filename): # get full urls
page_content_dict = api_page(filename, 'file')
if 'imageinfo' in page_content_dict.keys():
imgurl = ((page_content_dict.get('imageinfo'))[0].get('url'))
return imgurl
else:
return None
def api_thumb_url(filename):
'''get thumbnail url of image'''
page_content_dict = api_page(filename, 'imageinfo')
if 'imageinfo' in page_content_dict.keys():
thumburl = ((page_content_dict.get('imageinfo'))[0].get('thumburl'))
return thumburl
# http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&titles=File:2x2 905.jpg&prop=imageinfo&iiprop=url&iiurlwidth=300
# http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&pageids=10603&prop=revisions&rvprop=content
# http://pzwiki.wdka.nl/mw-mediadesign/api.php?format=json&action=query&titles=Graduation_Website_Braindump&prop=revisions&rvprop=content
# XML MODULES
def write_html_file(html_tree, filename):
doctype = "<!DOCTYPE HTML>"
html = doctype + ET.tostring(html_tree, encoding='utf-8', method='html')
edited = open(filename, 'w') #write
edited.write(html)
edited.close()
# Conversion Modules
def pandoc2html(mw_content):
if mw_content:
mw_content = mw_content.encode('utf-8')
# convert from mw to html
args_echo =shlex.split( ('echo "{}"'.format(mw_content)) )
args_pandoc = shlex.split( 'pandoc -f mediawiki -t html5' )
p1 = subprocess.Popen(args_echo, stdout=subprocess.PIPE)
p2 = subprocess.Popen(args_pandoc, stdin=p1.stdout, stdout=subprocess.PIPE)
html = (p2.communicate())[0]
return html
def pandoc(filename, title, creator, date, website, thumbnail, bio, description, extra, template) :
'''pandoc: convert mediawiki syntax to html'''
# mw_content = mw_content.encode('utf-8')
args_echo =shlex.split( ('echo "{}"'.format( extra )) )
args_pandoc = shlex.split( 'pandoc -s -f mediawiki -t html \
--template {template} --variable title="{title}" --variable creator="{creator}" --variable date="{date}" --variable website="{website}" --variable website="{website}" --variable thumbnail="{thumbnail}" --variable bio="""{bio}""" -o {filename}'.format(template=template, title=title, creator=creator, date=date, website=website, thumbnail=thumbnail, bio=bio, description=description, extra=extra, filename=filename) )
print args_pandoc
p1 = subprocess.Popen(args_echo, stdout=subprocess.PIPE)
p2 = subprocess.Popen(args_pandoc, stdin=p1.stdout, stdout=subprocess.PIPE)
html = (p2.communicate())[0]
# return html
# pandoc either reades input from stdin or through input file
# pandoc DOES NOT convert variables
# pandoc = 'pandoc -s -f mediawiki -t html5 \
# --template template_article.html \
# --variable title="{title}" \
# --variable section="{section}" \
# --variable topics="{topics}" \
# --variable issueName="{iname}" \
# --variable issueNumber="{inum}" \
# "articles/tmp_content.mw" -o "{articlepath}/{htmlfile}.html"'.format(articlepath=path, title=(pagename).replace("_"," "), section=in_section, topics=in_topic, iname=in_issuename, inum=in_issue, htmlfile=pagename)
# subprocess.call(pandoc, shell=True) # saved in tmp_content.html html
# html = open('tmp_content.html', 'r') #write mediawiki content to html in tmp_content.html
# html = html.read()
# return html
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