#author("2024-02-15T15:13:40+09:00","default:pico_wiki","pico_wiki")
#author("2024-02-15T15:14:04+09:00","default:pico_wiki","pico_wiki")
[[pico_wiki_driver_03.py]]

** pico_wiki_driver_ex02.py が読み書きする PukiWiki のページの例 [#l90ec396]
&ref(pico_wiki_driver_ex02.py/20240207-wiki-01.png,50%);

** pico_wiki_driver_ex02.py [#a673a4ab]
#code(python){{
# pico_wiki_driver_ex02.py, by Takashi Yamanoue, Fukuyama University, Jan. 2024
# pico_wiki_driver_ex02.py, by Takashi Yamanoue, Fukuyama University, Feb. 7,  2024
import machine
import time
import network
import urequests
import socket
try:
    import ustruct as struct
except:
    import struct
from time import sleep
from machine import Timer
import gc

device_id="yama_pico_0000_0000_0000_0001"
init_url="http://192.168.13.30/pukiwiki/pico_wiki/"
init_page="20240207-01"
ssid = 'Wi-Fi_Access_Point_SSID'
password = 'Wi_Fi_Access_Point_Password' 

class pico_net:

    def __init__(self):
        print('current ssid= '+ssid)
    
    def set_access_point(self, ssid, password):
        self.ssid = ssid
        self.password = password

    def connect(self):
        self.wlan = network.WLAN(network.STA_IF)
        self.wlan.active(True)
        chipid = int.from_bytes(self.wlan.config('mac'), 'big') % 65536
        self.wlan.connect(ssid, password)
        max_wait = 10
        while max_wait > 0:
            if self.wlan.status() < 0 or self.wlan.status() >= 3:
                break
            max_wait -= 1
            print('waiting for connection...')
            time.sleep(1)
    
        if self.wlan.status() != 3:
            raise RuntimeError('network connection failed')
        else:
            print('connected')
            self.status = self.wlan.ifconfig()
            print('ip = ' + self.status[0])

class pico_http:
    def __init__(self):
        print('pico_http')

    def set_url(self, url):
        self.url = url

    def get(self, url):
        #print('pico_http.get(urL='+url+')')
        self.url=url
        #print('urL='+url)
        try:
            r = urequests.get(url)
            #print(r)
            r_text=r.text
            r.close()
            #print('pico_http.get...return')
            return r_text
        except:
            print('pico_http.get...exception')
            return ""

    def put(self, url, payload):
        #print('pico_http.put(urL='+url+'payload='+str(payload)+')')
        #payload is a dictionary
        uri=url+'?'
        for key in payload:
            #print(key+'=',end="")
            #print(payload[key])
            uri=uri+key+'='+payload[key]+'&'
        uri=uri[:-1]
        #print('uri='+uri)
        #r = urequests.post(uri)
        #r = urequests.post(url,data=payload)
        r = urequests.get(uri)
        if r=="":
            print('pico_http.put...exception')
            return ""
        #print(r)
        r_text=r.text
        r.close()
        #print(r_json)  
        return r_text
    
    #post, ref: https://docs.micropython.org/en/latest/library/urequests.html
    def post(self, url, body, headers):
        #print('pico_http.post(url='+url+' headers='+str(headers)+')')
        try:
            r = urequests.post(url,data=body,headers=headers)
            #print(r)
            r_text=r.text
            #print(r_text)
            #r_json=r.json()
            r.close()
            #print(r_json)  
            print('pico_http.post...return')
            return r_text
        except:
            print('pico_http.post...exception')
            return ""
    
class pico_time:
    # reference: https://wisteriahill.sakura.ne.jp/CMS/WordPress/2023/05/10/raspberry-pi-pico-w-ntp-server/
    # NTPサーバー
    #ntp_host = "time-c.nist.gov"
    #ntp_host = "time.cloudflare.com"
    #ntp_host = "time.google.com"
    ntp_host = "ntp.nict.jp"

    def __init__(self):
        print('pico_time')

    def ptime(self):
        NTP_DELTA = 2208988800
        
        NTP_QUERY = bytearray(48)
        NTP_QUERY[0] = 0x1b
        addr = socket.getaddrinfo(self.ntp_host, 123)[0][-1]
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        # s.settimeout(1)
        res = s.sendto(NTP_QUERY, addr)
        msg = s.recv(48)
        s.close()
        val = struct.unpack("!I", msg[40:44])[0]
        return val - NTP_DELTA

    wday = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun']
     
    def zfill(self, s, width):
        if len(s) < width:
            return ("0" * (width - len(s))) + s
        else:
            return s

    def get_now(self):
        t=self.ptime()
        t = t + 9*60*60
        #print(t)
        datetimetuple = time.gmtime(t)
        year = str(datetimetuple[0])
        month = self.zfill(str(datetimetuple[1]),2)
        mday = self.zfill(str(datetimetuple[2]),2)
        hour = self.zfill(str(datetimetuple[3]),2)
        minute = self.zfill(str(datetimetuple[4]),2)
        seconds = self.zfill(str(datetimetuple[5]),2)
        wd_index = datetimetuple[6]
        #日付と時刻を分けるデリミター("|")を付加
        #datetime = year + "/" + month + "/" + mday + " " + self.wday[wd_index] + "|" + hour + ":" + minute + ":" + seconds
        datetime = year + "/" + month + "/" + mday + " "  + hour + ":" + minute + ":" + seconds
        return datetime
    
class html_parser:
    html=""
    tokens=[]
    reserved_words={'<':'&lt;','>':'&gt;','&':'&amp;','"':'&quot;','\'':'&apos;',
                   ' ':'&nbsp;','\n':'<br>','\t':'&nbsp;&nbsp;&nbsp;&nbsp;','\r':'',
                   '\\':'&#92;','{':'&#123;','}':'&#125;','[':'&#91;',']':'&#93'
                   }
    a2c={' ':"+",'\n':"%0D%0A",'\r':"%0D",'\t':"%09",'!':"%21",'"':"%22",    
        '#':"%23",'$':"%24",'%':"%25",'&':"%26", '\'':"%27",'(': "%28",')':"%29",
        '*':"%2A",'+':"%2B",',':"%2C",'-':"%2D",'/':"%2F",':':"%3A",
        ';':"%3B",'<':"%3C",'=':"%3D",'>':"%3E",'?':"%3F",
        '@':"%40",'[':"%5B",'\\':"%5C",']':"%5D",'^':"%5E",
        '`':"%60",'{':"%7B",'|':"%7C",'}':"%7D",'~':"%7E"}
    #Spechal char to ascii code
    sc2a={'&lt;':"<",'&gt;':">",'&amp;':"&",'&quot;':'"','&apos;':"\'"}
    def __init__(self,html):
        self.html=html
    def html_tokenizer(self):
        #print("html_tokenizer")
        self.token_ith=0
        w=""
        tx=[""]
        rest=[""]
        inx=self.html
        xlen=len(self.html)
        while xlen>0:
            if self.parse_tag(inx,tx,rest):
                if w!="":
                    self.tokens.append(w)
                    w=""
                self.tokens.append('<'+tx[0]+'>')
                #print('<'+tx[0]+'>')
                inx=rest[0]
                xlen=len(inx)
            elif self.parse_String_Constant(inx,tx,rest):
                if w!="":
                    self.tokens.append(w)
                    w=""
                self.tokens.append('"'+tx[0]+'"')
                #print('"'+tx[0]+'"')
                inx=rest[0]
                xlen=len(inx)
            else:
                wc=inx[0]
                inx=inx[1:xlen]
                xlen=len(inx)
                w=w+wc
        if w!="":
            self.tokens.append(w)
            w=""
    def get_html(self):
        return self.html
    def parse_key(self,key,inx,rest):
        #print("parse_key-"+key)
        keylen=len(key)
        inlen=len(inx)
        if inx.startswith(key):
            rest[0]=inx[keylen:inlen]
            return True
        rest[0]=inx
        return False
   
    def parse_String_Constant(self,inx,strc,rest):
        rx=[""]
        xstr=""
        if self.parse_key('"',inx,rx):
            #print("parse_String_Constant")
            inx=rx[0]
            fx=inx[0]
            xlen=len(inx)
            while fx!='"':
                if xlen==0:
                    return False
                if fx=='\\':
                   inx=inx[1:xlen]
                else:
                   xstr=xstr+fx
                xlen=len(inx)
                inx=inx[1:xlen]
                fx=inx[0]
            if self.parse_key('"',inx,rx):
                strc[0]=xstr
                rest[0]=rx[0]
                return True
        return False
    def parse_tag(self,inx,tag,rest):
        rx=[""]
        xstr=""
        wstr=[""]
        if self.parse_key('<',inx,rx):
            #print("parse_tag")
            inx=rx[0]
            fx=inx[0]
            xlen=len(inx)
            while fx!='>':
                if xlen<=0:
                    return False
                if fx=='\\':
                   inx=inx[1:xlen]
                if self.parse_String_Constant(inx,wstr,rx):
                    inx=rx[0]
                    xstr=xstr+'"'+wstr[0]+'"'
                else:
                    xstr=xstr+fx
                    inx=inx[1:xlen]
                fx=inx[0]
                xlen=len(inx)
            if self.parse_key('>',inx,rx):
                tag[0]=xstr
                rest[0]=rx[0]
                return True
        return False
    def get_tokens(self):
        return self.tokens
    def next_token(self):
        if self.token_ith<len(self.tokens):
            self.token_ith=self.token_ith+1
            return self.tokens[self.token_ith-1]
        return ""
    def has_more_tokens(self):
        if self.token_ith<len(self.tokens):
            return True
        return False
    
    def index_from(self,i,str):
        p=self.html[i:].index(str)
        return p
    
    def p_url(self,p,url):
        #print("p_url p="+str(p)+' '+self.html[p:p+10])
        rx=[""]
        xurl=""
        pw=p
        xlen=len(self.html)
        q=self.p_key('http://',p)
        if p!=q:
            xurl='http://'
        else:
            q=self.p_key('https://',p)
            if p!=q:
                xurl='https://'
            else:
                return pw
        p=q
        while not (self.html[p] in [' ','\n','\t','\r']):
            p=p+1
            if p>=xlen:
                p=xlen
                break
        url[0]=xurl+self.html[q:p]
        return p
    def p_key(self,key,p):
        #print("p_key key="+key+" p="+str(p)+" "+self.html[p:p+10])
        keylen=len(key)
        if p+keylen>=len(self.html):
            return p
        q=p
        if self.html[q:q+keylen]==key:  
            #print("key="+key+" have found, q="+str(q+keylen))
            return q+keylen
        #print("key="+key+" is not found, p="+str(p))
        return p 
      
    def p_String_Constant(self,p,strc):
        #print("p_String_Constant p="+str(p)+' '+self.html[p:p+10])
        rx=[""]
        xstr=""
        pw=p
        q=self.p_key('"',p)
        if p!=q:
            #print("p_String_Constant p="+str(p)+' '+self.html[p:p+10])
            p=q
            fx=self.html[p]
            xlen=len(self.html)-p
            while fx!='"':
                if xlen==0:
                    return pw
                if fx in ['\n','\t','\r']:
                    return pw
                if fx=='\\':
                   p=p+1
                p=p+1
                fx=self.html[p]
                xlen=len(self.html)-p
            q=self.p_key('"',p)
            if p!=q:
                strc[0]=self.html[pw+1:q-1]
                #print("p_string_constant, strc="+str(strc[0]))
                return q
        #print("p_String_Constant, fail to find \"")
        return pw
    def p_Date(self,p,date_str):
        #print("p_Date p="+str(p)+' '+self.html[p:p+10])
        rx=[""]
        xstr=""
        pw=p
        year=[0]
        q=self.p_number(p,year)
        if p==q:
            return pw
        p=q
        q=self.p_key('/',p)
        if p==q:
            return pw
        p=q
        month=[0]
        q=self.p_number(p,month)
        if p==q:
            return pw
        p=q
        q=self.p_key('/',p)
        if p==q:
            return pw
        p=q
        day=[0]
        q=self.p_number(p,day)
        if p==q:
            return False
        p=q
        q=self.p_b(p)
        if p==q:
            return pw
        p=q
        hour=[0]
        q=self.p_number(p,hour)
        if p==q:
            return pw
        p=q
        q=self.p_key(':',p)
        if p==q:
            return pw
        p=q
        minute=[0]
        q=self.p_number(p,minute)
        if p==q:
            return pw
        p=q
        q=self.p_key(':',p)
        if p==q:
            return pw
        p=q
        second=[0]
        q=self.p_number(p,second)
        if p==q:
            return pw
        p=q
        date_str[0]=str(year[0])+'/'+str(month[0])+'/'+str(day[0])+' '+str(hour[0])+':'+str(minute[0])+':'+str(second[0])
        return p
 
   
    def p_b(self,p):
        #print("parse_b")
        #print("p="+str(p))
        xlen=len(self.html)
        if p>=xlen:
            return p
        while self.html[p]==' ':
            p=p+1
            if p>=xlen:
                p=xlen
                break
        return p

    def p_name(self,p,name):
        #print("p_name, self.html="+ self.html[p:p+10])
        #print("p="+str(p)+"..."+self.html[p:p+10])
        q=p
        last_p=len(self.html)
        while self.html[q] in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_':
            #print("self.html["+str(q)+"]="+self.html[q])
            q=q+1
            if q>=last_p:
                break
        name[0]=self.html[p:q]
        #print("p_name p="+str(p)+" q="+str(q)+" name="+str(name[0]))
        #print("p="+str(p)+" q="+str(q)+" name="+name[0])
        #print("name[0]= "+name[0])
        return q
    def p_number(self,p,num):
        #print("p_number")
        #print("p="+str(p))
        n=""
        last_p=len(self.html)
        q=p
        while self.html[p] in '0123456789':
            q=q+1
            if q>=last_p:
                break
        num[0]=int(self.html[p:q])
        #print("p_number p="+str(p)+" q="+str(q)+" num="+str(num[0]))
        return q
    def p_an_equation(self,p,eqs):
        #print("p_an_equation p="+str(p)+' '+self.html[p:p+10])
        pw=p
        p=self.p_b(p)
        nx=[""]
        strc=[""]
        q=self.p_name(p,nx)
        #print("p_an_equation name="+nx[0])
        if p==q:
            #print("p_an_equation fail to find name")
            return pw
        p=q
        p=self.p_b(p)
        q=self.p_key('=',p)
        if p==q:
            #print("p_an_equation fail to find =")
            return pw
        p=q
        p=self.p_b(p)
        q=self.p_String_Constant(p,strc)
        if p==q:
            num=[0]
            q=self.p_number(p,num)
            if p==q:
                return pw
            else:
                eqs[nx[0]]=num[0]
                #print("p_qn_equation get "+nx[0]+"="+str(num[0]))
                return q
        else:
            eqs[nx[0]]=strc[0]
            #print("p_an_equation get "+nx[0]+"="+str(strc[0]))
            return q
    def p_equations(self,p,eqs):
        #print("parse_equations")
        #print("p="+str(p))
        pw=p
        while True:
            q=self.p_an_equation(p,eqs)
            if p==q:
                return p
            p=q
    def p_get_equations_of_the_tag(self,p,tag_name,eqs):
        #print("p_get_equations_of_the_tag <"+tag_name)
        #print("p="+str(p)+' '+self.html[p:p+10])
        pw=p
        try:
            q=self.html.index('<'+tag_name,pw)
        except:
            #print("p_get_equation_of_the_tag fail to find <"+tag_name)
            return p
        #print("q="+str(q)+' '+self.html[q:q+10])
        q=q+len('<'+tag_name)
        #print("q="+str(q)+' '+self.html[q:q+10])
        if pw==q:
            #print("p_get_equation_of_the_tag fail to find <"+tag_name)
            return p
        pw=q
        q=self.p_equations(pw,eqs)
        #print("after equations q="+str(q) +' '+self.html[q:q+10])
        if pw==q:
            return p
        pw=q
        pw=self.p_b(pw)
        #print("pw="+str(pw))
        q=self.p_key('>',pw)
        #print("after > q="+str(q)+' '+self.html[q:q+10])
        if pw!=q:
            return q
        q=self.p_key('/>',pw)
        if pw!=q:
            return q
        return p
    def p_get_between_tag_with_cond(self,p,tag_name,cond,rtn):
        #print("p_get_between_with_cond <"+tag_name+">...</"+tag_name+">")
        #print("p="+str(p)+' '+self.html[p:p+10])
        pw=p
        q=pw
        while True:
            try:
                q=self.html.index('<'+tag_name,p)
                #print("p_get_between_with_cond p="+str(p)+" q="+str(q)+' '+self.html[q:q+20])
                if p==q:
                    return pw
                q=q+len('<'+tag_name)
                p=q
                eqs={}
                q=self.p_equations(p,eqs)
                #print("after equations q="+str(q) +' '+self.html[q:q+10])
                if p!=q:
                    if cond[0] in eqs:
                        #print("cond[0]="+cond[0]+" eqs[cond[0]]="+eqs[cond[0]]+" cond[1]="+cond[1])
                        if eqs[cond[0]]!=cond[1]:
                            continue
                        else:
                            break
            except:
                #print("fail to find <"+tag_name)
                return pw
        p=self.p_b(p)
        #print("p_get_between q="+str(q)+' '+self.html[q:q+10])
        #print("pw="+str(pw))
        q=self.p_key('>',p)
        if p!=q:
            start_p=q
        else:
            q=self.p_key('/>',p)
            if pw!=q:
                start_p=q
            else:
                return p
        try:
            q=self.html.index('</'+tag_name,start_p)
        except:
            return pw
        end_p=q-1
        p=q
        q=q+len('</'+tag_name) 
        p=self.p_b(q)
        #print("pw="+str(pw))
        q=self.p_key('>',p)
        if q==p:
            return pw
        rtn[0]=(start_p,end_p)
        #rx=self.html[start_p:end_p]
        #print("rx="+rx)
        return q

    def p_get_between(self,p,tag_name,eqs,rtn):
        #print("p_get_between <"+tag_name+">...</"+tag_name+">")
        #print("p="+str(p)+' '+self.html[p:p+10])
        pw=p
        q=pw
        try:
            q=self.html.index('<'+tag_name,p)
        except:
            return pw
        #print("p_get_between 2 q="+str(q)+' '+self.html[q:q+10])
        q=q+len('<'+tag_name)
        #print("p_get_between 3 q="+str(q)+' '+self.html[q:q+10])
        if p==q:
            return pw
        p=q
        q=self.p_equations(p,eqs)
        #print("after equations q="+str(q) +' '+self.html[q:q+10])
        p=q
        p=self.p_b(p)
        #print("p="+str(p))
        q=self.p_key('>',p)
        #print("after > q="+str(q)+' p='+str(p)+' '+self.html[p:q+10])
        if p!=q:
            start_p=q
        else:
            q=self.p_key('/>',p)
            if p!=q:
                end_p=q
                rtn[0]=""
                return q
            else:
                #print("get between "+tag_name+",fail to find <"+tag_name+"/>")
                return pw
        try:
            q=self.html.index('</'+tag_name,start_p)
        except:
            #print("get between "+tag_name+",fail to find </"+tag_name)
            return pw
        end_p=q-1
        p=q
        q=q+len('</'+tag_name) 
        p=self.p_b(q)
        #print("p="+str(p))
        q=self.p_key('>',p)
        if q==p:
            return pw
        rtn[0]=(start_p,end_p)
        #rx=self.html[start_p:end_p+1]
        #print("get between "+tag_name+" rx="+rx)
        return q
    
    def code_convert(self,code):
        if code in self.a2c:
            return self.a2c[code]
        else:
            return code
    def text_to_post_form(self,text):
        #print("html_parser.text_to_post_form")
        #print(text)
        rtn=""
        for i in range(len(text)):
            rtn+=self.code_convert(text[i])

        return rtn
    def special_char_to_text(self,text):
        #print("special_char_to_text")
        #print(text)
        text=text.replace("&lt;","<")
        text=text.replace("&gt;",">")
        text=text.replace("&amp;","&")
        text=text.replace("&quot;",'"')
        text=text.replace("&apos;","'")
        return text
    def post(self,url,body):
        #payload is a dictionary
        headers={'Content-Type': 'application/x-www-form-urlencoded'}
        #def http_post(url, value):
        # https://docs.openmv.io/library/urequests.html
        #body = "value=%s" % (value)
        #headers = {'Content-Type': 'application/x-www-form-urlencoded'}
        response = urequests.post(url, data=body, headers=headers)
        r_text=response.text
        #print(r_text)
        response.close()
        #print(r_json)  
        return r_text

class pico_wiki_driver:
    url=""

    def __init__(self,url,page):
        print('pico_wiki_driver')
        self.url=url
        self.page=page
        self.Pico_Net=pico_net()
        self.Pico_Time=pico_time()
        self.Pico_Http=pico_http()
        self.Pico_Net.connect()
        t=self.Pico_Time.get_now()
        print('time now='+t)
    def set_url(self,url):
        self.url=url
    def set_page(self,page):
        self.page=page
    def get_url(self):
        return self.url
    def get_html(self):
        uri = self.url+"?"+self.page
        all_page=self.Pico_Http.get(uri)
        #print("get_html..."+uri)
        #print(all_page)
        return all_page

    def get_wiki_page(self):
        uri = self.url+"?"+self.page
        all_page=self.Pico_Http.get(uri)
        print("get_wiki_page...")
        #print(all_page)
        parser=html_parser(all_page)
        p=0
        rtn=[(0,0)]
        q=parser.p_get_between_tag_with_cond(p,"div",('id',"body"),rtn)
        if p!=q:
            #print("get_wiki_page id=body..."+all_page[rtn[0][0]:rtn[0][1]])
            body_x=all_page[rtn[0][0]:rtn[0][1]]
            body=parser.special_char_to_text(body_x)
            return body
    def replace_wiki_page(self,new_body):
        print("pico_wiki_driver.replace_wiki_page...")
        payload={'cmd':'edit', 'page':self.page}
        #r=Pico_Http.get(url+'index.php?work202401')
        #print('uri='+r)
        r = self.Pico_Http.put(self.url,payload)
        #print(r)
        edit_html=html_parser(r)
        #edit_html.html_tokenizer()
        p=0
        array_of_equations=[]
        equations={}
        q=edit_html.p_get_equations_of_the_tag(p,'input',equations) 
        while p!=q:
            #print('p='+str(p)+' q='+str(q)+' <input....>')
            array_of_equations.append(equations)
            p=q
            equations={}
            q=edit_html.p_get_equations_of_the_tag(p,'input',equations)
        for i in range(len(array_of_equations)):
            #print('i='+str(i))
            eqs=array_of_equations[i]
            for key in eqs:
                #print(key+'='+array_of_equations[i][key])
                if key=='name' and array_of_equations[i][key]=="encode_hint":
                    payload['encode_hint']=array_of_equations[i]['value']
                elif key=='name' and array_of_equations[i][key]=="template":
                    payload['template']=array_of_equations[i]['value']
                elif key=='name' and array_of_equations[i][key]=="cmd":
                    payload['cmd']=array_of_equations[i]['value']
                elif  key=='name' and array_of_equations[i][key]=="page":
                    payload['page']=array_of_equations[i]['value']
                elif key=='name' and array_of_equations[i][key]=="digest":
                    payload['digest']=array_of_equations[i]['value']
                elif key=='name' and array_of_equations[i][key]=="write":
                    payload['write']=array_of_equations[i]['value']

        #tokens=edit_html.get_tokens()
        #for i in range(len(tokens)):_
        #    print(str(i)+'-'+tokens[i])
        eqs_msg={}
        rtn=[(0,0)]
        p=edit_html.p_get_between(0,'textarea',eqs_msg,rtn)
        if rtn[0]!=(0,0):
            start_p=rtn[0][0]
            end_p=rtn[0][1]
            body=r[0:start_p]+new_body+r[end_p+1:]
            print("newbody=......")
            print(new_body)
            payload['msg']=edit_html.text_to_post_form(new_body)
            print('msg=......')
            print(payload['msg'])
        gc.collect()
        eqs_original={}
        rtn=[(0,0)]
        p=edit_html.p_get_between(p,'textarea',eqs_original,rtn)
        if rtn[0]!=(0,0):
            start_p=rtn[0][0]
            end_p=rtn[0][1]
            original_pre=r[start_p:end_p+1]
            original=edit_html.special_char_to_text(original_pre)
            payload['original']=edit_html.text_to_post_form(original)

        body=""
        body=body+'encode_hint='+payload['encode_hint']+'&'
        body=body+'template_page=&'
        body=body+'cmd='+payload['cmd']+'&'
        body=body+'page='+payload['page']+'&'
        body=body+'digest='+payload['digest']+'&'
        body=body+'msg='+payload['msg']+'&'
        body=body+'write='+payload['write']+'&'
        body=body+'original='+payload['original']
        #print('body='+body)
        try:
            edit_html.post(self.url, body)
        except:
            print("something wrong in edit_html.post")
        #print(r)
    def get_wiki_source(self):
        payload={'cmd':'edit', 'page':self.page}
        #r=Pico_Http.get(url+'index.php?work202401')
        #print('uri='+r)
        r = self.Pico_Http.put(self.url,payload)
        #print(r)
        edit_html=html_parser(r)
        #edit_html.html_tokenizer()
        p=0
        array_of_equations=[]
        equations={}
        q=edit_html.p_get_equations_of_the_tag(p,'input',equations) 
        while p!=q:
            #print('p='+str(p)+' q='+str(q)+' <input....>')
            array_of_equations.append(equations)
            p=q
            equations={}
            q=edit_html.p_get_equations_of_the_tag(p,'input',equations)
        for i in range(len(array_of_equations)):
            #print('i='+str(i))
            eqs=array_of_equations[i]
            for key in eqs:
                #print(key+'='+array_of_equations[i][key])
                if key=='name' and array_of_equations[i][key]=="encode_hint":
                    payload['encode_hint']=array_of_equations[i]['value']
                elif key=='name' and array_of_equations[i][key]=="template":
                    payload['template']=array_of_equations[i]['value']
                elif key=='name' and array_of_equations[i][key]=="cmd":
                    payload['cmd']=array_of_equations[i]['value']
                elif  key=='name' and array_of_equations[i][key]=="page":
                    payload['page']=array_of_equations[i]['value']
                elif key=='name' and array_of_equations[i][key]=="digest":
                    payload['digest']=array_of_equations[i]['value']
                elif key=='name' and array_of_equations[i][key]=="cancel":
                    payload['cancel']=array_of_equations[i]['value']

        #tokens=edit_html.get_tokens()
        #for i in range(len(tokens)):_
        #    print(str(i)+'-'+tokens[i])
        eqs_msg={}
        rtn=[(0,0)]
        p=edit_html.p_get_between(0,'textarea',eqs_msg,rtn)
        wiki_source=""
        if rtn[0]!=(0,0):
            start_p=rtn[0][0]
            end_p=rtn[0][1]
            wiki_source=r[start_p:end_p]
        #body=""
        #body=body+'encode_hint='+payload['encode_hint']+'&'
        #body=body+'cmd='+payload['cmd']+'&'
        #body=body+'page='+payload['page']+'&'
        #body=body+'cancel='+payload['cancel']
        ##print('body='+body)
        #r=edit_html.post(self.url,body)
        return wiki_source
def main(): 
    bot=pico_wiki_driver(init_url,init_page)
    print("-----------get_wiki_page-----------------")
    print(bot.get_wiki_page())
    print("-----------get_wiki_source-----------------")
    print(bot.get_wiki_source())
    print("-----------replace_wiki_page-----------------")
    print(bot.replace_wiki_page(" abcdefgh, \n Hello, world!"))
    print("-----------get_wiki_source-----------------")
    print(bot.get_wiki_source())
 
main()
}}
----
#counter

トップ   編集 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS