示例#1
0
 def get_items(self, label):
     """ Return the list of items for the given label"""
     params = {
         'action': 'wbsearchentities',
         'format': 'json',
         'language': 'en',
         'type': 'item',
         'search': label
     }
     request = api.Request(site=self.wikidata_site, parameters=params)
     result = request.submit()
     return result['search'] if len(result['search']) > 0 else None
示例#2
0
 def test_access_denied_no_username(self, exception, warning):
     """Test the query without a username."""
     self.site._username = [None, None]
     req = api.Request(site=self.site, parameters={'action': 'query'})
     self.assertRaises(pywikibot.NoUsername, req.submit)
     # FIXME: T100965
     self.assertRaises(api.APIError, req.submit)
     warning.assert_called_with(
         'API error readapidenied: '
         'You need read permission to use this module.')
     exception.assert_called_with(
         'You have no API read permissions. Seems you are not logged in')
示例#3
0
 def test_API_error_encoding_Unicode(self):
     """Test a Page instance as parameter using non-ASCII chars."""
     page = pywikibot.page.Page(self.site, 'Ümlä  üt')
     req = api.Request(site=self.site,
                       parameters={
                           'action': 'query',
                           'fake': True,
                           'titles': page
                       })
     self.assert_parameters = {'fake': ''}
     with PatchedHttp(api, self._dummy_request):
         self.assertRaises(api.APIMWException, req.submit)
def wiki_item_exists(wikibase_repo, label):
    params = {
        'action': 'wbsearchentities',
        'format': 'json',
        'language': 'en',
        'type': 'property',
        'limit': 1,
        'search': label
    }
    request = api.Request(site=wikibase_repo, parameters=params)
    result = request.submit()
    return result['search']
示例#5
0
def setAlias(site, repo, item, itemLabel, alias):
    token = repo.token(pywikibot.Page(repo, itemLabel), 'edit')
    params = {
        'action': 'wbsetaliases',
        'language': 'en',
        'id': item,
        'set': alias,
        'bot': True,
        'token': token
    }
    request = api.Request(site=site, **params)
    data = request.submit()
    print data
示例#6
0
def patrol(rcid, token):
    params = {'action':'patrol',
              'rcid':rcid,
              'token':token
    }
    req = api.Request(site=site, **params)
    try:
        data = req.submit()
    except pywikibot.data.api.APIError, e:
        print e.code
        print e.info
        #print 'error'
        return
示例#7
0
def api_query(params):
    """make a query
    
    arguments:
    params -- dictionary of params to pass to wikipedia api
    
    result:
    dict as json result of api
    """

    query = api.Request(site=pywikibot.Site(), **params)
    datas = query.submit()
    return datas
示例#8
0
def af_info(username):
    l = []
    #action=query&list=abuselog&afluser=Lichj&format=jsonfm
    params = {'action': 'query',
              'list': 'abuselog',
              'afluser': username,
              'afllimit': 50,
              }
    req = api.Request(**params)
    data = req.submit()
    count = len(data['query']['abuselog'])
    if count:
        l.append('af hits: {0}'.format(count))
    return l
示例#9
0
 def test_access_denied_notexist_username(self, error, warning, output):
     """Test the query with a username which does not exist."""
     self.site._username = '******'
     req = api.Request(site=self.site, parameters={'action': 'query'})
     self.assertRaises(pywikibot.NoUsername, req.submit)
     # FIXME: T100965
     self.assertRaises(api.APIError, req.submit)
     warning.assert_called_with(
         'API error readapidenied: '
         'You need read permission to use this module.')
     error.assert_called_with(
         'You have no API read permissions. Seems you are not logged in.')
     self.assertIn('Logging in to steward:steward as ',
                   output.call_args[0][0])
示例#10
0
 def test_access_denied_no_username(self, error, warning):
     """Test the query without a username."""
     self.site._username = None
     req = api.Request(site=self.site, parameters={'action': 'query'})
     with self.assertRaises(NoUsernameError):
         req.submit()
     # FIXME: T100965
     with self.assertRaises(APIError):
         req.submit()
     warning.assert_called_with(
         'API error readapidenied: '
         'You need read permission to use this module.')
     error.assert_called_with(
         'You have no API read permissions. Seems you are not logged in.')
示例#11
0
def GetData(request, site=None, back_response=False):
    if site:
        request['site'] = site

    req = api.Request(**request)
    result = req.submit()

    if back_response:
        pywikibot.warning(u"back_response is no longer supported; an empty "
                          u"response object will be returned")
        res_dummy = StringIO.StringIO()
        res_dummy.__dict__.update({u'code': 0, u'msg': u''})
        return res_dummy, result
    return result
示例#12
0
 def _search_item_by_label(self, site, item_name):
     all_results = []
     last_continue = 0
     params = { 'action' :'wbsearchentities' , 'format' : 'json' ,
                'language' : 'en', 'type' : 'item',
                'search': item_name}
     while isinstance(last_continue, int):
         params["continue"] = last_continue
         request = api.Request(site=site,**params)
         result = request.submit()
         for item_entry in result['search']:
             all_results.append(item_entry)
         last_continue = result['search'
         '-continue'] if "search-continue" in result else None
     return(all_results)
示例#13
0
 def testObjectCreation(self):
     """Test that api.Request() creates an object with desired attributes"""
     req = api.Request(site=mysite, action="test", foo="", bar="test")
     self.assert_(req)
     self.assertEqual(req.site, mysite)
     self.assert_("foo" in req.params)
     self.assertEqual(req["bar"], "test")
     # test item assignment
     req["one"] = "1"
     self.assertEqual(req.params['one'], "1")
     # test compliance with dict interface
     # req.keys() should contain "action", "foo", "bar", "one"
     self.assertEqual(len(req.keys()), 4)
     self.assert_("test" in req.values())
     self.assert_(all(len(item) == 2 for item in req.iteritems()))
示例#14
0
    def test_access_denied_no_username(self):
        """Test the query without a username."""
        self.site._username = [None, None]

        # FIXME: The following prevents LoginManager
        # from loading the username from the config when the site
        # username is None. i.e. site.login(user=None) means load
        # username from the configuration.
        if 'steward' in pywikibot.config.usernames:
            del pywikibot.config.usernames['steward']

        req = api.Request(site=self.site, action='query')
        self.assertRaises(pywikibot.NoUsername, req.submit)
        # FIXME: T100965
        self.assertRaises(api.APIError, req.submit)
示例#15
0
def get_users():
    #action=query&list=allusers&augroup=confirmed&auprop=groups&aulimit=max&format=jsonfm
    params = {
        'action': 'query',
        'list': 'allusers',
        'augroup': 'confirmed',
        'auprop': 'groups',
        'aulimit': 'max'
    }
    req = api.Request(site=site, **params)
    data = req.submit()
    for user in data['query']['allusers']:
        print user
        if 'autoconfirmed' in user['groups']:
            #print user
            yield user['name']
示例#16
0
def get_items(api, site, itemtitle):
    params = {
        'action': 'wbsearchentities',
        'format': 'json',
        'language': 'fr',
        'type': 'item',
        'search': itemtitle,
        'limit': 10
    }
    #params.update({'continue': 1})
    request = api.Request(site=site, parameters=params)
    search_results = request.submit()
    if search_results['success'] != 1:
        print('WD search failed')
    else:
        return search_results
示例#17
0
    def test_access_denied_no_username(self):
        """Test the query without a username."""
        self.site._username = [None, None]

        # FIXME: The following prevents LoginManager
        # from loading the username from the config when the site
        # username is None. i.e. site.login(user=None) means load
        # username from the configuration.
        if 'steward' in pywikibot.config.usernames:
            del pywikibot.config.usernames['steward']

        req = api.Request(site=self.site, parameters={'action': 'query'})
        self.assertRaisesRegex(pywikibot.NoUsername,
                               'If you have an account for that site',
                               req.submit)
        # FIXME: T100965
        self.assertRaisesRegex(api.APIError, read_perm_req, req.submit)
示例#18
0
def main():
    params = {
        'action': 'query',
        'list': 'querypage',
        'qppage': 'DoubleRedirects',
        'qplimit': 5000
    }
    req = api.Request(site=site, **params)
    data = req.submit()
    for m in data['query']['querypage']['results']:
        try:
            if m['ns'] == 0:
                item1 = pywikibot.ItemPage(repo, m['title'])
                item2 = item1.getRedirectTarget().getRedirectTarget().getID()
                redirect(m['title'], item2)
        except:
            pass
