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

import requests
import json
import urllib.parse
import time

language = 'en' # Wikimedia language code
category = 'Rowers'
year = 2012

sparql = """SELECT ?event ?class ?games ?name ?regatta WHERE {{
  #BIND(wd:Q9375319 AS ?event) .
  BIND(<https://{}.wikipedia.org/> AS ?project) .
  ?event wdt:P31 wd:Q21246076 , wd:Q18536594 .
  ?event wdt:P361 ?regatta .
  ?regatta wdt:P361 ?games .
  ?event wdt:P2094/wdt:P1813 ?class .
  ?games wdt:P31 wd:Q159821 .
  ?event wdt:P585 ?pointInTime .
  #FILTER(YEAR(?pointInTime) = {}) .
  ?wiki schema:about ?event; schema:isPartOf ?project; schema:name ?name.
}}""".format(language, year)

endpoint = 'https://query.wikidata.org/sparql?query={}'
headers = { 'Accept':'application/sparql-results+json', 'User-Agent': '{} (Wikidata bot by User:MisterSynergy; mailto:mister.synergy@yahoo.com)'.format(requests.utils.default_headers()['User-Agent']) }
wait = 1

templates = {
    "M1x" : "Template:Olympic champions – Men's single sculls",
    "M2x" : "Template:Olympic champions – Men's double sculls",
    "M4x" : "Template:Olympic champions – Men's quadruple sculls",
    "M2-" : "Template:Olympic champions – Men's coxless pair",
    "M2+" : "Template:Olympic champions – Men's coxed pair",
    "M4-" : "Template:Olympic champions – Men's coxless four",
    "M4+" : "Template:Olympic champions – Men's coxed four",
    "M8+" : "Template:Olympic champions – Men's eight",
    "LM2x" : "Template:Olympic champions – Men's lightweight double sculls",
    "W1x" : "Template:Olympic champions – Women's single sculls",
    "W2x" : "Template:Olympic champions – Women's double sculls",
    "W4x" : "Template:Olympic champions – Women's quadruple sculls",
    "W4x+" : "Template:Olympic champions – Women's quadruple sculls",
    "W2-" : "Template:Olympic champions – Women's coxless pair",
    "W8+" : "Template:Olympic champions – Women's eight",
    "LW2x" : "Template:Olympic champions – Women's lightweight double sculls",
}

petscan1 = """https://petscan.wmflabs.org/?language={}&project=wikipedia&depth=10&categories={}&ns%5B0%5D=1&outlinks_any={}&sparql=SELECT%20DISTINCT%20%3Fitem%20WHERE%20%7B%20%3Fitem%20p%3AP106%2Fps%3AP106%2Fwdt%3AP279*%20wd%3AQ26270618%3B%20p%3AP1344%2Fps%3AP1344%20wd%3A{}%20%7D&search_max_results=500&source_combination=categories%20and%20sparql&format=tsv&doit="""
petscan2 = """https://petscan.wmflabs.org/?language={}&project=wikipedia&depth=10&categories={}&ns%5B0%5D=1&outlinks_any={}&outlinks_no={}&search_max_results=500&common_wiki=wikidata&wikidata_prop_item_use=P1344&wpiu=none&format=tsv&doit="""
petscan3 = """https://petscan.wmflabs.org/?language={}&project=wikipedia&depth=10&categories={}&ns%5B0%5D=1&outlinks_any={}&outlinks_no={}&sparql=SELECT%20%3Fitem%20WHERE%20%7B%20%3Fitem%20p%3AP106%2Fps%3AP106%2Fwdt%3AP279*%20wd%3AQ26270618%20.%20MINUS%20%7B%20%3Fitem%20wdt%3AP1344%20wd%3A{}%20%7D%20%7D&source_combination=categories%20and%20sparql&search_max_results=500&common_wiki=wikidata&format=tsv&doit="""

raw_data = json.loads(requests.get(endpoint.format(urllib.parse.quote_plus(sparql)), headers=headers).text)

#print(raw_data)

