<commit_before>#!usr/bin/env python
# -*- coding: utf-8 -*-

'''
Authors: 
  <NAME> (sebastian.burgstaller' at 'gmail.com
  <NAME> (andra' at ' micelio.be)

This file is part of ProteinBoxBot.

ProteinBoxBot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ProteinBoxBot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ProteinBoxBot.  If not, see <http://www.gnu.org/licenses/>.
'''

__author__ = '<NAME>, <NAME>'
__license__ = 'GPL'

import pywikibot
from pywikibot.data import api
import time
import datetime
import json
import urllib2
import PBB_Debug
import wd_property_store


class WDItemEngine(object):

    wd_item_id = ''
    item_names = ''
    autoadd_references = False
    normalize = True

    # a list with all properties an item should have and/or modify
    property_list = []
    wd_json_representation = ''

    def __init__(self, wd_item_id='', item_name='', normalize=True, domain=''):
        """
        constructor
        :param wd_item_id: Wikidata item id
        :param item_names: Label of the wikidata item
        """
        self.wd_item_id = wd_item_id
        self.item_names = item_name
        self.autoadd_references = False
        self.normalize = normalize

        self.wd_json_representation = self.get_item_data(item_name, wd_item_id)
        self.property_list = self.get_property_list()

    def get_item_data(self, item_name='', item_id=''):
        """
        Instantiate a class by either providing a item name or a Wikidata item ID
        :param item_name: A name which should allow to find an item in Wikidata
        :param item_id: Wikidata item ID which allows loading of a Wikidata item
        :return: None
        """
        if item_name is '' and item_id is '':
            raise IDMissingError('No item name or WD identifyer was given')

        try:
            title_string = ''
            if item_id is not '':
                title_string = '&ids={}'.format(item_id)
            elif item_name is not '':
                title_string = '&titles={}'.format(urllib2.quote(item_name))

            query = 'https://www.wikidata.org/w/api.php?action=wbgetentities{}{}{}{}{}'.format(
                '&sites=enwiki',
                '&languages=en',
                title_string,
                '&props=labels|aliases|claims',
                '&format=json'
            )

            if self.normalize:
                query += '&normalize='

            return(json.load(urllib2.urlopen(query)))

        except urllib2.HTTPError as e:
            PBB_Debug.getSentryClient().captureException(PBB_Debug.getSentryClient())
            print(e)

    def get_property_list(self):
        for x in wd_property_store.wd_properties:

         
    def getItemsByProperty(self, wdproperty):
        """
        Gets all WikiData item IDs that contains statements containing property wdproperty
        """
        query - 'http'
        req = urllib2.Request("http://wdq.wmflabs.org/api?q=claim%5B"+wdproperty+"%5D&props="+wdproperty, None, {'user-agent':'proteinBoxBot'})
        opener = urllib2.build_opener()
        f = opener.open(req)
        return simplejson.load(f)
        
    def getClaims(self, wdItem, claimProperty):
        """<commit_msg>added property loading from wd_property_store<commit_after>#!usr/bin/env python
# -*- coding: utf-8 -*-

'''
Authors: 
  <NAME> (sebastian.burgstaller' at 'gmail.com
  <NAME> (andra' at ' micelio.be)

This file is part of ProteinBoxBot.

ProteinBoxBot is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

ProteinBoxBot is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with ProteinBoxBot.  If not, see <http://www.gnu.org/licenses/>.
'''

__author__ = '<NAME>, <NAME>'
__license__ = 'GPL'

import pywikibot
from pywikibot.data import api
import time
import datetime
import json
import urllib2
import PBB_Debug
import wd_property_store


class WDItemEngine(object):

    wd_item_id = ''
    item_names = ''
    domain = ''
    autoadd_references = False
    normalize = True

    # a list with all properties an item should have and/or modify
    property_list = []
    wd_json_representation = ''

    def __init__(self, wd_item_id='', item_name='', normalize=True, domain=''):
        """
        constructor
        :param wd_item_id: Wikidata item id
        :param item_names: Label of the wikidata item
        """
        self.wd_item_id = wd_item_id
        self.item_names = item_name
        self.domain = domain
        self.autoadd_references = False
        self.normalize = normalize

        self.wd_json_representation = self.get_item_data(item_name, wd_item_id)
        self.property_list = self.get_property_list()

    def get_item_data(self, item_name='', item_id=''):
        """
        Instantiate a class by either providing a item name or a Wikidata item ID
        :param item_name: A name which should allow to find an item in Wikidata
        :param item_id: Wikidata item ID which allows loading of a Wikidata item
        :return: None
        """
        if item_name is '' and item_id is '':
            raise IDMissingError('No item name or WD identifyer was given')

        try:
            title_string = ''
            if item_id is not '':
                title_string = '&ids={}'.format(item_id)
            elif item_name is not '':
                title_string = '&titles={}'.format(urllib2.quote(item_name))

            query = 'https://www.wikidata.org/w/api.php?action=wbgetentities{}{}{}{}{}'.format(
                '&sites=enwiki',
                '&languages=en',
                title_string,
                '&props=labels|aliases|claims',
                '&format=json'
            )

            if self.normalize:
                query += '&normalize='

            return(json.load(urllib2.urlopen(query)))

        except urllib2.HTTPError as e:
            PBB_Debug.getSentryClient().captureException(PBB_Debug.getSentryClient())
            print(e)

    def get_property_list(self):
        """
        extract the properties which belong to the domain of the WD item
        :return: a list of property strings is being returned
        """
        property_list = []
        for x in wd_property_store.wd_properties:
            if self.domain in x['domain']:
                property_list.append(x.key)

        return(property_list)

         
    def getItemsByProperty(self, wdproperty):
        """
        Gets all WikiData item IDs that contains statements containing property wdproperty
        """
        query - 'http'
        req = urllib2.Request("http://wdq.wmflabs.org/api?q=claim%5B"+wdproperty+"%5D&props="+wdproperty, None, {'user-agent':'proteinBoxBot'})
        opener = urllib2.build_opener()
        f = opener.open(req)
        return simplejson.load(f)
        
    def getClaims(self, wdItem, claimProperty):
        """
{'fraction_non_alphanumeric': 0.09492763072108489, 'fraction_numerical': 0.0026079019428869475, 'mean_word_length': 2.5542168674698793, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '44197', 'n_tokens_mistral': 2321, 'n_tokens_neox': 2241, 'n_words': 673}