示例#19
0
 def testObjectCreation(self):
     """Test api.Request() constructor."""
     mysite = self.get_site()
     req = api.Request(site=mysite, action="test", foo="", bar="test")
     self.assertTrue(req)
     self.assertEqual(req.site, mysite)
     self.assertIn("foo", req._params)
     self.assertEqual(req["bar"], ["test"])
     # test item assignment
     req["one"] = "1"
     self.assertEqual(req._params['one'], ["1"])
     # test compliance with dict interface
     # req.keys() should contain "action", "foo", "bar", "one"
     self.assertEqual(len(req.keys()), 4)
     self.assertIn("test", req._encoded_items().values())
     for item in req.items():
         self.assertEqual(len(item), 2, item)
示例#20
0
    def allusers(cls, **params):
        """
        Wrapper function to make a request with action=query and list=allusers.
        @:param params: query params
        @:type params: dict
        @:return: users
        @:rtype: dict
        """

        params = {**cls.DEFAULT_ALLUSERS_PARAMS, **params}

        params = {**params, **{
            'action': 'query',
            'list': 'allusers',
        }}

        request = api.Request(site=cls.SITE, **params)
        return request.submit()
示例#21
0
 def testObjectCreation(self):
     """Test api.Request() constructor."""
     mysite = self.get_site()
     req = api.Request(site=mysite, parameters={'action': 'test', 'foo': '',
                                                'bar': 'test'})
     self.assertTrue(req)
     self.assertEqual(req.site, mysite)
     self.assertIn('foo', req._params)
     self.assertEqual(req['bar'], ['test'])
     # test item assignment
     req['one'] = '1'
     self.assertEqual(req._params['one'], ['1'])
     # test compliance with dict interface
     # req.keys() should contain 'action', 'foo', 'bar', 'one'
     self.assertLength(req.keys(), 4)
     self.assertIn('test', req._encoded_items().values())
     for item in req.items():
         self.assertLength(item, 2)
示例#22
0
    def wbgetentities(self, ids):
        """
        Helper function to make a request with action wbgetentities.

        @:param ids: item ids
        @:type ids: list

        @:return: self
        @:rtype: MediaWikiAPI
        """
        params = {
            'action': 'wbgetentities',
            'format': 'json',
            'ids': '|'.join(ids)
        }
        request = api.Request(site=self.SITE, **params)
        self.response = request.submit()
        return self
示例#23
0
 def test_access_denied_notexist_username(self, error, warning, exception,
                                          output):
     """Test the query with a username which does not exist."""
     self.site._username = ['Not registered username', None]
     req = api.Request(site=self.site, parameters={'action': 'query'})
     self.assertRaises(pywikibot.NoUsername, req.submit)
     # FIXME: T100965
     self.assertRaises(api.APIError, req.submit)
     try:
         error.assert_called_with('Login failed (readapidenied).')
     except AssertionError:  # MW version is older than 1.34.0-wmf.13
         error.assert_called_with('Login failed (Failed).')
     warning.assert_called_with(
         'API error readapidenied: '
         'You need read permission to use this module.')
     exception.assert_called_with(
         'You have no API read permissions. Seems you are not logged in')
     self.assertIn('Logging in to steward:steward as ',
                   output.call_args[0][0])
示例#24
0
def GetData(request, site=None, back_response=False):
    """
    Query the server with the given request dict.

    DEPRECATED: Use pywikibot.data.api.Request instead.
    """
    if site:
        request['site'] = site

    req = api.Request(**request)
    result = req.submit()

    if back_response:
        pywikibot.warning(u"back_response is no longer supported; an empty "
                          u"response object will be returned")
        res_dummy = io.StringIO()
        res_dummy.__dict__.update({u'code': 0, u'msg': u''})
        return res_dummy, result
    return result
示例#25
0
    def users(cls, users, **params):
        """
        Wrapper function to make a request with action=query and list=users.
        @:param users: user names
        @:type users: list
        @:param params: query params
        @:type params: dict
        @:return: users
        @:rtype: dict
        """

        params = {**cls.DEFAULT_USERS_PARAMS, **params}

        params = {**params, **{
            'action': 'query',
            'list': 'users',
            'ususers': '|'.join(users),
        }}

        request = api.Request(site=cls.SITE, **params)
        return request.submit()
示例#26
0
    def revisions(cls, pages, **params):
        """
        Wrapper function to make a request with action=query and prop=revisions.
        @:param pages: page names
        @:type pages: list
        @:param params: query params
        @:type params: dict
        @:return: revisions
        @:rtype: dict
        """

        params = {**cls.DEFAULT_REVISIONS_PARAMS, **params}

        params = {**params, **{
            'action': 'query',
            'prop': 'revisions',
            'titles': '|'.join(pages),
        }}

        request = api.Request(site=cls.SITE, **params)
        return request.submit()
