« Exemple: Chronobot » : différence entre les versions

De Wikipast
Aller à la navigation Aller à la recherche
m (Arnau a déplacé la page Chronobot vers Exemple: Chronobot)
 
(Une version intermédiaire par un autre utilisateur non affichée)
Ligne 14 : Ligne 14 :


user='testbot'
user='testbot'
passw=urllib2.quote('dhbot2017')
passw='dhbot2017'
baseurl='http://wikipast.epfl.ch/wikipast/'
baseurl='http://wikipast.epfl.ch/wikipast/'
login_params='?action=login&lgname=%s&lgpassword=%s&format=json'% (user,passw)
summary='Wikipastbot update'
summary='Chronobot update'


dates=['0033/06/02']
dates=['1980/08/30','1980/08/31','1856/12/03','1856/12/02']
dates_convert=[]
dates_convert=[]


Ligne 30 : Ligne 29 :


# Login request
# Login request
r1=requests.post(baseurl+'api.php'+login_params)
payload={'action':'query','format':'json','utf8':'','meta':'tokens','type':'login'}
login_token=r1.json()['login']['token']
r1=requests.post(baseurl + 'api.php', data=payload)


#login confirm
#login confirm
login_params2=login_params+'&lgtoken=%s'% login_token
login_token=r1.json()['query']['tokens']['logintoken']
r2=requests.post(baseurl+'api.php'+login_params2,cookies=r1.cookies)
payload={'action':'login','format':'json','utf8':'','lgname':user,'lgpassword':passw,'lgtoken':login_token}
r2=requests.post(baseurl + 'api.php', data=payload, cookies=r1.cookies)


#get edit token2
#get edit token2
params3='?format=json&action=query&meta=tokens&continue='
params3='?format=json&action=query&meta=tokens&continue='
r3=requests.get(baseurl+'api.php'+params3,cookies=r2.cookies)
r3=requests.get(baseurl + 'api.php' + params3, cookies=r2.cookies)
edit_token=r3.json()['query']['tokens']['csrftoken']
edit_token=r3.json()['query']['tokens']['csrftoken']


Ligne 48 : Ligne 48 :
     content='\n==Date: '+str(dates[i])+'=='
     content='\n==Date: '+str(dates[i])+'=='
     # save action
     # save action
    headers={'content-type':'application/x-www-form-urlencoded'}
     #payload={'action':'edit','assert':'user','format':'json','utf8':'','text':content,'summary':summary,'title':dates_convert[i],'token':edit_token}
     #payload={'action':'edit','assert':'user','format':'json','text':content,'summary':summary,'title':dates_convert[i],'token':edit_token}
     payload={'action':'edit','assert':'user','format':'json','utf8':'','appendtext':content,'summary':summary,'title':dates_convert[i],'token':edit_token}
     payload={'action':'edit','assert':'user','format':'json','appendtext':content,'summary':summary,'title':dates_convert[i],'token':edit_token}
     r4=requests.post(baseurl+'api.php',data=payload,cookies=edit_cookie)
     r4=requests.post(baseurl+'api.php',headers=headers,data=payload,cookies=edit_cookie)
     print(r4.text)
     print(r4.text)
</nowiki>
</nowiki>

Dernière version du 2 mai 2017 à 11:28

Exemples de page créée par le bot

Code

# -*- coding: utf-8 -*-

import urllib2
import requests
import datetime

user='testbot'
passw='dhbot2017'
baseurl='http://wikipast.epfl.ch/wikipast/'
summary='Wikipastbot update'

dates=['1980/08/30','1980/08/31','1856/12/03','1856/12/02']
dates_convert=[]

for date in dates:
    the_date=datetime.datetime.strptime(date, "%Y/%m/%d")
    timestamp=(the_date-datetime.datetime(1970, 1, 1)).total_seconds()+36
    convert=int(1.0*timestamp/(24*3600))
    dates_convert.append(str(convert))
    print(str(date)+' => '+str(convert))

# 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':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)

for i in range(len(dates)):
    content='\n==Date: '+str(dates[i])+'=='
    # save action
    #payload={'action':'edit','assert':'user','format':'json','utf8':'','text':content,'summary':summary,'title':dates_convert[i],'token':edit_token}
    payload={'action':'edit','assert':'user','format':'json','utf8':'','appendtext':content,'summary':summary,'title':dates_convert[i],'token':edit_token}
    r4=requests.post(baseurl+'api.php',data=payload,cookies=edit_cookie)
    print(r4.text)