import json
import requests
import sys
import time
import pywikibot
from pywikibot import pagegenerators

wikidata_site = pywikibot.Site('wikidata', 'wikidata')
repo = wikidata_site.data_repository()

#### what to do? ####
# possible value:
# create --- new event items
# complement --- add statements which cannot be added during create
# watchlist --- list new items for watchlist import at https://www.wikidata.org/wiki/Special:EditWatchlist/raw
chooseFunction = 'complement'

#### input ####
withFisaWeblink = False
regattaItem = 'Q66124513' # find items to work on with this WDQS query: https://w.wiki/4jm
eventDistance = 1000 # in metres; if unknown, set None
events = { 
# modern World Rowing Championships
#    'M1x', 'M2x', 'M2-', 'M4x', 'M4-', 'M8+',
#    'W1x', 'W2x', 'W2-', 'W4x', 'W4-', 'W8+',
#    'LM1x', 'LM2x', 'LM2-', 'LM4x',
#    'LW1x', 'LW2x', 'LW2-', 'LW4x',
#    'PR1 W1x', 'PR1 M1x', 'PR2 Mix2x', 'PR2 M1x', 'PR2 W1x', 'PR3 Mix2x', 'PR3 Mix4+', 'PR3 M2-', 'PR3 W2-'

# modern World Rowing Junior Championships
#    'JM1x', 'JM2x', 'JM2-', 'JM4-', 'JM4x', 'JM4+', 'JM8+',
#    'JW1x', 'JW2x', 'JW2-', 'JW4-', 'JW4x', 'JW4+', 'JW8+'

# modern World Rowing U23 Championships
#    'BM1x', 'BM2x', 'BM2-', 'BM4x', 'BM4-', 'BM4+', 'BM8+',
#    'BW1x', 'BW2x', 'BW2-', 'BW4x', 'BW4-', 'BW4+', 'BW8+',
#    'BLM1x', 'BLM2x', 'BLM2-', 'BLM4x',
#    'BLW1x', 'BLW2x', 'BLW2-', 'BLW4x'

    'M1x', 'LM1x', 'W1x', 'LW1x'
}