示例#27
0
    def get_whitelist(self):
        """Get whitelist."""
        self.knownWords = set()
        self.seenUnresolvedLinks = set()

        # TODO: handle "continue"
        if self.site.code in self.whitelists:
            wlpage = self.whitelists[self.site.code]
            pywikibot.output('Loading whitelist from {}'.format(wlpage))
            wlparams = {
                'action': 'query',
                'prop': 'links',
                'titles': wlpage,
                'redirects': '',
                'indexpageids': '',
                'pllimit': 'max',
            }

            req = api.Request(site=self.site, parameters=wlparams)
            data = req.submit()
            if len(data['query']['pageids']) == 1:
                pageid = data['query']['pageids'][0]
                links = data['query']['pages'][pageid]['links']

                self.knownWords = {
                    nn
                    for n in links for nn in self.FindBadWords(n['title'])
                }

            else:
                raise ValueError('The number of pageids is not 1')

            pywikibot.output('Loaded whitelist with {} items'.format(
                len(self.knownWords)))
            if self.knownWords:
                pywikibot.log('Whitelist: ' + ', '.join(
                    self.MakeLink(i, False) for i in self.knownWords))
        else:
            pywikibot.output('Whitelist is not known for language ' +
                             self.site.code)
示例#28
0
def updateDiseaseOntologyVersion(site, repo, diseaseOntology):
    global globalDiseaseOntology
    globalDiseaseOntology = diseaseOntology

    global globalSite
    globalSite = site

    doDate = diseaseOntology.findall('.//oboInOwl:date', namespaces)
    doversion = diseaseOntology.findall('.//owl:versionIRI', namespaces)
    for name, value in doversion[0].items():
        doUrlversion = value
    dateList = doDate[0].text.split(' ')[0].split(":")
    searchTerm = "Disease ontology release " + dateList[2] + "-" + dateList[
        1] + "-" + dateList[0]

    if len(getItems(site, searchTerm)['search']) == 0:
        metadata = {u'entities': {}}
        metadata["entities"]["claims"] = dict()
        token = repo.token(pywikibot.Page(repo, searchTerm), 'edit')
        metadata = addNewLabel(metadata, searchTerm)
        metadata = addNewClaims(metadata, 'P856',
                                ["http://disease-ontology.org"], 'string',
                                False)  # official website P856
        metadata = addNewClaims(metadata, 'P1065', [
            "http://purl.obolibrary.org/obo/doid/releases/" + dateList[2] +
            "-" + dateList[1] + "-" + dateList[0] + "/doid.owl"
        ], 'string', False)  # archive URL P1065
        request = api.Request(site=site,
                              action='wbeditentity',
                              format='json',
                              new='item',
                              bot=True,
                              token=token,
                              data=json.dumps(metadata['entities']))
        # pp.pprint(localdata)
        data = request.submit()
        # pp.pprint(data)
        ID = data['entity']['id']
        print "WikiData entry made for this version of Disease Ontology: " + ID
示例#29
0
def user_info(username, bot):
    text = ['[[User:{0}]]: https://en.wikipedia.org/wiki/Special:Contributions/{1}'.format(
        username, urllib.quote_plus(username.replace(' ', '_').encode('utf-8')))]
    #&list=users&ususers=Legoktm&usprop=blockinfo|groups|editcount|registration&format=jsonfm
    params = {'action': 'query',
              'list': 'users',
              'ususers': username,
              'usprop': 'blockinfo|groups|editcount|registration',
              }
    req = api.Request(**params)
    data = req.submit()
    data = data['query']['users'][0]
    if 'missing' in data:
        return text
    elif 'invalid' in data:  # IP address
        data = IPdata.get_data(username, bot)
        return text + IPdata.rDNS(data) + IPdata.geolocate(data)
    text.append('editcount: {0}'.format(data['editcount']))
    text.append('userrights: {0}'.format(', '.join(data['groups'])))
    if 'blockid' in data:
        text.append('{0} is currently blocked'.format(username))
    return text
示例#30
0
    def wbsearchentities(self, label, language):
        """
        Helper function to make a request with action wbgetentities.

        @:param label: The text string to search for
        @:type label: string
        @:param language: The language to search in
        @:type language: string

        @:return: self
        @:rtype: MediaWikiAPI
        """
        params = {
            'action': 'wbsearchentities',
            'format': 'json',
            'language': language,
            'type': 'item',
            'search': label
        }
        request = api.Request(site=self.SITE, **params)
        self.response = request.submit()
        return self