for result in raw_data['results']['bindings']:
    games = result['games']['value'][31:]
    event = result['event']['value'][31:]
    cclass = result['class']['value']
    name = result['name']['value'].replace('–', '–')
    regatta = result['regatta']['value'][31:]
    #print('{} --- {} --- {} --- {}'.format(event, cclass, name, games, regatta))
    
    workitem = games # games or regatta
    
    if cclass in templates:
        notemplate = urllib.parse.quote_plus(templates[cclass])
    else:
        notemplate = ''
    
    if False:
        p1 = requests.get(petscan1.format(language, category, urllib.parse.quote_plus(name), workitem), headers=headers)
        if p1.status_code == 200:
            #print(petscan1.format(language, category, urllib.parse.quote_plus(name), workitem))
            lines = p1.text.split("\n")
            #print(lines)
            for i, line in enumerate(lines):
                if i==0:
                    continue
                if line=='':
                    continue
                num, qid, pid, ns, length, touched = line.strip().split("\t")
                print('-{}\tP1344\t{}'.format(qid, workitem))
                print('{}\tP1344\t{}'.format(qid, event))
            
        time.sleep(wait)

    if False:
        p2 = requests.get(petscan2.format(language, category, urllib.parse.quote_plus(name), notemplate), headers=headers)
        if p2.status_code == 200:
            #print(petscan2.format(language, category, urllib.parse.quote_plus(name), notemplate))
            lines = p2.text.split("\n")
            #print(lines)
            for i, line in enumerate(lines):
                if i==0:
                    continue
                if line=='':
                    continue
                num, qid, pid, ns, length, touched = line.strip().split("\t")
                print('{}\tP1344\t{}'.format(qid, event))
            
        time.sleep(wait)
    
    if True:
        p3 = requests.get(petscan3.format(language, category, urllib.parse.quote_plus(name), notemplate, event), headers=headers)
        if p3.status_code == 200:
            #print(petscan3.format(language, category, urllib.parse.quote_plus(name), notemplate, event))
            lines = p3.text.split("\n")
            #print(lines)
            for i, line in enumerate(lines):
                if i==0:
                    continue
                if line=='':
                    continue
                num, qid, pid, ns, length, touched = line.strip().split("\t")
                print('{}\tP1344\t{}'.format(qid, event))
            
        time.sleep(wait)

