In [ ]:
#!/usr/bin/env python 
# -*- coding: UTF-8 -*-

import urllib.request, re, os, time, requests

x = 0
while x < 1 :
    try :
        file = urllib.request.urlopen('https://fr.wikinews.org/w/index.php?title=Sp%C3%A9cial:Journal&limit=200&type=newusers')
        #print("a")
        nom = []
        for i in file :
            i = i.decode('UTF-8')
            if i.startswith('<li data-mw-logid=') :
                try :
                    i = i.split('title="Spécial:Contributions/')
                    i = i[1].split('">contributions</a>')
                    i = i[0]
                
                    nom.append(i)
                except :
                    time.sleep(1)
        #print("b")
        a = 0
        idpage = []
        for j in nom :
            nomurl = urllib.parse.quote(nom[a])
            file2 = urllib.request.urlopen('https://fr.wikinews.org/w/api.php?action=query&format=json&prop=info&titles=Discussion+utilisateur%3A'+nomurl+'+&utf8=1&inprop=')
            a = a + 1
            for i in file2 :
                i = i.decode('utf-8')
                i = i.split('"pages":{"')
                i = i[1].split('"')
                i = i[0]
                idpage.append(i)
        #print (idpage)
        a = 0
        pages = []
        while a < len(nom) :
            if idpage[a] == '-1' : 
                pages.append('Discussion_utilisateur:'+nom[a])
            
            else : 
                a = a
                #print('non')
            a = a + 1
    
        contenu = '{{subst:Bienvenue}}'
        #contenu = contenu+"\n"+'{{subst:Créer une brève}}'
    
        for i in pages :
        
            username = 'Matthobot'
            password = 'Matthobot@6l5d2or74gf4pr2q663p5e4j5n9nmov4'
            baseurl = 'https://fr.wikinews.org/w/'
            summary = "[Action Bot] Bienvenue sur Wikinews, n'hésitez pas à participer"
            message = contenu
            #sectiontitle = bloc1
            title = i

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

            #print('1')
            # login confirm
            login_token = r1.json()['query']['tokens']['logintoken']
            payload = {'action': 'login', 'format': 'json', 'utf8': '', 'lgname': username, 'lgpassword': password, 'lgtoken': login_token}
            r2 = requests.post(baseurl + 'api.php', data=payload, cookies=r1.cookies)

            #print('2')
            # 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']

            #print('3')
            edit_cookie = r2.cookies.copy()
            edit_cookie.update(r3.cookies) 
            #print (edit_token)

            # save action
            payload = {'action': 'edit', 'assert': 'user', 'format': 'json', 'bot': '1', 'utf8': '', 'text': message,'summary': summary, 'title': title, 'token': edit_token}
            r4 = requests.post(baseurl + 'api.php', data=payload, cookies=edit_cookie)

            #print(i, 'bienvenuté')
            #time.sleep(3)
        
        time.sleep(150)
    except : 
        #print("c")
        time.sleep(150)
In [ ]:
 
In [ ]: