import re
import pywikibot
from collections import namedtuple
pages = pywikibot.site.APISite.allpages(site,prefix='गणितसारसङ्ग्रहः॒रङ्गाचार्येणानू',namespace="पृष्ठम्", filterredir=None, content=False)
for p in pages:
    print(p.text)
    print("\n\n")
page = pywikibot.Page(site,'पृष्ठम्:गणितसारसङ्ग्रहः॒रङ्गाचार्येणानूदितः॒१९१२.djvu/३६८')
print(page.properties())
{'proofread_page_quality_level': '3'}
t = page.text
t = re.sub('difference ','difference ****** ',t)
page.text=t
page.status="→‎Not proofread"
page.save(summary=u'→‎Not proofread')
Page [[wikisource:sa:पृष्ठम्:गणितसारसङ्ग्रहः॒रङ्गाचार्येणानूदितः॒१९१२.djvu/३६८]] saved
def to_sans(numb):
    str_numb ="{}".format(numb)
    x =  [chr(ord(i) + 2358) for i in str_numb]
    return ''.join(x)

def to_eng(sans_num):
    x =  [chr(ord(i) - 2358) for i in sans_num]
    return int(''.join(x))

def get_range(p):
    regex = re.compile("([^:\s]*):?")
    matches = regex.findall(p)
    matches.pop()
    matches.extend((None,None,None))
    (start,end,step)  = matches[:3]

    #default missing values 
    if not step: step=1
    if not start:    start = 0
    if not end: end = -1
    print("start={}; end={}; step={};".format(start,end,step))

    return(int(start),int(end),int(step))

def update_page(pagetext, header):
    rexp2 = re.compile(r"^(\s*<noinclude><.*?/>)(.*?)(</noinclude>)")
    find=rexp2.findall(pagetext)
    print(find)
   # if find[0][1] == '':
    pagetext= rexp2.sub(r"\1" + header+r"\3",pagetext) 
    print("inserted page header for this page")
    return(1,pagetext)
   # else:
   #     print("header already found...")
   #     return (-1,pagetext)

PageInfo = namedtuple('PageInfo',['url','pageno'])
def generate_pages(prefix,rng,displayno):
    (start,end,step) = get_range(rng) 
    for i in range(start,end,step):
        page = "{}{}".format(prefix, to_sans(i))
        yield(PageInfo(page,displayno))
        displayno = displayno + step
        
def add_headers(site,prefix, range, pageno,header):
    pages=generate_pages(prefix,range,pageno)
    for p in pages:
        print(p)
        print("================================================")
        newheader = re.sub("<pageno>",str(p.pageno),header)        
        page = pywikibot.Page(site,p.url)
        print("header={}".format(newheader))
        (status,newtext) = update_page(page.text,newheader)
        page.text=newtext
        if status == 1:
            page.text=newtext
            #page.status=u"→‎Not proofread"
            #page.save(summary=u'→‎Not proofread')
            page.save(u'Added header' + newheader)
            print("--done--")
        else:
            print("ignoring this page as header already exists.")
        print("........done.........")
        
str2="""<noinclude><pagequality level="3" user="Venkateswaran raman" /></noinclude>\n67 to 71. The denominators (of certain given fractions) are stated to be 19, 23, 62, 29, 123, 35, 188, 87, 98, 47, 140, 141, 116, 31, 92, 57, 73, 55, 110, 49, 74, 219, (in order); and the numerators begin with 1 and rise successively in value by 1 (in order). Add (all) these (fractions) and give the result. O you who have reached the other shore of the ocean of simple fractions.\n\nHere, the rule for arriving at the numerators, (when the denominators and the sum of a number of fractions are given, is as follows):--\n\n72. Make \'\'one\'\'  the numerator (in relation to all the given denominators); then, multiply by means of such (numbers) as are optionally chosen, those numerators which (are derived from these fractions so as to) have a common denominator. (Here), those (numbers) turn out to be the required numerators, the sum of the products whereof, obtained by multiplying them with the numerators (derived as above), is equal to the numerator of) the given sum(of the fractions concerned). \n\nThe rule for arriving at the numerators, (the denominators and the sum being given as before), in relation to such (fractional) quantities as have their numerators (successively) rising in value by \'\'one\'\', when, in the (given) sum (of these fractions), the denominator is higher in value than the numerator:--\n\n73. The quotient obtained by dividing the (given) sum (of the fractions concerned) by the sum of those (tentative fractions)\n\n{{rule}}\n\n<small>\n72. This rule will become clear from the working of the example in stana No. 74, wherein we assume 1 to be the provisional numerator in relation to each of the given denominators; thus we get <math>\\tfrac{1}{9}, \\tfrac{1}{10}</math> and <math>\\tfrac{1}{11}</math> which, being reduced so as to have a common denominator, become <math>\\tfrac{110}{990}, \\tfrac{99}{990}</math> and <math>\\tfrac{90}{990}</math>. When the numerators are multiplied by 2, 3 and 4 in order, the sum of the products thus obtained becomes equal to the numerator of the given sum, namely, 877. Hence, 2, 3, and 4 are the required numerators. Here it may be pointed out that this given sum also must be understood to have the same denominators as the common denominator of the fractions\n\n73. To work out the sum given under 74 below, according to this rule:--<br>\nReducing to the same denominator the fractions formed by assuming 1 to be the numerator in relation to each of the given denominators, we get, <math>\\tfrac{110}{990}, \\tfrac{99}{990}</math> and <math>\\tfrac{90}{990}</math>. Dividing the given sum <math>\\tfrac{877}{990}</math>  by the sum of these fractions <math>\\tfrac{299}{990}</math>, we get the quotient 2, which is the numerator in relation to the first denominator. The remainder 279\n</small><noinclude></noinclude>"""
site = pywikibot.Site("sa", "wikisource")
add_headers(site,"पृष्ठम्:गणितसारसङ्ग्रहः॒रङ्गाचार्येणानूदितः॒१९१२.djvu/","368:377:2",172,"{{rh|left=<pageno>|center=GAŅITASĀRASAŃGRAHA.}}")
PageInfo._fields
('url', 'displayno')
for i in PageInfo._fields:
    print(i)
url
displayno