« BioPathBot » : différence entre les versions

De Wikipast
Aller à la navigation Aller à la recherche
(Wikipastbot update)
Ligne 48 : Ligne 48 :
==Code==
==Code==


   import <span style="color:red">urllib</span> (correction(s): <span style="color:green">
   import urllib.request
</span>).<span style="color:red">request</span> (correction(s): <span style="color:green">
   import requests
</span>)
   from bs4 import BeautifulSoup
   import <span style="color:red">requests</span> (correction(s): <span style="color:green">
</span>)
   <span style="color:red">from</span> (correction(s): <span style="color:green">rom, froc
</span>) <span style="color:red">bs</span> (correction(s): <span style="color:green">bé, ès, s, by, bi, bis, b, es, bus, bas, as, bu, os, us
</span>)4 import BeautifulSoup
   import re
   import re
   import math
   import math
   import <span style="color:red">datetime</span> (correction(s): <span style="color:green">
   import numpy as np
</span>)
  import datetime
   <span style="color:red">from</span> (correction(s): <span style="color:green">rom, froc
  import random
</span>) <span style="color:red">geopy</span> (correction(s): <span style="color:green">
  import copy
</span>).<span style="color:red">geocoders</span> (correction(s): <span style="color:green">
   from geopy.geocoders import Nominatim
</span>) import Nominatim
   from mpl_toolkits.basemap import Basemap
   <span style="color:red">from</span> (correction(s): <span style="color:green">rom, froc
   import matplotlib.pyplot as plt
</span>) <span style="color:red">mpl</span> (correction(s): <span style="color:green">mil, mal, mol, ml
   from colorsys import hsv_to_rgb
</span>)_<span style="color:red">toolkits</span> (correction(s): <span style="color:green">
   from matplotlib.colors import rgb2hex
</span>).<span style="color:red">basemap</span> (correction(s): <span style="color:green">
  import pdb
</span>) import Basemap
  import time
   import <span style="color:red">matplotlib</span> (correction(s): <span style="color:green">
  import itertools
</span>).<span style="color:red">pyplot</span> (correction(s): <span style="color:green">
  from geopy.exc import GeocoderTimedOut
</span>) as <span style="color:red">plt</span> (correction(s): <span style="color:green">plut, plet, pet, pût, plût, pli, plot, plat, pst, put, pât, plu, pat, pot
</span>)
   <span style="color:red">from</span> (correction(s): <span style="color:green">rom, froc
</span>) <span style="color:red">colorsys</span> (correction(s): <span style="color:green">
</span>) import <span style="color:red">hsv</span> (correction(s): <span style="color:green">hiv
</span>)_to_<span style="color:red">rgb</span> (correction(s): <span style="color:green">rab
</span>)
   <span style="color:red">from</span> (correction(s): <span style="color:green">rom, froc
</span>) <span style="color:red">matplotlib</span> (correction(s): <span style="color:green">
</span>).<span style="color:red">colors</span> (correction(s): <span style="color:green">colore, coloris, colorés, colos, colora, coloré, colons
</span>) import <span style="color:red">rgb</span> (correction(s): <span style="color:green">rab
</span>)2<span style="color:red">hex</span> (correction(s): <span style="color:green">hem, heu, hep, rex, tex
</span>)
   SEGMENTS = 100
   SEGMENTS = 100
    
    
  # <span style="color:red">draw</span> (correction(s): <span style="color:green">drap, drag
</span>) plots <span style="color:red">inline</span> (correction(s): <span style="color:green">incline
</span>) <span style="color:red">rather</span> (correction(s): <span style="color:green">ratier, rater
</span>) <span style="color:red">than</span> (correction(s): <span style="color:green">tian, ahan, tan, thon, thaï, han, khan
</span>) in a <span style="color:red">seperate</span> (correction(s): <span style="color:green">
</span>) <span style="color:red">window</span> (correction(s): <span style="color:green">
</span>)
  %<span style="color:red">matplotlib</span> (correction(s): <span style="color:green">
</span>) <span style="color:red">inline</span> (correction(s): <span style="color:green">incline
</span>)
  # <span style="color:red">draw</span> (correction(s): <span style="color:green">drap, drag
</span>) plots <span style="color:red">bigger</span> (correction(s): <span style="color:green">bigler, jigger
</span>)
  <span style="color:red">plt</span> (correction(s): <span style="color:green">plut, plet, pet, pût, plût, pli, plot, plat, pst, put, pât, plu, pat, pot
</span>).rcParams["figure.figsize"] = [20.0, 10.0]
    
    
   user='BioPathBot'
   # draw plots inline rather than in a seperate window
   <span style="color:red">passw</span> (correction(s): <span style="color:green">passe, passé, passa
  # %matplotlib inline
</span>)='<span style="color:red">chkiroju</span> (correction(s): <span style="color:green">
  # draw plots bigger
</span>)'
   plt.rcParams["figure.figsize"] = [20.0, 10.0]
   <span style="color:red">baseurl</span> (correction(s): <span style="color:green">
 
</span>)='<span style="color:red">http</span> (correction(s): <span style="color:green">
  bot_user='BioPathBot'
</span>)://<span style="color:red">wikipast</span> (correction(s): <span style="color:green">
  passw='chkiroju'
</span>).<span style="color:red">epfl</span> (correction(s): <span style="color:green">
   baseurl='http://wikipast.epfl.ch/wikipast/'
</span>).<span style="color:red">ch</span> (correction(s): <span style="color:green">chu, co, h, ce, ah, eh, c, ph, oh, ci, cm, chi
   summary='Wikipastbot update'
</span>)/<span style="color:red">wikipast</span> (correction(s): <span style="color:green">
   protected_logins=["Frederickaplan","Maud","Vbuntinx","Testbot","IB","SourceBot","PageUpdaterBot","Orthobot","BioPathBot","ChronoBOT","Amonbaro","AntoineL","AntoniasBanderos","Arnau","Arnaudpannatier","Aureliver","Brunowicht","Burgerpop","Cedricviaccoz","Christophe","Claudioloureiro","Ghislain","Gregoire3245","Hirtg","Houssm","Icebaker","JenniCin","JiggyQ","JulienB","Kl","Kperrard","Leandro Kieliger","Marcus","Martin","MatteoGiorla","Mireille","Mj2905","Musluoglucem","Nacho","Nameless","Nawel","O'showa","PA","Qantik","QuentinB","Raphael.barman","Roblan11","Romain Fournier","Sbaaa","Snus","Sonia","Tboyer","Thierry","Titi","Vlaedr","Wanda"]
</span>)/'
   depuis_date='2017-02-02T16:00:00Z'
   <span style="color:red">summary</span> (correction(s): <span style="color:green">
</span>)='Wikipastbot update'
   <span style="color:red">protected</span> (correction(s): <span style="color:green">
</span>)_<span style="color:red">logins</span> (correction(s): <span style="color:green">logis, login, lopins, logions
</span>)=["Frederickaplan","Maud","Vbuntinx","Testbot","IB","SourceBot","PageUpdaterBot","Orthobot","BioPathBot","ChronoBOT","Amonbaro","AntoineL","AntoniasBan  deros","Arnau","Arnaudpannatier","Aureliver","Brunowicht","Burgerpop","Cedricviaccoz","Christophe","Claudioloureiro","Ghislain","Gregoire3245","Hirtg","Houssm","Icebak  er","JenniCin","JiggyQ","JulienB","Kl","Kperrard","Leandro   Kieliger","Marcus","Martin","MatteoGiorla","Mireille","Mj2905","Musluoglucem","Nacho","Nameless","Nawel","O'showa","PA","Qantik","QuentinB","Raphael.barman","Roblan11" ,"Romain Fournier","Sbaaa","Snus","Sonia","Tboyer","Thierry","Titi","Vlaedr","Wanda"]
   depuis_date='2017-05-02T16:00:00Z'
   liste_pages=[]
   liste_pages=[]
   for user in <span style="color:red">protected</span> (correction(s): <span style="color:green">
   for user in protected_logins:
</span>)_<span style="color:red">logins</span> (correction(s): <span style="color:green">logis, login, lopins, logions
       result=requests.post(baseurl+'api.php?action=query&list=usercontribs&ucuser='+user+'&format=xml&ucend='+depuis_date+'&ucshow=new')
</span>):
       soup=BeautifulSoup(result.content,'lxml')
       <span style="color:red">result</span> (correction(s): <span style="color:green">
       for primitive in soup.usercontribs.findAll('item'):
</span>)=<span style="color:red">requests</span> (correction(s): <span style="color:green">
           title = primitive['title']
</span>).post(<span style="color:red">baseurl</span> (correction(s): <span style="color:green">
           if 'Fichier' not in title and 'BioPathBot' not in title:
</span>)+'api.<span style="color:red">php</span> (correction(s): <span style="color:green">phi, pep, pop, ph
              liste_pages.append(primitive['title'])
</span>)?action=<span style="color:red">query</span> (correction(s): <span style="color:green">
</span>)&<span style="color:red">list</span> (correction(s): <span style="color:green">liste, listé, lise, lest, lis, liât, lift, lit
</span>)=<span style="color:red">usercontribs</span> (correction(s): <span style="color:green">
</span>)&<span style="color:red">ucuser</span> (correction(s): <span style="color:green">
</span>)='+user+'&format=<span style="color:red">xml</span> (correction(s): <span style="color:green">ml
</span>)&<span style="color:red">ucend</span> (correction(s): <span style="color:green">
</span>)='+depuis_date)
       <span style="color:red">soup</span> (correction(s): <span style="color:green">houp, stup, soupe, soupé, youp, soul, sous, souk, soupa, sou, coup, loup, soue
</span>)=BeautifulSoup(<span style="color:red">result</span> (correction(s): <span style="color:green">
</span>).content,'<span style="color:red">lxml</span> (correction(s): <span style="color:green">
</span>)')
       for primitive in <span style="color:red">soup</span> (correction(s): <span style="color:green">houp, stup, soupe, soupé, youp, soul, sous, souk, soupa, sou, coup, loup, soue
</span>).<span style="color:red">usercontribs</span> (correction(s): <span style="color:green">
</span>).findAll('item'):
           liste_pages.<span style="color:red">append</span> (correction(s): <span style="color:green">appends, appendu, apprend
</span>)(primitive['<span style="color:red">title</span> (correction(s): <span style="color:green">tille, titre
</span>)'])
           <span style="color:red">print</span> (correction(s): <span style="color:green">peint, prin, priant, priât, point, sprint, prit, prient
</span>)(primitive['<span style="color:red">title</span> (correction(s): <span style="color:green">tille, titre
</span>)'])
    
    
   <span style="color:red">names</span> (correction(s): <span style="color:green">nases, dames, lames, nages, nazes, cames, rames
   names=list(set(liste_pages))
</span>)=<span style="color:red">list</span> (correction(s): <span style="color:green">liste, listé, lise, lest, lis, liât, lift, lit
  for title in names:
</span>)(set(liste_pages))
      print(title)
    
    
   # Login <span style="color:red">request</span> (correction(s): <span style="color:green">
   # Login request
</span>)
   payload={'action':'query','format':'json','utf8':'','meta':'tokens','type':'login'}
   <span style="color:red">payload</span> (correction(s): <span style="color:green">
   r1=requests.post(baseurl + 'api.php', data=payload)
</span>)={'action':'<span style="color:red">query</span> (correction(s): <span style="color:green">
</span>)','format':'<span style="color:red">json</span> (correction(s): <span style="color:green">son
</span>)','<span style="color:red">utf</span> (correction(s): <span style="color:green">urf, ut
</span>)8':'','<span style="color:red">meta</span> (correction(s): <span style="color:green">jeta, feta, mata, méta, mena, mets, met, mesa, mita
</span>)':'<span style="color:red">tokens</span> (correction(s): <span style="color:green">
</span>)','type':'login'}
   <span style="color:red">r</span> (correction(s): <span style="color:green">ô, , k, re, ri, q, or, z, v, t, l, é, n, b, o, j, m, ru, s, d, y, a, f, e, à, ré, h, c, x, g, w, u, ra, i
</span>)1=<span style="color:red">requests</span> (correction(s): <span style="color:green">
</span>).post(<span style="color:red">baseurl</span> (correction(s): <span style="color:green">
</span>) + 'api.<span style="color:red">php</span> (correction(s): <span style="color:green">phi, pep, pop, ph
</span>)', data=<span style="color:red">payload</span> (correction(s): <span style="color:green">
</span>))
    
    
   #login <span style="color:red">confirm</span> (correction(s): <span style="color:green">confire, confirma, confirme, confirmé
   #login confirm
