User:Eatcha/resettrans

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
import pywikibot
from pywikibot import pagegenerators
import requests
"""
Please do not set -pt:0 ,
I am not responsible for what you do using this script.

This script is in public domain.
"""
PASSWORD = "YOURPASSWORD" # The account password, NOT the bot password. This works in paws, a Jupyter which uses oAuth, you can't get password :( and usual usage on computer or tool-forge, . If you will be using paws, please remove you password after you finish using this script cuz all files are public. Or implement supporting paswword arg from command line. If you don't know what is bot password, don't even care about that, just enter you login password.

CATEGORY = "Videos of 2014" # Category without the name space ( e.g. Category:Videos of 2014 -- > Videos of 2014 )
API_ENDPOINT  = 'https://commons.wikimedia.org/w/api.php'
SESSION = requests.Session()

def GetLoginToken():
    LoginTokenParams = {
        "action": "query",
        "meta": "tokens",
        "type": "login",
        "format": "json",

    }

    Req = SESSION.get(
        API_ENDPOINT,
        params=LoginTokenParams,
        )

    return (Req.json())["query"]["tokens"]["logintoken"]

def Login():

    LoginParams = {
        "action": "login",
        "lgname": UserName,
        "lgpassword": PASSWORD,
        "format": "json",
        "lgtoken": GetLoginToken(),

    }

    Req = SESSION.post(
        API_ENDPOINT,
        data=LoginParams,
        )
    Response = Req.json()
    print( '%s in logging as %s.' % (Response["login"]["result"], Response["login"]["lgusername"],))


def GetCSRFToken():
    Login() # Get token for the user
    CSRFTokenParams = {
        "action": "query",
        "meta": "tokens",
        "format": "json",

    }

    Req = SESSION.get(API_ENDPOINT, params=CSRFTokenParams)
    CSRFToken = (Req.json())["query"]["tokens"]["csrftoken"]
    return CSRFToken

def retranscode():
    category = pywikibot.Category(
        SITE,
        CATEGORY,
    )
    CSRFToken = GetCSRFToken()
    gen = pagegenerators.CategorizedPageGenerator(category,)
    filec = 0
    for page in gen:
        filename = page.title()
        filec += 1
        print(filec, filename)
        page = pywikibot.Page(
            SITE,
            filename,
            )
        post_fields = {
            'action': 'transcodereset',
            'title': filename,
            'token': CSRFToken,
            'format' : 'json'
            
        }
                      

        Req = SESSION.post(
            API_ENDPOINT,
            data=post_fields,
            )
        if "removed transcode" in str(Req.json()):
            print("Transcode reset successful")
        else:
            print("Failed to reset Transcode")
            print(Req.json())
        SESSION.close()

def main(*args):
    global SITE
    global UserName
    args = pywikibot.handle_args(*args)
    SITE = pywikibot.Site()
    UserName = pywikibot.BaseSite.username(SITE)
    retranscode()

if __name__ == "__main__":
    try:
        main()
    finally:
        pywikibot.stopme()