#!/usr/bin/python3

import pywikibot
from pywikibot import pagegenerators as pg
import requests

def get_snl_copyright(snl_id):
    url = "https://snl.no/{}.json".format(snl_id)
    r = requests.get(url)
    text = r.json()
    return text.get("license_name")

def generate_qs_free_article(qid, snl_id):
    license_p = "P275" 
    license_q = "Q63340742"
    snl_id = '"' + snl_id + '"'
    return "{}|P4342|{}|{}|{}".format(qid,
                                      snl_id,
                                      license_p,
                                      license_q)

with open('snl_no_license.rq', 'r') as query_file:
    QUERY = query_file.read().replace('\n', '')

wikidata_site = pywikibot.Site("wikidata", "wikidata")
generator = pg.WikidataSPARQLPageGenerator(QUERY, site=wikidata_site)



for x in generator:
    item_dict = x.get()

    clm_dict = item_dict["claims"]
    clm_list = clm_dict["P4342"]
    for clm in clm_list:
        snl_id = clm.getTarget()
        if get_snl_copyright(snl_id) == "fri":
            print(generate_qs_free_article(x.getID(), snl_id))
Q19974732|P4342|"Vilhelm_Tveteraas"|P275|Q63340742
Q14753326|P4342|"Theodor_Madsen"|P275|Q63340742
Q48844|P4342|"Sortland"|P275|Q63340742
Q11963793|P4342|"Christopher_Blix_Hammer"|P275|Q63340742
Q63323802|P4342|"Emil_L._Mengshoel"|P275|Q63340742
Q16009599|P4342|"Joakim_Lund_Ihlen"|P275|Q63340742
Q11988908|P4342|"meis_-_mengdeenhet"|P275|Q63340742
Q502912|P4342|"John_Bagnell_Bury"|P275|Q63340742
Q609391|P4342|"Ants_Antson"|P275|Q63340742
Q1395112|P4342|"Helge_Krog"|P275|Q63340742
Q3136605|P4342|"Orléans_-_historie"|P275|Q63340742
Q1777507|P4342|"femtosekund"|P275|Q63340742
Q7790380|P4342|"Thomas_Hammond"|P275|Q63340742
Q980|P4342|"Bayern"|P275|Q63340742
Q7323740|P4342|"Richard_Andvord"|P275|Q63340742
Q12001534|P4342|"Smaalenenes_amt"|P275|Q63340742
Q4590933|P4342|"Anne-Lisa_Amadou"|P275|Q63340742
Q3303830|P4342|"Erling_Sven_Lorentzen"|P275|Q63340742
Q16234318|P4342|"Kristian_Støback_Wilhelmsen"|P275|Q63340742
Q7448610|P4342|"Sellebakk"|P275|Q63340742
Q1431513|P4342|"Honningsvåg_lufthavn,_Valan"|P275|Q63340742
Q1998762|P4342|"Nordstrand_-_Oslo"|P275|Q63340742
Q126084|P4342|"Acre_-_by_i_Israel"|P275|Q63340742
Q256763|P4342|"Anne_Holt"|P275|Q63340742
Q173412|P4342|"radiologisk_datering"|P275|Q63340742
Q2278042|P4342|"Sigurd_Evensmo"|P275|Q63340742
Q202943|P4342|"Brahmagupta"|P275|Q63340742
Q23008449|P4342|"Marit_Bolling"|P275|Q63340742
Q1580010|P4342|"Hans_Hagerup_Falbe"|P275|Q63340742
Q17130896|P4342|"Niels_Nielsen_Thaaning"|P275|Q63340742
Q11956767|P4342|"ab_absurdo"|P275|Q63340742
Q1772113|P4342|"Fred_Kavli"|P275|Q63340742
Q4588795|P4342|"Karen_Elisabeth_Grude_Koht"|P275|Q63340742
Q17195623|P4342|"Lars_Vivelid"|P275|Q63340742
Q6160365|P4342|"Harald_Christian_Pedersen"|P275|Q63340742
Q421471|P4342|"Aksel_Hennie"|P275|Q63340742
Q4948262|P4342|"Helga_Flatland"|P275|Q63340742
Q1770895|P4342|"Hill-Marta_Solberg"|P275|Q63340742
Q3435963|P4342|"Niels_Krog_Bredal"|P275|Q63340742
Q4794679|P4342|"Arne_Sunde"|P275|Q63340742
Q543738|P4342|"Aud_Schønemann"|P275|Q63340742
Q639065|P4342|"Stephen_Greenblatt"|P275|Q63340742
Q2468|P4342|"hydrogenfluorid"|P275|Q63340742
Q5980291|P4342|"Iacob_Dybwad_Sømme"|P275|Q63340742
Q157793|P4342|"Christian_5"|P275|Q63340742
Q832214|P4342|"Sámuel_Gyarmathi"|P275|Q63340742
Q7606778|P4342|"Stein_Reegård"|P275|Q63340742
Q640991|P4342|"Gustaf_Retzius"|P275|Q63340742
Q11961823|P4342|"bolle_-_volumenhet"|P275|Q63340742
Q980235|P4342|"Malthe_Conrad_Bruun"|P275|Q63340742
Q16905889|P4342|"Nils_Werenskiold"|P275|Q63340742
Q7106259|P4342|"Oscar_Strugstad_-_den_eldre"|P275|Q63340742
Q5697531|P4342|"Domkirkeodden_-_museum"|P275|Q63340742
Q4585238|P4342|"Carl_Emil_Christian_Bonnevie"|P275|Q63340742
Q48897|P4342|"Evenes"|P275|Q63340742
Q4584829|P4342|"Ole_Rømer_Aagaard_Sandberg"|P275|Q63340742
Q282077|P4342|"Elisabeth_Aspaker"|P275|Q63340742
Q5744652|P4342|"Nicolaus_Christian_Friis"|P275|Q63340742
Q4993927|P4342|"Eli_Arnstad"|P275|Q63340742
Q4570007|P4342|"Knut_Møyen"|P275|Q63340742
Q4588478|P4342|"Edøy"|P275|Q63340742
Q131355|P4342|"Zulfikar_Ali_Bhutto"|P275|Q63340742
Q11974693|P4342|"Heidi_Nordby_Lunde"|P275|Q63340742
Q16170263|P4342|"Zacharias_Backer"|P275|Q63340742
Q4560141|P4342|"Signe_Øye"|P275|Q63340742
Q181989|P4342|"småmaneter"|P275|Q63340742
Q11957999|P4342|"Andreas_Georg_Nordvi"|P275|Q63340742
Q1809560|P4342|"Hans_Rasmus_Astrup_-_politiker"|P275|Q63340742
Q19859320|P4342|"Carl_Christian_August_Michalsen"|P275|Q63340742
Q7083228|P4342|"Olav_Terje_Bergo"|P275|Q63340742
Q4112212|P4342|"inklinasjon_-_astronomi"|P275|Q63340742
Q1921|P4342|"Blaise_Matuidi"|P275|Q63340742
---------------------------------------------------------------------------
JSONDecodeError                           Traceback (most recent call last)
<ipython-input-1-480c55409dde> in <module>()
     35     for clm in clm_list:
     36         snl_id = clm.getTarget()