</span>)
   login_token=r1.json()['query']['tokens']['logintoken']
   login_<span style="color:red">token</span> (correction(s): <span style="color:green">
   payload={'action':'login','format':'json','utf8':'','lgname':bot_user,'lgpassword':passw,'lgtoken':login_token}
</span>)=r1.<span style="color:red">json</span> (correction(s): <span style="color:green">son
   r2=requests.post(baseurl + 'api.php', data=payload, cookies=r1.cookies)
</span>)()['<span style="color:red">query</span> (correction(s): <span style="color:green">
</span>)']['<span style="color:red">tokens</span> (correction(s): <span style="color:green">
</span>)']['logintoken']
   <span style="color:red">payload</span> (correction(s): <span style="color:green">
</span>)={'action':'login','format':'<span style="color:red">json</span> (correction(s): <span style="color:green">son
</span>)','<span style="color:red">utf</span> (correction(s): <span style="color:green">urf, ut
</span>)8':'','<span style="color:red">lgname</span> (correction(s): <span style="color:green">igname
</span>)':user,'<span style="color:red">lgpassword</span> (correction(s): <span style="color:green">
</span>)':passw,'<span style="color:red">lgtoken</span> (correction(s): <span style="color:green">
</span>)':login_token}
   <span style="color:red">r</span> (correction(s): <span style="color:green">ô, , k, re, ri, q, or, z, v, t, l, é, n, b, o, j, m, ru, s, d, y, a, f, e, à, ré, h, c, x, g, w, u, ra, i
</span>)2=<span style="color:red">requests</span> (correction(s): <span style="color:green">
</span>).post(<span style="color:red">baseurl</span> (correction(s): <span style="color:green">
</span>) + 'api.<span style="color:red">php</span> (correction(s): <span style="color:green">phi, pep, pop, ph
</span>)', data=<span style="color:red">payload</span> (correction(s): <span style="color:green">
</span>), cookies=<span style="color:red">r</span> (correction(s): <span style="color:green">ô, , k, re, ri, q, or, z, v, t, l, é, n, b, o, j, m, ru, s, d, y, a, f, e, à, ré, h, c, x, g, w, u, ra, i
</span>)1.cookies)
    
    
   #<span style="color:red">get</span> (correction(s): <span style="color:green">cet, pet, let, gel, guet, ket, gît, net, et, set, met, jet, gent, gut
   #get edit token2
</span>) <span style="color:red">edit</span> (correction(s): <span style="color:green">redit, dit, exit, édit, ledit
   params3='?format=json&action=query&meta=tokens&continue='
</span>) <span style="color:red">token</span> (correction(s): <span style="color:green">
   r3=requests.get(baseurl + 'api.php' + params3, cookies=r2.cookies)
</span>)2
   edit_token=r3.json()['query']['tokens']['csrftoken']
   <span style="color:red">params</span> (correction(s): <span style="color:green">parais, paras
</span>)3='?format=<span style="color:red">json</span> (correction(s): <span style="color:green">son
</span>)&action=<span style="color:red">query</span> (correction(s): <span style="color:green">
</span>)&<span style="color:red">meta</span> (correction(s): <span style="color:green">jeta, feta, mata, méta, mena, mets, met, mesa, mita
</span>)=<span style="color:red">tokens</span> (correction(s): <span style="color:green">
</span>)&continue='
   <span style="color:red">r</span> (correction(s): <span style="color:green">ô, , k, re, ri, q, or, z, v, t, l, é, n, b, o, j, m, ru, s, d, y, a, f, e, à, ré, h, c, x, g, w, u, ra, i
</span>)3=<span style="color:red">requests</span> (correction(s): <span style="color:green">
</span>).<span style="color:red">get</span> (correction(s): <span style="color:green">cet, pet, let, gel, guet, ket, gît, net, et, set, met, jet, gent, gut
</span>)(<span style="color:red">baseurl</span> (correction(s): <span style="color:green">
</span>) + 'api.<span style="color:red">php</span> (correction(s): <span style="color:green">phi, pep, pop, ph
</span>)' + <span style="color:red">params</span> (correction(s): <span style="color:green">parais, paras
</span>)3, cookies=r2.cookies)
   <span style="color:red">edit</span> (correction(s): <span style="color:green">redit, dit, exit, édit, ledit
</span>)_<span style="color:red">token</span> (correction(s): <span style="color:green">
</span>)=r3.<span style="color:red">json</span> (correction(s): <span style="color:green">son
</span>)()['<span style="color:red">query</span> (correction(s): <span style="color:green">
</span>)']['<span style="color:red">tokens</span> (correction(s): <span style="color:green">
</span>)']['csrftoken']
    
    
   <span style="color:red">edit</span> (correction(s): <span style="color:green">redit, dit, exit, édit, ledit
   edit_cookie=r2.cookies.copy()
</span>)_cookie=<span style="color:red">r</span> (correction(s): <span style="color:green">ô, , k, re, ri, q, or, z, v, t, l, é, n, b, o, j, m, ru, s, d, y, a, f, e, à, ré, h, c, x, g, w, u, ra, i
   edit_cookie.update(r3.cookies)
</span>)2.cookies.<span style="color:red">copy</span> (correction(s): <span style="color:green">cops, cosy, cop
</span>)()
   <span style="color:red">edit</span> (correction(s): <span style="color:green">redit, dit, exit, édit, ledit
</span>)_cookie.update(<span style="color:red">r</span> (correction(s): <span style="color:green">ô, , k, re, ri, q, or, z, v, t, l, é, n, b, o, j, m, ru, s, d, y, a, f, e, à, ré, h, c, x, g, w, u, ra, i
</span>)3.cookies)
    
    
   #<span style="color:red">setup</span> (correction(s): <span style="color:green">stup
   #setup geolocator
