File indexing completed on 2024-07-14 05:16:09

0001 import re
0002 import sys
0003 import json
0004 import time
0005 from datetime import datetime
0006 import arrow
0007 import requests
0008 from os.path import dirname, join
0009 from bs4 import BeautifulSoup
0010 from adapt.intent import IntentBuilder
0011 from mycroft import MycroftSkill, intent_handler, intent_file_handler
0012 from mycroft.messagebus.message import Message
0013 
0014 zurl = "https://www.wikidata.org/entity/"
0015 entity_json = ""
0016 
0017 class WikidataSkill(MycroftSkill):
0018     """
0019     Wikidata Skill
0020     """
0021     def __init__(self):
0022         super(WikidataSkill, self).__init__(name="WikidataSkill")
0023     
0024     def initialize(self):
0025         self.add_event('skill-wikidata.aiix.home', self.showHome)
0026 
0027     @intent_file_handler('show.wikidata.home.intent')
0028     def showHome(self, message):
0029         self.gui.clear()
0030         self.displayHome()
0031 
0032     def displayHome(self):
0033         self.gui.show_page("homepage.qml", override_idle=True)
0034 
0035     @intent_file_handler('birth.place.of.person.intent')
0036     def handle_wikidata_birthplace_intent(self, message):
0037         """
0038         Wikidata BirthPlace Intent
0039         """
0040         global entity_json
0041         entity_id = self.getEntityId(message.data['person'])
0042         person_name = message.data['person']
0043         thumb_image = self.always_get_image(entity_id)
0044         try:
0045             get_entity_birth_place_id = entity_json['entities'][entity_id]['claims']['P19'][0]['mainsnak']['datavalue']['value']['id']
0046             try:
0047                 get_entity_birth_place_response = requests.get(zurl+get_entity_birth_place_id)
0048                 entity_birth_place_json =  get_entity_birth_place_response.json()
0049                 entity_birth_place = entity_birth_place_json['entities'][get_entity_birth_place_id]['labels']['en']['value']
0050                 result_speak = self.translate('birthplace', {'person': person_name, 'place': entity_birth_place})
0051                 self.gui.clear()
0052                 self.gui["personContext"] = person_name
0053                 self.gui["answerData"] = result_speak
0054                 self.gui["imgLink"] = thumb_image
0055                 self.gui.show_page("answer.qml", override_idle=30)
0056                 self.speak(result_speak)
0057             except Exception as e:
0058                 notFoundMessage = self.translate('birthplace.not.found',
0059                                                  {'person': person_name})
0060                 self.speak(notFoundMessage)
0061                 entity_birth_place = False
0062         except:
0063             notFoundMessage = self.translate('birthplace.not.found',
0064                                              {'person': person_name})
0065             self.speak(notFoundMessage)
0066             get_entity_birth_place_id = False
0067 
0068     @intent_file_handler('death.place.of.person.intent')
0069     def handle_wikidata_deathplace_intent(self, message):
0070         """
0071         Wikidata DeathPlace Intent
0072         """
0073         global entity_json
0074         entity_id = self.getEntityId(message.data['person'])
0075         person_name = message.data['person']
0076         thumb_image = self.always_get_image(entity_id)
0077         try:
0078             get_entity_death_place_id = entity_json['entities'][entity_id]['claims']['P20'][0]['mainsnak']['datavalue']['value']['id']
0079             try:
0080                 get_entity_death_place_response = requests.get(zurl+get_entity_death_place_id)
0081                 entity_death_place_json = get_entity_death_place_response.json()
0082                 entity_death_place = entity_death_place_json['entities'][get_entity_death_place_id]['labels']['en']['value']
0083                 result_speak = self.translate('deathplace',
0084                                               {'person': person_name,
0085                                                'place': entity_death_place})
0086                 self.gui.clear()
0087                 self.gui["personContext"] = person_name
0088                 self.gui["answerData"] = result_speak
0089                 self.gui["imgLink"] = thumb_image
0090                 self.gui.show_page("answer.qml", override_idle=30)
0091                 self.speak(result_speak)
0092             except Exception as e:
0093                 notFoundMessage = self.translate('deathplace.not.found',
0094                                                  {'person': person_name})
0095                 self.speak(notFoundMessage)
0096                 entity_death_place = False
0097         except:
0098             notFoundMessage = self.translate('deathplace.not.found',
0099                                              {'person': person_name})
0100             self.speak(notFoundMessage)
0101             get_entity_death_place_id = False
0102 
0103     @intent_file_handler('how.old.is.person.intent')
0104     def handle_wikidata_age_intent(self, message):
0105         """
0106         Wikidata SearchAge Intent
0107         """
0108         global entity_json
0109         entity_id = self.getEntityId(message.data['person'])
0110         person_name = message.data['person']
0111         thumb_image = self.always_get_image(entity_id)
0112         try:
0113             get_entity_dob_id = entity_json['entities'][entity_id]['claims']['P569'][0]['mainsnak']['datavalue']['value']['time']
0114             try:
0115                 fix_gedid = get_entity_dob_id.replace("+", "") 
0116                 a = arrow.get(fix_gedid)
0117                 birth_date = a.format("DD/MM/YYYY")
0118                 dob = birth_date
0119                 dob = datetime.strptime(dob, '%d/%m/%Y')
0120                 age = ("%d" % ((datetime.today() - dob).days/365))
0121                 result_msg = self.translate('age', {'person': person_name,
0122                                                     'age': age})
0123                 self.gui.clear()
0124                 self.gui["personContext"] = person_name
0125                 self.gui["answerData"] = result_msg
0126                 self.gui["imgLink"] = thumb_image
0127                 self.gui.show_page("answer.qml", override_idle=30)
0128                 self.speak(result_msg)
0129             except Exception as e:
0130                 notFoundMessage = self.translate('age.not.found',
0131                                                  {'person': person_name})
0132                 self.speak(notFoundMessage)
0133                 entity_death_place = False
0134         except:
0135             notFoundMessage = self.translate('age.not.found',
0136                                              {'person': person_name})
0137             self.speak(notFoundMessage)
0138             get_entity_death_place_id = False
0139 
0140     @intent_file_handler('what.is.the.gender.intent')
0141     def handle_wikidata_gender_intent(self, message):
0142         """
0143         Wikidata Gender Intent
0144         """
0145         global entity_json
0146         entity_id = self.getEntityId(message.data['person'])
0147         person_name = message.data['person']
0148         thumb_image = self.always_get_image(entity_id)
0149         try:
0150             get_entity_gender_id = entity_json['entities'][entity_id]['claims']['P21'][0]['mainsnak']['datavalue']['value']['id']
0151             try:
0152                 get_entity_gender_id_response = requests.get(zurl+get_entity_gender_id)
0153                 entity_gender_id_json =  get_entity_gender_id_response.json()
0154                 entity_gender = entity_gender_id_json['entities'][get_entity_gender_id]['labels']['en']['value']
0155                 self.speak(entity_gender)
0156                 self.gui.clear()
0157                 self.gui["personContext"] = person_name
0158                 self.gui["answerData"] = entity_gender
0159                 self.gui["imgLink"] = thumb_image
0160                 self.gui.show_page("answer.qml", override_idle=30)
0161                 self.speak(entity_gender)
0162             except Exception as e:
0163                 notFoundMessage = self.translate('gender.not.found',
0164                                                  {'person': person_name})
0165                 self.speak(notFoundMessage)
0166                 entity_gender_id = False
0167         except:
0168             notFoundMessage = self.translate('gender.not.found',
0169                                              {'person': person_name})
0170             self.speak(notFoundMessage)
0171             get_entity_gender_id = False
0172 
0173     @intent_file_handler('who.is.the.spouse.intent')
0174     def handle_wikidata_spouse_intent(self, message):
0175         """
0176         Wikidata Spouse Intent
0177         """
0178         global entity_json
0179         entity_id = self.getEntityId(message.data['person'])
0180         person_name = message.data['person']
0181         thumb_image = self.always_get_image(entity_id)
0182         try:
0183             get_entity_spouse_id = entity_json['entities'][entity_id]['claims']['P26'][0]['mainsnak']['datavalue']['value']['id']
0184             try:
0185                 get_entity_spouse_id_response = requests.get(zurl+get_entity_spouse_id)
0186                 entity_spouse_id_json = get_entity_spouse_id_response.json()
0187                 entity_spouse_id = entity_spouse_id_json['entities'][get_entity_spouse_id]['labels']['en']['value']
0188                 self.gui.clear()
0189                 self.gui["personContext"] = person_name
0190                 self.gui["answerData"] = entity_spouse_id
0191                 self.gui["imgLink"] = thumb_image
0192                 self.gui.show_page("answer.qml", override_idle=30)
0193                 self.speak(entity_spouse_id)
0194             except Exception as e:
0195                 notFoundMessage = self.translate('spouse.not.found',
0196                                                  {'person': person_name})
0197                 self.speak(notFoundMessage)
0198                 entity_spouse_id = False
0199         except:
0200             notFoundMessage = self.translate('spouse.not.found',
0201                                              {'person': person_name})
0202             self.speak(notFoundMessage)
0203             get_entity_spouse_id = False
0204 
0205     @intent_file_handler('which.country.is.from.intent')
0206     def handle_wikidata_country_citizenship(self, message):
0207         """
0208         Wikidata Country Citizenship
0209         """
0210         global entity_json
0211         entity_id = self.getEntityId(message.data['person'])
0212         person_name = message.data['person']
0213         thumb_image = self.always_get_image(entity_id)
0214         try:
0215             get_entity_country_citizenship_id = entity_json['entities'][entity_id]['claims']['P27'][0]['mainsnak']['datavalue']['value']['id']
0216             try:
0217                 get_entity_country_citizenship_id_response = requests.get(zurl+get_entity_country_citizenship_id)
0218                 entity_country_citizenship_json = get_entity_country_citizenship_id_response.json()
0219                 entity_country_citizenship = entity_country_citizenship_json['entities'][get_entity_country_citizenship_id]['labels']['en']['value']
0220                 self.gui.clear()
0221                 self.gui["personContext"] = person_name
0222                 self.gui["answerData"] = entity_country_citizenship
0223                 self.gui["imgLink"] = thumb_image
0224                 self.gui.show_page("answer.qml", override_idle=30)
0225                 self.speak(entity_country_citizenship)
0226             except Exception as e:
0227                 notFoundMessage = self.translate('country.not.found',
0228                                                  {'person': person_name})
0229                 self.speak(notFoundMessage)
0230                 entity_country_citizenship = False
0231         except:
0232             notFoundMessage = self.translate('country.not.found',
0233                                              {'person': person_name})
0234             self.speak(notFoundMessage)
0235             get_entity_country_citizenship_id = False
0236 
0237     @intent_file_handler('what.children.name.intent')
0238     def handle_wikidata_child_name(self, message):
0239         """
0240         Wikidata Child Name
0241         """
0242         global entity_json
0243         entity_id = self.getEntityId(message.data['person'])
0244         person_name = message.data['person']
0245         thumb_image = self.always_get_image(entity_id)
0246         try:
0247             get_entity_child_id = entity_json['entities'][entity_id]['claims']['P40'][0]['mainsnak']['datavalue']['value']['id']
0248             try:
0249                 get_entity_child_id_response = requests.get(zurl+get_entity_child_id)
0250                 entity_child_id_json = get_entity_child_id_response.json()
0251                 entity_child_id = entity_child_id_json = entity_child_id_json['entities'][get_entity_child_id]['labels']['en']['value']
0252                 self.gui.clear()
0253                 self.gui["personContext"] = person_name
0254                 self.gui["answerData"] = entity_child_id
0255                 self.gui["imgLink"] = thumb_image
0256                 self.gui.show_page("answer.qml", override_idle=30)
0257                 self.speak(entity_child_id)
0258             except Exception as e:
0259                 notFoundMessage = self.translate('children.not.found',
0260                                                  {'person': person_name})
0261                 self.speak(notFoundMessage)
0262                 entity_child_id = False
0263         except:
0264             notFoundMessage = self.translate('children.not.found',
0265                                              {'person': person_name})
0266             self.speak(notFoundMessage)
0267             get_entity_child_id = False
0268 
0269     @intent_file_handler('what.occupation.intent')
0270     def handle_wikidata_occupation(self, message):
0271         """
0272         Wikidata Occupation
0273         """
0274         global entity_json
0275         entity_id = self.getEntityId(message.data['person'])
0276         person_name = message.data['person']
0277         thumb_image = self.always_get_image(entity_id)
0278         try:
0279             get_entity_occupation_id = entity_json['entities'][entity_id]['claims']['P106'][0]['mainsnak']['datavalue']['value']['id']
0280             try:
0281                 get_entity_occupation_id_response = requests.get(zurl+get_entity_occupation_id)
0282                 entity_occupation_id_json = get_entity_occupation_id_response.json()
0283                 entity_occupation_id = entity_occupation_id_json['entities'][get_entity_occupation_id]['labels']['en']['value']
0284                 self.gui.clear()
0285                 self.gui["personContext"] = person_name
0286                 self.gui["answerData"] = entity_occupation_id
0287                 self.gui["imgLink"] = thumb_image
0288                 self.gui.show_page("answer.qml", override_idle=30)
0289                 self.speak(entity_occupation_id)
0290             except Exception as e:
0291                 notFoundMessage = self.translate('occupation.not.found',
0292                                                  {'person': person_name})
0293                 self.speak(notFoundMessage)
0294                 entity_occupation_id = False
0295         except:
0296             notFoundMessage = self.translate('occupation.not.found',
0297                                              {'person': person_name})
0298             self.speak(notFoundMessage)
0299             get_entity_occupation_id = False
0300 
0301     @intent_file_handler('burial.location.intent')
0302     def handle_wikidata_place_of_burial(self, message):
0303         """
0304         Wikidata Place Of Burial
0305         """
0306         global entity_json
0307         entity_id = self.getEntityId(message.data['person'])
0308         person_name = message.data['person']
0309 
0310         try:
0311             get_entity_place_of_burial_id = entity_json['entities'][entity_id]['claims']['P119'][0]['mainsnak']['datavalue']['value']['id']
0312             try:
0313                 get_entity_place_of_burial_id_response = requests.get(zurl+get_entity_place_of_burial_id)
0314                 entity_place_of_burial_id_json = get_entity_place_of_burial_id_response.json()
0315                 entity_place_of_burial_id = entity_place_of_burial_id_json['entities'][get_entity_place_of_burial_id]['labels']['en']['value']
0316                 self.gui.clear()
0317                 self.gui["personContext"] = person_name
0318                 self.gui["answerData"] = entity_place_of_burial_id
0319                 self.gui["imgLink"] = thumb_image
0320                 self.gui.show_page("answer.qml", override_idle=30)
0321                 self.speak(entity_place_of_burial_id)
0322             except Exception as e:
0323                 notFoundMessage = self.translate('burial.not.found',
0324                                                  {'person': person_name})
0325                 self.speak(notFoundMessage)
0326                 entity_place_of_burial_id = False
0327         except:
0328             notFoundMessage = self.translate('burial.not.found',
0329                                              {'person': person_name})
0330             self.speak(notFoundMessage)
0331             get_entity_place_of_burial_id = False
0332 
0333     @intent_file_handler('date.of.birth.intent')
0334     def handle_wikidata_date_of_birth(self, message):
0335         """
0336         Wikidata Date of Birth
0337         """
0338         global entity_json
0339         entity_id = self.getEntityId(message.data['person'])
0340         person_name = message.data['person']
0341         thumb_image = self.always_get_image(entity_id)
0342         try:
0343             get_entity_dob_id = entity_json['entities'][entity_id]['claims']['P569'][0]['mainsnak']['datavalue']['value']['time']
0344             try:
0345                 fix_gedobid = get_entity_dob_id.replace("+", "")
0346                 a = arrow.get(fix_gedobid)
0347                 birth_date = a.format("DD,MM,YYYY")
0348                 birth_date_breakup = birth_date.split(",")
0349                 person_name = message.data['person']
0350                 day = birth_date_breakup[0]
0351                 month = birth_date_breakup[1]
0352                 year = birth_date_breakup[2]
0353                 birth_date_to_speak = self.translate('date.of.birth',
0354                                                      {'person': person_name,
0355                                                       'day': day,
0356                                                       'month': month,
0357                                                       'year': year})
0358                 self.gui.clear()
0359                 self.gui["personContext"] = person_name
0360                 self.gui["answerData"] = birth_date
0361                 self.gui["imgLink"] = thumb_image
0362                 self.gui.show_page("answer.qml", override_idle=30)
0363                 self.speak(birth_date_to_speak)
0364             except Exception as e:
0365                 notFoundMessage = self.translate('date.of.birth.not.found',
0366                                                  {'person': person_name})
0367                 self.speak(notFoundMessage)
0368                 entity_dob = False
0369         except:
0370             notFoundMessage = self.translate('date.of.birth.not.found',
0371                                              {'person': person_name})
0372             self.speak(notFoundMessage)
0373             get_entity_dob_id = False
0374 
0375     @intent_file_handler('date.of.death.intent')
0376     def handle_wikidata_date_of_death(self, message):
0377         """
0378         Wikidata Date of Death
0379         """
0380         global entity_json
0381         entity_id = self.getEntityId(message.data['person'])
0382         person_name = message.data['person']
0383         thumb_image = self.always_get_image(entity_id)
0384         try:
0385             get_entity_dod_id = entity_json['entities'][entity_id]['claims']['P570'][0]['mainsnak']['datavalue']['value']['time']
0386             try:
0387                 fix_gedodid = get_entity_dod_id.replace("+", "")
0388                 a = arrow.get(fix_gedodid)
0389                 death_date = a.format("DD,MM,YYYY")
0390                 death_date_breakup = death_date.split(",")
0391                 person_name = message.data['person']
0392                 day = death_date_breakup[0]
0393                 month = death_date_breakup[1]
0394                 year = death_date_breakup[2]
0395                 death_date_to_speak = self.translate('date.of.death',
0396                                                      {'person': person_name,
0397                                                       'day': day,
0398                                                       'month': month,
0399                                                       'year': year})
0400                 self.gui.clear()
0401                 self.gui["personContext"] = person_name
0402                 self.gui["answerData"] = death_date
0403                 self.gui["imgLink"] = thumb_image
0404                 self.gui.show_page("answer.qml", override_idle=30)
0405                 self.speak(death_date_to_speak)
0406             except Exception as e:
0407                 notFoundMessage = self.translate('date.of.death.not.found',
0408                                                  {'person': person_name})
0409                 self.speak(notFoundMessage)
0410                 entity_dod = False
0411         except:
0412             notFoundMessage = self.translate('date.of.death.not.found',
0413                                              {'person': person_name})
0414             self.speak(notFoundMessage)
0415             get_entity_dob_id = False
0416 
0417     def getAge(self, dateofbirth):
0418         d = datetime.strptime(s, '%d %B, %Y')
0419         print(d.strftime('%Y-%m-%d'))
0420         return d.strftime('%Y-%m-%d')
0421 
0422     def get_info_wikidata(self, datavalue):
0423         name = datavalue.strip()
0424         wikidataObject = self._wikidata(name)
0425         r = wikidataObject['search'][0]['concepturi']
0426         return r
0427 
0428     def _wikidata(self, name):
0429         url = "http://www.wikidata.org/w/api.php"
0430         params = {
0431             "search": name,
0432             "action": "wbsearchentities",
0433             "format": "json",
0434             "language": "en",
0435             "type": "item",
0436             "continue": "0",
0437             "limit": "10"
0438         }
0439         return requests.get(url, params=params).json()
0440 
0441 
0442     def getEntityId(self, itemcontext):
0443         item = itemcontext
0444         global entity_json
0445         try:
0446             result = self.get_info_wikidata(item)
0447             if result != None:
0448                 entity_result = requests.get(result)
0449                 entity_json = entity_result.json()
0450                 for key in entity_json['entities'].keys():
0451                     entity_id = key
0452                     return entity_id
0453         except:
0454             return False
0455 
0456     def always_get_image(self, datakey):
0457             global entity_json
0458             entity_id = datakey
0459             try:
0460                 get_entity_pic_link = entity_json['entities'][entity_id]['claims']['P18'][0]['mainsnak']['datavalue']['value']
0461                 get_entity_pic = get_entity_pic_link.replace(" ", "_")
0462                 get_entity_pic_hyperlink = "https://commons.wikimedia.org/wiki/File:{0}".format(get_entity_pic)
0463                 get_entity_pic_content = requests.get(get_entity_pic_hyperlink)
0464                 soup = BeautifulSoup(get_entity_pic_content.content, 'html.parser')
0465                 imagelist = []
0466                 for link in soup.find_all('div', class_="fullImageLink"):
0467                     for x in link.find_all('a'):
0468                         r = x.get('href')
0469                         imagelist.append(r)
0470 
0471                 return imagelist[0]
0472             except:
0473                 get_entity_pic_link = False
0474                 print(get_entity_pic_link)
0475                 return False
0476 
0477     def stop(self):
0478         pass
0479 
0480 
0481 def create_skill():
0482     return WikidataSkill()