#### static input ####
languages = [ 'en', 'de' ]
url = 'http://www.worldrowing.com/events/{}/{}/'
terms = {
    'labels' : {},
    'descriptionsForEvents' : {
        'en' : 'rowing event',
        'de' : 'Ruderwettbewerb'
    }
}
competitionClassData = {
    'M1x' : { 'short' : 'M1x', 'item' : 'Q26214792', 'labels' : { 'en' : "Men's single scull", 'de' : "Einer der Männer" }, 'urlPartClass' : 'mens-single-sculls' },
    'M2-' : { 'short' : 'M2-', 'item' : 'Q26214795', 'labels' : { 'en' : "Men's coxless pair", 'de' : "Zweier ohne Steuermann" }, 'urlPartClass' : 'mens-pair' },
    'M2+' : { 'short' : 'M2+', 'item' : 'Q26214796', 'labels' : { 'en' : "Men's coxed pair", 'de' : "Zweier mit Steuermann" }, 'urlPartClass' : 'mens-coxed-pair' },
    'M2x' : { 'short' : 'M2x', 'item' : 'Q26214793', 'labels' : { 'en' : "Men's double scull", 'de' : "Doppelzweier der Männer" }, 'urlPartClass' : 'mens-double-sculls' },
    'M4-' : { 'short' : 'M4-', 'item' : 'Q26214797', 'labels' : { 'en' : "Men's coxless four", 'de' : "Vierer ohne Steuermann" }, 'urlPartClass' : 'mens-four' },
    'M4+' : { 'short' : 'M4+', 'item' : 'Q26214798', 'labels' : { 'en' : "Men's coxed four", 'de' : "Vierer mit Steuermann" }, 'urlPartClass' : 'mens-coxed-four' },
    'M4x' : { 'short' : 'M4x', 'item' : 'Q26214794', 'labels' : { 'en' : "Men's quad scull", 'de' : "Doppelvierer der Männer" }, 'urlPartClass' : 'mens-quadruple-sculls' },
    'M8+' : { 'short' : 'M8+', 'item' : 'Q26214799', 'labels' : { 'en' : "Men's eight", 'de' : "Achter der Männer" }, 'urlPartClass' : 'mens-eight' },
    'W1x' : { 'short' : 'W1x', 'item' : 'Q26214784', 'labels' : { 'en' : "Women's single scull", 'de' : "Einer der Frauen" }, 'urlPartClass' : 'womens-single-sculls' },
    'W2-' : { 'short' : 'W2-', 'item' : 'Q26214788', 'labels' : { 'en' : "Women's coxless pair", 'de' : "Zweier ohne Steuerfrau" }, 'urlPartClass' : 'womens-pair' },
    'W2x' : { 'short' : 'W2x', 'item' : 'Q26214785', 'labels' : { 'en' : "Women's double scull", 'de' : "Doppelzweier der Frauen" }, 'urlPartClass' : 'womens-double-sculls' },
    'W4-' : { 'short' : 'W4-', 'item' : 'Q26214789', 'labels' : { 'en' : "Women's coxless four", 'de' : "Vierer ohne Steuerfrau" }, 'urlPartClass' : 'womens-four' },
    'W4+' : { 'short' : 'W4+', 'item' : 'Q26214790', 'labels' : { 'en' : "Women's coxed four", 'de' : "Vierer mit Steuerfrau" }, 'urlPartClass' : 'womens-coxed-four' },
    'W4x' : { 'short' : 'W4x', 'item' : 'Q26214786', 'labels' : { 'en' : "Women's quad scull", 'de' : "Doppelvierer der Frauen" }, 'urlPartClass' : 'womens-quadruple-sculls' },
    'W4x+' : { 'short' : 'W4x+', 'item' : 'Q26214787', 'labels' : { 'en' : "Women's coxed quad scull", 'de' : "Doppelvierer mit Steuerfrau" }, 'urlPartClass' : 'womens-coxed-quadruple-sculls' },
    'W8+' : { 'short' : 'W8+', 'item' : 'Q26214791', 'labels' : { 'en' : "Women's eight", 'de' : "Achter der Frauen" }, 'urlPartClass' : 'womens-eight' },
    'LM1x' : { 'short' : 'LM1x', 'item' : 'Q26214807', 'labels' : { 'en' : "Lightweight men's single scull", 'de' : "Leichtgewichts-Einer der Männer" }, 'urlPartClass' : 'lightweight-mens-single-sculls' },
    'LM2-' : { 'short' : 'LM2-', 'item' : 'Q26214810', 'labels' : { 'en' : "Lightweight men's coxless pair", 'de' : "Leichtgewichts-Zweier ohne Steuermann" }, 'urlPartClass' : 'lightweight-mens-pair' },
    'LM2x' : { 'short' : 'LM2x', 'item' : 'Q26214808', 'labels' : { 'en' : "Lightweight men's double scull", 'de' : "Leichtgewichts-Doppelzweier der Männer" }, 'urlPartClass' : 'lightweight-mens-double-sculls' },
    'LM4-' : { 'short' : 'LM4-', 'item' : 'Q26214811', 'labels' : { 'en' : "Lightweight men's coxless four", 'de' : "Leichtgewichts-Vierer ohne Steuermann" }, 'urlPartClass' : 'lightweight-mens-four' },
    'LM4x' : { 'short' : 'LM4x', 'item' : 'Q26214809', 'labels' : { 'en' : "Lightweight men's quad scull", 'de' : "Leichtgewichts-Doppelvierer der Männer" }, 'urlPartClass' : 'lightweight-mens-quadruple-sculls' },
    'LM8+' : { 'short' : 'LM8+', 'item' : 'Q26214812', 'labels' : { 'en' : "Lightweight men's eight", 'de' : "Leichtgewichts-Achter der Männer" }, 'urlPartClass' : 'lightweight-mens-eight' },
    'LW1x' : { 'short' : 'LW1x', 'item' : 'Q26214800', 'labels' : { 'en' : "Lightweight women's single scull", 'de' : "Leichtgewichts-Einer der Frauen" }, 'urlPartClass' : 'lightweight-womens-single-sculls' },
    'LW2-' : { 'short' : 'LW2-', 'item' : 'Q26214803', 'labels' : { 'en' : "Lightweight women's coxless pair", 'de' : "Leichtgewichts-Zweier ohne Steuerfrau" }, 'urlPartClass' : 'lightweight-womens-pair' },
    'LW2x' : { 'short' : 'LW2x', 'item' : 'Q26214801', 'labels' : { 'en' : "Lightweight women's double scull", 'de' : "Leichtgewichts-Doppelzweier der Frauen" }, 'urlPartClass' : 'lightweight-womens-double-sculls' },
    'LW4-' : { 'short' : 'LW4-', 'item' : 'Q26214804', 'labels' : { 'en' : "Lightweight women's coxless four", 'de' : "Leichtgewichts-Vierer ohne Steuerfrau" }, 'urlPartClass' : 'lightweight-womens-four' },
    'LW4x' : { 'short' : 'LW4x', 'item' : 'Q26214802', 'labels' : { 'en' : "Lightweight women's quad scull", 'de' : "Leichtgewichts-Doppelvierer der Frauen" }, 'urlPartClass' : 'lightweight-womens-quadruple-sculls' },
    'LW8+' : { 'short' : 'LW8+', 'item' : 'Q26214805', 'labels' : { 'en' : "Lightweight women's eight", 'de' : "Leichtgewichts-Achter der Frauen" }, 'urlPartClass' : 'lightweight-womens-eight' },
    'BM1x' : { 'short' : 'BM1x', 'item' : 'Q26214819', 'labels' : { 'en' : "U23 men's single scull", 'de' : "U23-Männer-Einer" }, 'urlPartClass' : 'u23-mens-single-sculls' },
    'BM2-' : { 'short' : 'BM2-', 'item' : 'Q26214822', 'labels' : { 'en' : "U23 men's coxless pair", 'de' : "U23-Zweier ohne Steuermann" }, 'urlPartClass' : 'u23-mens-pair' },
    'BM2+' : { 'short' : 'BM2+', 'item' : 'Q47507737', 'labels' : { 'en' : "U23 men's coxed pair", 'de' : "U23-Zweier mit Steuermann" }, 'urlPartClass' : '' },
    'BM2x' : { 'short' : 'BM2x', 'item' : 'Q26214820', 'labels' : { 'en' : "U23 men's double scull", 'de' : "U23-Männer-Doppelzweier" }, 'urlPartClass' : 'u23-mens-double-sculls' },
    'BM4-' : { 'short' : 'BM4-', 'item' : 'Q26214823', 'labels' : { 'en' : "U23 men's coxless four", 'de' : "U23-Vierer ohne Steuermann" }, 'urlPartClass' : 'u23-mens-four' },
    'BM4+' : { 'short' : 'BM4+', 'item' : 'Q26214824', 'labels' : { 'en' : "U23 men's coxed four", 'de' : "U23-Vierer mit Steuermann" }, 'urlPartClass' : 'u23-mens-coxed-four' },
    'BM4x' : { 'short' : 'BM4x', 'item' : 'Q26214821', 'labels' : { 'en' : "U23 men's quad scull", 'de' : "U23-Männer-Doppelvierer" }, 'urlPartClass' : 'u23-mens-quadruple-sculls' },
    'BM8+' : { 'short' : 'BM8+', 'item' : 'Q26214825', 'labels' : { 'en' : "U23 men's eight", 'de' : "U23-Männer-Achter" }, 'urlPartClass' : 'u23-mens-eight' },
    'BW1x' : { 'short' : 'BW1x', 'item' : 'Q26214813', 'labels' : { 'en' : "U23 women's single scull", 'de' : "U23-Frauen-Einer" }, 'urlPartClass' : 'u23-womens-single-sculls' },
    'BW2-' : { 'short' : 'BW2-', 'item' : 'Q26214816', 'labels' : { 'en' : "U23 women's coxless pair", 'de' : "U23-Zweier ohne Steuerfrau" }, 'urlPartClass' : 'u23-womens-pair' },
    'BW2x' : { 'short' : 'BW2x', 'item' : 'Q26214814', 'labels' : { 'en' : "U23 women's double scull", 'de' : "U23-Frauen-Doppelzweier" }, 'urlPartClass' : 'u23-womens-double-sculls' },
    'BW4-' : { 'short' : 'BW4-', 'item' : 'Q26214817', 'labels' : { 'en' : "U23 women's coxless four", 'de' : "U23-Vierer ohne Steuerfrau" }, 'urlPartClass' : 'u23-womens-four' },
    'BW4+' : { 'short' : 'BW4+', 'item' : 'Q55711627', 'labels' : { 'en' : "U23 women's coxed four", 'de' : "U23-Vierer mit Steuerfrau" }, 'urlPartClass' : 'under-23-womens-coxed-four' },
    'BW4x' : { 'short' : 'BW4x', 'item' : 'Q26214815', 'labels' : { 'en' : "U23 women's quad scull", 'de' : "U23-Frauen-Doppelvierer" }, 'urlPartClass' : 'u23-womens-quadruple-sculls' },
    'BW8+' : { 'short' : 'BW8+', 'item' : 'Q26214818', 'labels' : { 'en' : "U23 women's eight", 'de' : "U23-Frauen-Achter" }, 'urlPartClass' : 'u23-womens-eight' },
    'BLM1x' : { 'short' : 'BLM1x', 'item' : 'Q26214829', 'labels' : { 'en' : "U23 lightweight men's single scull", 'de' : "U23-Leichtgewichts-Männer-Einer" }, 'urlPartClass' : 'u23-lightweight-mens-single-sculls' },
    'BLM2-' : { 'short' : 'BLM2-', 'item' : 'Q26214832', 'labels' : { 'en' : "U23 lightweight men's coxless pair", 'de' : "U23-Leichtgewichts-Zweier ohne Steuermann" }, 'urlPartClass' : 'u23-lightweight-mens-pair' },
    'BLM2x' : { 'short' : 'BLM2x', 'item' : 'Q26214830', 'labels' : { 'en' : "U23 lightweight men's double scull", 'de' : "U23-Leichtgewichts-Männer-Doppelzweier" }, 'urlPartClass' : 'u23-lightweight-mens-double-sculls' },
    'BLM4-' : { 'short' : 'BLM4-', 'item' : 'Q26214833', 'labels' : { 'en' : "U23 lightweight men's coxless four", 'de' : "U23-Leichtgewichts-Vierer ohne Steuermann" }, 'urlPartClass' : 'u23-lightweight-mens-four' },
    'BLM4x' : { 'short' : 'BLM4x', 'item' : 'Q26214831', 'labels' : { 'en' : "U23 lightweight men's quad scull", 'de' : "U23-Leichtgewichts-Männer-Doppelvierer" }, 'urlPartClass' : 'u23-lightweight-mens-quadruple-sculls' },
    'BLW1x' : { 'short' : 'BLW1x', 'item' : 'Q26214826', 'labels' : { 'en' : "U23 lightweight women's single scull", 'de' : "U23-Leichtgewichts-Frauen-Einer" }, 'urlPartClass' : 'u23-lightweight-womens-single-sculls' },
    'BLW2-' : { 'short' : 'BLW2-', 'item' : 'Q55711597', 'labels' : { 'en' : "U23 lightweight women's coxless pair", 'de' : "U23-Leichtgewichts-Zweier ohne Steuerfrau" }, 'urlPartClass' : 'under-23-lightweight-womens-pair' },
    'BLW2x' : { 'short' : 'BLW2x', 'item' : 'Q26214827', 'labels' : { 'en' : "U23 lightweight women's double scull", 'de' : "U23-Leichtgewichts-Frauen-Doppelzweier" }, 'urlPartClass' : 'u23-lightweight-womens-double-sculls' },
    'BLW4x' : { 'short' : 'BLW4x', 'item' : 'Q26214828', 'labels' : { 'en' : "U23 lightweight women's quad scull", 'de' : "U23-Leichtgewichts-Frauen-Doppelvierer" }, 'urlPartClass' : 'u23-lightweight-womens-quadruple-sculls' },
    'JM1x' : { 'short' : 'JM1x', 'item' : 'Q26214842', 'labels' : { 'en' : "Junior men's single scull", 'de' : "Junioren-Einer" }, 'urlPartClass' : 'junior-mens-single-sculls' },
    'JM2-' : { 'short' : 'JM2-', 'item' : 'Q26214845', 'labels' : { 'en' : "Junior men's coxless pair", 'de' : "Junioren-Zweier ohne Steuermann" }, 'urlPartClass' : 'junior-mens-pair' },
    'JM2+' : { 'short' : 'JM2+', 'item' : 'Q26214846', 'labels' : { 'en' : "Junior men's coxed pair", 'de' : "Junioren-Zweier mit Steuermann" }, 'urlPartClass' : 'junior-mens-coxed-pair' },
    'JM2x' : { 'short' : 'JM2x', 'item' : 'Q26214843', 'labels' : { 'en' : "Junior men's double scull", 'de' : "Junioren-Doppelzweier" }, 'urlPartClass' : 'junior-mens-double-sculls' },
    'JM4-' : { 'short' : 'JM4-', 'item' : 'Q26214847', 'labels' : { 'en' : "Junior men's coxless four", 'de' : "Junioren-Vierer ohne Steuermann" }, 'urlPartClass' : 'junior-mens-four' },
    'JM4+' : { 'short' : 'JM4+', 'item' : 'Q26214848', 'labels' : { 'en' : "Junior men's coxed four", 'de' : "Junioren-Vierer mit Steuermann" }, 'urlPartClass' : 'junior-mens-coxed-four' },
    'JM4x' : { 'short' : 'JM4x', 'item' : 'Q26214844', 'labels' : { 'en' : "Junior men's quad scull", 'de' : "Junioren-Doppelvierer" }, 'urlPartClass' : 'junior-mens-quadruple-sculls' },
    'JM8+' : { 'short' : 'JM8+', 'item' : 'Q26214849', 'labels' : { 'en' : "Junior men's eight", 'de' : "Junioren-Achter" }, 'urlPartClass' : 'junior-mens-eight' },
    'JW1x' : { 'short' : 'JW1x', 'item' : 'Q26214835', 'labels' : { 'en' : "Junior women's single scull", 'de' : "Juniorinnen-Einer" }, 'urlPartClass' : 'junior-womens-single-sculls' },
    'JW2-' : { 'short' : 'JW2-', 'item' : 'Q26214839', 'labels' : { 'en' : "Junior women's coxless pair", 'de' : "Juniorinnen-Zweier ohne Steuerfrau" }, 'urlPartClass' : 'junior-womens-pair' },
    'JW2x' : { 'short' : 'JW2x', 'item' : 'Q26214836', 'labels' : { 'en' : "Junior women's double scull", 'de' : "Juniorinnen-Doppelzweier" }, 'urlPartClass' : 'junior-womens-double-sculls' },
    'JW4-' : { 'short' : 'JW4-', 'item' : 'Q26214840', 'labels' : { 'en' : "Junior women's coxless four", 'de' : "Juniorinnen-Vierer ohne Steuerfrau" }, 'urlPartClass' : 'junior-womens-four' },
    'JW4+' : { 'short' : 'JW4+', 'item' : 'Q47507738', 'labels' : { 'en' : "Junior women's coxed four", 'de' : "Juniorinnen-Vierer mit Steuerfrau" }, 'urlPartClass' : 'junior-womens-coxed-four' },
    'JW4x' : { 'short' : 'JW4x', 'item' : 'Q26214837', 'labels' : { 'en' : "Junior women's quad scull", 'de' : "Juniorinnen-Doppelvierer" }, 'urlPartClass' : 'junior-womens-quadruple-sculls' },
    'JW4x+' : { 'short' : 'JW4x+', 'item' : 'Q26214838', 'labels' : { 'en' : "Junior women's coxed quad scull", 'de' : "Juniorinnen-Doppelvierer mit Steuerfrau" }, 'urlPartClass' : 'junior-womens-coxed-quadruple-sculls' },
    'JW8+' : { 'short' : 'JW8+', 'item' : 'Q26214841', 'labels' : { 'en' : "Junior women's eight", 'de' : "Juniorinnen-Achter" }, 'urlPartClass' : 'junior-womens-eight' },
    'PR1 M1x' : { 'short' : 'PR1 M1x', 'item' : 'Q47509009', 'labels' : { 'en' : "PR1 men's single scull", 'de' : "PR1 Einer der Männer" }, 'urlPartClass' : 'pr1-mens-single-sculls' },
    'PR1 W1x' : { 'short' : 'PR1 W1x', 'item' : 'Q47509011', 'labels' : { 'en' : "PR1 women's single scull", 'de' : "PR1 Einer der Frauen" }, 'urlPartClass' : 'pr1-womens-single-sculls' },
    'PR2 M1x' : { 'short' : 'PR2 M1x', 'item' : 'Q47509017', 'labels' : { 'en' : "PR2 men's single scull", 'de' : "PR2 Einer der Männer" }, 'urlPartClass' : 'pr2-mens-single-sculls' },
    'PR2 Mix1x' : { 'short' : 'PR2 Mix1x', 'item' : 'Q47509013', 'labels' : { 'en' : "PR2 mixed single scull", 'de' : "PR2 Mixed-Einer" }, 'urlPartClass' : '' },
    'PR2 Mix2x' : { 'short' : 'PR2 Mix2x', 'item' : 'Q47509024', 'labels' : { 'en' : "PR2 mixed double scull", 'de' : "PR2 Mixed-Doppelzweier" }, 'urlPartClass' : 'pr2-mixed-double-sculls' },
    'PR2 W1x' : { 'short' : 'PR2 W1x', 'item' : 'Q47509020', 'labels' : { 'en' : "PR2 women's single scull", 'de' : "PR2 Einer der Frauen" }, 'urlPartClass' : 'pr2-womens-single-sculls' },
    'PR3 M2-' : { 'short' : 'PR3 M2-', 'item' : 'Q47509028', 'labels' : { 'en' : "PR3 men's coxless pair", 'de' : "PR3 Zweier ohne Steuermann" }, 'urlPartClass' : 'pr3-mens-pair' },
    'PR3 M4+' : { 'short' : 'PR3 M4+', 'item' : 'Q47509036', 'labels' : { 'en' : "PR3 men's coxed four", 'de' : "PR3 Vierer mit Steuermann" }, 'urlPartClass' : '' },
    'PR3 M4x+' : { 'short' : 'PR3 M4x+', 'item' : 'Q47509038', 'labels' : { 'en' : "PR3 men's coxed quad scull", 'de' : "PR3 Doppelvierer mit Steuermann" }, 'urlPartClass' : '' },
    'PR3 Mix2x' : { 'short' : 'PR3 Mix2x', 'item' : 'Q47509026', 'labels' : { 'en' : "PR3 mixed double scull", 'de' : "PR3 Mixed-Doppelzweier" }, 'urlPartClass' : 'pr3-mixed-double-sculls' },
    'PR3 Mix4+' : { 'short' : 'PR3 Mix4+', 'item' : 'Q47509031', 'labels' : { 'en' : "PR3 mixed coxed four", 'de' : "PR3 Mixed-Vierer mit Steuermann" }, 'urlPartClass' : 'pr3-mixed-coxed-four' },
    'PR3 Mix4x+' : { 'short' : 'PR3 Mix4x+', 'item' : 'Q47509034', 'labels' : { 'en' : "PR3 mixed coxed quad scull", 'de' : "PR3 Mixed-Doppelvierer mit Steuermann" }, 'urlPartClass' : '' },
    'PR3 W2-' : { 'short' : 'PR3 W2-', 'item' : 'Q47509030', 'labels' : { 'en' : "PR3 women's coxless pair", 'de' : "PR3 Zweier ohne Steuerfrau" }, 'urlPartClass' : 'pr3-womens-pair' },
    'ID Mix4+' : { 'short' : 'ID Mix4+', 'item' : 'Q47509040', 'labels' : { 'en' : "ID mixed coxed four", 'de' : "ID Mixed-Vierer mit Steuermann" }, 'urlPartClass' : '' },
}