</span>) <span style="color:red">geolocator</span> (correction(s): <span style="color:green">
   geolocator = Nominatim(timeout=30)
</span>)
   <span style="color:red">geolocator</span> (correction(s): <span style="color:green">
</span>) = Nominatim(<span style="color:red">timeout</span> (correction(s): <span style="color:green">
</span>)=10)
    
    
  # upload config
  def uploadMap(filename):
    
    
  # <span style="color:red">upload</span> (correction(s): <span style="color:green">
      # read local file
</span>) <span style="color:red">config</span> (correction(s): <span style="color:green">confit, confié, confia, confie
      upload_file = open(filename,"rb")
</span>)
      upload_contents = upload_file.read()
  <span style="color:red">def</span> (correction(s): <span style="color:green">zef, der, des, nef, de, dey
      upload_file.close()
</span>) uploadMap(<span style="color:red">filename</span> (correction(s): <span style="color:green">
</span>)):
    
    
       # <span style="color:red">read</span> (correction(s): <span style="color:green">rend, rad
       # setting parameters for upload
</span>) local file
       # ref: https://www.mediawiki.org/wiki/API:Upload
       <span style="color:red">upload</span> (correction(s): <span style="color:green">
       payload={'action':'upload','filename':filename, 'ignorewarnings':1, 'token':edit_token}
</span>)_file = open(<span style="color:red">filename</span> (correction(s): <span style="color:green">
       files={'file':upload_contents}
</span>),"<span style="color:red">rb</span> (correction(s): <span style="color:green">ru, ré, b, rab, re, ri, ra, ob
</span>)")
       <span style="color:red">upload</span> (correction(s): <span style="color:green">
</span>)_contents = <span style="color:red">upload</span> (correction(s): <span style="color:green">
</span>)_file.<span style="color:red">read</span> (correction(s): <span style="color:green">rend, rad
</span>)()
       <span style="color:red">upload</span> (correction(s): <span style="color:green">
</span>)_file.close()
    
    
       # <span style="color:red">setting</span> (correction(s): <span style="color:green">betting
       # upload the image
</span>) <span style="color:red">parameters</span> (correction(s): <span style="color:green">
       print("Uploading file to %s via API..." % (baseurl+"index.php/Fichier:"+filename))
</span>) for <span style="color:red">upload</span> (correction(s): <span style="color:green">
       r4=requests.post(baseurl+'api.php',data=payload,files=files,cookies=edit_cookie)
</span>)
       # <span style="color:red">ref</span> (correction(s): <span style="color:green">zef, rez, rep, re, rif, nef, reg, bref, rex, réf
</span>): <span style="color:red">https</span> (correction(s): <span style="color:green">
</span>)://<span style="color:red">www</span> (correction(s): <span style="color:green">
</span>).<span style="color:red">mediawiki</span> (correction(s): <span style="color:green">
</span>).<span style="color:red">org</span> (correction(s): <span style="color:green">orge, ore, ors, or, ort, ord, erg
</span>)/<span style="color:red">wiki</span> (correction(s): <span style="color:green">kiki, tiki
</span>)/API:Upload
       <span style="color:red">payload</span> (correction(s): <span style="color:green">
</span>)={'action':'<span style="color:red">upload</span> (correction(s): <span style="color:green">
</span>)','<span style="color:red">filename</span> (correction(s): <span style="color:green">
</span>)':<span style="color:red">filename</span> (correction(s): <span style="color:green">
</span>), '<span style="color:red">ignorewarnings</span> (correction(s): <span style="color:green">
</span>)':1, '<span style="color:red">token</span> (correction(s): <span style="color:green">
</span>)':<span style="color:red">edit</span> (correction(s): <span style="color:green">redit, dit, exit, édit, ledit
</span>)_<span style="color:red">token</span> (correction(s): <span style="color:green">
</span>)}
      files={'file':<span style="color:red">upload</span> (correction(s): <span style="color:green">
</span>)_contents}
    
    
       # <span style="color:red">upload</span> (correction(s): <span style="color:green">
       # in case of error print the response
</span>) <span style="color:red">the</span> (correction(s): <span style="color:green">tee, te, tue, thé
       # print(r4.text)
</span>) image
       <span style="color:red">print</span> (correction(s): <span style="color:green">peint, prin, priant, priât, point, sprint, prit, prient
</span>)("Uploading file to %s via API..." % (<span style="color:red">baseurl</span> (correction(s): <span style="color:green">
</span>)+"index.<span style="color:red">php</span> (correction(s): <span style="color:green">phi, pep, pop, ph
</span>)/Fichier:"+<span style="color:red">filename</span> (correction(s): <span style="color:green">
</span>)))
      r4=<span style="color:red">requests</span> (correction(s): <span style="color:green">
</span>).post(<span style="color:red">baseurl</span> (correction(s): <span style="color:green">
</span>)+'api.<span style="color:red">php</span> (correction(s): <span style="color:green">phi, pep, pop, ph
</span>)',data=<span style="color:red">payload</span> (correction(s): <span style="color:green">
</span>),files=files,cookies=<span style="color:red">edit</span> (correction(s): <span style="color:green">redit, dit, exit, édit, ledit
</span>)_cookie)
    
    
      # in case <span style="color:red">of</span> (correction(s): <span style="color:green">bof, f, on, if, ou, lof, o, off, oh, où, or, ob, ok, ouf, os
  # add link to biopath in original page if not already existing
</span>) <span style="color:red">error</span> (correction(s): <span style="color:green">errer
  def addLinkToOriginalPage(name):
</span>) <span style="color:red">print</span> (correction(s): <span style="color:green">peint, prin, priant, priât, point, sprint, prit, prient
</span>) <span style="color:red">the</span> (correction(s): <span style="color:green">tee, te, tue, thé
</span>) <span style="color:red">response</span> (correction(s): <span style="color:green">
</span>)
      #<span style="color:red">print</span> (correction(s): <span style="color:green">peint, prin, priant, priât, point, sprint, prit, prient
</span>)(r4.<span style="color:red">text</span> (correction(s): <span style="color:green">texto, texte, test, tex
</span>))
    
    
      result=requests.post(baseurl+'api.php?action=query&titles='+name+'&export&exportnowrap')
      soup=BeautifulSoup(result.text, "lxml")
      #soup=BeautifulSoup(result.text)
      code=''
      for primitive in soup.findAll("text"):
          code+=primitive.string
    
    
  <span style="color:red">def</span> (correction(s): <span style="color:green">zef, der, des, nef, de, dey
      exist = re.findall("(\[\["+name+" BioPathBot\]\])",code)
</span>) addToPage(<span style="color:red">name</span> (correction(s): <span style="color:green">nave, naze, rame, mame, lame, dame, game, came, nase, nage, nome
      if(len(exist)==0):
</span>), <span style="color:red">img</span> (correction(s): <span style="color:green">
          title = name
</span>)):
          content = "\n\n"+"[["+name+" BioPathBot]]"
      <span style="color:red">title</span> (correction(s): <span style="color:green">tille, titre
          requests.post(baseurl+'api.php?action=query&titles='+title+'&export&exportnowrap')
</span>) = <span style="color:red">name</span> (correction(s): <span style="color:green">nave, naze, rame, mame, lame, dame, game, came, nase, nage, nome
          payload={'action':'edit','assert':'user','format':'json','utf8':'','appendtext':content,'summary':summary,'title':title,'token':edit_token}
</span>) + " BioPathBot"
          r4=requests.post(baseurl+'api.php',data=payload,cookies=edit_cookie)
      content = "[[Fichier: "+ <span style="color:red">img</span> (correction(s): <span style="color:green">
</span>) +"]]"
      pageToChange = <span style="color:red">requests</span> (correction(s): <span style="color:green">
</span>).post(<span style="color:red">baseurl</span> (correction(s): <span style="color:green">
</span>)+'api.<span style="color:red">php</span> (correction(s): <span style="color:green">phi, pep, pop, ph
</span>)?action=<span style="color:red">query</span> (correction(s): <span style="color:green">
</span>)&<span style="color:red">titles</span> (correction(s): <span style="color:green">tilles, titres
</span>)='+<span style="color:red">title</span> (correction(s): <span style="color:green">tille, titre
</span>)+'&export&<span style="color:red">exportnowrap</span> (correction(s): <span style="color:green">
</span>)')
      <span style="color:red">payload</span> (correction(s): <span style="color:green">
</span>)={'action':'<span style="color:red">edit</span> (correction(s): <span style="color:green">redit, dit, exit, édit, ledit
</span>)','<span style="color:red">assert</span> (correction(s): <span style="color:green">
</span>)':'user','format':'<span style="color:red">json</span> (correction(s): <span style="color:green">son
</span>)','<span style="color:red">utf</span> (correction(s): <span style="color:green">urf, ut
</span>)8':'','<span style="color:red">text</span> (correction(s): <span style="color:green">texto, texte, test, tex
</span>)':content,'<span style="color:red">summary</span> (correction(s): <span style="color:green">
</span>)':<span style="color:red">summary</span> (correction(s): <span style="color:green">
</span>),'<span style="color:red">title</span> (correction(s): <span style="color:green">tille, titre
</span>)':<span style="color:red">title</span> (correction(s): <span style="color:green">tille, titre
</span>),'<span style="color:red">token</span> (correction(s): <span style="color:green">
</span>)':<span style="color:red">edit</span> (correction(s): <span style="color:green">redit, dit, exit, édit, ledit
</span>)_<span style="color:red">token</span> (correction(s): <span style="color:green">
</span>)}
      <span style="color:red">r</span> (correction(s): <span style="color:green">ô, , k, re, ri, q, or, z, v, t, l, é, n, b, o, j, m, ru, s, d, y, a, f, e, à, ré, h, c, x, g, w, u, ra, i
</span>)4=<span style="color:red">requests</span> (correction(s): <span style="color:green">
</span>).post(<span style="color:red">baseurl</span> (correction(s): <span style="color:green">
</span>)+'api.<span style="color:red">php</span> (correction(s): <span style="color:green">phi, pep, pop, ph
</span>)',data=<span style="color:red">payload</span> (correction(s): <span style="color:green">
</span>),cookies=<span style="color:red">edit</span> (correction(s): <span style="color:green">redit, dit, exit, édit, ledit
</span>)_cookie)
    
    
   # BioPathBot : <span style="color:red">add</span> (correction(s): <span style="color:green">ado, aïd, adp, ada
    
