#!/usr/local/bin/python

from urllib2 import build_opener, HTTPCookieProcessor, Request
from urllib import urlencode, urlretrieve
from BeautifulSoup import BeautifulSoup, BeautifulStoneSoup
import re
import time
import subprocess  #os
import cgitb; #cgitb.enable(display=0, logdir="/tmp")

model_summary_page = "Extracted_model_log"

#specify the root URL of the wiki
url_root = 'http://www.yeastpheromonemodel.org'

#the folder where the html documents will be stored
date = time.strftime("%Y_%m_%d_%Hh%Mm%Ss")
docFolder =  date + '/'
docPath = '/data/www/html/alpha/ModelDocs/'

retcode = subprocess.call(["mkdir", docPath + docFolder])


#Build opener
opener = build_opener(HTTPCookieProcessor)

#Authenticate - note that I took the password out of the authentication...
f=opener.open(Request(url_root + "/index.php?title=Special:Uerlogin",urlencode(dict(wpName="Wiki parser",wpPassword="",action="submitlogin"))))
f.close()




#Edit the Extracted model log page
f=opener.open(url_root + "/index.php?title=" + model_summary_page + "&action=edit")
#Read the HTML contents into a string of text
doc = f.read()
f.close()

soup = BeautifulSoup(doc)

editForm = soup.find("form", id="editform")

new_text = "==Model " + date + "=="
new_text = new_text + "\n*It takes about 20 minutes for the model file and all the documentation to be produced, so if the links do not work yet, please be patient."
new_text = new_text + "\n*Model documentation: " + url_root + '/ModelDocs/' + docFolder
new_text = new_text + "\n*Main model page: " + url_root + '/ModelDocs/' + docFolder + "Yeast_pheromone_response_model.html"
new_text = new_text + "\n*Model file: " + url_root + '/ModelDocs/' + docFolder + "model-" + date + ".bngl"
new_text = new_text + "\n*Zip file of documentation: " + url_root + '/ModelDocs/' + docFolder + date + ".zip"
new_text = new_text + "\n\n" + editForm.textarea.string


wpSection = editForm.find(attrs={"name":"wpSection"})
wpSection["value"] = new_text

inputs = editForm.findAll("input")


data = {'wpTextbox1' : wpSection["value"],
        'wpSummary' : "",
        'wpMinoredit' : "1",
        'wpSave' : "Save page",
        'wpEditToken' : editForm.find(attrs={"name":"wpEditToken"})["value"],
        'wpAutoSummary':editForm.find(attrs={"name":"wpAutoSummary"})["value"],
        'wpStarttime':editForm.find(attrs={"name":"wpStarttime"})["value"],
        'wpEdittime':editForm.find(attrs={"name":"wpEdittime"})["value"]}

urldata = urlencode(data)

f=opener.open(url_root + "/index.php?title=" + model_summary_page + "&action=submit", urldata)
f.close()


print 'Content-Type: text/html\n\n'     # HTML is following

print '<html><head>\n'
print '<META HTTP-EQUIV="Refresh" CONTENT="0; URL=' + url_root + '/index.php?title=' + model_summary_page + '&action=purge">'
print '</head></html>'


f = open(docPath + docFolder + "logfile.log", "w")
pid = subprocess.Popen(["./wiki_parser_v2.py", date], stdout = f, stderr = f).pid
f.close()