#### parameters for data retrieval via WDQS ####
sparqlHeaders = {
    'User-Agent': '{} (Wikidata bot by User:MisterSynergy; mailto:mister.synergy@yahoo.com)'.format(requests.utils.default_headers()['User-Agent'])
}
sparqlEndpoint = 'https://query.wikidata.org/sparql'
sparqlQueryLabels = """SELECT ?label ?lang WHERE {{
  wd:{} rdfs:label ?label .
  BIND(LANG(?label) AS ?lang) .
  FILTER(?lang IN ('en', 'de')) .
}}"""
sparqlQueryUrl = """SELECT ?url ?part WHERE {{
  wd:{} wdt:P973 ?url .
  FILTER(CONTAINS(STR(?url), 'worldrowing.com')) .
  BIND(STRBEFORE(STRAFTER(STR(?url), 'events/'), '/') AS ?part) .
}}"""
sparqlQueryYear = """SELECT ?year WHERE {{
  wd:{} wdt:P585 ?time .
  BIND(YEAR(?time) AS ?year) .
}}"""
sparqlQueryParts = """SELECT ?regatta ?event WHERE {{
  BIND(wd:{} AS ?regatta) .
  ?event wdt:P361 ?regatta .
  MINUS {{ ?regatta wdt:P527 ?event }}
}}"""
sparqlQueryAntecessors = """SELECT ?antecessor ?event WHERE {{
  BIND(wd:{} AS ?regatta) .
  ?event wdt:P361 ?regatta; wdt:P155 ?antecessor .
  MINUS {{ ?antecessor wdt:P156 ?event }}
}}"""
sparqlQueryListParts = """SELECT DISTINCT ?part WHERE {{
  ?part wdt:P361 wd:{} .
}}"""