</span>) line <span style="color:red">of</span> (correction(s): <span style="color:green">bof, f, on, if, ou, lof, o, off, oh, où, or, ob, ok, ouf, os
  def addToPage(name, images, legend):
</span>) <span style="color:red">databiographie</span> (correction(s): <span style="color:green">
      title = name + " BioPathBot"
</span>) to <span style="color:red">the</span> (correction(s): <span style="color:green">tee, te, tue, thé
      content = "[["+name+"]]<br>"+'<div style="display:inline-block;">'+legend+'</div>'
</span>) <span style="color:red">right</span> (correction(s): <span style="color:green">light
      for img in images:
</span>) page (<span style="color:red">time</span> (correction(s): <span style="color:green">tire, mime, tige, cime, tome, tipe, lime, aime, trime, rime, tine
          content += "[[Fichier: "+ img +"|left]]"
</span>) <span style="color:red">and</span> (correction(s): <span style="color:green">ans, aïd, land, an, anda, ana
 
</span>) <span style="color:red">space</span> (correction(s): <span style="color:green">espace
      pageToChange = requests.post(baseurl+'api.php?action=query&titles='+title+'&export&exportnowrap')
</span>))
      payload={'action':'edit','assert':'user','format':'json','utf8':'','text':content,'summary':summary,'title':title,'token':edit_token}
   <span style="color:red">def</span> (correction(s): <span style="color:green">zef, der, des, nef, de, dey
      r4=requests.post(baseurl+'api.php',data=payload,cookies=edit_cookie)
</span>) getDataFromPage(<span style="color:red">name</span> (correction(s): <span style="color:green">nave, naze, rame, mame, lame, dame, game, came, nase, nage, nome
      print(r4.text)
</span>)):
 
   # BioPathBot : add line of databiographie to the right page (time and space)
  def getDataFromPage(name):
       data = []
       data = []
       <span style="color:red">result</span> (correction(s): <span style="color:green">
       dates = []
</span>)=<span style="color:red">requests</span> (correction(s): <span style="color:green">
      places = []
</span>).post(<span style="color:red">baseurl</span> (correction(s): <span style="color:green">
      print("Page Created: " + name)
</span>)+'api.<span style="color:red">php</span> (correction(s): <span style="color:green">phi, pep, pop, ph
      result=requests.post(baseurl+'api.php?action=query&titles='+name+'&export&exportnowrap')
</span>)?action=<span style="color:red">query</span> (correction(s): <span style="color:green">
       soup=BeautifulSoup(result.text, "lxml")
</span>)&<span style="color:red">titles</span> (correction(s): <span style="color:green">tilles, titres
       #soup=BeautifulSoup(result.text)
</span>)='+<span style="color:red">name</span> (correction(s): <span style="color:green">nave, naze, rame, mame, lame, dame, game, came, nase, nage, nome
</span>)+'&export&<span style="color:red">exportnowrap</span> (correction(s): <span style="color:green">
</span>)')
       <span style="color:red">soup</span> (correction(s): <span style="color:green">houp, stup, soupe, soupé, youp, soul, sous, souk, soupa, sou, coup, loup, soue
</span>)=BeautifulSoup(<span style="color:red">result</span> (correction(s): <span style="color:green">
</span>).<span style="color:red">text</span> (correction(s): <span style="color:green">texto, texte, test, tex
</span>), "<span style="color:red">lxml</span> (correction(s): <span style="color:green">
</span>)")
       #<span style="color:red">soup</span> (correction(s): <span style="color:green">houp, stup, soupe, soupé, youp, soul, sous, souk, soupa, sou, coup, loup, soue
</span>)=BeautifulSoup(<span style="color:red">result</span> (correction(s): <span style="color:green">
</span>).<span style="color:red">text</span> (correction(s): <span style="color:green">texto, texte, test, tex
</span>))
       code=''
       code=''
       for primitive in <span style="color:red">soup</span> (correction(s): <span style="color:green">houp, stup, soupe, soupé, youp, soul, sous, souk, soupa, sou, coup, loup, soue
       for primitive in soup.findAll("text"):
</span>).findAll("<span style="color:red">text</span> (correction(s): <span style="color:green">texto, texte, test, tex
          if primitive.string:
</span>)"):
              code+=primitive.string
          code+=primitive.string
    
    
       # <span style="color:red">split</span> (correction(s): <span style="color:green">salit
       # split on list (*)
</span>) on <span style="color:red">list</span> (correction(s): <span style="color:green">liste, listé, lise, lest, lis, liât, lift, lit
       lines = code.split("*")
</span>) (*)
       for line in lines :
       <span style="color:red">lines</span> (correction(s): <span style="color:green">vines, liées, linos, lignes, mines, lises, lins, limes, lires, lices, lianes, laines, linges, lunes, liges, lies, pines, aines, line, fines
</span>) = code.<span style="color:red">split</span> (correction(s): <span style="color:green">salit
</span>)("*")
       for line in <span style="color:red">lines</span> (correction(s): <span style="color:green">vines, liées, linos, lignes, mines, lises, lins, limes, lires, lices, lianes, laines, linges, lunes, liges, lies, pines, aines, line, fines
</span>) :
    
    
           # <span style="color:red">add</span> (correction(s): <span style="color:green">ado, aïd, adp, ada
           # add breaking lines (otherwise will be appened directly in one line)
</span>) <span style="color:red">breaking</span> (correction(s): <span style="color:green">
</span>) <span style="color:red">lines</span> (correction(s): <span style="color:green">vines, liées, linos, lignes, mines, lises, lins, limes, lires, lices, lianes, laines, linges, lunes, liges, lies, pines, aines, line, fines
</span>) (<span style="color:red">otherwise</span> (correction(s): <span style="color:green">
</span>) <span style="color:red">will</span> (correction(s): <span style="color:green">till, bill, twill
</span>) <span style="color:red">be</span> (correction(s): <span style="color:green">bé, bi, re, ne, bée, de, beu, bye, by, b, bue, se, le, bel, bey, e, bu, me, te, ce, je, bec, ber, ben
</span>) <span style="color:red">appened</span> (correction(s): <span style="color:green">
</span>) <span style="color:red">directly</span> (correction(s): <span style="color:green">
</span>) in <span style="color:red">one</span> (correction(s): <span style="color:green">ont, gone, oie, ose, onde, on, ope, none, onc, ode, oye, zone, orne, ore, ne, âne, onze, once, ove, une
</span>) line)
           line = "\n\n"+line
           line = "\n\n"+line
    
    
           # <span style="color:red">get</span> (correction(s): <span style="color:green">cet, pet, let, gel, guet, ket, gît, net, et, set, met, jet, gent, gut
           # get date if exist
</span>) date if <span style="color:red">exist</span> (correction(s): <span style="color:green">exista, existé, existe, exit
           date = re.findall("((?<=\[\[)\d*(\.*\d*\.*\d*)*(?=\]\]))",line)
</span>)
           date = re.<span style="color:red">findall</span> (correction(s): <span style="color:green">
</span>)("((?<=\[\[)\d*(\.*\d*\.*\d*)*(?=\]\]))",line)
           dateToAdd = ""
           dateToAdd = ""
    
    