print('Job finished, all done')
Q2448534	P1344	Q7371948
Q1388569	P1344	Q7371981
Q23655835	P1344	Q7371981
Q56222477	P1344	Q7371981
Q85960	P1344	Q7371982
Q47470802	P1344	Q7371982
Q3426134	P1344	Q7371983
Q1250727	P1344	Q7371984
Q44710085	P1344	Q7371984
Q47469976	P1344	Q7371984
Q1729837	P1344	Q7371980
Q47456823	P1344	Q7371980
Q47481460	P1344	Q7371979
Q56222568	P1344	Q7371979
Q56222571	P1344	Q7371979
Q56254458	P1344	Q7371979
Q56254461	P1344	Q7371979
Q1889907	P1344	Q22968940
Q6847693	P1344	Q22968940
Q47467449	P1344	Q22968940
Q47467664	P1344	Q22968940
Q47468195	P1344	Q22968940
Q47482014	P1344	Q22968940
Q56222679	P1344	Q18391268
Q871368	P1344	Q22968939
Q47470388	P1344	Q22968939
Q47481460	P1344	Q22968939
Q56223530	P1344	Q22968939
Q56254502	P1344	Q22968939
Q47468224	P1344	Q22968937
Q47469161	P1344	Q22968937
Q47481907	P1344	Q22968937
Q47465796	P1344	Q22968936
Q56222715	P1344	Q22968936
Q47470698	P1344	Q22968938
Q47470802	P1344	Q22968938
Q47581902	P1344	Q22968938
Q1812425	P1344	Q7371957
Q3059797	P1344	Q7371957
Q54935475	P1344	Q16536288
Q56253337	P1344	Q16536293
Q47471911	P1344	Q17502505
Q56253460	P1344	Q17502505
Q2046075	P1344	Q17623680
Q13471286	P1344	Q16536298
Q18507176	P1344	Q17623689
Q56043318	P1344	Q17623689
Q55979229	P1344	Q17623677
Q55958219	P1344	Q17623696
Q56253600	P1344	Q17623696
Q56253603	P1344	Q17623696
Q55958219	P1344	Q17623693
Q55978825	P1344	Q17623693
Q55979229	P1344	Q17623693
Q50859744	P1344	Q21282700
Q50851576	P1344	Q18661272
Q4352396	P1344	Q18661271
Q5317610	P1344	Q18661271
Q3440967	P1344	Q18661273
Q19873721	P1344	Q18661273
Q49105272	P1344	Q18661273
Q50852648	P1344	Q18661273
Q50852651	P1344	Q18661273
Q47469905	P1344	Q21666211
Q47470533	P1344	Q21666211
Q47471240	P1344	Q21666211
Q55958219	P1344	Q20642503
Q56065685	P1344	Q20642503
Q7114350	P1344	Q22968931
Q47471670	P1344	Q22968931
Q56065128	P1344	Q22968930
Q56065130	P1344	Q22968930
Q3440967	P1344	Q22968929
Q56254147	P1344	Q22968929
Q3817495	P1344	Q22968932
Q6873972	P1344	Q22968932
Q7114350	P1344	Q22968932
Q9324017	P1344	Q22968932
Q22959333	P1344	Q22968932
Q22959334	P1344	Q22968932
Q23682745	P1344	Q22968932
Q47471240	P1344	Q22968932
Q50095699	P1344	Q22968932
Q56087063	P1344	Q22968933
Q56087065	P1344	Q22968933
Q56254386	P1344	Q22968933
Q5689018	P1344	Q22964434
Q6375789	P1344	Q22964434
Q8071037	P1344	Q22964434
Q47333926	P1344	Q22964434
Q47463085	P1344	Q22964434
Q47466317	P1344	Q22964434
Q47466458	P1344	Q22964434
Q47482418	P1344	Q22964434
Q47482467	P1344	Q22964434
Q47491438	P1344	Q22964434
Q47492239	P1344	Q22964434
Q47481460	P1344	Q22964444
Q21173964	P1344	Q22964441
Q47334095	P1344	Q22964441
Q4684999	P1344	Q22964446
Q47491335	P1344	Q22964446
Q533259	P1344	Q22964437
Q2231989	P1344	Q22964437
Q47463041	P1344	Q22964432
Q47463046	P1344	Q22964432
Q47466442	P1344	Q22964432
Q47500799	P1344	Q22964432
Q47500934	P1344	Q22964432
Q47500946	P1344	Q22964432
Q5034305	P1344	Q22964439
Q5689018	P1344	Q22964439
Q8071037	P1344	Q22964439
Q12743534	P1344	Q22964439
Q47333926	P1344	Q22964439
Q47412616	P1344	Q22964439
Q47431717	P1344	Q22964439
Q47434314	P1344	Q22964439
Q47460455	P1344	Q22964439
Q47460479	P1344	Q22964439
Q47460483	P1344	Q22964439
Q47467176	P1344	Q22964439
Q47467430	P1344	Q22964439
Q47467650	P1344	Q22964439
Q47467731	P1344	Q22964439
Q47468205	P1344	Q22964439
Q47471317	P1344	Q22964439
Q47471447	P1344	Q22964439
Q47471496	P1344	Q22964439
Q47482413	P1344	Q22964439
Q47482429	P1344	Q22964439
Q47482455	P1344	Q22964439
Q47492071	P1344	Q22964439
Q58494	P1344	Q22964435
Q4498176	P1344	Q22964435
Q6480436	P1344	Q22964435
Q47460315	P1344	Q22964435
Q47460396	P1344	Q22964435
Q47463076	P1344	Q22964435
Q47481996	P1344	Q22964435
Q175347	P1344	Q22073342
Q12630285	P1344	Q4059153
Q12645743	P1344	Q4059153
Q7179175	P1344	Q4059155
Q21535017	P1344	Q4059155
Q56282532	P1344	Q4059152
Q56418594	P1344	Q4059152
Q746810	P1344	Q22964416
Q4576905	P1344	Q17515789
Q1152837	P1344	Q18391270
Q21534864	P1344	Q22964413
Q21534920	P1344	Q22964413
Q21535017	P1344	Q22964413
Q21535050	P1344	Q22964413
Q478308	P1344	Q22964412
Q830003	P1344	Q22964412
Q275580	P1344	Q22964423
Q276155	P1344	Q22964408
Q4776350	P1344	Q22964408
Q6069797	P1344	Q22964408
Q7934278	P1344	Q22964408
Q56478946	P1344	Q22964408
Q12645743	P1344	Q22964415
Q47482195	P1344	Q22964415
Q3438281	P1344	Q22964425
Q3474892	P1344	Q22964429
Q2554899	P1344	Q22964428
Q379430	P1344	Q22964431
Q12645743	P1344	Q22964426
Q21535050	P1344	Q22964426
Q1056206	P1344	Q22964424
Q7608332	P1344	Q22964424
Q1681200	P1344	Q22968984
Q498951	P1344	Q17515790
Q47482539	P1344	Q17515791
Q47491034	P1344	Q17515791
Q47491172	P1344	Q17515791
Q47492147	P1344	Q17515791
Q47492179	P1344	Q17515791
Q47500855	P1344	Q17515791
Q7174526	P1344	Q22964458
Q7627008	P1344	Q22964458
Q47469682	P1344	Q22964455
Q4917146	P1344	Q22964452
Q3438242	P1344	Q7371987
Q7360687	P1344	Q7371987
Q7938827	P1344	Q7371987
Q1770732	P1344	Q22964403
Q47466756	P1344	Q22964403
Q47471191	P1344	Q22964403
Q56191631	P1344	Q22964403
Q56223598	P1344	Q22964403
Q3921826	P1344	Q7371988
Q12792789	P1344	Q7371988
Q830600	P1344	Q7371989
Q56226291	P1344	Q7371989
Q56236144	P1344	Q7371989
Q56222679	P1344	Q7371990
Q21063782	P1344	Q22964399
Q56154248	P1344	Q17508956
Q8074378	P1344	Q22964397
Q12630285	P1344	Q22964402
Q4308547	P1344	Q1222270
Q8334546	P1344	Q1005683
Q18026939	P1344	Q1005683
Q19371859	P1344	Q1005683
Q19382945	P1344	Q1005683
Q2341477	P1344	Q1068773
Q5633528	P1344	Q1068773
Q5633652	P1344	Q1068773
Q177039	P1344	Q22968981
Q27074050	P1344	Q22968981
Q14949521	P1344	Q22968972
Q527929	P1344	Q22968976
Q9369987	P1344	Q634029
Q9248185	P1344	Q2322860
Q11816180	P1344	Q685266
Q3473444	P1344	Q1222543
Q9391694	P1344	Q1222549
Q9335945	P1344	Q2455198
Q785494	P1344	Q2731998
Q9334690	P1344	Q1005506
Q9368819	P1344	Q1005506
Q9369987	P1344	Q1005506
Q19376056	P1344	Q1005506
Q19400574	P1344	Q1005506
Q2438496	P1344	Q536380
Q1736508	P1344	Q370500
Q5052007	P1344	Q7372004
Q47412616	P1344	Q7372004
Q27064161	P1344	Q7372009
Q3435519	P1344	Q3655566
Q9202600	P1344	Q3655566
Q6503049	P1344	Q3655573
Q2088791	P1344	Q7372001
Q2088797	P1344	Q7372001
Q2591427	P1344	Q7372001
Q2615499	P1344	Q7372001
Q2409934	P1344	Q7372003
Q104230	P1344	Q3655569
Q1755028	P1344	Q3655569
Q18026939	P1344	Q3655569
Q2591427	P1344	Q3655567
Q827201	P1344	Q7371997
Q7289897	P1344	Q7371997
Q452629	P1344	Q7372007
Q3473444	P1344	Q7372007
Q6306851	P1344	Q16968702
Q533885	P1344	Q3655568
Q47466685	P1344	Q3655568
Q126119	P1344	Q735286
Q3438190	P1344	Q7371998
Q3438236	P1344	Q7371998
Q1369693	P1344	Q7371999
Q1819384	P1344	Q7371999
Q2387686	P1344	Q7372000
Q2611870	P1344	Q7372000
Q3525201	P1344	Q7372000
Q4027368	P1344	Q7372000
Q498951	P1344	Q7372002
Job finished, all done