r_labels = requests.get(
    sparqlEndpoint,
    params={
        'format' : 'json',
        'query' : sparqlQueryLabels.format(regattaItem)
    },
    headers=sparqlHeaders
)
if r_labels.status_code == 200:
    labels = {}
    for row in json.loads(r_labels.text)['results']['bindings']:
        label = row['label']['value']
        lang = row['lang']['value']
        labels[lang] = label
    print(labels)
    for language in languages:
        if language in labels:
            terms['labels'][language] = labels[language]

r_url = requests.get(
    sparqlEndpoint,
    params={
        'format' : 'json',
        'query' : sparqlQueryUrl.format(regattaItem)
    },
    headers=sparqlHeaders
)
if r_url.status_code == 200:
    fullUrl = None
    for row in json.loads(r_url.text)['results']['bindings']:
        fullUrl = row['url']['value']
        urlPartEvent = row['part']['value']
    if withFisaWeblink==True and fullUrl!=None:
        worldrowing_fullUrl = requests.head(
            fullUrl,
            headers = {
                'User-Agent' : 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:67.0) Gecko/20100101 Firefox/67.0'
            }
        )
        print('"{}" ({}) and "{}"'.format(fullUrl, worldrowing_fullUrl.status_code, urlPartEvent))

r_year = requests.get(
    sparqlEndpoint,
    params={
        'format' : 'json',
        'query' : sparqlQueryYear.format(regattaItem)
    },
    headers=sparqlHeaders
)
if r_year.status_code == 200:
    for row in json.loads(r_year.text)['results']['bindings']:
        year = row['year']['value']
    print('Year: {}'.format(year))
    print()