Ligne 406 : Ligne 191 :
               dateToAdd = date[0][0]
               dateToAdd = date[0][0]
    
    
           # <span style="color:red">get</span> (correction(s): <span style="color:green">cet, pet, let, gel, guet, ket, gît, net, et, set, met, jet, gent, gut
           # get place if exist
</span>) place if <span style="color:red">exist</span> (correction(s): <span style="color:green">exista, existé, existe, exit
          place = re.findall("(?<=\/\s\[\[)[A-zÀ-ÿ\s\-]*(?=\]\])",line)
</span>)
          if(len(place)==0):
          place = re.<span style="color:red">findall</span> (correction(s): <span style="color:green">
              place = re.findall("(?<=\/\[\[)[A-zÀ-ÿ\s\-]*(?=\]\])",line)
</span>)("(?<=\/\s\[\[)[A-zÀ-ÿ]*(?=\]\])",line)
           placeToAdd = ""
           location = ""
           if len(place) != 0:
           if len(place) != 0:
              placeToAdd = place[0]
              placeToAdd = place[0]
              location = <span style="color:red">geolocator</span> (correction(s): <span style="color:green">
              if placeToAdd == "Rome":
</span>).geocode(placeToAdd)
                  placeToAdd = "Roma"
 
          # if both the date and the location are available, append in data array
          if dateToAdd and placeToAdd:
              location = ""
              for retries in range(5):
                  try:
                      location = geolocator.geocode(placeToAdd)
                  except GeocoderTimedOut:
                      continue
                  break
 
              # geopy usage policy max 1 request/sec
              # https://operations.osmfoundation.org/policies/nominatim/
              time.sleep(2)
 
              if location:
                  print("Location: " + placeToAdd + " : " + str(location.longitude) + "," + str(location.latitude))
                  dataToAdd = [location.longitude,location.latitude];
                  dates.append(dateToAdd)
                  places.append(placeToAdd)
                  data.append(dataToAdd)
    
    
           # if both <span style="color:red">the</span> (correction(s): <span style="color:green">tee, te, tue, thé
           # stop getting data if find [[Décès]]
</span>) date <span style="color:red">and</span> (correction(s): <span style="color:green">ans, aïd, land, an, anda, ana
          foundDeces = re.findall("(\[\[Décès*\]\] (de |d)\[\["+name+")",line)
</span>) <span style="color:red">the</span> (correction(s): <span style="color:green">tee, te, tue, thé
           if(len(foundDeces) != 0):
</span>) location are available, <span style="color:red">append</span> (correction(s): <span style="color:green">appends, appendu, apprend
               break
</span>) in data array
           if dateToAdd <span style="color:red">and</span> (correction(s): <span style="color:green">ans, aïd, land, an, anda, ana
</span>) location:
               dataToAdd = [location.longitude,location.latitude];
              data.<span style="color:red">append</span> (correction(s): <span style="color:green">appends, appendu, apprend
</span>)(dataToAdd);
      return data
    
    
      return [data, dates, places]
    
    
   # finds <span style="color:red">the</span> (correction(s): <span style="color:green">tee, te, tue, thé
   # finds the minimal and maximal longitude and latitude
</span>) minimal <span style="color:red">and</span> (correction(s): <span style="color:green">ans, aïd, land, an, anda, ana
   def findCorners(pts):
</span>) maximal longitude <span style="color:red">and</span> (correction(s): <span style="color:green">ans, aïd, land, an, anda, ana
</span>) latitude
   <span style="color:red">def</span> (correction(s): <span style="color:green">zef, der, des, nef, de, dey
</span>) findCorners(pts):
       minlon = maxlon = pts[0][0]
       minlon = maxlon = pts[0][0]
       minlat = maxlat = pts[0][1]
       minlat = maxlat = pts[0][1]
Ligne 453 : Ligne 248 :
       return [minlon, maxlon, minlat, maxlat]
       return [minlon, maxlon, minlat, maxlat]
    
    
   # draws <span style="color:red">the</span> (correction(s): <span style="color:green">tee, te, tue, thé
 
</span>) map, some points <span style="color:red">and</span> (correction(s): <span style="color:green">ans, aïd, land, an, anda, ana
   # draws the map, some points and the lines
</span>) <span style="color:red">the</span> (correction(s): <span style="color:green">tee, te, tue, thé
   def drawmap_colors(pts, dates, places, filename, export=False):
</span>) <span style="color:red">lines</span> (correction(s): <span style="color:green">vines, liées, linos, lignes, mines, lises, lins, limes, lires, lices, lianes, laines, linges, lunes, liges, lies, pines, aines, line, fines
</span>)
   <span style="color:red">def</span> (correction(s): <span style="color:green">zef, der, des, nef, de, dey
</span>) drawmap(pts, <span style="color:red">filename</span> (correction(s): <span style="color:green">
</span>), export=False):
       n_pts = len(pts)
       n_pts = len(pts)
       corners = findCorners(pts)
       corners = findCorners(pts)
      txt = ""
       m = Basemap(llcrnrlon=corners[0]-1, llcrnrlat=corners[2]-1, urcrnrlon=corners[1]+1, urcrnrlat=corners[3]+1, resolution='i')
       m = Basemap(llcrnrlon=corners[0]-1, llcrnrlat=corners[2]-1, urcrnrlon=corners[1]+1, urcrnrlat=corners[3]+1, resolution='i')
       m.drawmapboundary(fill_color='0.6')
       m.drawmapboundary(fill_color='0.6')
       m.drawcountries(linewidth=1.0, color='0.6')
       m.drawcountries(linewidth=1.0, color='0.6')
       m.fillcontinents(color='white', lake_color='white')
       m.fillcontinents(color='white', lake_color='white')
       for i in range(n_pts-1): # <span style="color:red">draw</span> (correction(s): <span style="color:green">drap, drag
       for i in range(n_pts-1): # draw lines
</span>) <span style="color:red">lines</span> (correction(s): <span style="color:green">vines, liées, linos, lignes, mines, lises, lins, limes, lires, lices, lianes, laines, linges, lunes, liges, lies, pines, aines, line, fines
</span>)
           for j in range(SEGMENTS):
           for j in range(SEGMENTS):
               start = pts[i] + (pts[i+1]-pts[i])*(j/SEGMENTS)
               start = pts[i] + (pts[i+1]-pts[i])*(j/SEGMENTS)
               end = pts[i] + (pts[i+1]-pts[i])*((j+1)/SEGMENTS)
               end = pts[i] + (pts[i+1]-pts[i])*((j+1)/SEGMENTS)
               m.plot([start[0], end[0]], [start[1], end[1]], color=<span style="color:red">hsv</span> (correction(s): <span style="color:green">hiv
               m.plot([start[0], end[0]], [start[1], end[1]], color=hsv_to_rgb((i+j/SEGMENTS)/n_pts, 1, 1))
</span>)_to_<span style="color:red">rgb</span> (correction(s): <span style="color:green">rab
       for i in range(n_pts): # draw points
</span>)((i+j/SEGMENTS)/n_pts, 1, 1))
          curr_color = hsv_to_rgb(i/n_pts, 1, 1)
       for i in range(n_pts): # <span style="color:red">draw</span> (correction(s): <span style="color:green">drap, drag
           m.plot(pts[i][0], pts[i][1], marker='o', color=curr_color, fillstyle='full', markeredgewidth=0.0)
