2021-06-16 16:46:25 +02:00
import urllib . request
import os
import re
import json
import jinja2
2021-09-01 13:09:09 +02:00
STATIC_FOLDER_PATH = ' . ' # without trailing slash
WRAPPING_TEMPLATES_DIR = ' ./templates '
def API_request ( url , pagename ) :
2021-06-16 16:46:25 +02:00
"""
url = API request url ( string )
data = { ' query ' :
' pages ' :
pageid : {
' links ' : {
' ? ' : ' ? '
' title ' : ' pagename '
}
}
}
}
"""
response = urllib . request . urlopen ( url ) . read ( )
data = json . loads ( response )
# Save response as JSON to be able to inspect API call
2021-09-01 13:09:09 +02:00
json_file = f ' { STATIC_FOLDER_PATH } / { pagename } .json '
2021-06-16 16:46:25 +02:00
print ( ' Saving JSON: ' , json_file )
with open ( json_file , ' w ' ) as out :
out . write ( json . dumps ( data , indent = 4 ) )
out . close ( )
return data
2021-09-01 13:09:09 +02:00
def download_media ( html , images , wiki ) :
2021-06-16 16:46:25 +02:00
"""
html = string ( HTML )
images = list of filenames ( str )
"""
# check if 'images/' already exists
2021-09-01 13:09:09 +02:00
if not os . path . exists ( f ' { STATIC_FOLDER_PATH } /images ' ) :
os . makedirs ( f ' { STATIC_FOLDER_PATH } /images ' )
2021-06-16 16:46:25 +02:00
# download media files
for filename in images :
filename = filename . replace ( ' ' , ' _ ' ) # safe filenames
# check if the image is already downloaded
# if not, then download the file
2021-09-01 13:09:09 +02:00
if not os . path . isfile ( f ' { STATIC_FOLDER_PATH } /images/ { filename } ' ) :
2021-06-16 16:46:25 +02:00
# first we search for the full filename of the image
url = f ' { wiki } /api.php?action=query&list=allimages&aifrom= { filename } &format=json '
response = urllib . request . urlopen ( url ) . read ( )
data = json . loads ( response )
# we select the first search result
# (assuming that this is the image we are looking for)
image = data [ ' query ' ] [ ' allimages ' ] [ 0 ]
# then we download the image
image_url = image [ ' url ' ]
image_filename = image [ ' name ' ]
print ( ' Downloading: ' , image_filename )
image_response = urllib . request . urlopen ( image_url ) . read ( )
# and we save it as a file
2021-09-01 13:09:09 +02:00
image_path = f ' { STATIC_FOLDER_PATH } /images/ { image_filename } '
2021-06-16 16:46:25 +02:00
out = open ( image_path , ' wb ' )
out . write ( image_response )
out . close ( )
import time
time . sleep ( 3 ) # do not overload the server
# replace src link
2021-09-01 13:09:09 +02:00
image_path = f ' / { STATIC_FOLDER_PATH } /images/ { filename } ' # here the images need to link to the / of the domain, for flask :/// confusing! this breaks the whole idea to still be able to make a local copy of the file
2021-06-16 16:46:25 +02:00
html = re . sub ( rf ' src= " /book/images/.* { filename } " ' , f ' src= " { image_path } " ' , html )
return html
def clean_up ( html ) :
"""
html = string ( HTML )
"""
html = re . sub ( r ' \ [.*edit.* \ ] ' , ' ' , html ) # remove the [edit]
2021-09-01 13:09:09 +02:00
html = re . sub ( r ' href= " /book/index.php \ ?title= ' , ' href= " # ' , html ) # remove the internal wiki links
2021-06-16 16:46:25 +02:00
return html
2021-09-01 13:09:09 +02:00
def parse_page ( pagename , wiki ) :
2021-06-16 16:46:25 +02:00
"""
pagename = string
html = string ( HTML )
"""
parse = f ' { wiki } /api.php?action=parse&page= { pagename } &pst=True&format=json '
2021-09-01 13:09:09 +02:00
data = API_request ( parse , pagename )
2021-06-16 16:46:25 +02:00
# print(json.dumps(data, indent=4))
if ' parse ' in data :
html = data [ ' parse ' ] [ ' text ' ] [ ' * ' ]
images = data [ ' parse ' ] [ ' images ' ]
2021-09-01 13:09:09 +02:00
html = download_media ( html , images , wiki )
2021-06-16 16:46:25 +02:00
html = clean_up ( html )
else :
html = None
return html
2021-09-01 13:09:09 +02:00
def save ( html , pagename , publication_unfolded ) :
2021-06-16 16:46:25 +02:00
"""
html = string ( HTML )
pagename = string
"""
if html :
# save final page that will be used with PagedJS
2021-09-01 13:09:09 +02:00
template_file = open ( f ' { STATIC_FOLDER_PATH } / { WRAPPING_TEMPLATES_DIR } /template.html ' ) . read ( )
2021-06-16 16:46:25 +02:00
template = jinja2 . Template ( template_file )
html = template . render ( publication_unfolded = publication_unfolded , title = pagename )
2021-09-01 13:09:09 +02:00
html_file = f ' { STATIC_FOLDER_PATH } / { pagename } .html '
2021-06-16 16:46:25 +02:00
print ( ' Saving HTML: ' , html_file )
with open ( html_file , ' w ' ) as out :
out . write ( html )
out . close ( )
# save extra html page for debugging
2021-09-01 13:09:09 +02:00
template_file = open ( f ' { STATIC_FOLDER_PATH } / { WRAPPING_TEMPLATES_DIR } /template.inspect.html ' ) . read ( )
2021-06-16 16:46:25 +02:00
template = jinja2 . Template ( template_file )
html = template . render ( publication_unfolded = publication_unfolded , title = pagename )
2021-09-01 13:09:09 +02:00
html_file = f ' { STATIC_FOLDER_PATH } / { pagename } .inspect.html '
2021-06-16 16:46:25 +02:00
print ( ' Saving HTML: ' , html_file )
with open ( html_file , ' w ' ) as out :
out . write ( html )
out . close ( )
2021-09-01 13:09:09 +02:00
def update_material_now ( pagename , wiki ) :
2021-06-16 16:46:25 +02:00
"""
pagename = string
publication_unfolded = string ( HTML )
"""
2021-09-01 13:09:09 +02:00
publication_unfolded = parse_page ( pagename , wiki )
2021-06-16 16:46:25 +02:00
return publication_unfolded
# ---
2021-09-01 13:09:09 +02:00
if __name__ == " __main__ " :
2021-06-16 16:46:25 +02:00
2021-09-01 13:09:09 +02:00
wiki = ' https://possiblebodies.constantvzw.org/book ' # remove tail slash '/'
pagename = ' Unfolded '
publication_unfolded = update_material_now ( pagename , wiki ) # download the latest version of the page
save ( publication_unfolded , pagename , publication_unfolded ) # save the page to file
2021-06-16 16:46:25 +02:00