import pandas as pd
import pywikibot as pb

site = pb.Site("wikidata", "wikidata")
repo = site.data_repository()
commons_site = pb.Site("commons", "commons")
!pip install xlrd
red_list_df = pd.read_excel("listaroja4.xlsx")
red_list_df = red_list_df[['Nombre', 'Artículo en español', 'Wikidata ID',
                           'Latitud', 'Longitud', 'Localidad', 
                           'Localidad ID', 'Municipio', 'Municipio ID', 'Provincia', 'Comunidad autónoma',
                           'Tipo Wikidata', 'Fecha de inclusión', 'Fecha de retirada', 'estatus', 
                           'URL', 'Categoría', 'Imagen']]
def assign_color (value):
    if value == "verde":
        return "008000"
    elif value == "negra":
        return "A9A9A9"
    else :
        return "800000"
red_list_df["color"] = red_list_df["estatus"].map(assign_color)
red_list_df.columns = ["name", "article", "wikidata_id", "lat", "lon", 
                       "location", "location_id", "municipality", "municipality_id", "province", "autonomous_comm",
                       "wikidata_instance", "start_date", "end_date", "status", "url", "category", "image", "color"]
import pywikibot
site = pywikibot.Site("en", "wikipedia")
page = pywikibot.Page(site, u"Douglas Adams")
item = pywikibot.ItemPage.fromPage(page)
dictionary = item.get()
print(dictionary)
print(dictionary.keys())
print(item)
WARNING: API error mwoauth-invalid-authorization-invalid-user: The authorization headers in your request are for a user that does not exist here
---------------------------------------------------------------------------
NoUsername                                Traceback (most recent call last)
<ipython-input-1-c03a7cf185c4> in <module>
      1 import pywikibot
      2 site = pywikibot.Site("en", "wikipedia")
----> 3 page = pywikibot.Page(site, u"Douglas Adams")
      4 item = pywikibot.ItemPage.fromPage(page)
      5 dictionary = item.get()

/srv/paws/pwb/pywikibot/tools/__init__.py in wrapper(*__args, **__kw)
   1736                              cls, depth)
   1737                     del __kw[old_arg]
-> 1738             return obj(*__args, **__kw)
   1739 
   1740         if not __debug__:

/srv/paws/pwb/pywikibot/page.py in __init__(self, source, title, ns)
   2299                 raise ValueError('Title must be specified and not empty '
   2300                                  'if source is a Site.')
-> 2301         super(Page, self).__init__(source, title, ns)
   2302 
   2303     @property

/srv/paws/pwb/pywikibot/page.py in __init__(self, source, title, ns)
    192 
    193         if isinstance(source, pywikibot.site.BaseSite):
--> 194             self._link = Link(title, source=source, default_namespace=ns)
    195             self._revisions = {}
    196         elif isinstance(source, Page):

/srv/paws/pwb/pywikibot/tools/__init__.py in wrapper(*__args, **__kw)
   1736                              cls, depth)
   1737                     del __kw[old_arg]
-> 1738             return obj(*__args, **__kw)
   1739 
   1740         if not __debug__:

/srv/paws/pwb/pywikibot/page.py in __init__(self, text, source, default_namespace)
   5563         # See bug T104864, default_namespace might have been deleted.
   5564         try:
-> 5565             self._defaultns = self._source.namespaces[default_namespace]
   5566         except KeyError:
   5567             self._defaultns = default_namespace

/srv/paws/pwb/pywikibot/site.py in namespaces(self)
   1018         """Return dict of valid namespaces on this wiki."""
   1019         if not hasattr(self, '_namespaces'):
-> 1020             self._namespaces = NamespacesDict(self._build_namespaces())
   1021         return self._namespaces
   1022 

/srv/paws/pwb/pywikibot/site.py in _build_namespaces(self)
   2638         _namespaces = {}
   2639 
-> 2640         for nsdata in self.siteinfo.get('namespaces', cache=False).values():
   2641             ns = nsdata.pop('id')
   2642             custom_name = None

/srv/paws/pwb/pywikibot/site.py in get(self, key, get_default, cache, expiry)
   1668                 elif not Siteinfo._is_expired(cached[1], expiry):
   1669                     return copy.deepcopy(cached[0])
-> 1670         preloaded = self._get_general(key, expiry)
   1671         if not preloaded:
   1672             preloaded = self._get_siteinfo(key, expiry)[key]

/srv/paws/pwb/pywikibot/site.py in _get_general(self, key, expiry)
   1614                     .format("', '".join(props)), _logger)
   1615             props += ['general']
-> 1616             default_info = self._get_siteinfo(props, expiry)
   1617             for prop in props:
   1618                 self._cache[prop] = default_info[prop]

/srv/paws/pwb/pywikibot/site.py in _get_siteinfo(self, prop, expiry)
   1537         request._warning_handler = warn_handler
   1538         try:
-> 1539             data = request.submit()
   1540         except api.APIError as e:
   1541             if e.code == 'siunknown_siprop':

/srv/paws/pwb/pywikibot/data/api.py in submit(self)
   2232         cached_available = self._load_cache()
   2233         if not cached_available:
-> 2234             self._data = super(CachedRequest, self).submit()
   2235             self._write_cache(self._data)
   2236         else:

/srv/paws/pwb/pywikibot/data/api.py in submit(self)
   2059                     continue
   2060                 raise NoUsername('Failed OAuth authentication for %s: %s'
-> 2061                                  % (self.site, info))
   2062             if code == 'cirrussearch-too-busy-error':  # T170647
   2063                 self.wait()

NoUsername: Failed OAuth authentication for wikipedia:en: The authorization headers in your request are for a user that does not exist here
# Hispania Nostra id verification
import requests

for index, row in red_list_df.iterrows():
    if pd.notnull(row["wikidata_id"]) :
        item = pb.ItemPage(repo, row["wikidata_id"])
        try:
            item_dict = item.get()
        except Exception as e:
            continue
    else :
        print("Red list item is not in Wikidata")
        continue
        
    if item.claims and 'P4868' in item.claims:
        ids = [claim.getTarget() for claim in item.claims['P4868']]
        id_i = ids[0]
        page_url = f'https://listarojapatrimonio.org/ficha/{id_i}/'
        page_request = requests.get(page_url)
        if page_request.status_code != 200:
            print (page_request.status_code)
            print(row['wikidata_id'])
            print (page_url)
    else :
        print(f"Item with {row['wikidata_id']} does not have Hispania Nostra id")