</span>) points
          txt += "<br><span style='color:" + rgb2hex(curr_color) + "; font-weight:bold'>" + dates[i] + " / " + places[i] + ". </span>"
           m.plot(pts[i][0], pts[i][1], marker='o', color=<span style="color:red">hsv</span> (correction(s): <span style="color:green">hiv
</span>)_to_<span style="color:red">rgb</span> (correction(s): <span style="color:green">rab
</span>)(i/n_pts, 1, 1), fillstyle='full', markeredgewidth=0.0)
       if export:
       if export:
           <span style="color:red">plt</span> (correction(s): <span style="color:green">plut, plet, pet, pût, plût, pli, plot, plat, pst, put, pât, plu, pat, pot
           plt.savefig(filename, bbox_inches='tight')
</span>).savefig(<span style="color:red">filename</span> (correction(s): <span style="color:green">
          plt.close()
</span>), bbox_inches='tight')
      # plt.show()
      <span style="color:red">plt</span> (correction(s): <span style="color:green">plut, plet, pet, pût, plût, pli, plot, plat, pst, put, pât, plu, pat, pot
      return txt
</span>).show()
 
  # inp: point inside the box
  # hs: half dimensions of the box
  # outp: another point
  # finds the intersection of the segment inp-outp and the box
  def line_box(inp, hs, outp):
      dir = outp - inp
      if dir[0] == 0: dir[0] = np.nextafter(0, 1)
      if dir[1] == 0: dir[1] = np.nextafter(0, 1)
      ref = np.array([np.copysign(hs[0],dir[0]), np.copysign(hs[1],dir[1])])
      dir_x = np.array([(ref[1]/dir[1])*dir[0], ref[1]])
      dir_y = np.array([ref[0], (ref[0]/dir[0])*dir[1]])
      fdir = dir_x if np.linalg.norm(dir_x) < np.linalg.norm(dir_y) else dir_y
      return inp+fdir
 
  # computes the repulsion force if 2 boxes are overlapping
  def repulsion_force(pos1, pos2, bbox1, bbox2):
      hs1 = np.array([bbox1.width,bbox1.height])/2
      hs2 = np.array([bbox2.width,bbox2.height])/2
      c1 = pos1 + hs1
      c2 = pos2 + hs2
 
      # if both same position, choose a random direction
      if all(c1==c2):
          return (np.random.rand(2)-np.array([0.5,0.5]))*hs1[1]
 
      b1 = line_box(c1, hs1*1.5, c2)
      b2 = line_box(c2, hs2*1.5, c1)
 
      # if pointing in the opposite direction
      if np.dot(b1-c1,b2-b1) < 0:
          return b2-b1
      return np.zeros(2)
 
  # readjust text labels so there is no overlap
  def adjust_text(texts, text_width, text_height,num_iterations=20,eta=0.5):
      text_pos = [np.array(text.get_position()) for text in texts]
      indices = list(range(len(texts)))
      colliding = [True for text in texts]
 
      # get text bounding boxes
      f = plt.gcf()
      r = f.canvas.get_renderer()
      ax = plt.gca()
      bboxes = [text.get_window_extent(renderer=r).transformed(ax.transData.inverted()) for text in texts]
 
      # center text pos on markers
      for i in range(len(texts)):
          text_pos[i][0] -= bboxes[i].width/2
          text_pos[i][1] -= bboxes[i].height/2
 
      # readjust text labels
      for _ in range(num_iterations):
          random.shuffle(indices)
          for (i,j) in itertools.combinations(indices, 2):
              if i == j:
                  continue
              # pdb.set_trace()
              f = repulsion_force(text_pos[i], text_pos[j], bboxes[i], bboxes[j])
              text_pos[i] += f*eta
 
      # delete text objects and create annotations on the readjusted positions
      for i in range(len(texts)):
          a = plt.annotate(texts[i].get_text(), xy=texts[i].get_position(), xytext=text_pos[i],
              arrowprops=dict(arrowstyle="-", color='k', lw=0.5, alpha=0.6),bbox=dict(facecolor='b', alpha=0.2))
 
          # plt.plot(text_pos[i][0], text_pos[i][1], marker='o',color='r', fillstyle='full', markeredgewidth=0.0,alpha=0.7)
          # plt.plot(text_pos[i][0]+bboxes[i].width, text_pos[i][1], marker='o',color='r', fillstyle='full', markeredgewidth=0.0,alpha=0.7)
          # plt.plot(text_pos[i][0]+bboxes[i].width, text_pos[i][1]+bboxes[i].height, marker='o',color='r', fillstyle='full', markeredgewidth=0.0,alpha=0.7)
          # plt.plot(text_pos[i][0], text_pos[i][1]+bboxes[i].height, marker='o',color='r', fillstyle='full', markeredgewidth=0.0,alpha=0.7)
 
          a.draggable();
          texts[i].remove()
 
  # draws the map, some points and the lines
  def drawmap_date(pts, dates, places, filename, export=False):
      n_pts = len(pts)
      corners = findCorners(pts)
      txt = ""
 
      # ratio correction to 2:1
      lon_width = min((corners[1]-corners[0]+10)*1.1, 360)
      lat_width = min((corners[3]-corners[2]+10)*1.1, 180)
      lon_center = (corners[1]+corners[0])/2
      lat_center = (corners[3]+corners[2])/2
      if lon_width > lat_width*2:
          lat_width = lon_width/2
      else:
          lon_width = lat_width*2
 
      corners[0] = lon_center-lon_width/2
      corners[1] = lon_center+lon_width/2
      corners[2] = lat_center-lat_width/2
      corners[3] = lat_center+lat_width/2
    
    
      if corners[0] < -180:
          corners[1] -= corners[0]-(-180)
          corners[0] = -180
      elif corners[1] > 180:
          corners[0] -= corners[1]-180
          corners[1] = 180
      if corners[2] < -90:
          corners[3] -= corners[2]-(-90)
          corners[2] = -90
      elif corners[3] > 90:
          corners[2] -= corners[3]-90
          corners[3] = 90
 
      # draw map background
      m = Basemap(llcrnrlon=corners[0], llcrnrlat=corners[2], urcrnrlon=corners[1], urcrnrlat=corners[3], resolution='i')
      m.drawmapboundary(fill_color='0.6')
      m.drawcountries(linewidth=1.0, color='0.6')
      m.fillcontinents(color='white', lake_color='white')
      texts = []
      '''
      for i in range(n_pts-1): # draw lines
          for j in range(SEGMENTS):
              start = pts[i] + (pts[i+1]-pts[i])*(j/SEGMENTS)
              end = pts[i] + (pts[i+1]-pts[i])*((j+1)/SEGMENTS)
              m.plot([start[0], end[0]], [start[1], end[1]], color=hsv_to_rgb((i+j/SEGMENTS)/n_pts, 1, 1))
      '''
      for i in range(n_pts): # draw points
          curr_color = hsv_to_rgb(i/n_pts, 1, 1)
          x,y = m(pts[i][0], pts[i][1])
          m.plot(x, y, marker='o', color=curr_color, fillstyle='full', markeredgewidth=0.0,alpha=0.7)
          texts.append(plt.text(x, y, dates[i]))
          txt += "<span style='color:" + rgb2hex(curr_color) + "; font-weight:bold'>" + dates[i] + " / " + places[i] + ". </span> <br>"
      adjust_text(texts, 1, 0.4)
      if export:
          plt.savefig(filename, bbox_inches='tight')
          plt.close()
      return txt
    
    
   for <span style="color:red">name</span> (correction(s): <span style="color:green">nave, naze, rame, mame, lame, dame, game, came, nase, nage, nome
   for name in names:
</span>) in <span style="color:red">names</span> (correction(s): <span style="color:green">nases, dames, lames, nages, nazes, cames, rames
      image_filename_colors = (name + "_colors_biopath.png").replace(" ","_")
</span>):
      image_filename_date = (name + "_date_biopath.png").replace(" ","_")
      image_<span style="color:red">filename</span> (correction(s): <span style="color:green">
       data = getDataFromPage(name)
</span>) = (<span style="color:red">name</span> (correction(s): <span style="color:green">nave, naze, rame, mame, lame, dame, game, came, nase, nage, nome
      if len(data[0]) != 0:
</span>) + "_biopath.png").replace(" ","_")
          legend_colors = drawmap_colors(np.array(data[0]), data[1], data[2], image_filename_colors, True)
       data = getDataFromPage(<span style="color:red">name</span> (correction(s): <span style="color:green">nave, naze, rame, mame, lame, dame, game, came, nase, nage, nome
          drawmap_date(np.array(data[0]), data[1], data[2], image_filename_date, True)
</span>))
          uploadMap(image_filename_date)
      drawmap(np.array(data), image_<span style="color:red">filename</span> (correction(s): <span style="color:green">
          uploadMap(image_filename_colors)
</span>), True)
          addToPage(name, [image_filename_colors, image_filename_date], legend_colors)
      uploadMap(image_<span style="color:red">filename</span> (correction(s): <span style="color:green">
          addLinkToOriginalPage(name)
</span>))
          print("end")
      addToPage(name, image_<span style="color:red">filename</span> (correction(s): <span style="color:green">
</span>))
 
 


==Groupe==
==Groupe==

Version du 30 mai 2017 à 08:15


Description