---> 37         if get_snl_copyright(snl_id) == "fri":
     38             print(generate_qs_free_article(x.getID(), snl_id))

<ipython-input-1-480c55409dde> in get_snl_copyright(snl_id)
      8     url = "https://snl.no/{}.json".format(snl_id)
      9     r = requests.get(url)
---> 10     text = r.json()
     11     return text.get("license_name")
     12 

/srv/paws/lib/python3.6/site-packages/requests/models.py in json(self, **kwargs)
    894                     # used.
    895                     pass
--> 896         return complexjson.loads(self.text, **kwargs)
    897 
    898     @property

/usr/lib/python3.6/json/__init__.py in loads(s, encoding, cls, object_hook, parse_float, parse_int, parse_constant, object_pairs_hook, **kw)
    352             parse_int is None and parse_float is None and
    353             parse_constant is None and object_pairs_hook is None and not kw):
--> 354         return _default_decoder.decode(s)
    355     if cls is None:
    356         cls = JSONDecoder

/usr/lib/python3.6/json/decoder.py in decode(self, s, _w)
    337 
    338         """
--> 339         obj, end = self.raw_decode(s, idx=_w(s, 0).end())
    340         end = _w(s, end).end()
    341         if end != len(s):

/usr/lib/python3.6/json/decoder.py in raw_decode(self, s, idx)
    355             obj, end = self.scan_once(s, idx)
    356         except StopIteration as err:
--> 357             raise JSONDecodeError("Expecting value", s, err.value) from None
    358         return obj, end

JSONDecodeError: Expecting value: line 1 column 1 (char 0)