#!/usr/bin/python3

import requests

S = requests.Session()

URL = "https://test.wikidata.org/w/api.php"

# Step 1: Retrieve a login token
PARAMS_1 = {
    "action": "query",
    "meta": "tokens",
    "type": "login",
    "format": "json"
}

R = S.get(url=URL, params=PARAMS_1)
DATA = R.json()

LOGIN_TOKEN = DATA["query"]["tokens"]["logintoken"]

# Step 2: Send a post request to log in. For this login 
# method, Obtain credentials by first visiting
# https://test.wikipedia.org/wiki/Special:BotPasswords/
# See https://www.mediawiki.org/wiki/API:Login for more
# information on log in methods.
PARAMS_2 = {
    "action": "login",
    "lgname": "enter_your_username",
    "lgpassword": "enter_your_password",
    "format": "json",
    "lgtoken": LOGIN_TOKEN
}

R = S.post(URL, data=PARAMS_2)

# Step 3: While logged in, retrieve a CSRF token
PARAMS_3 = {
    "action": "query",
    "meta": "tokens",
    "format": "json"
}

R = S.get(url=URL, params=PARAMS_3)
DATA = R.json()

CSRF_TOKEN = DATA["query"]["tokens"]["csrftoken"]

ID = "Q210218"
PROPERTY = "P7"

# Step 4: Send a post request to remove the claim from the item Q210218
DATA = '{"claims":[{"id":"Q210218$B623AFA9-4365-49E5-85AE-0386165D3E49","remove":""}]}'

PARAMS_4 = {
    'action': "wbeditentity",
    'id': ID,
    'data': DATA,
    'token': CSRF_TOKEN,
    'format':"json"
}

R = S.post(URL, data=PARAMS_4)

print(R.text)