if len(terms['labels']) == 0:
    print('Error: did not find suitable labels; stopping')
    sys.exit(1)
if withFisaWeblink==True and worldrowing_fullUrl.status_code not in [ 200, 307 ]:
    print('Error: invalid FISA weblink; stopping')
    sys.exit(1)
    
if chooseFunction=='create':
    for event in events:
        print('CREATE')
        for language in languages:
            if language in competitionClassData[event]['labels'] and language in terms['labels']:
                print('LAST\tL{}\t"{}{}"'.format(language, terms['labels'][language], competitionClassData[event]['labels'][language]))
            if language in terms['descriptionsForEvents']:
                print('LAST\tD{}\t"{}"'.format(language, terms['descriptionsForEvents'][language]))
        print('LAST\tP31\tQ21246076') # instance of: rowing event
        print('LAST\tP361\t{}'.format(regattaItem)) # part of: rowing competition
        #print('{}\tP527\tLAST'.format(regattaItem)) # rowing competition: has part: rowing event
        print('LAST\tP585\t+{}-01-01T00:00:00Z/9'.format(year))
        print('LAST\tP2094\t{}'.format(competitionClassData[event]['item'])) # competition class
        if eventDistance!=None:
            print('LAST\tP3157\t{}U11573'.format(eventDistance)) # event distance
        if withFisaWeblink==True and urlPartEvent!=None and 'urlPartClass' in competitionClassData[event] and competitionClassData[event]['urlPartClass'] != '':
            print('LAST\tP973\t"{}"'.format(url.format(urlPartEvent, competitionClassData[event]['urlPartClass']))) # described at URL
        
        antecessor_query = "SELECT ?item WHERE {{ wd:{} wdt:P155/wdt:P527 ?item . ?item wdt:P2094/wdt:P1813 '{}'@en . }}".format(regattaItem, event)
        antecessors = []
        for itemkey in pagegenerators.WikidataSPARQLPageGenerator(antecessor_query, site=wikidata_site):
            antecessors.append(itemkey.title())
        if len(antecessors)==1:
            print('LAST\tP155\t{}'.format(antecessors[0]))
            #print('{}\tP156\tLAST'.format(antecessors[0]))
            pass
        time.sleep(1)
