import pywikibot
import requests
import mwparserfromhell
import time

site = pywikibot.Site('commons', 'commons')

def data_from_petscan(psid, total):
    params = {'format': 'json', 'output_compatibility': 'catscan',
              'sparse': 'on', 'psid': psid, 'output_limit': total}
    headers = {'user-agent': 'AntiCompositeBot BadGPS on PAWS'}
    r = requests.get('https://petscan.wmflabs.org/',
                     params=params, headers=headers)
    r.raise_for_status()
    files = r.json()['*'][0]['a']['*']
    return files

def add_template(page, template):
    wikitext = mwparserfromhell.parse(page.text)
    s = wikitext.get_sections()
    wikitext.insert_after(s[1].strip(),
                          '\n{{{{{template}}}}}'.format(template=template))
    return str(wikitext)

def time_check(start_time):
    end_time = time.time()
    diff = end_time - start_time
    min_wait = 5
    if diff < min_wait:
        print('Sleeping...')
        time.sleep(min_wait - diff)
        return
    else:
        return
    
def main():
    psid = 10891872
    template = 'Location estimated'
    total = 5
    
    files = data_from_petscan(psid, total)
    for filename in files:
        start_time = time.time()
        page = pywikibot.Page(site, filename)
        page.text = add_template(page, template)
        summary = ('Adding {{{{{template}}}}} to files '
                   'in [[petscan:{psid}]] per author request, '
                   'see user page for details (BadGPS)').format(
            template=template, psid=psid)
        
        time_check(start_time)
        #page.save(summary=summary, botflag=True)
        
        print(page.title())
        print(page.text)
        print('\n', summary)
        

main()
    
Sleeping...
File:Tocqueville-les-Murs (Seine-Mar.) entrée.jpg
=={{int:filedesc}}==
{{Information
|description={{fr|1=Tocqueville-les-Murs (Seine-Mar.) entrée}}
|date=2019-07-23 08:52:45
|source={{own}}
|author=[[User:Havang(nl)|Havang(nl)]]
|permission=
|other versions=
}}
{{Location|49.665925|0.51851|heading:209.75}}
{{Location estimated}}

=={{int:license-header}}==
{{self|cc-zero}}

[[Category:Photographs by Henk van Gaal]]
[[Category:Tocqueville-les-Murs]]
[[Category:City limit signs in Seine-Maritime]]

 Adding {{Location estimated}} to files in [[petscan:10891872]] per author request, see user page for details (BadGPS)
Sleeping...
File:Tocqueville-les-Murs (Seine-Mar.) église (01).jpg
=={{int:filedesc}}==
{{Information
|description={{fr|1=Tocqueville-les-Murs (Seine-Mar.) eglise}}
|date=2019-07-23 08:57:24
|source={{own}}
|author=[[User:Havang(nl)|Havang(nl)]]
|permission=
|other versions=
}}
{{Location|49.665925|0.51851|heading:74.25}}
{{Location estimated}}

=={{int:license-header}}==
{{self|cc-zero}}

[[Category:Photographs by Henk van Gaal]]
[[Category:Tocqueville-les-Murs]]
[[Category:Churches in Seine-Maritime]]

 Adding {{Location estimated}} to files in [[petscan:10891872]] per author request, see user page for details (BadGPS)
Sleeping...
File:Tocqueville-les-Murs (Seine-Mar.) eglise (02).jpg
=={{int:filedesc}}==
{{Information
|description={{fr|1=Tocqueville-les-Murs (Seine-Mar.) eglise}}
|date=2019-07-23 08:57:55
|source={{own}}
|author=[[User:Havang(nl)|Havang(nl)]]
|permission=
|other versions=
}}
{{Location|49.665925|0.51851|heading:16}}
{{Location estimated}}

=={{int:license-header}}==
{{self|cc-zero}}

[[Category:Photographs by Henk van Gaal]]
[[Category:Tocqueville-les-Murs]]
[[Category:Churches in Seine-Maritime]]

 Adding {{Location estimated}} to files in [[petscan:10891872]] per author request, see user page for details (BadGPS)
Sleeping...
File:Tocqueville-les-Murs (Seine-Mar.) monument aux morts.jpg
=={{int:filedesc}}==
{{Information
|description={{fr|1=Tocqueville-les-Murs (Seine-Mar.) monument aux morts}}
|date=2019-07-23 08:57:09
|source={{own}}
|author=[[User:Havang(nl)|Havang(nl)]]
|permission=
|other versions=
}}
{{Location|49.665925|0.51851|heading:230.25}}
{{Location estimated}}

=={{int:license-header}}==
{{self|cc-zero}}

[[Category:Photographs by Henk van Gaal]]
[[Category:Tocqueville-les-Murs]]
[[Category:War memorials in Seine-Maritime]]

 Adding {{Location estimated}} to files in [[petscan:10891872]] per author request, see user page for details (BadGPS)
Sleeping...
File:Tocqueville-les-Murs (Seine-Mar.) croix de chemin.jpg
=={{int:filedesc}}==
{{Information
|description={{fr|1=Tocqueville-les-Murs (Seine-Mar.) croix de chemin}}
|date=2019-07-23 09:01:32
|source={{own}}
|author=[[User:Havang(nl)|Havang(nl)]]
|permission=
|other versions=
}}
{{Location|49.665925|0.51851|heading:66.25}}
{{Location estimated}}

=={{int:license-header}}==
{{self|cc-zero}}

[[Category:Photographs by Henk van Gaal]]
[[Category:Tocqueville-les-Murs]]
[[Category:Wayside crosses in Seine-Maritime]]

 Adding {{Location estimated}} to files in [[petscan:10891872]] per author request, see user page for details (BadGPS)

Licensing

Copyright 2019 AntiCompositeNumber

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

To fork:

  1. Open the raw page and save it to your computer
  2. Go to your PAWS control panel and sign in using OAUTH
  3. Click Upload and upload the file from step 1
  4. To run, open the notebook and click Cell > Run All