Depuis une databiographie (correction(s): ) suivant le format utilisé sur le wiki (correction(s): kiki, tiki ) (ex (correction(s): exo, en, eu, eux, es, eh, et, x, e, rex, tex ): 1890.03.19 / Genève), le BioPathBot est capable d'extraire les informations temporelles et spatiales. Ces deux données forment un tuple (correction(s): tuile, tulle ) et sont parsées (correction(s): parées, pansées, parlées, passées ) de façon suivante :

  • La date est formée d'une année, d'un mois et d'un jour. Si des informations venaient à manquer, la date est complétée par défaut avec le mois de janvier et le premier du mois.
  • Le lieu est transformé en coordonnées géographiques (utilisant la librairie python [https (correction(s):

)://pypi.python.org (correction(s): orge, ore, ors, or, ort, ord, erg )/pypi/geopy (correction(s): ) geopy (correction(s): )])

Ainsi transformées et triées chronologiquement, ces données permettent de générer une carte utilisant [http (correction(s): )://matplotlib (correction(s): ).org (correction(s): orge, ore, ors, or, ort, ord, erg )/basemap (correction(s): )/index.html basemap (correction(s): )], où sont dessinés des points pour marquer les lieux, ainsi que des traits mettant en avant la trajectoire de la personne. Cette carte est ensuite importée sur wikipast (correction(s): ) puis insérée sur une page annexe.

Par exemple la carte de Jean Tinguely se visualise comme:

Jean Tinguely biopath.png

Critique

  • Peu importe le type de page (une page concernant une personne, un hypermot...), une carte est générée. Donc si une page ne relate pas la vie d'une personne, la carte aura peu de sens.
  • Avec l'algorithme actuel, les dates et les lieux sont parfaitement extraits et placé sur la carte. Cependant, si les données ne sont pas suffisamment précises, elles seront approximées (correction(s):

) (1980 devient 1980.01.01 tandis (correction(s): candis, tendis, taudis ) que le point géographique correspondant à la Suisse sera son centre géographique). Cela peut mener à des erreurs de trajectoire lors du tri chronologique ou simplement à une position différente de la réalité sur la carte.

  • S'il existe des événements post-mortem (correction(s): mortel, mortes, morte

) mais que la mort de la personne n'est pas mentionnée ou ne suit pas la convention de l'hypermot Décès, ces données seront aussi extraites et placées sur la carte, alors même que la personne ne se déplace pas.

  • Si des événements sont spatialement trop proches, ils vont se superposer sur la carte et il deviendra difficile de retracer les événements d'une façon visuellement claire. Par exemple, pour un trait entre Berne et Lausanne, il sera difficile de juger si une personne a fait plusieurs fois ce même déplacement ou ne l'a fait qu'une seule fois au cours de sa vie.
  • Parfois l'outil qui transforme les lieux en coordonnées géographiques crée des erreurs. Par exemple "Rome" devient "Lomé" qui se situe au Togo.
  • Certains événements se passe sans que la personne soit physiquement présente, mais on ne peut pas le détecter automatiquement. Les lieux apparaissent donc sur la carte.

Code

 import urllib.request
 import requests
 from bs4 import BeautifulSoup
 import re
 import math
 import numpy as np
 import datetime
 import random
 import copy
 from geopy.geocoders import Nominatim
 from mpl_toolkits.basemap import Basemap
 import matplotlib.pyplot as plt
 from colorsys import hsv_to_rgb
 from matplotlib.colors import rgb2hex
 import pdb
 import time
 import itertools
 from geopy.exc import GeocoderTimedOut
 SEGMENTS = 100
 
 
 # draw plots inline rather than in a seperate window
 # %matplotlib inline
 # draw plots bigger
 plt.rcParams["figure.figsize"] = [20.0, 10.0]
 
 bot_user='BioPathBot'
 passw='chkiroju'
 baseurl='http://wikipast.epfl.ch/wikipast/'
 summary='Wikipastbot update'
 protected_logins=["Frederickaplan","Maud","Vbuntinx","Testbot","IB","SourceBot","PageUpdaterBot","Orthobot","BioPathBot","ChronoBOT","Amonbaro","AntoineL","AntoniasBanderos","Arnau","Arnaudpannatier","Aureliver","Brunowicht","Burgerpop","Cedricviaccoz","Christophe","Claudioloureiro","Ghislain","Gregoire3245","Hirtg","Houssm","Icebaker","JenniCin","JiggyQ","JulienB","Kl","Kperrard","Leandro Kieliger","Marcus","Martin","MatteoGiorla","Mireille","Mj2905","Musluoglucem","Nacho","Nameless","Nawel","O'showa","PA","Qantik","QuentinB","Raphael.barman","Roblan11","Romain Fournier","Sbaaa","Snus","Sonia","Tboyer","Thierry","Titi","Vlaedr","Wanda"]
 depuis_date='2017-02-02T16:00:00Z'
 liste_pages=[]
 for user in protected_logins:
     result=requests.post(baseurl+'api.php?action=query&list=usercontribs&ucuser='+user+'&format=xml&ucend='+depuis_date+'&ucshow=new')
     soup=BeautifulSoup(result.content,'lxml')
     for primitive in soup.usercontribs.findAll('item'):
         title = primitive['title']
         if 'Fichier' not in title and 'BioPathBot' not in title:
             liste_pages.append(primitive['title'])
 
 names=list(set(liste_pages))
 for title in names:
     print(title)
 
 # Login request
 payload={'action':'query','format':'json','utf8':,'meta':'tokens','type':'login'}
 r1=requests.post(baseurl + 'api.php', data=payload)
 
 #login confirm
 login_token=r1.json()['query']['tokens']['logintoken']
 payload={'action':'login','format':'json','utf8':,'lgname':bot_user,'lgpassword':passw,'lgtoken':login_token}
 r2=requests.post(baseurl + 'api.php', data=payload, cookies=r1.cookies)
 
 #get edit token2
 params3='?format=json&action=query&meta=tokens&continue='
 r3=requests.get(baseurl + 'api.php' + params3, cookies=r2.cookies)
 edit_token=r3.json()['query']['tokens']['csrftoken']
 
 edit_cookie=r2.cookies.copy()
 edit_cookie.update(r3.cookies)
 
 #setup geolocator
 geolocator = Nominatim(timeout=30)
 
 # upload config
 def uploadMap(filename):
 
     # read local file
     upload_file = open(filename,"rb")
     upload_contents = upload_file.read()
     upload_file.close()
 
     # setting parameters for upload
     # ref: https://www.mediawiki.org/wiki/API:Upload
     payload={'action':'upload','filename':filename, 'ignorewarnings':1, 'token':edit_token}
     files={'file':upload_contents}
 
     # upload the image
     print("Uploading file to %s via API..." % (baseurl+"index.php/Fichier:"+filename))
     r4=requests.post(baseurl+'api.php',data=payload,files=files,cookies=edit_cookie)
 
     # in case of error print the response
     # print(r4.text)
 
 # add link to biopath in original page if not already existing
 def addLinkToOriginalPage(name):
 
     result=requests.post(baseurl+'api.php?action=query&titles='+name+'&export&exportnowrap')
     soup=BeautifulSoup(result.text, "lxml")
     #soup=BeautifulSoup(result.text)
     code=
     for primitive in soup.findAll("text"):
         code+=primitive.string
 
     exist = re.findall("(\[\["+name+" BioPathBot\]\])",code)
     if(len(exist)==0):
         title = name
         content = "\n\n"+""+name+" BioPathBot"
         requests.post(baseurl+'api.php?action=query&titles='+title+'&export&exportnowrap')
         payload={'action':'edit','assert':'user','format':'json','utf8':,'appendtext':content,'summary':summary,'title':title,'token':edit_token}
         r4=requests.post(baseurl+'api.php',data=payload,cookies=edit_cookie)
 
 
 def addToPage(name, images, legend):
     title = name + " BioPathBot"

content = ""+name+"
"+'

'+legend+'

'

     for img in images:

content += "

"

     pageToChange = requests.post(baseurl+'api.php?action=query&titles='+title+'&export&exportnowrap')
     payload={'action':'edit','assert':'user','format':'json','utf8':,'text':content,'summary':summary,'title':title,'token':edit_token}
     r4=requests.post(baseurl+'api.php',data=payload,cookies=edit_cookie)
     print(r4.text)
 
 # BioPathBot : add line of databiographie to the right page (time and space)
 def getDataFromPage(name):
     data = []
     dates = []
     places = []
     print("Page Created: " + name)
     result=requests.post(baseurl+'api.php?action=query&titles='+name+'&export&exportnowrap')
     soup=BeautifulSoup(result.text, "lxml")
     #soup=BeautifulSoup(result.text)
     code=
     for primitive in soup.findAll("text"):
         if primitive.string:
             code+=primitive.string
 
     # split on list (*)
     lines = code.split("*")
     for line in lines :
 
         # add breaking lines (otherwise will be appened directly in one line)
         line = "\n\n"+line
 
         # get date if exist
         date = re.findall("((?<=\[\[)\d*(\.*\d*\.*\d*)*(?=\]\]))",line)
         dateToAdd = ""
 
         if len(date) != 0 :
             dateToAdd = date[0][0]
 
         # get place if exist
         place = re.findall("(?<=\/\s\[\[)[A-zÀ-ÿ\s\-]*(?=\]\])",line)
         if(len(place)==0):
             place = re.findall("(?<=\/\[\[)[A-zÀ-ÿ\s\-]*(?=\]\])",line)
         placeToAdd = ""
         if len(place) != 0:
             placeToAdd = place[0]
             if placeToAdd == "Rome":
                 placeToAdd = "Roma"
 
         # if both the date and the location are available, append in data array
         if dateToAdd and placeToAdd:
             location = ""
             for retries in range(5):
                 try:
                     location = geolocator.geocode(placeToAdd)
                 except GeocoderTimedOut:
                     continue
                 break
 
             # geopy usage policy max 1 request/sec
             # https://operations.osmfoundation.org/policies/nominatim/
             time.sleep(2)
 
             if location:
                 print("Location: " + placeToAdd + " : " + str(location.longitude) + "," + str(location.latitude))
                 dataToAdd = [location.longitude,location.latitude];
                 dates.append(dateToAdd)
                 places.append(placeToAdd)
                 data.append(dataToAdd)
 
         # stop getting data if find Décès
         foundDeces = re.findall("(\[\[Décès*\]\] (de |d)\[\["+name+")",line)
         if(len(foundDeces) != 0):
             break
 
     return [data, dates, places]
 
 # finds the minimal and maximal longitude and latitude
 def findCorners(pts):
     minlon = maxlon = pts[0][0]
     minlat = maxlat = pts[0][1]
     for p in pts:
         currlon = p[0]
         if currlon<minlon:
             minlon = currlon
         elif currlon>maxlon:
             maxlon = currlon
 
         currlat = p[1]
         if currlat<minlat:
             minlat = currlat
         elif currlat>maxlat:
             maxlat = currlat
 
     return [minlon, maxlon, minlat, maxlat]
 
 
 # draws the map, some points and the lines
 def drawmap_colors(pts, dates, places, filename, export=False):
     n_pts = len(pts)
     corners = findCorners(pts)
     txt = ""
     m = Basemap(llcrnrlon=corners[0]-1, llcrnrlat=corners[2]-1, urcrnrlon=corners[1]+1, urcrnrlat=corners[3]+1, resolution='i')
     m.drawmapboundary(fill_color='0.6')
     m.drawcountries(linewidth=1.0, color='0.6')
     m.fillcontinents(color='white', lake_color='white')
     for i in range(n_pts-1): # draw lines
         for j in range(SEGMENTS):
             start = pts[i] + (pts[i+1]-pts[i])*(j/SEGMENTS)
             end = pts[i] + (pts[i+1]-pts[i])*((j+1)/SEGMENTS)
             m.plot([start[0], end[0]], [start[1], end[1]], color=hsv_to_rgb((i+j/SEGMENTS)/n_pts, 1, 1))
     for i in range(n_pts): # draw points
         curr_color = hsv_to_rgb(i/n_pts, 1, 1)
         m.plot(pts[i][0], pts[i][1], marker='o', color=curr_color, fillstyle='full', markeredgewidth=0.0)
         txt += "
" + dates[i] + " / " + places[i] + ". " if export: plt.savefig(filename, bbox_inches='tight') plt.close() # plt.show() return txt # inp: point inside the box # hs: half dimensions of the box # outp: another point # finds the intersection of the segment inp-outp and the box def line_box(inp, hs, outp): dir = outp - inp if dir[0] == 0: dir[0] = np.nextafter(0, 1) if dir[1] == 0: dir[1] = np.nextafter(0, 1) ref = np.array([np.copysign(hs[0],dir[0]), np.copysign(hs[1],dir[1])]) dir_x = np.array([(ref[1]/dir[1])*dir[0], ref[1]]) dir_y = np.array([ref[0], (ref[0]/dir[0])*dir[1]]) fdir = dir_x if np.linalg.norm(dir_x) < np.linalg.norm(dir_y) else dir_y return inp+fdir # computes the repulsion force if 2 boxes are overlapping def repulsion_force(pos1, pos2, bbox1, bbox2): hs1 = np.array([bbox1.width,bbox1.height])/2 hs2 = np.array([bbox2.width,bbox2.height])/2 c1 = pos1 + hs1 c2 = pos2 + hs2 # if both same position, choose a random direction if all(c1==c2): return (np.random.rand(2)-np.array([0.5,0.5]))*hs1[1] b1 = line_box(c1, hs1*1.5, c2) b2 = line_box(c2, hs2*1.5, c1) # if pointing in the opposite direction if np.dot(b1-c1,b2-b1) < 0: return b2-b1 return np.zeros(2) # readjust text labels so there is no overlap def adjust_text(texts, text_width, text_height,num_iterations=20,eta=0.5): text_pos = [np.array(text.get_position()) for text in texts] indices = list(range(len(texts))) colliding = [True for text in texts] # get text bounding boxes f = plt.gcf() r = f.canvas.get_renderer() ax = plt.gca() bboxes = [text.get_window_extent(renderer=r).transformed(ax.transData.inverted()) for text in texts] # center text pos on markers for i in range(len(texts)): text_pos[i][0] -= bboxes[i].width/2 text_pos[i][1] -= bboxes[i].height/2 # readjust text labels for _ in range(num_iterations): random.shuffle(indices) for (i,j) in itertools.combinations(indices, 2): if i == j: continue # pdb.set_trace() f = repulsion_force(text_pos[i], text_pos[j], bboxes[i], bboxes[j]) text_pos[i] += f*eta # delete text objects and create annotations on the readjusted positions for i in range(len(texts)): a = plt.annotate(texts[i].get_text(), xy=texts[i].get_position(), xytext=text_pos[i], arrowprops=dict(arrowstyle="-", color='k', lw=0.5, alpha=0.6),bbox=dict(facecolor='b', alpha=0.2)) # plt.plot(text_pos[i][0], text_pos[i][1], marker='o',color='r', fillstyle='full', markeredgewidth=0.0,alpha=0.7) # plt.plot(text_pos[i][0]+bboxes[i].width, text_pos[i][1], marker='o',color='r', fillstyle='full', markeredgewidth=0.0,alpha=0.7) # plt.plot(text_pos[i][0]+bboxes[i].width, text_pos[i][1]+bboxes[i].height, marker='o',color='r', fillstyle='full', markeredgewidth=0.0,alpha=0.7) # plt.plot(text_pos[i][0], text_pos[i][1]+bboxes[i].height, marker='o',color='r', fillstyle='full', markeredgewidth=0.0,alpha=0.7) a.draggable(); texts[i].remove() # draws the map, some points and the lines def drawmap_date(pts, dates, places, filename, export=False): n_pts = len(pts) corners = findCorners(pts) txt = "" # ratio correction to 2:1 lon_width = min((corners[1]-corners[0]+10)*1.1, 360) lat_width = min((corners[3]-corners[2]+10)*1.1, 180) lon_center = (corners[1]+corners[0])/2 lat_center = (corners[3]+corners[2])/2 if lon_width > lat_width*2: lat_width = lon_width/2 else: lon_width = lat_width*2 corners[0] = lon_center-lon_width/2 corners[1] = lon_center+lon_width/2 corners[2] = lat_center-lat_width/2 corners[3] = lat_center+lat_width/2 if corners[0] < -180: corners[1] -= corners[0]-(-180) corners[0] = -180 elif corners[1] > 180: corners[0] -= corners[1]-180 corners[1] = 180 if corners[2] < -90: corners[3] -= corners[2]-(-90) corners[2] = -90 elif corners[3] > 90: corners[2] -= corners[3]-90 corners[3] = 90 # draw map background m = Basemap(llcrnrlon=corners[0], llcrnrlat=corners[2], urcrnrlon=corners[1], urcrnrlat=corners[3], resolution='i') m.drawmapboundary(fill_color='0.6') m.drawcountries(linewidth=1.0, color='0.6') m.fillcontinents(color='white', lake_color='white') texts = [] for i in range(n_pts-1): # draw lines for j in range(SEGMENTS): start = pts[i] + (pts[i+1]-pts[i])*(j/SEGMENTS) end = pts[i] + (pts[i+1]-pts[i])*((j+1)/SEGMENTS) m.plot([start[0], end[0]], [start[1], end[1]], color=hsv_to_rgb((i+j/SEGMENTS)/n_pts, 1, 1)) for i in range(n_pts): # draw points curr_color = hsv_to_rgb(i/n_pts, 1, 1) x,y = m(pts[i][0], pts[i][1]) m.plot(x, y, marker='o', color=curr_color, fillstyle='full', markeredgewidth=0.0,alpha=0.7) texts.append(plt.text(x, y, dates[i])) txt += "" + dates[i] + " / " + places[i] + ".
" adjust_text(texts, 1, 0.4) if export: plt.savefig(filename, bbox_inches='tight') plt.close() return txt for name in names: image_filename_colors = (name + "_colors_biopath.png").replace(" ","_") image_filename_date = (name + "_date_biopath.png").replace(" ","_") data = getDataFromPage(name) if len(data[0]) != 0: legend_colors = drawmap_colors(np.array(data[0]), data[1], data[2], image_filename_colors, True) drawmap_date(np.array(data[0]), data[1], data[2], image_filename_date, True) uploadMap(image_filename_date) uploadMap(image_filename_colors) addToPage(name, [image_filename_colors, image_filename_date], legend_colors) addLinkToOriginalPage(name) print("end")

Groupe

Nom et Prénom Pseudo
Christophe Badoux Christophe
Julien Burkhard JulienB
Kim Lan Phan Hoang Kl
Robin Lang Roblan11

Code