elif chooseFunction=='complement':
    r_parts = requests.get(
        sparqlEndpoint,
        params={
            'format' : 'json',
            'query' : sparqlQueryParts.format(regattaItem)
        },
        headers=sparqlHeaders
    )
    if r_parts.status_code == 200:
        for row in json.loads(r_parts.text)['results']['bindings']:
            print('{}\tP527\t{}'.format(row['regatta']['value'][31:], row['event']['value'][31:]))
    
    r_antecessors = requests.get(
        sparqlEndpoint,
        params={
            'format' : 'json',
            'query' : sparqlQueryAntecessors.format(regattaItem)
        },
        headers=sparqlHeaders
    )
    if r_antecessors.status_code == 200:
        for row in json.loads(r_antecessors.text)['results']['bindings']:
            print('{}\tP156\t{}'.format(row['antecessor']['value'][31:], row['event']['value'][31:]))
elif chooseFunction == 'watchlist':
    r_listparts = requests.get(
        sparqlEndpoint,
        params={
            'format' : 'json',
            'query' : sparqlQueryListParts.format(regattaItem)
        },
        headers=sparqlHeaders
    )
    if r_parts.status_code == 200:
        for row in json.loads(r_listparts.text)['results']['bindings']:
            print('{}'.format(row['part']['value'][31:]))
    
        print()
        print('https://www.wikidata.org/wiki/Special:EditWatchlist/raw')
{'en': 'Rowing at the 2019 African Games', 'de': 'Ruderregatta der Afrikaspiele 2019'}
Year: 2019

Q66124513	P527	Q67460542
Q66124513	P527	Q67460543
Q66124513	P527	Q67460541
Q66124513	P527	Q67460546
Q66124513	P527	Q67460544
Q66124513	P527	Q67460550
Q66124513	P527	Q67460548
Q66124513	P527	Q67460549
Q66124513	P527	Q67460552
Q67442958	P156	Q67460546
Q67442966	P156	Q67460550
Q67442970	P156	Q67460549
Q67442969	P156	Q67460548